def test_top_incident_reasons_by_timeframes_returns_proper_counts(self): [StandardizedFireIncidentFactory(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)] [StandardizedFireIncidentFactory(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)] [StandardizedFireIncidentFactory(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)] [StandardizedFireIncidentFactory(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)] [StandardizedPoliceIncidentFactory(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)] [StandardizedPoliceIncidentFactory(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)] [StandardizedPoliceIncidentFactory(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)] [StandardizedPoliceIncidentFactory(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_address_page_shows_correct_business_info_with_no_businesses(self): [StandardizedFireIncidentFactory(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_address(self): [StandardizedFireIncidentFactory(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_posting_a_comment_creates_an_audit_log(self): app.config['AUDIT_DISABLED'] = False [StandardizedFireIncidentFactory(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"
def test_address_page_with_incidents_returns_200(self): [StandardizedFireIncidentFactory(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_no_comment_msg_shows_on_address_with_none(self): [StandardizedFireIncidentFactory(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
def test_count_incidents_returns_proper_counts_for_default_days(self): [StandardizedFireIncidentFactory(standardized_address="123 MAIN ST", alarm_datetime=get_date_days_ago(5)) for i in range(0, 5)] [StandardizedFireIncidentFactory(standardized_address="123 MAIN ST", alarm_datetime=get_date_days_ago(20)) for i in range(0, 8)] [StandardizedFireIncidentFactory(standardized_address="123 MAIN ST", alarm_datetime=get_date_days_ago(40)) for i in range(0, 7)] [StandardizedFireIncidentFactory(standardized_address="123 MAIN ST", alarm_datetime=get_date_days_ago(200)) for i in range(0, 10)] [StandardizedPoliceIncidentFactory(standardized_address="123 MAIN ST", call_datetime=get_date_days_ago(5)) for i in range(0, 3)] [StandardizedPoliceIncidentFactory(standardized_address="123 MAIN ST", call_datetime=get_date_days_ago(20)) for i in range(0, 8)] [StandardizedPoliceIncidentFactory(standardized_address="123 MAIN ST", call_datetime=get_date_days_ago(40)) for i in range(0, 9)] [StandardizedPoliceIncidentFactory(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_viewing_an_address_does_not_show_fire_data_if_not_allowed(self): [StandardizedFireIncidentFactory(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
def test_address_page_shows_correct_business_info_with_one_business(self): [StandardizedFireIncidentFactory(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_activation_endpoint_activates_address(self): [StandardizedFireIncidentFactory(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())
def test_fetch_incident_at_address_works_if_lowercase_supplied(self): [StandardizedFireIncidentFactory(standardized_address="123 MAIN ST") for i in range(0, 5)] [StandardizedPoliceIncidentFactory(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_fetch_incident_at_address_returns_correct_number_of_items(self): [StandardizedFireIncidentFactory(standardized_address="123 MAIN ST") for i in range(5)] [StandardizedPoliceIncidentFactory(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_activating_address_adds_to_action_station(self): [StandardizedFireIncidentFactory(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
def test_posting_a_comment_shows_it_on_the_page(self): [StandardizedFireIncidentFactory(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
def test_posting_a_comment_loads_a_comment_into_database(self): [StandardizedFireIncidentFactory(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))
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 = [StandardizedFireIncidentFactory(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_posting_two_comments_shows_the_most_recent_last(self): [StandardizedFireIncidentFactory(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')