-
Notifications
You must be signed in to change notification settings - Fork 6
/
tests.py
687 lines (516 loc) · 25.7 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
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
import unittest
import mock
import os
import datetime
import pytz
from httmock import response, HTTMock
os.environ['APP_SETTINGS'] = 'config.TestingConfig'
from app import app, db
from app import fetch_incidents_at_address, count_incidents_by_timeframes
from app import get_top_incident_reasons_by_timeframes
import models
from count_calls_for_service import count_calls
from factories import FireIncidentFactory, PoliceIncidentFactory, BusinessLicenseFactory, UserFactory
from flask.ext.login import login_user
def get_date_days_ago(days):
return datetime.datetime.now() - datetime.timedelta(days=days)
def persona_verify(url, request):
if url.geturl() == 'https://verifier.login.persona.org/verify':
return response(200, '''{"status": "okay", "email": "user@example.com"}''')
else:
raise Exception('Asked for unknown URL ' + url.geturl())
def setup_google_mock(can_view='Y', can_view_fire='Y', email='user@example.com'):
mock_client = mock.MagicMock()
instance = mock_client.return_value
worksheets_mock = mock.Mock()
worksheet_mock = mock.Mock()
worksheet_mock.id = mock.Mock()
worksheet_mock.id.text = 'foo/bar/abc'
worksheets_mock.entry = [worksheet_mock]
instance.get_worksheets.return_value = worksheets_mock
sample_row = {
'email': email,
'name': 'Joe Fireworks',
'canviewsite': can_view,
'canviewfiredata': can_view_fire
}
row_mock = mock.Mock()
row_mock.to_dict.return_value = sample_row
list_feed_mock = mock.Mock()
list_feed_mock.entry = [row_mock]
instance.get_list_feed.return_value = list_feed_mock
return mock_client
class HomeTestCase(unittest.TestCase):
def setUp(self):
self.app = app.test_client()
def testHomePageReturns200(self):
rv = self.app.get('/')
assert rv.status_code == 200
class LoginTestCase(unittest.TestCase):
# @maybe: Refactor test_login and test_logout.
# @todo: Integrate this with others, probably, so you confirm that access
# control is as it should be throughout.
def setUp(self):
self.app = app.test_client()
db.create_all()
def tearDown(self):
db.drop_all()
@mock.patch('app.SpreadsheetsClient', setup_google_mock())
def test_login(self):
''' Check basic log in flow without talking to Persona.
'''
response = self.app.get('/')
self.assertFalse('user@example.com' in response.data)
with HTTMock(persona_verify):
response = self.app.post('/log-in', data={'assertion': 'sampletoken'})
self.assertEquals(response.status_code, 200)
response = self.app.get('/browse')
self.assertTrue('user@example.com' in response.data)
@mock.patch('app.SpreadsheetsClient', setup_google_mock(email="notexample@example.com"))
def test_login_fails_when_not_in_spreadsheet(self):
response = self.app.get('/')
self.assertFalse('user@example.com' in response.data)
with HTTMock(persona_verify):
response = self.app.post('/log-in', data={'assertion': 'sampletoken'})
self.assertEquals(response.status_code, 403)
response = self.app.get('/')
self.assertFalse('user@example.com' in response.data)
@mock.patch('app.SpreadsheetsClient', setup_google_mock(can_view='N'))
def test_login_fails_when_not_allowed_to_view(self):
response = self.app.get('/')
self.assertFalse('user@example.com' in response.data)
with HTTMock(persona_verify):
response = self.app.post('/log-in', data={'assertion': 'sampletoken'})
self.assertEquals(response.status_code, 403)
response = self.app.get('/')
self.assertFalse('user@example.com' in response.data)
@mock.patch('app.SpreadsheetsClient', setup_google_mock())
def test_login_successfully_pulls_in_name(self):
response = self.app.get('/')
self.assertFalse('user@example.com' in response.data)
with HTTMock(persona_verify):
response = self.app.post('/log-in', data={'assertion': 'sampletoken'})
self.assertEquals(response.status_code, 200)
response = self.app.get('/browse')
self.assertTrue('Joe Fireworks' in response.data)
@mock.patch('app.SpreadsheetsClient', setup_google_mock(can_view_fire='Y'))
def test_login_successfully_pulls_in_fire_viewable_true(self):
response = self.app.get('/')
self.assertFalse('user@example.com' in response.data)
with HTTMock(persona_verify):
response = self.app.post('/log-in', data={'assertion': 'sampletoken'})
self.assertEquals(response.status_code, 200)
user = db.session.query(models.User).filter(models.User.email=='user@example.com').first()
assert user.can_view_fire_data == True
@mock.patch('app.SpreadsheetsClient', setup_google_mock(can_view_fire='N'))
def test_login_successfully_pulls_in_fire_viewable_false(self):
response = self.app.get('/')
self.assertFalse('user@example.com' in response.data)
with HTTMock(persona_verify):
response = self.app.post('/log-in', data={'assertion': 'sampletoken'})
self.assertEquals(response.status_code, 200)
user = db.session.query(models.User).filter(models.User.email=='user@example.com').first()
assert user.can_view_fire_data == False
@mock.patch('app.SpreadsheetsClient', setup_google_mock())
def test_logout(self):
''' Check basic log out flow without talking to Persona.
'''
response = self.app.get('/')
with HTTMock(persona_verify):
response = self.app.post('/log-in', data={'assertion': 'sampletoken'})
response = self.app.get('/')
response = self.app.post('/log-out')
self.assertEquals(response.status_code, 200)
response = self.app.get('/')
self.assertFalse('user@example.com' in response.data)
class AddressUtilityTestCase(unittest.TestCase):
def setUp(self):
self.app = app.test_client()
db.create_all()
def tearDown(self):
db.drop_all()
def test_fetch_incidents_at_address_returns_all_empty_types(self):
incidents = fetch_incidents_at_address("123 main st")
assert 'fire' in incidents
assert 'police' in incidents
assert 'businesses' in incidents
assert len(incidents['fire']) == 0
assert len(incidents['police']) == 0
assert len(incidents['businesses']) == 0
def test_fetch_incident_at_address_returns_correct_number_of_items(self):
[FireIncidentFactory(standardized_address="123 MAIN ST")
for i in range(5)]
[PoliceIncidentFactory(standardized_address="123 MAIN ST")
for i in range(3)]
[BusinessLicenseFactory(business_address="123 MAIN ST")
for i in range(1)]
db.session.flush()
incidents = fetch_incidents_at_address("123 MAIN ST")
assert len(incidents['fire']) == 5
assert len(incidents['police']) == 3
assert len(incidents['businesses']) == 1
def test_fetch_incident_at_address_works_if_lowercase_supplied(self):
[FireIncidentFactory(standardized_address="123 MAIN ST")
for i in range(0, 5)]
[PoliceIncidentFactory(standardized_address="123 MAIN ST")
for i in range(0, 3)]
[BusinessLicenseFactory(business_address="123 MAIN ST")
for i in range(0, 1)]
db.session.flush()
incidents = fetch_incidents_at_address("123 main st")
assert len(incidents['fire']) == 5
assert len(incidents['police']) == 3
assert len(incidents['businesses']) == 1
def test_count_incidents_returns_proper_counts_for_default_days(self):
[FireIncidentFactory(standardized_address="123 MAIN ST",
alarm_datetime=get_date_days_ago(5))
for i in range(0, 5)]
[FireIncidentFactory(standardized_address="123 MAIN ST",
alarm_datetime=get_date_days_ago(20))
for i in range(0, 8)]
[FireIncidentFactory(standardized_address="123 MAIN ST",
alarm_datetime=get_date_days_ago(40))
for i in range(0, 7)]
[FireIncidentFactory(standardized_address="123 MAIN ST",
alarm_datetime=get_date_days_ago(200))
for i in range(0, 10)]
[PoliceIncidentFactory(standardized_address="123 MAIN ST",
call_datetime=get_date_days_ago(5))
for i in range(0, 3)]
[PoliceIncidentFactory(standardized_address="123 MAIN ST",
call_datetime=get_date_days_ago(20))
for i in range(0, 8)]
[PoliceIncidentFactory(standardized_address="123 MAIN ST",
call_datetime=get_date_days_ago(40))
for i in range(0, 9)]
[PoliceIncidentFactory(standardized_address="123 MAIN ST",
call_datetime=get_date_days_ago(200))
for i in range(0, 6)]
db.session.flush()
incidents = fetch_incidents_at_address("123 main st")
counts = count_incidents_by_timeframes(incidents, [7, 30, 90, 365])
assert counts['fire'][7] == 5
assert counts['fire'][30] == 13
assert counts['fire'][90] == 20
assert counts['fire'][365] == 30
assert counts['police'][7] == 3
assert counts['police'][30] == 11
assert counts['police'][90] == 20
assert counts['police'][365] == 26
def test_count_incidents_returns_zeros_when_no_incidents(self):
incidents = fetch_incidents_at_address("123 main st")
counts = count_incidents_by_timeframes(incidents, [7, 30, 90, 365])
assert counts['fire'][7] == 0
assert counts['fire'][30] == 0
assert counts['fire'][90] == 0
assert counts['fire'][365] == 0
assert counts['police'][7] == 0
assert counts['police'][30] == 0
assert counts['police'][90] == 0
assert counts['police'][365] == 0
def test_top_incident_reasons_by_timeframes_returns_proper_counts(self):
[FireIncidentFactory(standardized_address="123 MAIN ST",
alarm_datetime=get_date_days_ago(5),
actual_nfirs_incident_type_description="Broken Nose")
for i in range(0, 5)]
[FireIncidentFactory(standardized_address="123 MAIN ST",
alarm_datetime=get_date_days_ago(20),
actual_nfirs_incident_type_description="Stubbed Toe")
for i in range(0, 8)]
[FireIncidentFactory(standardized_address="123 MAIN ST",
alarm_datetime=get_date_days_ago(40),
actual_nfirs_incident_type_description="Myocardial Infarction")
for i in range(0, 7)]
[FireIncidentFactory(standardized_address="123 MAIN ST",
alarm_datetime=get_date_days_ago(200),
actual_nfirs_incident_type_description="Lung Fell Off")
for i in range(0, 10)]
[PoliceIncidentFactory(standardized_address="123 MAIN ST",
call_datetime=get_date_days_ago(5),
final_cad_call_type_description="Stepped on a Crack")
for i in range(0, 3)]
[PoliceIncidentFactory(standardized_address="123 MAIN ST",
call_datetime=get_date_days_ago(20),
final_cad_call_type_description="Whipped It")
for i in range(0, 8)]
[PoliceIncidentFactory(standardized_address="123 MAIN ST",
call_datetime=get_date_days_ago(40),
final_cad_call_type_description="Safety Dance")
for i in range(0, 9)]
[PoliceIncidentFactory(standardized_address="123 MAIN ST",
call_datetime=get_date_days_ago(200),
final_cad_call_type_description="Runnin' With The Devil")
for i in range(0, 6)]
db.session.flush()
incidents = fetch_incidents_at_address("123 MAIN ST")
actual_top_reasons = get_top_incident_reasons_by_timeframes(incidents, [7, 30, 90, 365])
expected_top_reasons = {
'fire': {
7: [('Broken Nose', 5)],
30: [('Stubbed Toe', 8), ('Broken Nose', 5)],
90: [('Stubbed Toe', 8), ('Myocardial Infarction', 7), ('Broken Nose', 5)],
365: [('Lung Fell Off', 10), ('Stubbed Toe', 8), ('Myocardial Infarction', 7), ('Broken Nose', 5)]
},
'police': {
7: [('Stepped on a Crack', 3)],
30: [('Whipped It', 8), ('Stepped on a Crack', 3)],
90: [('Safety Dance', 9), ('Whipped It', 8), ('Stepped on a Crack', 3)],
365: [('Safety Dance', 9), ('Whipped It', 8), ("Runnin' With The Devil", 6), ('Stepped on a Crack', 3)]
}
}
self.assertEquals(expected_top_reasons, actual_top_reasons)
def test_top_incident_reasons_by_timeframes_returns_empty_lists_if_no_incident(self):
incidents = fetch_incidents_at_address("123 MAIN ST")
actual_top_reasons = get_top_incident_reasons_by_timeframes(incidents, [7, 30, 90, 365])
expected_top_reasons = {
'fire': {
7: [],
30: [],
90: [],
365: []
},
'police': {
7: [],
30: [],
90: [],
365: []
}
}
self.assertEquals(expected_top_reasons, actual_top_reasons)
def test_address_page_with_incidents_returns_200(self):
[FireIncidentFactory(standardized_address="123 MAIN ST")
for i in range(0, 5)]
db.session.flush()
rv = self.app.get('/address/123 main st')
assert rv.status_code == 200
def test_address_page_with_no_incidents_returns_404(self):
rv = self.app.get('/address/123 main st')
assert "Page not found" in rv.data
def test_address_page_shows_correct_address(self):
[FireIncidentFactory(standardized_address="456 LALA LN")
for i in range(0, 5)]
db.session.flush()
rv = self.app.get('/address/456 lala ln')
assert '456 Lala Ln' in rv.data
def test_address_page_shows_correct_business_info_with_no_businesses(self):
[FireIncidentFactory(standardized_address="456 LALA LN")
for i in range(0, 5)]
db.session.flush()
rv = self.app.get('/address/456 lala ln')
assert 'No business is registered' in rv.data
def test_address_page_shows_correct_business_info_with_one_business(self):
[FireIncidentFactory(standardized_address="456 LALA LN")
for i in range(0, 5)]
[BusinessLicenseFactory(business_address="456 LALA LN",
business_service_description="Bar",
name="The Pub")]
db.session.flush()
rv = self.app.get('/address/456 lala ln')
assert "Bar" in rv.data
assert "The Pub" in rv.data
def test_address_page_shows_correct_business_info_with_multiple_businesses(self):
[FireIncidentFactory(standardized_address="456 LALA LN")
for i in range(0, 5)]
BusinessLicenseFactory(business_address="456 LALA LN",
business_service_description="Bar",
name="The Pub")
BusinessLicenseFactory(business_address="456 LALA LN",
business_service_description="Lawncare",
name="Mowers R Us")
db.session.flush()
rv = self.app.get('/address/456 lala ln')
assert "Bar, Lawncare" in rv.data
assert "The Pub, Mowers R Us" in rv.data
def test_no_comment_msg_shows_on_address_with_none(self):
[FireIncidentFactory(standardized_address="456 LALA LN")
for i in range(0, 5)]
db.session.flush()
rv = self.app.get('/address/456 lala ln')
assert 'no-action-found' in rv.data
assert 'Nothing has been done yet with this address. Add a note below, or click the activate button!' in rv.data
@mock.patch('app.SpreadsheetsClient', setup_google_mock())
def test_posting_a_comment_loads_a_comment_into_database(self):
[FireIncidentFactory(standardized_address="456 LALA LN")
for i in range(0, 5)]
db.session.flush()
with HTTMock(persona_verify):
response = self.app.post('/log-in', data={'assertion': 'sampletoken'})
rv = self.app.post('/address/456 lala ln/comments', data={
'content': 'This is a test comment'
})
self.assertEquals(302, rv.status_code)
comments = models.Action.query.all()
self.assertEquals(1, len(comments))
@mock.patch('app.SpreadsheetsClient', setup_google_mock())
def test_posting_a_comment_shows_it_on_the_page(self):
[FireIncidentFactory(standardized_address="456 LALA LN")
for i in range(0, 5)]
db.session.flush()
with HTTMock(persona_verify):
response = self.app.post('/log-in', data={'assertion': 'sampletoken'})
rv = self.app.post('/address/456 lala ln/comments', data={
'content': 'This is a test comment'
})
self.assertEquals(302, rv.status_code)
rv = self.app.get('/address/456 lala ln')
assert 'This is a test comment' in rv.data
@mock.patch('app.SpreadsheetsClient', setup_google_mock())
def test_posting_two_comments_shows_the_most_recent_last(self):
[FireIncidentFactory(standardized_address="456 LALA LN")
for i in range(0, 5)]
db.session.flush()
with HTTMock(persona_verify):
response = self.app.post('/log-in', data={'assertion': 'sampletoken'})
self.app.post('/address/456 lala ln/comments', data={
'content': 'Test 1'
})
self.app.post('/address/456 lala ln/comments', data={
'content': 'Test 2'
})
rv = self.app.get('/address/456 lala ln')
assert 'Test 1' in rv.data
assert 'Test 2' in rv.data
assert rv.data.find('Test 1') < rv.data.find('Test 2')
@mock.patch('app.SpreadsheetsClient', setup_google_mock())
def test_logging_in_creates_an_audit_log(self):
app.config['AUDIT_DISABLED'] = False
db.session.flush()
with HTTMock(persona_verify):
response = self.app.post('/log-in', data={'assertion': 'sampletoken'})
del app.config['AUDIT_DISABLED']
# Length should be 2: one log entry for login, one for accessing page.
assert len(models.AuditLogEntry.query.all()) == 1
first_entry = models.AuditLogEntry.query.first()
assert first_entry.user_id != None
assert first_entry.resource == '/log-in'
assert first_entry.method == 'POST'
assert first_entry.response_code == "200"
@mock.patch('app.SpreadsheetsClient', setup_google_mock())
def test_viewing_an_address_creates_an_audit_log(self):
app.config['AUDIT_DISABLED'] = False
[FireIncidentFactory(standardized_address="456 LALA LN")
for i in range(0, 5)]
db.session.flush()
with HTTMock(persona_verify):
response = self.app.post('/log-in', data={'assertion': 'sampletoken'})
rv = self.app.get('/address/456 lala ln')
del app.config['AUDIT_DISABLED']
# Length should be 2: one log entry for login, one for accessing page.
assert len(models.AuditLogEntry.query.all()) == 2
second_entry = models.AuditLogEntry.query.all()[1]
assert second_entry.user_id != None
assert second_entry.method == 'GET'
assert second_entry.resource == '/address/456 lala ln'
assert second_entry.response_code == "200"
@mock.patch('app.SpreadsheetsClient', setup_google_mock())
def test_posting_a_comment_creates_an_audit_log(self):
app.config['AUDIT_DISABLED'] = False
[FireIncidentFactory(standardized_address="456 LALA LN")
for i in range(0, 5)]
db.session.flush()
with HTTMock(persona_verify):
response = self.app.post('/log-in', data={'assertion': 'sampletoken'})
rv = self.app.post('/address/456 lala ln/comments', data={
'content': 'This is a test comment'
})
del app.config['AUDIT_DISABLED']
# Length should be 2: one log entry for login, one for accessing page.
assert len(models.AuditLogEntry.query.all()) == 2
second_entry = models.AuditLogEntry.query.all()[1]
assert second_entry.user_id != None
assert second_entry.method == 'POST'
assert second_entry.resource == '/address/456 lala ln/comments'
assert second_entry.response_code == "302"
@mock.patch('app.SpreadsheetsClient', setup_google_mock(can_view_fire='N'))
def test_viewing_an_address_does_not_show_fire_data_if_not_allowed(self):
[FireIncidentFactory(standardized_address="456 LALA LN", alarm_datetime=get_date_days_ago(5),
actual_nfirs_incident_type_description="Broken Nose")
for i in range(0, 5)]
db.session.flush()
with HTTMock(persona_verify):
response = self.app.post('/log-in', data={'assertion': 'sampletoken'})
rv = self.app.get('/address/456 lala ln')
assert 'Broken Nose' not in rv.data
@mock.patch('app.SpreadsheetsClient', setup_google_mock())
def test_activation_endpoint_activates_address(self):
[FireIncidentFactory(standardized_address="456 LALA LN")
for i in range(0, 5)]
db.session.flush()
with HTTMock(persona_verify):
response = self.app.post('/log-in', data={'assertion': 'sampletoken'})
rv = self.app.post('/address/456 lala ln/activate')
assert 200 == rv.status_code
assert 1 == len(models.ActivatedAddress.query.all())
@mock.patch('app.SpreadsheetsClient', setup_google_mock())
def test_deactivation_endpoint_deactivates_address(self):
[FireIncidentFactory(standardized_address="456 LALA LN")
for i in range(0, 5)]
db.session.flush()
with HTTMock(persona_verify):
response = self.app.post('/log-in', data={'assertion': 'sampletoken'})
self.app.post('/address/456 lala ln/activate')
rv = self.app.post('/address/456 lala ln/deactivate')
assert 200 == rv.status_code
assert 0 == len(models.ActivatedAddress.query.all())
@mock.patch('app.SpreadsheetsClient', setup_google_mock())
def test_activating_address_adds_to_action_station(self):
[FireIncidentFactory(standardized_address="456 LALA LN")
for i in range(0, 5)]
db.session.flush()
with HTTMock(persona_verify):
response = self.app.post('/log-in', data={'assertion': 'sampletoken'})
self.app.post('/address/456 lala ln/activate')
rv = self.app.get('/address/456 lala ln')
assert 200 == rv.status_code
assert 'activated this address' in rv.data
@mock.patch('app.SpreadsheetsClient', setup_google_mock())
def test_deactivating_address_adds_to_action_station(self):
[FireIncidentFactory(standardized_address="456 LALA LN")
for i in range(0, 5)]
db.session.flush()
with HTTMock(persona_verify):
response = self.app.post('/log-in', data={'assertion': 'sampletoken'})
self.app.post('/address/456 lala ln/activate')
self.app.post('/address/456 lala ln/deactivate')
rv = self.app.get('/address/456 lala ln')
assert 200 == rv.status_code
assert 'deactivated this address' in rv.data
class CountCallsTestCase(unittest.TestCase):
def setUp(self):
self.app = app.test_client()
db.create_all()
def tearDown(self):
db.drop_all()
def test_count_calls_returns_empty_when_given_no_incidents(self):
counts = count_calls([], 'alarm_datetime', 'fire_counts', [7, 14])
assert counts == {}
def test_count_calls_returns_correctly_for_fire_responses(self):
def get_date_days_ago(days):
return datetime.datetime.now(pytz.utc) - datetime.timedelta(days=days)
incidents = [FireIncidentFactory(standardized_address="123 MAIN ST",
alarm_datetime=get_date_days_ago(5))
for i in range(0, 5)]
incident_tuples = [(incident.standardized_address, incident.alarm_datetime)
for incident in incidents]
counts = count_calls(incident_tuples, 'alarm_datetime', 'fire_counts', [7, 14])
assert '123 MAIN ST' in counts
assert 'fire_counts' in counts['123 MAIN ST']
assert 7 in counts['123 MAIN ST']['fire_counts']
assert counts['123 MAIN ST']['fire_counts'][7] == 5
assert counts['123 MAIN ST']['fire_counts'][14] == 5
def test_count_calls_returns_correctly_for_police_responses(self):
def get_date_days_ago(days):
return datetime.datetime.now(pytz.utc) - datetime.timedelta(days=days)
incidents = [PoliceIncidentFactory(standardized_address="123 MAIN ST",
call_datetime=get_date_days_ago(5))
for i in range(0, 5)]
incident_tuples = [(incident.standardized_address, incident.call_datetime)
for incident in incidents]
counts = count_calls(incident_tuples, 'call_datetime', 'police_counts', [7, 14])
assert '123 MAIN ST' in counts
assert 'police_counts' in counts['123 MAIN ST']
assert 7 in counts['123 MAIN ST']['police_counts']
assert counts['123 MAIN ST']['police_counts'][7] == 5
assert counts['123 MAIN ST']['police_counts'][14] == 5
if __name__ == '__main__':
unittest.main()