-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmas_unit_test.py
490 lines (440 loc) · 21.6 KB
/
mas_unit_test.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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
#!/usr/bin/env python
#
###############################################################################
# - Mini Automation Server (MAS) Unit Tests -
#
# For testing purposes only - not needed to execute the application
#
# Author: Joel Eriksson ([email protected])
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
###############################################################################
import unittest
import mas, sunstate, datetime
class TestParsing(unittest.TestCase):
telldus_library = None
groups = None
def SetUp(self):
try:
self.telldus_library = mas.TelldusLibrary();
except:
sys.stderr.write("Telldus core library is missing. " +
"Please install before use.\n")
exit(3)
self.groups = mas.Groups()
def test_parse_LAT_LONG_pass(self):
lat_long = mas.parse_LAT_LONG('LAT_LONG 59.17 18.3')
self.assertTrue(lat_long[0] == 59.17)
self.assertTrue(lat_long[1] == 18.3)
def test_parse_LAT_LONG_wrong_format(self):
with self.assertRaises(Exception):
mas.parse_LAT_LONG('LAT_LONG 59.17 18.3 23')
with self.assertRaises(Exception):
mas.parse_LAT_LONG('LAT_LONG 59.17')
with self.assertRaises(Exception):
mas.parse_LAT_LONG('LAT_LONG aa bb')
def test_parse_GROUP_pass1(self):
group = mas.parse_GROUP('GROUP 12 "My name" 2 4 6')
self.assertTrue(group.id == 12)
self.assertTrue(group.name == "My name")
self.assertTrue(group.devices == [2,4,6])
def test_parse_GROUP_pass2(self):
group = mas.parse_GROUP('GROUP 1 "A" 456')
self.assertTrue(group.id == 1)
self.assertTrue(group.name == "A")
self.assertTrue(group.devices == [456])
def test_parse_GROUP_pass3(self):
group = mas.parse_GROUP('GROUP G8 "a group name" 0')
self.assertTrue(group.id == 8)
self.assertTrue(group.name == "a group name")
self.assertTrue(group.devices == [0])
def test_parse_GROUP_no_wrong_format(self):
with self.assertRaises(Exception):
mas.parse_GROUP('GROUP 1 456')
with self.assertRaises(Exception):
mas.parse_GROUP('GROUP 456 "Hello there"')
with self.assertRaises(Exception):
mas.parse_GROUP('GROUP "Hello there" 3')
def test_parse_EVENT_too_short(self):
with self.assertRaises(Exception):
mas.parse_EVENT("EVENT 01:23", self.telldus_library,
self.groups)
with self.assertRaises(Exception):
mas.parse_EVENT("EVENT", self.telldus_library,
self.groups)
def test_parse_EVENT_too_long(self):
with self.assertRaises(Exception):
mas.parse_EVENT("EVENT 01:23 Sunup illegal off(1)",
self.telldus_library, self.groups)
def test_parse_EVENT_wrong_time(self):
with self.assertRaises(Exception):
mas.parse_EVENT("EVENT 1344 Sunup off(1)",
self.telldus_library, self.groups)
with self.assertRaises(Exception):
mas.parse_EVENT("EVENT 25:23 Sunup off(1)",
self.telldus_library, self.groups)
with self.assertRaises(Exception):
mas.parse_EVENT("EVENT 23:61 Sunup off(1)",
self.telldus_library, self.groups)
def test_parse_EVENT_function(self):
with self.assertRaises(Exception):
mas.parse_EVENT("EVENT 12:10 Sunup dummy(1)",
self.telldus_library, self.groups)
def test_parse_EVENT_on(self):
event = mas.parse_EVENT("EVENT 00:01 Mon/Tue on(43)",
self.telldus_library, self.groups, False)
self.assertTrue(event.hour == 0)
self.assertTrue(event.minute == 1)
self.assertTrue(event.weekday ==
[True,True,False,False,False,False,False])
self.assertTrue(event.restriction ==
mas.TimeEvent.RESTRICTION_NONE)
self.assertTrue(isinstance(event.function,
mas.FunctionOn))
self.assertTrue(event.function.devices == [43])
def test_parse_EVENT_off(self):
event = mas.parse_EVENT("EVENT 01:23 Sunup off(1)",
self.telldus_library, self.groups)
self.assertTrue(event.hour == 1)
self.assertTrue(event.minute == 23)
self.assertTrue(event.restriction ==
mas.TimeEvent.RESTRICTION_SUNUP)
self.assertTrue(isinstance(event.function,
mas.FunctionOff))
self.assertTrue(event.function.devices == [1])
def test_parse_EVENT_dim(self):
event = mas.parse_EVENT("EVENT 01:23 Sundown dim(5,50)",
self.telldus_library, self.groups)
self.assertTrue(isinstance(event.function,
mas.FunctionDim))
self.assertTrue(event.function.devices == [5])
self.assertTrue(event.function.dim_level == 50)
def test_parse_EVENT_sunrise(self):
event = mas.parse_EVENT("EVENT Sunrise off(4)",
self.telldus_library, self.groups)
self.assertTrue(event.hour == mas.TimeEvent.TIME_SUNRISE)
self.assertTrue(event.minute == 0)
self.assertTrue(event.weekday ==
[True,True,True,True,True,True,True])
self.assertTrue(event.restriction ==
mas.TimeEvent.RESTRICTION_NONE)
self.assertTrue(isinstance(event.function,
mas.FunctionOff))
self.assertTrue(event.function.devices == [4])
def test_parse_EVENT_sunset(self):
event = mas.parse_EVENT("EVENT Sunset on(7)",
self.telldus_library, self.groups)
self.assertTrue(event.hour == mas.TimeEvent.TIME_SUNSET)
self.assertTrue(event.minute == 0)
self.assertTrue(event.weekday ==
[True,True,True,True,True,True,True])
self.assertTrue(event.restriction ==
mas.TimeEvent.RESTRICTION_NONE)
self.assertTrue(isinstance(event.function,
mas.FunctionOn))
self.assertTrue(event.function.devices == [7])
def test_parse_EVENT_sunrise_offset_1(self):
event = mas.parse_EVENT("EVENT Sunrise+1 off(4)",
self.telldus_library, self.groups)
self.assertTrue(event.hour == mas.TimeEvent.TIME_SUNRISE)
self.assertTrue(event.minute == 60)
def test_parse_EVENT_sunrise_offset_2p5(self):
event = mas.parse_EVENT("EVENT Sunrise+2.5 off(4)",
self.telldus_library, self.groups)
self.assertTrue(event.hour == mas.TimeEvent.TIME_SUNRISE)
self.assertTrue(event.minute == 150)
def test_parse_EVENT_sunrise_offset_minus0p5(self):
event = mas.parse_EVENT("EVENT Sunrise-0.5 off(4)",
self.telldus_library, self.groups)
self.assertTrue(event.hour == mas.TimeEvent.TIME_SUNRISE)
self.assertTrue(event.minute == -30)
def test_parse_EVENT_sunset_offset_plus3p2(self):
event = mas.parse_EVENT("EVENT Sunset+3.2 off(4)",
self.telldus_library, self.groups)
self.assertTrue(event.hour == mas.TimeEvent.TIME_SUNSET)
self.assertTrue(event.minute == 192)
def test_parse_EVENT_sunset_offset_plus4p1(self):
event = mas.parse_EVENT("EVENT Sunset+4.1 off(4)",
self.telldus_library, self.groups)
self.assertTrue(event.hour == mas.TimeEvent.TIME_SUNSET)
self.assertTrue(event.minute == 246)
def test_parse_EVENT_group_pass(self):
groups = mas.Groups()
groups.add(mas.parse_GROUP('GROUP 3 "My name" 2 4 6'))
event = mas.parse_EVENT("EVENT 01:23 Sundown on(G3)",
self.telldus_library, groups)
self.assertTrue(event.function.devices == [2,4,6])
def test_parse_EVENT_group_pass2(self):
groups = mas.Groups()
groups.add(mas.parse_GROUP('GROUP 1 "g1" 2 4 6'))
groups.add(mas.parse_GROUP('GROUP 2 "g2" 3'))
groups.add(mas.parse_GROUP('GROUP 3 "g3" 1 7'))
event = mas.parse_EVENT("EVENT 01:23 Sundown on(G2)",
self.telldus_library, groups)
self.assertTrue(event.function.devices == [3])
def test_parse_EVENT_group_does_not_exist(self):
groups = mas.Groups()
with self.assertRaises(Exception):
mas.parse_EVENT("EVENT 01:23 Sundown on(G2)",
self.telldus_library, groups)
def test_parse_EVENT_lat_long_not_set(self):
with self.assertRaises(Exception):
mas.parse_EVENT("EVENT 01:23 Sundown on(1)",
self.telldus_library, groups, False)
with self.assertRaises(Exception):
mas.parse_EVENT("EVENT 01:23 Sunup on(1)",
self.telldus_library, groups, False)
with self.assertRaises(Exception):
mas.parse_EVENT("EVENT Sunrise on(1)",
self.telldus_library, groups, False)
with self.assertRaises(Exception):
mas.parse_EVENT("EVENT Sunset on(1)",
self.telldus_library, groups, False)
class TestEvent(unittest.TestCase):
telldus_library = None
groups = None
def SetUp(self):
try:
self.telldus_library = mas.TelldusLibrary();
except:
sys.stderr.write("Telldus core library is missing. " +
"Please install before use.\n")
exit(3)
self.groups = mas.Groups()
def test_time_trig(self):
event = mas.parse_EVENT("EVENT 13:00 on(1)",
self.telldus_library, self.groups, False)
t_sunrise = datetime.time(10)
t_sunset = datetime.time(18)
self.assertFalse(event.time_match(datetime.datetime(2014,3,3,10,00),
t_sunrise,t_sunset))
self.assertFalse(event.time_match(datetime.datetime(2014,3,3,12,59),
t_sunrise,t_sunset))
self.assertTrue(event.time_match(datetime.datetime(2014,3,3,13,00),
t_sunrise,t_sunset))
self.assertFalse(event.time_match(datetime.datetime(2014,3,3,13,1),
t_sunrise,t_sunset))
self.assertFalse(event.time_match(datetime.datetime(2014,3,3,18,00),
t_sunrise,t_sunset))
self.assertFalse(event.time_match(datetime.datetime(2014,3,3,23,59),
t_sunrise,t_sunset))
def test_sunrise_trig(self):
event = mas.parse_EVENT("EVENT Sunrise on(1)",
self.telldus_library, self.groups, True)
t_sunrise = datetime.time(10)
t_sunset = datetime.time(18)
self.assertFalse(event.time_match(datetime.datetime(2014,3,3,7,12),
t_sunrise,t_sunset))
self.assertFalse(event.time_match(datetime.datetime(2014,3,3,9,59),
t_sunrise,t_sunset))
self.assertTrue(event.time_match(datetime.datetime(2014,3,3,10,00),
t_sunrise,t_sunset))
self.assertFalse(event.time_match(datetime.datetime(2014,3,3,10,1),
t_sunrise,t_sunset))
self.assertFalse(event.time_match(datetime.datetime(2014,3,3,23,59),
t_sunrise,t_sunset))
def test_sunset_trig(self):
event = mas.parse_EVENT("EVENT Sunset on(1)",
self.telldus_library, self.groups, True)
t_sunrise = datetime.time(10)
t_sunset = datetime.time(18)
self.assertFalse(event.time_match(datetime.datetime(2014,3,3,7,12),
t_sunrise,t_sunset))
self.assertFalse(event.time_match(datetime.datetime(2014,3,3,17,59),
t_sunrise,t_sunset))
self.assertTrue(event.time_match(datetime.datetime(2014,3,3,18,00),
t_sunrise,t_sunset))
self.assertFalse(event.time_match(datetime.datetime(2014,3,3,18,1),
t_sunrise,t_sunset))
self.assertFalse(event.time_match(datetime.datetime(2014,3,3,23,59),
t_sunrise,t_sunset))
def test_sunrise_offset_trig_plus(self):
event = mas.parse_EVENT("EVENT Sunrise+3.5 on(1)",
self.telldus_library, self.groups, True)
t_sunrise = datetime.time(10,1)
t_sunset = datetime.time(18)
self.assertFalse(event.time_match(datetime.datetime(2014,3,3,10,00),
t_sunrise,t_sunset))
self.assertTrue(event.time_match(datetime.datetime(2014,3,3,13,31),
t_sunrise,t_sunset))
def test_sunrise_offset_trig_minus(self):
event = mas.parse_EVENT("EVENT Sunrise-3.5 on(1)",
self.telldus_library, self.groups, True)
t_sunrise = datetime.time(4)
t_sunset = datetime.time(18)
self.assertTrue(event.time_match(datetime.datetime(2014,3,3,00,30),
t_sunrise,t_sunset))
def test_sunset_offset_trig_plus(self):
event = mas.parse_EVENT("EVENT Sunset+4.1 on(1)",
self.telldus_library, self.groups, True)
t_sunrise = datetime.time(10)
t_sunset = datetime.time(18,4)
self.assertTrue(event.time_match(datetime.datetime(2014,3,3,22,10),
t_sunrise,t_sunset))
def test_sunset_offset_trig_minus(self):
event = mas.parse_EVENT("EVENT Sunset-0.2 on(1)",
self.telldus_library, self.groups, True)
t_sunrise = datetime.time(10)
t_sunset = datetime.time(18)
self.assertTrue(event.time_match(datetime.datetime(2014,3,3,17,48),
t_sunrise,t_sunset))
self.assertFalse(event.time_match(datetime.datetime(2014,3,3,18,00),
t_sunrise,t_sunset))
def test_sunset_offset_trig_over_day(self):
event = mas.parse_EVENT("EVENT Sunset+3 on(1)",
self.telldus_library, self.groups, True)
t_sunrise = datetime.time(1)
t_sunset = datetime.time(22)
self.assertFalse(event.time_match(datetime.datetime(2014,3,3,22,00),
t_sunrise,t_sunset))
self.assertTrue(event.time_match(datetime.datetime(2014,3,4,1,00),
t_sunrise,t_sunset))
def test_weekday1(self):
event = mas.parse_EVENT("EVENT 10:00 Tue/Sat on(1)",
self.telldus_library, self.groups, False)
t_sunrise = datetime.time(10)
t_sunset = datetime.time(18)
# 2014-03-03 is a Monday
self.assertFalse(event.time_match(datetime.datetime(2014,3,3,10,0),
t_sunrise,t_sunset))
self.assertTrue(event.time_match(datetime.datetime(2014,3,4,10,0),
t_sunrise,t_sunset))
self.assertFalse(event.time_match(datetime.datetime(2014,3,5,10,0),
t_sunrise,t_sunset))
self.assertFalse(event.time_match(datetime.datetime(2014,3,6,10,0),
t_sunrise,t_sunset))
self.assertFalse(event.time_match(datetime.datetime(2014,3,7,10,0),
t_sunrise,t_sunset))
self.assertTrue(event.time_match(datetime.datetime(2014,3,8,10,0),
t_sunrise,t_sunset))
self.assertFalse(event.time_match(datetime.datetime(2014,3,9,10,0),
t_sunrise,t_sunset))
def test_weekday2(self):
event = mas.parse_EVENT("EVENT 10:00 Mon/Sun on(1)",
self.telldus_library, self.groups, False)
t_sunrise = datetime.time(10)
t_sunset = datetime.time(18)
# 2014-03-03 is a Monday
self.assertTrue(event.time_match(datetime.datetime(2014,3,3,10,0),
t_sunrise,t_sunset))
self.assertFalse(event.time_match(datetime.datetime(2014,3,4,10,0),
t_sunrise,t_sunset))
self.assertFalse(event.time_match(datetime.datetime(2014,3,5,10,0),
t_sunrise,t_sunset))
self.assertFalse(event.time_match(datetime.datetime(2014,3,6,10,0),
t_sunrise,t_sunset))
self.assertFalse(event.time_match(datetime.datetime(2014,3,7,10,0),
t_sunrise,t_sunset))
self.assertFalse(event.time_match(datetime.datetime(2014,3,8,10,0),
t_sunrise,t_sunset))
self.assertTrue(event.time_match(datetime.datetime(2014,3,9,10,0),
t_sunrise,t_sunset))
def test_restriction_sunup(self):
event = mas.parse_EVENT("EVENT 13:00 Sunup on(1)",
self.telldus_library, self.groups, True)
t = datetime.datetime(2014,3,3,13,0)
t_sunrise = datetime.time(10)
t_sunset = datetime.time(18)
self.assertFalse(event.time_match(t,datetime.time(10),
datetime.time(12,59)))
self.assertTrue(event.time_match(t, datetime.time(10),
datetime.time(13,1)))
self.assertTrue(event.time_match(t, datetime.time(12,59),
datetime.time(18,0)))
self.assertFalse(event.time_match(t, datetime.time(13,1),
datetime.time(18,0)))
def test_restriction_sundown(self):
event = mas.parse_EVENT("EVENT 13:00 Sundown on(1)",
self.telldus_library, self.groups, True)
t = datetime.datetime(2014,3,3,13,0)
t_sunrise = datetime.time(10)
t_sunset = datetime.time(18)
self.assertTrue(event.time_match(t,datetime.time(10),
datetime.time(12,59)))
self.assertFalse(event.time_match(t, datetime.time(10),
datetime.time(13,1)))
self.assertFalse(event.time_match(t, datetime.time(12,59),
datetime.time(18,0)))
self.assertTrue(event.time_match(t, datetime.time(13,1),
datetime.time(18,0)))
class TestSun(unittest.TestCase):
# NOTE! These test cases will only pass if script is executed
# on a computer located in Sweden or in a country with equal
# UTC offset and daylight saving times as Sweden.
TOLERANCE_SECONDS = 6 * 60 # Tolerance in seconds
def test_stockholm_sunrise_sunset(self):
lat = 59.20
long = 18.3
test_set = [
# Test data input from www.sunset-and-sunrise.com
# [year, month, day, sunrise_h, sunrise_m, sunset_h, sunset_m]
[2014, 1, 1, 8,43, 14,59],
[2014, 1,17, 8,26, 15,29],
[2014, 2, 1, 7,56, 16, 6],
[2014, 2,21, 7,10, 16,51],
[2014, 3,15, 6, 2, 17,50],
[2014, 3,29, 5,21, 18,23],
[2014, 3,30, 6,18, 19,26], # Daylight saving start
[2014, 4,15, 5,31, 20, 4],
[2014, 5,15, 4,13, 21,15],
[2014, 6,15, 3,30, 22, 5],
[2014, 7,15, 3,58, 21,49],
[2014, 8,15, 5, 6, 20,37],
[2014, 9,15, 6,17, 19, 8],
[2014,10,15, 7,26, 17,40],
[2014,10,25, 7,50, 17,12],
[2014,10,26, 6,53, 16, 9], # Daylight saving end
[2014,11,15, 7,42, 15,22],
[2014,12,15, 8,38, 14,47]
]
for t in test_set:
self._test_sunrise_sunset(lat, long, t[0], t[1],
t[2], t[3], t[4], t[5], t[6])
def test_sunup(self):
# Values taken from test_stockholm_sunrise_sunset
lat = 59.20
long = 18.3
t = [2014, 3,29, 5,21, 18,23]
s = sunstate.Sun(lat, long, sunstate.LocalTimezone())
self.assertFalse(s.sunup(datetime.datetime(
t[0],t[1],t[2],t[3],t[4]-10)))
self.assertTrue(s.sunup(datetime.datetime(
t[0],t[1],t[2],t[3],t[4]+10)))
self.assertTrue(s.sunup(datetime.datetime(
t[0],t[1],t[2],t[5],t[6]-10)))
self.assertFalse(s.sunup(datetime.datetime(
t[0],t[1],t[2],t[5],t[6]+10)))
def _test_sunrise_sunset(self, lat, long, year, month, day,
expected_sunrise_hour, expected_sunrise_minute,
expected_sunset_hour, expected_sunset_minute):
date = datetime.date(year, month, day)
s = sunstate.Sun(lat, long, sunstate.LocalTimezone())
sunrise = s.sunrise(date)
sunset = s.sunset(date)
status1=self._compare_time(sunrise, expected_sunrise_hour,
expected_sunrise_minute)
status2=self._compare_time(sunset, expected_sunset_hour,
expected_sunset_minute)
self.assertFalse((status1 != "Ok") or (status2 != "Ok"), str(year)+"-"+
str(month)+"-"+str(day)+" Sunrise: "+status1+" Sunset: "+status2)
def _compare_time(self, time1, hour, minute):
time2 = datetime.time(hour, minute)
date = datetime.date(2000, 1, 1) #Dummy
diff = abs(datetime.datetime.combine(date, time2) -
datetime.datetime.combine(date, time1))
if((diff.days != 0) or (diff.seconds > self.TOLERANCE_SECONDS)):
return "Got: "+str(time1)+" Expected: "+str(time2)
return "Ok"
if __name__ == '__main__':
unittest.main()