-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunit_tests.py
202 lines (167 loc) · 9.91 KB
/
unit_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
#!/usr/bin/env python
"""
Unit tests for Sim900 GSM module
"""
from os import remove
import sqlite3
import unittest
from core import SensorReadingsDataStore, DataParser, MessageFormatError
from sim900 import Sim900, SMSReader, TextMsg
from utils import get_config, parse_sensor_types_config, create_logger_from_config
class TestSMSReader(unittest.TestCase):
def setUp(self):
mock_sim900 = {}
self.sms_reader = SMSReader(mock_sim900)
def test_extract_valid_sms(self):
sms = "\r\n+CMT: \"+12223334444\",\"\",\"14/05/30,00:13:34-32\"\r\n1399735312 100 10;20 200 20;40 400 40;65 600 60\r\n"
result_text_msg = self.sms_reader.extract_sms(sms)
expected_text_msg = TextMsg("+12223334444", "14/05/30,00:13:34-32", "1399735312 100 10;20 200 20;40 400 40;65 600 60")
self.assertEqual(result_text_msg, expected_text_msg)
def test_extract_valid_sms_with_gibberish(self):
expected_sms = "\r\n+CMT: \"+12223334444\",\"\",\"14/05/30,00:13:34-32\"\r\n1399735312 100 10;20 200 20;40 400 40;65 600 60\r\n"
gibberish_sms = "asdadad adasds+CMadTsadad\r\nsdasdasd" + expected_sms + "\r\nsdsdfsdf sf\r\n+CM"
result_text_msg = self.sms_reader.extract_sms(expected_sms)
expected_text_msg = TextMsg("+12223334444", "14/05/30,00:13:34-32", "1399735312 100 10;20 200 20;40 400 40;65 600 60")
self.assertEqual(result_text_msg, expected_text_msg)
def test_extract_invalid_sms_missing_cmt(self):
sms = "sdfsdfsdf\r\n+CMDT: \"+12223334444\",\"\",\"14/05/30,00:13:34-32\"\r\n1399735312 100 10;20 200 20;40 400 40;65 600 60\r\ndsfsd\r\nf"
result_text_msg = self.sms_reader.extract_sms(sms)
self.assertIsNone(result_text_msg)
def test_extract_invalid_sms_wrong_header(self):
sms = "sdfsdfsdf\r\n+CMT: \"+12223334444\",\"\",\"14/05/30,00:13:34-32\", \"extra_param\"\r\n399735312 100 10;20 200 20;40 400 40;65 6dsfsdff"
result_text_msg = self.sms_reader.extract_sms(sms)
self.assertIsNone(result_text_msg)
def test_extract_invalid_sms_garbled_ending(self):
sms = "sdfsdfsdf\r\n+CMT: \"+12223334444\",\"\",\"14/05/30,00:13:34-32\"\r\n399735312 100 10;20 200 20;40 400 40;65 6dsfsdff"
result_text_msg = self.sms_reader.extract_sms(sms)
self.assertIsNone(result_text_msg)
def test_extract_positive_timediff(self):
sms = "\r\n+CMT: \"+12223334444\",\"\",\"14/05/30,00:13:34+32\"\r\n1399735312 100 10;20 200 20;40 400 40;65 600 60\r\n"
result_text_msg = self.sms_reader.extract_sms(sms)
expected_text_msg = TextMsg("+12223334444", "14/05/30,00:13:34+32", "1399735312 100 10;20 200 20;40 400 40;65 600 60")
self.assertEqual(result_text_msg, expected_text_msg)
class TestUtils(unittest.TestCase):
def test_util_create_logger_from_config(self):
test_log_file = 'unit_test.log'
config = {
'DATA_LOGGER_ERROR_FILE': test_log_file,
'DATA_LOGGER_ERROR_LEVEL': 'WARN'
}
logger = create_logger_from_config(config)
remove(test_log_file)
def test_util_parse_sensor_types_config(self):
expected_result = {
'a': {
'name': 'A_TYPE',
'data_types': (('distance, meters'), ('temperature celsius'))
},
'b': {
'name': 'B_TYPE',
'data_types': (('distance, meters'))
},
'c': {
'name': 'B_TYPE',
'data_types': (('moisture, percent'), ('temperature celsius'), ('distance, meters'))
},
}
sensor_types = (
("a", "A_TYPE", "distance meters; temperature celsius"),
("b", "B_TYPE", "distance meters"),
("c", "C_TYPE", "moisture percent; temperature celsius; distance meters"),
)
result = parse_sensor_types_config(sensor_types)
self.assertItemsEqual(result, expected_result)
class TestDataParser(unittest.TestCase):
def setUp(self):
config = get_config()
sensor_types_map = parse_sensor_types_config(config['SENSOR_TYPES'])
self.parser = DataParser(sensor_types_map)
def test_valid_readings(self):
expected_readings = [
["+12223334444", 'Water', 1399735312, 100, 'distance', 'centimeters'],
["+12223334444", 'Water', 1399735312, 10, 'temperature', 'celsius'],
["+12223334444", 'Water', 1399736512, 200, 'distance', 'centimeters'],
["+12223334444", 'Water', 1399736512, 20, 'temperature', 'celsius'],
["+12223334444", 'Water', 1399737712, 400, 'distance', 'centimeters'],
["+12223334444", 'Water', 1399737712, 40, 'temperature', 'celsius'],
["+12223334444", 'Water', 1399739212, 600, 'distance', 'centimeters'],
["+12223334444", 'Water', 1399739212, 60, 'temperature', 'celsius'],
]
text_msg = TextMsg("+12223334444", "14/05/30,00:13:34-32", "d 1399735312 100 10;20 200 20;40 400 40;65 600 60")
readings = self.parser.parse(text_msg.phone_number, text_msg.message)
self.assertItemsEqual(readings, expected_readings)
def test_valid_negative_readings(self):
expected_readings = [
["+12223334444", 'Water', 1399735312, -100, 'distance', 'centimeters'],
["+12223334444", 'Water', 1399735312, 10, 'temperature', 'celsius'],
["+12223334444", 'Water', 1399736512, 200, 'distance', 'centimeters'],
["+12223334444", 'Water', 1399736512, -20, 'temperature', 'celsius'],
["+12223334444", 'Water', 1399737712, 400, 'distance', 'centimeters'],
["+12223334444", 'Water', 1399737712, 40, 'temperature', 'celsius'],
["+12223334444", 'Water', 1399739212, 600, 'distance', 'centimeters'],
["+12223334444", 'Water', 1399739212, 60, 'temperature', 'celsius'],
]
text_msg = TextMsg("+12223334444", "14/05/30,00:13:34-32", "d 1399735312 -100 10;20 200 -20;40 400 40;65 600 60")
readings = self.parser.parse(text_msg.phone_number, text_msg.message)
self.assertItemsEqual(readings, expected_readings)
def test_multiple_valid_readings(self):
expected_readings = [
# First Set
["+12223334444", 'Water', 1399735312, 100, 'distance', 'centimeters'],
["+12223334444", 'Water', 1399735312, 10, 'temperature', 'celsius'],
["+12223334444", 'Water', 1399736512, 200, 'distance', 'centimeters'],
["+12223334444", 'Water', 1399736512, 20, 'temperature', 'celsius'],
["+12223334444", 'Water', 1399737712, 400, 'distance', 'centimeters'],
["+12223334444", 'Water', 1399737712, 40, 'temperature', 'celsius'],
["+12223334444", 'Water', 1399739212, 600, 'distance', 'centimeters'],
["+12223334444", 'Water', 1399739212, 60, 'temperature', 'celsius'],
# Second set
["+12223334444", 'Water', 1399737712, 400, 'distance', 'centimeters'],
["+12223334444", 'Water', 1399737712, 40, 'temperature', 'celsius'],
["+12223334444", 'Water', 1399739212, 600, 'distance', 'centimeters'],
["+12223334444", 'Water', 1399739212, 60, 'temperature', 'celsius'],
]
text_msg = TextMsg("+12223334444", "14/05/30,00:13:34-32", "d 1399735312 100 10;20 200 20;40 400 40;65 600 60=d 1399737712 400 40;25 600 60")
readings = self.parser.parse(text_msg.phone_number, text_msg.message)
self.assertItemsEqual(readings, expected_readings)
def test_no_sensor_type_reading(self):
with self.assertRaises(MessageFormatError):
text_msg = TextMsg("+12223334444", "14/05/30,00:13:34-32", "1399735312 100 10;20 200 20;40 400 40;65 600 60123; 23123 23")
readings = self.parser.parse(text_msg.phone_number, text_msg.message)
def test_incorrect_sensor_type_reading(self):
with self.assertRaises(MessageFormatError):
text_msg = TextMsg("+12223334444", "14/05/30,00:13:34-32", "g 1399735312 100 10;20 200 20;40 400 40;65 600 60123; 23123 23")
readings = self.parser.parse(text_msg.phone_number, text_msg.message)
def test_garbled_reading(self):
with self.assertRaises(MessageFormatError):
text_msg = TextMsg("+12223334444", "14/05/30,00:13:34-32", "d 1399735312 100 10;20 200 20;40 400 40;65 600 60123; 23123 23")
readings = self.parser.parse(text_msg.phone_number, text_msg.message)
def test_valid_invalid_readings(self):
with self.assertRaises(MessageFormatError):
text_msg = TextMsg("+12223334444", "14/05/30,00:13:34-32", "Hello, this is a random text message.")
readings = self.parser.parse(text_msg.phone_number, text_msg.message)
class TestSensorReadingsDataStore(unittest.TestCase):
def setUp(self):
self.data_logger = SensorReadingsDataStore(':memory:', True)
self.data_logger.setup()
def test_valid_readings(self):
readings = [
["+12223334444", 'distance', 1399735312, 100, 'distance', 'centimeters'],
["+12223334444", 'distance', 1399735312, 20, 'temperature', 'celsius'],
["+12223334444", 'gate', 1399735312, 100, 'distance', 'centimeters'],
["+12223334444", 'soil', 1399737712, 90, 'moisture', 'percent'],
["+12223334444", 'soil', 1399737712, 30, 'temperature', 'celsius'],
]
self.data_logger.store_readings(readings)
def test_invalid_readings(self):
readings = [
["+12223334444", 'distance', 1399735312, 100, 'distance', 'centimeters'],
["+12223334444", 'distance', 1399735312, 20, 'temperature', 'celsius'],
["+12223334444", 'gate', 1399735312, 100, 'distance'],
["+12223334444", 'soil', 1399737712, 90, 'moisture', 'percent'],
["+12223334444", 'soil', 1399737712, 30, 'temperature', 'celsius'],
]
with self.assertRaises(sqlite3.ProgrammingError):
self.data_logger.store_readings(readings)
if __name__ == '__main__':
unittest.main()