-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-llap.py
124 lines (105 loc) · 5.17 KB
/
test-llap.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
__author__ = 'timhodson'
from LLAP import LLAP, LlapException
from pprint import pformat
import unittest
class TestLLAPFunctionality(unittest.TestCase):
def setUp(self):
self.llap = LLAP()
def test_build_request(self):
self.assertEqual(self.llap.build_request('AA', 'TEMP'), 'aAATEMP-----')
self.assertEqual(self.llap.build_request('AA', 'HELLO'), 'aAAHELLO----')
self.assertRaises(LlapException, self.llap.build_request, 'AAB', 'HELLO')
self.assertRaises(LlapException, self.llap.build_request, 'AB', 'HELLOHELLO')
def test_register_observer(self):
self.assertEqual(len(self.llap.observers), 0)
# note that we test with a fictitious event name
# These tests run in parallel and don't seem to be run with their own llap object so events get fired!
self.llap.register_observer('TMP', self.print_data)
self.assertEqual(len(self.llap.observers), 1)
self.assertDictEqual(self.llap.observers[0], {'TMP': self.print_data})
self.llap.unregister_observer('TMP', self.print_data)
self.assertEqual(len(self.llap.observers), 0)
def test_get_responses(self):
data = self.llap.get_responses("aABBATT2.67-")
self.assertDictEqual(self.update_time(data[0]),
{'responseType': 'BATT', 'raw': 'aABBATT2.67-', 'responseValue': '2.67', 'deviceId': 'AB',
'time': 0})
data = self.llap.get_responses("aABBATTLOW--")
self.assertDictEqual(self.update_time(data[0]),
{'responseType': 'BATT', 'raw': 'aABBATTLOW--', 'responseValue': 'LOW', 'deviceId': 'AB',
'time': 0})
data = self.llap.get_responses("aABAWAKE----")
self.assertDictEqual(self.update_time(data[0]),
{'responseType': 'AWAKE', 'raw': 'aABAWAKE----', 'responseValue': '', 'deviceId': 'AB',
'time': 0})
data = self.llap.get_responses("aABSLEEPING-")
self.assertDictEqual(self.update_time(data[0]),
{'responseType': 'SLEEPING', 'raw': 'aABSLEEPING-', 'responseValue': '', 'deviceId': 'AB',
'time': 0})
def test_multiple_responses(self):
data = self.llap.get_responses("aABTMPA20.24aABAWAKE----aABBATT2.76-aABSLEEPING-")
data = [self.update_time(x) for x in data]
expected = [{'deviceId': 'AB',
'raw': 'aABTMPA20.24',
'responseType': 'TMPA',
'responseValue': '20.24',
'time': 0},
{'deviceId': 'AB',
'raw': 'aABAWAKE----',
'responseType': 'AWAKE',
'responseValue': '',
'time': 0},
{'deviceId': 'AB',
'raw': 'aABBATT2.76-',
'responseType': 'BATT',
'responseValue': '2.76',
'time': 0},
{'deviceId': 'AB',
'raw': 'aABSLEEPING-',
'responseType': 'SLEEPING',
'responseValue': '',
'time': 0}]
self.assertEqual(data, expected)
def test_messages_of_incorrect_length(self):
"""
The messages should still get completed even if one of the messages was not read completely from serial input.
(all llap messages should start with a lower case 'a' and run for 12 chars.
"""
data = self.llap.get_responses("--aZZTMPA20.23aABAWAKE----aABBATT2.76-aABSLEEPING-aZZ-----")
data = [self.update_time(x) for x in data]
expected = [{'deviceId': 'ZZ',
'raw': 'aZZTMPA20.23',
'responseType': 'TMPA',
'responseValue': '20.23',
'time': 0},
{'deviceId': 'AB',
'raw': 'aABAWAKE----',
'responseType': 'AWAKE',
'responseValue': '',
'time': 0},
{'deviceId': 'AB',
'raw': 'aABBATT2.76-',
'responseType': 'BATT',
'responseValue': '2.76',
'time': 0},
{'deviceId': 'AB',
'raw': 'aABSLEEPING-',
'responseType': 'SLEEPING',
'responseValue': '',
'time': 0}]
self.assertEqual(data, expected)
def update_time(self, d):
if d.get('time'):
d['time'] = 0
return d
def print_data(self, data):
print "EVENT %s TRIGGERED and passed DATA: %s" % (data['responseType'], pformat(data))
def suite1():
suite = unittest.TestSuite()
suite.addTest(TestLLAPFunctionality("test_build_request"))
suite.addTest(TestLLAPFunctionality("test_register_observer"))
suite.addTest(TestLLAPFunctionality("test_get_responses"))
suite.addTest(TestLLAPFunctionality("test_multiple_responses"))
suite.addTest(TestLLAPFunctionality("test_messages_of_incorrect_length"))
return suite
unittest.TextTestRunner(verbosity=2).run(suite1())