forked from thebrilliantidea/CyberQInterface
/
tests.py
442 lines (404 loc) · 16.5 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
#!/usr/bin/python
"""
Test Cases for the CyberQInterface Library
ChangeLog:
A00 - 11/04/2012 - Bryan Kemp - Initial release
"""
__author__ = "Bryan Kemp <bryan@thebrilliantidea.com>"
__version__ = "A00"
__date__ = '11/04/2012'
__license__ = "BSD New"
__license_text__ = """
Copyright (c) 2012, The Brilliant Idea
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the The Brilliant Idea, LLC. nor the names of its contributors may
be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""
import unittest
import requests
from mock import patch
from cyberqinterface.cyberqinterface import CyberQInterface
from cyberqinterface.cyberqinterface_exceptions import *
TestCyberQInterfaceSuite = unittest.TestLoader()
class TestCyberQInterfaceInit(unittest.TestCase):
"""Test that the initializer works and gets all of the needed variables"""
def setUp(self):
pass
def tearDown(self):
"""None"""
pass
def testProperInit(self):
"""Test host property is set to proper value"""
cqi = CyberQInterface("localhost")
self.assertEqual(cqi.host, "localhost")
def testInitWithHeaders(self):
"""Test supplying custom headers"""
cqi = CyberQInterface("localhost", {"Test-Header": "Test"})
self.assertEqual(cqi.headers["Test-Header"], "Test")
TestCyberQInterfaceSuite.loadTestsFromTestCase(TestCyberQInterfaceInit)
class TestCyberQInterfaceValidateParameters(unittest.TestCase):
"""Test to make sure that the validate parameters function is working"""
def setUp(self):
"""Setup: None"""
def tearDown(self):
"""TearDown: None"""
pass
def testAllParameters(self):
"""Test the full set of parameters are correct"""
cqi = CyberQInterface("127.0.0.1")
assert cqi._validateParameters( {"COOK_NAME": 1, "COOK_SET": 1,
"FOOD1_NAME": 1, "FOOD1_SET": 1,
"FOOD2_NAME": 1, "FOOD2_SET": 1,
"FOOD3_NAME": 1, "FOOD3_SET": 1,
"_COOK_TIMER": 1, "COOK_TIMER": 1,
"COOKHOLD": 1, "TIMEOUT_ACTION": 1,
"ALARMDEV": 1, "COOK_RAMP": 1,
"OPENDETECT": 1, "CYCTIME": 1,
"PROPBAND": 1, "MENU_SCROLLING": 1,
"LCD_BACKLIGHT": 1, "LCD_CONTRAST": 1,
"DEG_UNITS": 1, "ALARM_BEEPS": 1,
"KEY_BEEPS": 1}) == {}
def testBadParameters(self):
"""Test parameters with 3 bad parameters"""
cqi = CyberQInterface("127.0.0.1")
assert len(cqi._validateParameters( {"COK_NAME": 1, "COOK_SET": 1,
"FOOD1_NAME": 1, "FOOD1_SET": 1,
"FOOD2_NAME": 1, "FOOD2_SET": 1,
"FOOD3_NAME": 1, "FOOD3_SET": 1,
"_COOK_TIMER": 1, "COOK_TIMER": 1,
"COOKHOLD": 1, "TIEOUT_ACTION": 1,
"ALARMDEV": 1, "COOK_RAMP": 1,
"OPENDETECT": 1, "CYCTIME": 1,
"PROPBAND": 1, "MNU_SCROLLING": 1,
"LCD_BACKLIGHT": 1,
"LCD_CONTRAST": 1, "DEG_UNITS": 1,
"ALARM_BEEPS": 1,
"KEY_BEEPS": 1})) == 3
TestCyberQInterfaceSuite.loadTestsFromTestCase(
TestCyberQInterfaceValidateParameters)
class TestCyberQInterfaceRequests(unittest.TestCase):
"""
Test the interaction with the CyberQ web interface and XML to object
translation
"""
def setUp(self):
"""Setup: None"""
def tearDown(self):
"""TearDown: None"""
def testInvalidReturnCode(self):
"""
Test that the proper exception is thrown if the server returns bad
status code
"""
with patch.object(requests, 'get') as mockMethod:
with self.assertRaises(ResponseHTTPException):
cqi = CyberQInterface("127.0.0.1")
mockMethod.return_value.status_code = 500
cqi.getConfigXML()
def testValidReturnCode(self):
"""Test that a 200 code returns a string"""
with patch.object(requests, 'get') as mockMethod:
mockMethod.return_value.status_code = 200
mockMethod.return_value.text = "testcase"
xml = CyberQInterface("127.0.0.1").getConfigXML()
self.assertEqual(xml, "testcase")
def testGetStatusWithBadXML(self):
"""Test that the correct Object is returned"""
with patch.object(requests, 'get') as mockMethod:
with self.assertRaises(ResponseValidationException):
mockMethod.return_value.status_code = 200
mockMethod.return_value.text = """
<nutcstatus>
<!--all temperatures are displayed in tenths F, regardless of setting of unit-->
<!--all temperatures sent by browser to unit should be in F. you can send-->
<!--tenths F with a decimal place, ex: 123.5-->
<OUTPUT_PERCENT>100</OUTPUT_PERCENT>
<TIMER_CURR>00:00:00</TIMER_CURR>
<COOK_TEMP>3343</COOK_TEMP>
<FOOD1_TEMP>823</FOOD1_TEMP>
<FOOD2_TEM2_TEMP>
<FOOD3_TEMP>OPEN</FOOD3_TEMP>
<COOK_STATUS>0</COOKTUS>
<FOOD1_STATUS>0</FOOD1_STATUS>
<FOOD2_STATUS>4</FOOD2_STATUS>
<FOOD3_STATUS>4</FOOD3_STATUS>
<TIMER_STATUS>0</TIMER_STATUS>
<DEG_UNITS>1</DEG_UNITS>
<COOK_CYCTIME>6</COOK_CYCTIME>
<COOK_PROPBAND>500</COOK_PROPBAND>
<COOK_RAMP>0</COOK_RAMP>
</nutcstatus>"""
CyberQInterface("127.0.0.1").getStatus()
def testGetStatus(self):
"""Test that the correct Object is returned"""
with patch.object(requests, 'get') as mockMethod:
mockMethod.return_value.status_code = 200
mockMethod.return_value.text = """
<nutcstatus>
<!--all temperatures are displayed in tenths F, regardless of setting of unit-->
<!--all temperatures sent by browser to unit should be in F. you can send-->
<!--tenths F with a decimal place, ex: 123.5-->
<OUTPUT_PERCENT>100</OUTPUT_PERCENT>
<TIMER_CURR>00:00:00</TIMER_CURR>
<COOK_TEMP>3343</COOK_TEMP>
<FOOD1_TEMP>823</FOOD1_TEMP>
<FOOD2_TEMP>OPEN</FOOD2_TEMP>
<FOOD3_TEMP>OPEN</FOOD3_TEMP>
<COOK_STATUS>0</COOK_STATUS>
<FOOD1_STATUS>0</FOOD1_STATUS>
<FOOD2_STATUS>4</FOOD2_STATUS>
<FOOD3_STATUS>4</FOOD3_STATUS>
<TIMER_STATUS>0</TIMER_STATUS>
<DEG_UNITS>1</DEG_UNITS>
<COOK_CYCTIME>6</COOK_CYCTIME>
<COOK_PROPBAND>500</COOK_PROPBAND>
<COOK_RAMP>0</COOK_RAMP>
</nutcstatus>"""
status = CyberQInterface("127.0.0.1").getStatus()
self.assertEqual(status.tag, "nutcstatus")
def testGetAll(self):
"""Test that the All Object is returned"""
with patch.object(requests, 'get') as mockMethod:
with self.assertRaises(AttributeError):
mockMethod.return_value.status_code = 200
mockMethod.return_value.text = """
<nutcallstatus>
<!--this is similar to status.xml, but with more values-->
<!--all temperatures are displayed in tenths F, regardless of setting of unit-->
<!--all temperatures sent by browser to unit should be in F. you can send-->
<!--tenths F with a decimal place, ex: 123.5-->
<COOK>
<COOK_NAME>Big Green Egg</COOK_NAME>
<COOK_TEMP>3216</COOK_TEMP>
<COOK_SET>4000</COOK_SET>
<COOK_STATUS>0</COOK_STATUS>
</COOK>
<FOOD1>
<FOOD1_NAME>Chicken Quarters</FOOD1_NAME>
<FOOD1_TEMP>1482</FOOD1_TEMP>
<FOOD1_SET>1750</FOOD1_SET>
<FOOD1_STATUS>0</FOOD1_STATUS>
</FOOD1>
<FOOD2>
<FOOD2_NAME>Food2</FOOD2_NAME>
<FOOD2_TEMP>OPEN</FOOD2_TEMP>
<FOOD2_SET>1000</FOOD2_SET>
<FOOD2_STATUS>4</FOOD2_STATUS>
</FOOD2>
<FOOD3>
<FOOD3_NAME>Food3</FOOD3_NAME>
<FOOD3_TEMP>OPEN</FOOD3_TEMP>
<FOOD3_SET>1000</FOOD3_SET>
<FOOD3_STATUS>4</FOOD3_STATUS>
</FOOD3>
<OUTPUT_PERCENT>100</OUTPUT_PERCENT>
<TIMER_CURR>00:00:00</TIMER_CURR>
<TIMER_STATUS>0</TIMER_STATUS>
<DEG_UNITS>1</DEG_UNITS>
<COOK_CYCTIME>6</COOK_CYCTIME>
<COOK_PROPBAND>500</COOK_PROPBAND>
<COOK_RAMP>0</COOK_RAMP>
</nutcallstatus>
"""
allObj = CyberQInterface("127.0.0.1").getAll()
self.assertNotEqual(allObj.CONTROL.OPENDETECT, 1)
def testGetConfig(self):
"""Test that the config Object is returned"""
with patch.object(requests, 'get') as mockMethod:
mockMethod.return_value.status_code = 200
mockMethod.return_value.text = """
<nutcallstatus>
<!--this is similar to all.xml, but with more values-->
<!--all temperatures are displayed in tenths F, regardless of setting of unit-->
<!--all temperatures sent by browser to unit should be in F. you can send-->
<!--tenths F with a decimal place, ex: 123.5-->
<COOK>
<COOK_NAME>Big Green Egg</COOK_NAME>
<COOK_TEMP>3220</COOK_TEMP>
<COOK_SET>4000</COOK_SET>
<COOK_STATUS>0</COOK_STATUS>
</COOK>
<FOOD1>
<FOOD1_NAME>Chicken Quarters</FOOD1_NAME>
<FOOD1_TEMP>1493</FOOD1_TEMP>
<FOOD1_SET>1750</FOOD1_SET>
<FOOD1_STATUS>0</FOOD1_STATUS>
</FOOD1>
<FOOD2>
<FOOD2_NAME>Food2</FOOD2_NAME>
<FOOD2_TEMP>OPEN</FOOD2_TEMP>
<FOOD2_SET>1000</FOOD2_SET>
<FOOD2_STATUS>4</FOOD2_STATUS>
</FOOD2>
<FOOD3>
<FOOD3_NAME>Food3</FOOD3_NAME>
<FOOD3_TEMP>OPEN</FOOD3_TEMP>
<FOOD3_SET>1000</FOOD3_SET>
<FOOD3_STATUS>4</FOOD3_STATUS>
</FOOD3>
<OUTPUT_PERCENT>100</OUTPUT_PERCENT>
<TIMER_CURR>00:00:00</TIMER_CURR>
<TIMER_STATUS>0</TIMER_STATUS>
<SYSTEM>
<MENU_SCROLLING>1</MENU_SCROLLING>
<LCD_BACKLIGHT>47</LCD_BACKLIGHT>
<LCD_CONTRAST>10</LCD_CONTRAST>
<DEG_UNITS>1</DEG_UNITS>
<ALARM_BEEPS>0</ALARM_BEEPS>
<KEY_BEEPS>0</KEY_BEEPS>
</SYSTEM>
<CONTROL>
<TIMEOUT_ACTION>0</TIMEOUT_ACTION>
<COOKHOLD>2000</COOKHOLD>
<ALARMDEV>500</ALARMDEV>
<COOK_RAMP>0</COOK_RAMP>
<OPENDETECT>1</OPENDETECT>
<CYCTIME>6</CYCTIME>
<PROPBAND>500</PROPBAND>
</CONTROL>
<WIFI>
<IP>10.0.1.30</IP>
<NM>255.255.255.0</NM>
<GW>10.0.1.1</GW>
<DNS>10.0.1.1</DNS>
<WIFIMODE>0</WIFIMODE>
<DHCP>0</DHCP>
<SSID>Wireless Network</SSID>
<WIFI_ENC>6</WIFI_ENC>
<WIFI_KEY>SsecretKey</WIFI_KEY>
<HTTP_PORT>80</HTTP_PORT>
</WIFI>
<SMTP>
<SMTP_HOST>smtp.hostname.com</SMTP_HOST>
<SMTP_PORT>0</SMTP_PORT>
<SMTP_USER></SMTP_USER>
<SMTP_PWD></SMTP_PWD>
<SMTP_TO>destination@someplace.com</SMTP_TO>
<SMTP_FROM>source@someplace.com</SMTP_FROM>
<SMTP_SUBJ>Temperature Controller Status E-Mail</SMTP_SUBJ>
<SMTP_ALERT>0</SMTP_ALERT>
</SMTP>
</nutcallstatus>
"""
configObj = CyberQInterface("127.0.0.1").getConfig()
self.assertEqual(configObj.CONTROL.OPENDETECT, 1)
def testSendUpdate(self):
"""
Test the ability to send a message to the CyberQ with good parameters
"""
with patch.object(requests, 'post') as mockMethod:
mockMethod.return_value.status_code = 200
cqi = CyberQInterface("127.0.0.1")
self.assertEqual(cqi.sendUpdate({'FOOD1_NAME' : "Tri-Tip Roast",
'FOOD1_SET': '140',
'COOK_SET' : '300'}),True)
def testSendUpdateWithBadParameters(self):
"""
Test the ability to send a message to the CyberQ with bad parameters
"""
with patch.object(requests, 'post') as mockMethod:
with self.assertRaises(ParameterValidationException):
mockMethod.return_value.status_code = 200
cqi = CyberQInterface("127.0.0.1")
cqi.sendUpdate({'FOOD1_NME' : "Tri-Tip Roast",
'FOOD1_SET': '140', 'COOK_SET' : '300'})
def testSendUpdateWithBadResponse(self):
"""
Test the ability to send a message to the CyberQ with good parameters
but a bad response
"""
with patch.object(requests, 'post') as mockMethod:
with self.assertRaises(ResponseHTTPException):
mockMethod.return_value.status_code = 500
cqi = CyberQInterface("127.0.0.1")
cqi.sendUpdate({'FOOD1_NAME' : "Tri-Tip Roast",
'FOOD1_SET': '140', 'COOK_SET' : '300'})
TestCyberQInterfaceSuite.loadTestsFromTestCase(TestCyberQInterfaceRequests)
class TestCyberQInterfaceLookups(unittest.TestCase):
"""Test the lookup functionality for temperature, status, and ramp"""
def setUp(self):
"""Setup: None"""
pass
def tearDown(self):
"""TearDown: None"""
pass
def testRampLookups(self):
"""Test that all known ramp codes work"""
cqi = CyberQInterface("127.0.0.1")
rampCodes = ["OFF", "FOOD1", "FOOD2", "FOOD3"]
for code in range (0, len(rampCodes)-1):
self.assertEqual(cqi.rampLookup(code), rampCodes[code])
def testStatusLookups(self):
"""Test that all known status codes work"""
cqi = CyberQInterface("127.0.0.1")
statusCodes = ["OK", "HIGH", "LOW", "DONE", "ERROR", "HOLD", "ALARM",
"SHUTDOWN"]
for code in range (0, len(statusCodes)-1):
self.assertEqual(cqi.statusLookup(code), statusCodes[code])
def testTemperatureLookups(self):
"""Test that all known temperature codes work"""
cqi = CyberQInterface("127.0.0.1")
temperatureCodes = ["CELSIUS", "FAHRENHEIT"]
for code in range (0, len(temperatureCodes)-1):
self.assertEqual(cqi.temperatureLookup(code),
temperatureCodes[code])
def testBadTableName(self):
"""Test that an unknown table produces a known exception"""
with self.assertRaises(LookupException):
cqi = CyberQInterface("127.0.0.1")
cqi._lookup("badtable", 0)
def testInvalidCodeNumber(self):
"""Test that an index out of range is handled"""
with self.assertRaises(LookupException):
cqi = CyberQInterface("127.0.0.1")
cqi._lookup("temperature", 4)
def testLookupFromStatusObject(self):
"""Test that the code from an actual status object works properly"""
with patch.object(requests, 'get') as mockMethod:
mockMethod.return_value.status_code = 200
mockMethod.return_value.text = """
<nutcstatus>
<!--all temperatures are displayed in tenths F, regardless of setting of unit-->
<!--all temperatures sent by browser to unit should be in F. you can send-->
<!--tenths F with a decimal place, ex: 123.5-->
<OUTPUT_PERCENT>100</OUTPUT_PERCENT>
<TIMER_CURR>00:00:00</TIMER_CURR>
<COOK_TEMP>3343</COOK_TEMP>
<FOOD1_TEMP>823</FOOD1_TEMP>
<FOOD2_TEMP>OPEN</FOOD2_TEMP>
<FOOD3_TEMP>OPEN</FOOD3_TEMP>
<COOK_STATUS>0</COOK_STATUS>
<FOOD1_STATUS>0</FOOD1_STATUS>
<FOOD2_STATUS>4</FOOD2_STATUS>
<FOOD3_STATUS>4</FOOD3_STATUS>
<TIMER_STATUS>0</TIMER_STATUS>
<DEG_UNITS>1</DEG_UNITS>
<COOK_CYCTIME>6</COOK_CYCTIME>
<COOK_PROPBAND>500</COOK_PROPBAND>
<COOK_RAMP>0</COOK_RAMP>
</nutcstatus>"""
cqi = CyberQInterface("127.0.0.1")
status = cqi.getStatus()
self.assertEqual(cqi.statusLookup(status.FOOD1_STATUS),"OK")
TestCyberQInterfaceSuite.loadTestsFromTestCase(TestCyberQInterfaceLookups)
if __name__ == '__main__':
import nose
nose.main()