forked from c3bottles/c3bottles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_model.py
executable file
·796 lines (595 loc) · 21.8 KB
/
test_model.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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
#!/usr/bin/python
import sys
import unittest
from datetime import datetime, timedelta
from flask.ext.sqlalchemy import BaseQuery
from controller import c3bottles, db
from model.drop_point import DropPoint
from model.location import Location
from model.capacity import Capacity
from model.report import Report
from model.visit import Visit
print("Tests running with Python %s.%s" % (sys.version_info[0], sys.version_info[1]))
def load_config():
c3bottles.config['SQLALCHEMY_DATABASE_URI'] = "sqlite://"
c3bottles.config['TESTING'] = True
class C3bottlesModelTestCase(unittest.TestCase):
def setUp(self):
load_config()
self.c3bottles = c3bottles.test_client()
db.create_all()
def tearDown(self):
db.session.remove()
db.drop_all()
def test_drop_point_construction(self):
dp_number = 1
time = datetime.today() - timedelta(hours=1)
description = "somewhere"
lat = 20
lng = 10
level = 2
crate_count = 3
dp = DropPoint(
dp_number,
time=time,
description=description,
lat=lat,
lng=lng,
level=level,
crates=crate_count
)
db.session.commit()
self.assertEqual(
db.session.query(DropPoint).get(dp_number), dp,
"Drop point from DB session is not drop point created."
)
self.assertEqual(
dp.number, dp_number,
"Wrong drop point number"
)
self.assertFalse(
dp.removed,
"Drop point is removed."
)
self.assertIsInstance(
dp.reports, BaseQuery,
"Reports are not a query object."
)
self.assertFalse(
dp.reports.all(),
"Drop point has reports."
)
self.assertIsInstance(
dp.visits, BaseQuery,
"Visits are not a query object."
)
self.assertFalse(
dp.visits.all(),
"Drop point has visits."
)
self.assertIsInstance(
dp.locations[0], Location,
"Drop point has no Location object."
)
self.assertIsInstance(
dp.capacities[0], Capacity,
"Drop point has no Capacity object."
)
self.assertEqual(
dp.time, time,
"Drop point creation time not as expected."
)
self.assertEqual(
dp.locations[0].description, description,
"Location description not as expected."
)
self.assertEqual(
dp.locations[0].lat, lat,
"Latitude not as expected."
)
self.assertEqual(
dp.locations[0].lng, lng,
"Longitude not as expected."
)
self.assertEqual(
dp.locations[0].level, level,
"Building level not as expected."
)
self.assertEqual(
dp.locations[0].time, time,
"Location start time not as expected."
)
self.assertEqual(
dp.get_current_crate_count(), crate_count,
"Drop point crate count not as expected."
)
self.assertEqual(
dp.capacities[0].crates, crate_count,
"Capacity crate count not as expected."
)
self.assertEqual(
dp.capacities[0].time, time,
"Capacity start time not as expected."
)
def test_drop_point_construction_exceptions(self):
dp_number = 1
DropPoint(dp_number, lat=0, lng=0, level=1)
db.session.commit()
with self.assertRaisesRegexp(ValueError, "already exists"):
DropPoint(dp_number, lat=0, lng=0, level=1)
numbers = (-1, "foo", None)
for num in numbers:
with self.assertRaisesRegexp(ValueError, "number"):
DropPoint(num, lat=0, lng=0, level=1)
time_in_future = datetime.today() + timedelta(hours=1)
with self.assertRaisesRegexp(ValueError, "future"):
DropPoint(dp_number, time=time_in_future, lat=0, lng=0, level=1)
with self.assertRaisesRegexp(ValueError, "not a datetime"):
DropPoint(dp_number, time="foo", lat=0, lng=0, level=1)
def test_drop_point_getters(self):
dp_number = 1
time = datetime.today()
dp = DropPoint(
dp_number,
time=time,
lat=0,
lng=0,
level=1
)
db.session.commit()
self.assertEqual(
DropPoint.get(dp_number), dp,
"DropPoint.get() did not return the drop point created."
)
wrong_numbers = (dp_number+1, -1, "foo", None)
for number in wrong_numbers:
self.assertIsNone(
DropPoint.get(number),
"DropPoint.get() for wrong number number did not return None."
)
self.assertIsInstance(
dp.get_current_location(), Location,
"get_current_location() is not a Location object."
)
self.assertEqual(
dp.get_current_crate_count(), Capacity.default_crate_count,
"get_current_crate_count() did not return the default crate count."
)
self.assertEqual(
dp.get_last_state(), Report.states[1],
"get_last_state() did not return the default state."
)
self.assertEqual(
dp.get_total_report_count(), 0,
"get_total_report_count() did not return 0."
)
self.assertEqual(
dp.get_new_report_count(), 0,
"get_new_report_count() did not return 0."
)
self.assertFalse(
dp.get_last_visit(),
"get_last_visit() returned something not False."
)
self.assertFalse(
dp.get_last_report(),
"get_last_report() returned something not False."
)
self.assertFalse(
dp.get_new_reports(),
"get_new_reports() returned something not False."
)
self.assertGreater(
dp.get_visit_interval(), 0,
"get_visit_interval() returned a value <= 0."
)
self.assertIsInstance(
dp.get_history(), list,
"get_history() did not return a list."
)
for elem in dp.get_history():
self.assertIsInstance(
elem, dict,
"get_history() did not return a list of dicts."
)
# The history should have 3 entries:
# 1. drop point creation
# 2. location setting
# 3. capacity setting
assumed_history_length = 3
self.assertEqual(
len(dp.get_history()), assumed_history_length,
"get_history() does not have a length of 3."
)
self.assertIsInstance(
DropPoint.get_dps_json(), str,
"get_dps_json() did not return a string."
)
self.assertGreater(
len(DropPoint.get_dps_json()), 1,
"get_dps_json() did return a string too short."
)
self.assertEqual(
DropPoint.get_dps_json(datetime.today()), "{}",
"get_dps_json() for now did not return an empty JSON object."
)
self.assertEqual(
DropPoint.get_dps_json(datetime.today()), "{}",
"get_dps_json() JSON object for now not empty."
)
self.assertEqual(
DropPoint.get_dps_json(time), "{}",
"get_dps_json() JSON object for creation time not empty."
)
self.assertNotEqual(
DropPoint.get_dps_json(time - timedelta(seconds=1)), "{}",
"get_dps_json() JSON object for time < creation time empty."
)
def test_location_addition_to_drop_point(self):
dp_number = 3
first_description = "here"
first_lat = -23.5
first_lng = 84
first_level = 3
second_description = "there"
second_lat = 3.14
second_lng = -2.71828
second_level = 2
first_time = datetime.today() - timedelta(hours=2)
second_time = datetime.today() - timedelta(hours=2 - 1)
dp = DropPoint(
dp_number,
description=first_description,
lat=first_lat,
lng=first_lng,
level=first_level,
time=first_time
)
db.session.commit()
self.assertEqual(
len(dp.locations), 1,
"Drop point does not have exactly one location."
)
self.assertEqual(
db.session.query(Location).count(), 1,
"Not exactly one location in the database."
)
second_location = Location(
dp,
description=second_description,
lat=second_lat,
lng=second_lng,
level=second_level,
time=second_time
)
db.session.commit()
self.assertEqual(
len(dp.locations), 2,
"Drop point does not have exactly two locations."
)
self.assertEqual(
db.session.query(Location).count(), 2,
"Not exactly two locations in the database."
)
self.assertEqual(
dp.get_current_location(), second_location,
"Current drop point location is not second location."
)
self.assertEqual(
dp.locations[1].time -
dp.locations[0].time,
second_time - first_time,
"Wrong time difference between locations."
)
def test_location_construction_exceptions(self):
dp = DropPoint(1, lat=0, lng=0, level=1)
with self.assertRaisesRegexp(ValueError, "drop point"):
Location("foo")
time_in_future = datetime.today() + timedelta(hours=1)
with self.assertRaisesRegexp(ValueError, "future"):
Location(dp, time=time_in_future, lat=0, lng=0, level=1)
with self.assertRaisesRegexp(ValueError, "not a datetime"):
Location(dp, time="foo", lat=0, lng=0, level=1)
start_time = datetime.today()
with self.assertRaisesRegexp(ValueError, "older than current"):
Location(dp, time=start_time, lat=0, lng=0, level=1)
db.session.commit()
Location(dp, time=start_time - timedelta(hours=1))
invalid_lat = ("foo", -180, 91, None)
invalid_lng = ("bar", -181, 251.5, None)
invalid_level = ("quux", None)
for lat in invalid_lat:
with self.assertRaisesRegexp(ValueError, "lat"):
Location(dp, lat=lat, lng=0, level=1)
for lng in invalid_lng:
with self.assertRaisesRegexp(ValueError, "lng"):
Location(dp, lat=0, lng=lng, level=1)
for level in invalid_level:
with self.assertRaisesRegexp(ValueError, "level"):
Location(dp, lat=0, lng=0, level=level)
too_long = "a" * (Location.max_description + 1)
with self.assertRaisesRegexp(ValueError, "too long"):
Location(dp, lat=0, lng=0, level=1, description=too_long)
def test_capacity_addition_to_drop_point(self):
dp_number = 3
first_crate_count = 5
second_crate_count = 2
first_time = datetime.today() - timedelta(hours=2)
second_time = datetime.today() - timedelta(hours=2 - 1)
dp = DropPoint(
dp_number,
crates=first_crate_count,
time=first_time,
lat=0, lng=0, level=1
)
db.session.commit()
self.assertEqual(
len(dp.capacities), 1,
"Drop point does not have exactly one capacity."
)
self.assertEqual(
db.session.query(Capacity).count(), 1,
"Not exactly one capacity in the database."
)
second_capacity = Capacity(
dp,
crates=second_crate_count,
time=second_time
)
db.session.commit()
self.assertEqual(
len(dp.capacities), 2,
"Drop point does not have exactly two capacities."
)
self.assertEqual(
db.session.query(Capacity).count(), 2,
"Not exactly two capacities in the database."
)
self.assertEqual(
dp.capacities[-1], second_capacity,
"Current capacity is not the second capacity object."
)
self.assertEqual(
dp.get_current_crate_count(), second_crate_count,
"Current crate count is not second crate count."
)
self.assertEqual(
dp.capacities[1].time -
dp.capacities[0].time,
second_time - first_time,
"Wrong time difference between capacities."
)
def test_capacity_construction(self):
dp = DropPoint(1, lat=0, lng=0, level=1)
capacity = Capacity(dp)
db.session.commit()
self.assertEqual(
dp.capacities[-1], capacity,
"Capacity of drop point not capacity object created."
)
self.assertEqual(
capacity.crates, Capacity.default_crate_count,
"Crate count not default crate count."
)
def test_capacity_construction_exceptions(self):
dp = DropPoint(1, lat=0, lng=0, level=1)
with self.assertRaisesRegexp(ValueError, "drop point"):
Capacity("foo")
time_in_future = datetime.today() + timedelta(hours=1)
with self.assertRaisesRegexp(ValueError, "future"):
Capacity(dp, time_in_future)
with self.assertRaisesRegexp(ValueError, "not a datetime"):
Capacity(dp, "foo")
start_time = datetime.today()
with self.assertRaisesRegexp(ValueError, "older than current"):
Capacity(dp, time=start_time)
db.session.commit()
Capacity(dp, time=start_time - timedelta(hours=1))
invalid_crate_counts = (-1, "some")
for count in invalid_crate_counts:
with self.assertRaisesRegexp(ValueError, "count"):
Capacity(dp, crates=count)
def test_drop_point_removal(self):
dp = DropPoint(1, lat=0, lng=0, level=1)
self.assertFalse(
dp.removed,
"Freshly created drop point is removed."
)
removal_time = datetime.today()
dp.remove(time=removal_time)
self.assertIsInstance(
dp.removed, datetime,
"Drop point not removed or removal time not datetime."
)
self.assertEqual(
dp.removed, removal_time,
"Removal time of drop point not time given."
)
dp = DropPoint(2, lat=0, lng=0, level=1)
dp.remove()
self.assertIsInstance(
dp.removed, datetime,
"Drop point not removed or removal time not datetime."
)
self.assertEqual(
dp.get_priority(), 0,
"Non-zero visit priority of a removed drop point."
)
def test_drop_point_removal_exceptions(self):
dp = DropPoint(1, lat=0, lng=0, level=1)
time_in_future = datetime.today() + timedelta(hours=1)
with self.assertRaisesRegexp(ValueError, "future"):
dp.remove(time_in_future)
with self.assertRaisesRegexp(TypeError, "not a datetime"):
dp.remove("foo")
with self.assertRaisesRegexp(RuntimeError, "already removed"):
dp.remove()
dp.remove()
def test_drop_point_reporting(self):
states = Report.states
dp = DropPoint(1, lat=0, lng=0, level=1)
first_time = datetime.today()
first_state = states[0]
first_report = Report(dp, state=first_state, time=first_time)
db.session.commit()
self.assertEqual(
first_report.time, first_time,
"Report creation time not as expected."
)
self.assertEqual(
first_report.state, first_state,
"Report state not as expected."
)
self.assertEqual(
dp.reports[0], first_report,
"First report not first report of associated drop point."
)
self.assertEqual(
dp.get_last_report(), first_report,
"get_last_report() did not return first report."
)
self.assertEqual(
dp.get_last_state(), first_state,
"get_last_state() did not return state."
)
self.assertEqual(
dp.get_total_report_count(), 1,
"get_total_report_count() not as expected."
)
self.assertEqual(
dp.get_new_report_count(), 1,
"get_new_report_count() not as expected."
)
self.assertEqual(
dp.get_new_reports()[0], first_report,
"First element returned by get_new_reports() not the report wanted."
)
second_time = datetime.today()
second_state = states[-1]
second_report = Report(dp, state=second_state, time=second_time)
db.session.commit()
self.assertEqual(
second_report.state, second_state,
"Report state not as expected."
)
self.assertEqual(
dp.reports[-1], second_report,
"Second report not last report of associated drop point."
)
self.assertEqual(
dp.get_last_report(), second_report,
"get_last_report() did not return second report."
)
self.assertEqual(
dp.get_last_state(), second_state,
"get_last_state() did not return second state."
)
self.assertEqual(
dp.get_total_report_count(), 2,
"get_total_report_count() not as expected."
)
self.assertEqual(
dp.get_new_report_count(), 2,
"get_new_report_count() not as expected."
)
self.assertEqual(
dp.get_new_reports()[0], second_report,
"First element returned by get_new_reports() not the report wanted."
)
def test_report_construction_exceptions(self):
states = Report.states
dp = DropPoint(1, lat=0, lng=0, level=1)
with self.assertRaisesRegexp(ValueError, "drop point"):
Report(None)
with self.assertRaisesRegexp(ValueError, "state"):
Report(dp)
time_in_future = datetime.today() + timedelta(hours=1)
with self.assertRaisesRegexp(ValueError, "future"):
Report(dp, time=time_in_future, state=states[0])
with self.assertRaisesRegexp(ValueError, "not a datetime"):
Report(dp, time="foo", state=states[0])
with self.assertRaisesRegexp(ValueError, "state"):
Report(dp, state="whatever")
def test_report_weight_calculation(self):
pass # TODO
def test_drop_point_visiting(self):
actions = Visit.actions
dp = DropPoint(1, lat=0, lng=0, level=1)
first_time = datetime.today()
first_action = actions[1]
first_visit = Visit(dp, action=first_action, time=first_time)
db.session.commit()
self.assertEqual(
first_visit.time, first_time,
"Visit creation time not as expected."
)
self.assertEqual(
first_visit.action, first_action,
"Visit action not as expected."
)
self.assertEqual(
dp.visits[0], first_visit,
"First visit not first visit of associated drop point."
)
self.assertEqual(
dp.get_last_visit(), first_visit,
"get_last_visit() did not return first visit."
)
report_time = datetime.today()
report_state = Report.states[0]
report = Report(dp, state=report_state, time=report_time)
second_time = datetime.today()
second_action = actions[0]
second_visit = Visit(dp, action=second_action, time=second_time)
db.session.commit()
self.assertEqual(
second_visit.action, second_action,
"Visit action not as expected."
)
self.assertEqual(
dp.visits[-1], second_visit,
"Second visit not last visit of associated drop point."
)
self.assertEqual(
dp.get_last_visit(), second_visit,
"get_last_visit() did not return second visit."
)
self.assertNotEqual(
dp.get_last_state(), report_state,
"get_last_state() returns unchanged state after visit."
)
self.assertEqual(
dp.get_new_report_count(), 0,
"get_new_report_count() nonzero after visit."
)
self.assertFalse(
dp.get_new_reports(),
"get_new_reports() returned something not False after visit."
)
self.assertEqual(
dp.get_last_report(), report,
"get_last_report() did not return report after visit."
)
self.assertEqual(
dp.get_total_report_count(), 1,
"get_total_report_count() not as expected after visit."
)
def test_visit_construction_exceptions(self):
actions = Visit.actions
dp = DropPoint(1, lat=0, lng=0, level=1)
with self.assertRaisesRegexp(ValueError, "drop point"):
Visit(None)
with self.assertRaisesRegexp(ValueError, "action"):
Visit(dp)
time_in_future = datetime.today() + timedelta(hours=1)
with self.assertRaisesRegexp(ValueError, "future"):
Visit(dp, time=time_in_future, action=actions[0])
with self.assertRaisesRegexp(ValueError, "not a datetime"):
Visit(dp, time="foo", action=actions[0])
with self.assertRaisesRegexp(ValueError, "action"):
Visit(dp, action="whatever")
def test_drop_point_visit_priority_calculation(self):
pass # TODO
if __name__ == "__main__":
unittest.main()
# vim: set expandtab ts=4 sw=4: