1 | #line 2 "testWriteField.ino"
|
---|
2 | /*
|
---|
3 | testWriteField unit test
|
---|
4 |
|
---|
5 | Unit Test for the writeField function in the ThingSpeak Communication Library for Arduino
|
---|
6 |
|
---|
7 | ThingSpeak ( https://www.thingspeak.com ) is a free IoT service for prototyping
|
---|
8 | systems that collect, analyze, and react to their environments.
|
---|
9 |
|
---|
10 | This test use the ArduinoUnit 2.1.0 unit test framework. Visit https://github.com/mmurdoch/arduinounit to learn more.
|
---|
11 |
|
---|
12 | Copyright 2015, The MathWorks, Inc.
|
---|
13 |
|
---|
14 | Documentation for the ThingSpeak Communication Library for Arduino is in the extras/documentation folder where the library was installed.
|
---|
15 | See the accompaning licence.txt file for licensing information.
|
---|
16 | */
|
---|
17 |
|
---|
18 | #include <ArduinoUnit.h>
|
---|
19 | #include <ThingSpeak.h>
|
---|
20 |
|
---|
21 | #ifdef ARDUINO_AVR_YUN
|
---|
22 | #include "YunClient.h"
|
---|
23 | YunClient client;
|
---|
24 | #else
|
---|
25 | // Assume that we're using a wired Ethernet shield on a Mega
|
---|
26 | #include <SPI.h>
|
---|
27 | #include <Ethernet.h>
|
---|
28 | byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
|
---|
29 | EthernetClient client;
|
---|
30 | #endif
|
---|
31 |
|
---|
32 | unsigned long testChannelNumber = 31461;
|
---|
33 | const char * testChannelWriteAPIKey = "LD79EOAAWRVYF04Y";
|
---|
34 |
|
---|
35 | #define WRITE_DELAY_FOR_THINGSPEAK 15000
|
---|
36 |
|
---|
37 | test(writeFieldCase)
|
---|
38 | {
|
---|
39 | // Always wait to ensure that rate limit isn't hit
|
---|
40 | delay(WRITE_DELAY_FOR_THINGSPEAK);
|
---|
41 |
|
---|
42 | // Test basic value write
|
---|
43 | assertEqual(OK_SUCCESS, ThingSpeak.writeField(testChannelNumber, 1, (float)1.0, testChannelWriteAPIKey));
|
---|
44 |
|
---|
45 | // Test write when not enough time has elapsed
|
---|
46 | assertEqual(ERR_NOT_INSERTED, ThingSpeak.writeField(testChannelNumber, 1, (float)1.0, testChannelWriteAPIKey));
|
---|
47 |
|
---|
48 | // Allow enough time to pass to make sure that it would work
|
---|
49 | delay(WRITE_DELAY_FOR_THINGSPEAK);
|
---|
50 |
|
---|
51 | // Test write to field out of range
|
---|
52 | assertEqual(ERR_INVALID_FIELD_NUM, ThingSpeak.writeField(testChannelNumber, 0, (float)1.0, testChannelWriteAPIKey));
|
---|
53 | assertEqual(ERR_INVALID_FIELD_NUM, ThingSpeak.writeField(testChannelNumber, 9, (float)1.0, testChannelWriteAPIKey));
|
---|
54 |
|
---|
55 | // Test write to invalid channel #
|
---|
56 | // ThingSpeak accepts this, since it only uses the API key to write. The Channel number is ignored.
|
---|
57 | assertEqual(OK_SUCCESS, ThingSpeak.writeField(0, 1, (float)1.0, testChannelWriteAPIKey));
|
---|
58 | delay(WRITE_DELAY_FOR_THINGSPEAK);
|
---|
59 | assertEqual(OK_SUCCESS, ThingSpeak.writeField(4294967295L, 1, (float)1.0, testChannelWriteAPIKey));
|
---|
60 |
|
---|
61 | // Test write with invalid API key
|
---|
62 | delay(WRITE_DELAY_FOR_THINGSPEAK);
|
---|
63 | assertEqual(ERR_BADAPIKEY, ThingSpeak.writeField(testChannelNumber, 1, (float)1.0, "AFAKEAPIKEYFAKEX"));
|
---|
64 | }
|
---|
65 |
|
---|
66 | test(writeFieldFloatCase)
|
---|
67 | {
|
---|
68 | // Always wait to ensure that rate limit isn't hit
|
---|
69 | delay(WRITE_DELAY_FOR_THINGSPEAK);
|
---|
70 |
|
---|
71 | // Test float write
|
---|
72 | assertEqual(OK_SUCCESS, ThingSpeak.writeField(testChannelNumber, 1, (float)1.0, testChannelWriteAPIKey));
|
---|
73 |
|
---|
74 | // Test max range values
|
---|
75 | delay(WRITE_DELAY_FOR_THINGSPEAK);
|
---|
76 | assertEqual(OK_SUCCESS, ThingSpeak.writeField(testChannelNumber, 1, (float)-999999000000, testChannelWriteAPIKey));
|
---|
77 | delay(WRITE_DELAY_FOR_THINGSPEAK);
|
---|
78 | assertEqual(OK_SUCCESS, ThingSpeak.writeField(testChannelNumber, 1, (float)999999000000, testChannelWriteAPIKey));
|
---|
79 |
|
---|
80 | // Test high precision values
|
---|
81 | delay(WRITE_DELAY_FOR_THINGSPEAK);
|
---|
82 | assertEqual(OK_SUCCESS, ThingSpeak.writeField(testChannelNumber, 1, (float)3.14159, testChannelWriteAPIKey));
|
---|
83 | delay(WRITE_DELAY_FOR_THINGSPEAK);
|
---|
84 | assertEqual(OK_SUCCESS, ThingSpeak.writeField(testChannelNumber, 1, (float)-3.14159, testChannelWriteAPIKey));
|
---|
85 |
|
---|
86 | // Test passing NaN and Inf
|
---|
87 | delay(WRITE_DELAY_FOR_THINGSPEAK);
|
---|
88 | assertEqual(OK_SUCCESS, ThingSpeak.writeField(testChannelNumber, 1, (float)NAN, testChannelWriteAPIKey));
|
---|
89 | delay(WRITE_DELAY_FOR_THINGSPEAK);
|
---|
90 | assertEqual(OK_SUCCESS, ThingSpeak.writeField(testChannelNumber, 1, (float)INFINITY, testChannelWriteAPIKey));
|
---|
91 | delay(WRITE_DELAY_FOR_THINGSPEAK);
|
---|
92 | assertEqual(OK_SUCCESS, ThingSpeak.writeField(testChannelNumber, 1, (float)-INFINITY, testChannelWriteAPIKey));
|
---|
93 |
|
---|
94 | // Test out of range values
|
---|
95 | assertEqual(ERR_OUT_OF_RANGE, ThingSpeak.writeField(testChannelNumber, 1, (float)-1000000000000.0, testChannelWriteAPIKey));
|
---|
96 | assertEqual(ERR_OUT_OF_RANGE, ThingSpeak.writeField(testChannelNumber, 1, (float)1000000000000.0, testChannelWriteAPIKey));
|
---|
97 | }
|
---|
98 |
|
---|
99 | #ifdef ARDUINO_AVR_MEGA2560 // Only the mega has enough memory for all these tests
|
---|
100 | test(writeFieldIntCase)
|
---|
101 | {
|
---|
102 | // Always wait to ensure that rate limit isn't hit
|
---|
103 | delay(WRITE_DELAY_FOR_THINGSPEAK);
|
---|
104 |
|
---|
105 | // Test int write
|
---|
106 | assertEqual(OK_SUCCESS, ThingSpeak.writeField(testChannelNumber, 1, (int)1, testChannelWriteAPIKey));
|
---|
107 |
|
---|
108 | // Test max range values
|
---|
109 | delay(WRITE_DELAY_FOR_THINGSPEAK);
|
---|
110 | assertEqual(OK_SUCCESS, ThingSpeak.writeField(testChannelNumber, 1, (int)-32768, testChannelWriteAPIKey));
|
---|
111 | delay(WRITE_DELAY_FOR_THINGSPEAK);
|
---|
112 | assertEqual(OK_SUCCESS, ThingSpeak.writeField(testChannelNumber, 1, (int)32767, testChannelWriteAPIKey));
|
---|
113 | }
|
---|
114 |
|
---|
115 | test(writeFieldLongCase)
|
---|
116 | {
|
---|
117 | // Always wait to ensure that rate limit isn't hit
|
---|
118 | delay(WRITE_DELAY_FOR_THINGSPEAK);
|
---|
119 |
|
---|
120 | // Test long write
|
---|
121 | assertEqual(OK_SUCCESS, ThingSpeak.writeField(testChannelNumber, 1, 1000000L, testChannelWriteAPIKey));
|
---|
122 |
|
---|
123 | // Test max range values
|
---|
124 | delay(WRITE_DELAY_FOR_THINGSPEAK);
|
---|
125 | assertEqual(OK_SUCCESS, ThingSpeak.writeField(testChannelNumber, 1, (long)-2147483648L, testChannelWriteAPIKey));
|
---|
126 | delay(WRITE_DELAY_FOR_THINGSPEAK);
|
---|
127 | assertEqual(OK_SUCCESS, ThingSpeak.writeField(testChannelNumber, 1, (long)2147483647L, testChannelWriteAPIKey));
|
---|
128 | }
|
---|
129 |
|
---|
130 | test(writeFieldCharStarCase)
|
---|
131 | {
|
---|
132 | // Always wait to ensure that rate limit isn't hit
|
---|
133 | delay(WRITE_DELAY_FOR_THINGSPEAK);
|
---|
134 |
|
---|
135 | // Test char * write
|
---|
136 | assertEqual(OK_SUCCESS, ThingSpeak.writeField(testChannelNumber, 1, "The Rain in Spain", testChannelWriteAPIKey));
|
---|
137 |
|
---|
138 | // Test empty string
|
---|
139 | delay(WRITE_DELAY_FOR_THINGSPEAK);
|
---|
140 | assertEqual(OK_SUCCESS, ThingSpeak.writeField(testChannelNumber, 1, "", testChannelWriteAPIKey));
|
---|
141 |
|
---|
142 | char longString[300];
|
---|
143 |
|
---|
144 | // Test max string
|
---|
145 | memset(longString, '0',255);
|
---|
146 | longString[255] = 0;
|
---|
147 | delay(WRITE_DELAY_FOR_THINGSPEAK);
|
---|
148 | assertEqual(OK_SUCCESS, ThingSpeak.writeField(testChannelNumber, 1, longString, testChannelWriteAPIKey));
|
---|
149 |
|
---|
150 | // Test long string
|
---|
151 | memset(longString, '0',sizeof(longString)/sizeof(longString[0]) - 1);
|
---|
152 | longString[sizeof(longString)] = 0;
|
---|
153 | delay(WRITE_DELAY_FOR_THINGSPEAK);
|
---|
154 | assertEqual(ERR_OUT_OF_RANGE, ThingSpeak.writeField(testChannelNumber, 1, longString, testChannelWriteAPIKey));
|
---|
155 | }
|
---|
156 |
|
---|
157 | test(writeFieldStringCase)
|
---|
158 | {
|
---|
159 | // Always wait to ensure that rate limit isn't hit
|
---|
160 | delay(WRITE_DELAY_FOR_THINGSPEAK);
|
---|
161 |
|
---|
162 | // Test String write
|
---|
163 | assertEqual(OK_SUCCESS, ThingSpeak.writeField(testChannelNumber, 1, String("The Rain in Spain"), testChannelWriteAPIKey));
|
---|
164 |
|
---|
165 | // Test empty string
|
---|
166 | delay(WRITE_DELAY_FOR_THINGSPEAK);
|
---|
167 | assertEqual(OK_SUCCESS, ThingSpeak.writeField(testChannelNumber, 1, String(), testChannelWriteAPIKey));
|
---|
168 |
|
---|
169 | unsigned int numChar = 300;
|
---|
170 | String longString;
|
---|
171 | longString.reserve(numChar);
|
---|
172 |
|
---|
173 | // Test max string
|
---|
174 | for(unsigned int i = 0; i < 255; i++)
|
---|
175 | {
|
---|
176 | longString += '0';
|
---|
177 | }
|
---|
178 | delay(WRITE_DELAY_FOR_THINGSPEAK);
|
---|
179 | assertEqual(OK_SUCCESS, ThingSpeak.writeField(testChannelNumber, 1, longString, testChannelWriteAPIKey));
|
---|
180 |
|
---|
181 | // Test long string
|
---|
182 | longString.reserve(numChar);
|
---|
183 | for(unsigned int i = 0; i < numChar; i++)
|
---|
184 | {
|
---|
185 | longString += '0';
|
---|
186 | }
|
---|
187 | delay(WRITE_DELAY_FOR_THINGSPEAK);
|
---|
188 | assertEqual(ERR_OUT_OF_RANGE, ThingSpeak.writeField(testChannelNumber, 1, longString, testChannelWriteAPIKey));
|
---|
189 | }
|
---|
190 | #endif // Mega only tests
|
---|
191 |
|
---|
192 | void setup()
|
---|
193 | {
|
---|
194 | Serial.begin(9600);
|
---|
195 | while(!Serial); // for the Arduino Leonardo/Micro only
|
---|
196 | #ifdef ARDUINO_AVR_YUN
|
---|
197 | Bridge.begin();
|
---|
198 | #else
|
---|
199 | Ethernet.begin(mac);
|
---|
200 | #endif
|
---|
201 | ThingSpeak.begin(client);
|
---|
202 | }
|
---|
203 |
|
---|
204 | void loop()
|
---|
205 | {
|
---|
206 | Test::run();
|
---|
207 | }
|
---|
208 |
|
---|