def setUp(self): self.rank = 'SGT' self.star = '823' self.unit = '001' self.gender = 'M' self.crid_1 = '1234' self.crid_2 = '2345' self.crid_3 = '3456' police_unit = PoliceUnitFactory(unit_name=self.unit) self.officer = OfficerFactory(rank=self.rank, star=self.star, unit=police_unit, gender=self.gender) self.involved_officer = OfficerFactory() self.witness_officer = OfficerFactory() allegation_1 = AllegationFactory(crid=self.crid_1) OfficerAllegationFactory(officer=self.officer, allegation=allegation_1) allegation_2 = AllegationFactory(crid=self.crid_2) OfficerAllegationFactory(officer=self.officer, allegation=allegation_2) OfficerAllegationFactory( officer=self.officer, allegation=AllegationFactory(crid=self.crid_3)) OfficerAllegationFactory(officer=self.involved_officer, allegation=allegation_1) PoliceWitnessFactory(officer=self.witness_officer, crid=self.crid_2, allegation=allegation_2) self.setting = self.get_admin_settings()
def test_get_related_officer_in_police_witness_info(self): no_disp_code = '600' allegation1 = AllegationFactory() allegation2 = AllegationFactory() officer = OfficerFactory() OfficerAllegationFactory( allegation=allegation1, officer=officer, final_outcome=no_disp_code) OfficerAllegationFactory( allegation=allegation2, officer=officer) witness = OfficerFactory() PoliceWitnessFactory( crid=allegation1.crid, allegation=allegation1, officer=witness) OfficerAllegationFactory( allegation=allegation2, officer=witness, final_outcome=no_disp_code) result = self.client.get('/api/police-witness/', { 'crid': allegation1.crid}) data = json.loads(result.content.decode()) officers = data['police_witness'][0]['officers'] len(officers).should.equal(1) officers[0]['num_complaints'].should.equal(2) officers[0]['no_action_taken'].should.equal(2)
def test_age_range(self): age_values = [20, 25, 31, 41, 51, 61] expect_officer_age = { '20-30': 2, '31-40': 1, '41-50': 1, '51-60': 1, '61+': 1, } expected_witness_age = { '<20': 1, '21-30': 1, '31-40': 1, '41-50': 1, '51+': 2 } for val in age_values: allegation = AllegationFactory(incident_date=datetime(2000, 1, 1)) OfficerAllegationFactory(officer=OfficerFactory(birth_year=2000 - val), allegation=allegation) ComplainingWitnessFactory(age=val, allegation=allegation) allegation = AllegationFactory() OfficerAllegationFactory(officer=OfficerFactory(birth_year=None), allegation=allegation) ComplainingWitnessFactory(age=None, allegation=allegation) response, data = self.get_race_gender_info() response.status_code.should.equal(status.HTTP_200_OK) data['officers']['age'].should.equal(expect_officer_age) data['complaining_witness']['age'].should.equal(expected_witness_age)
def test_filter_by_has_address(self): data = self.fetch_officer_allegations(has_address='true') len(data).should.equal(0) allegation1 = AllegationFactory(add1=123) OfficerAllegationFactory(allegation=allegation1) allegation2 = AllegationFactory(add2='456') OfficerAllegationFactory(allegation=allegation2) allegation3 = AllegationFactory(add1=789, add2='abc') OfficerAllegationFactory(allegation=allegation3) result_count = 3 data = self.fetch_officer_allegations(has_address='true') len(data).should.equal(result_count) any([ data[i]['allegation']['id'] == allegation1.id for i in range(result_count) ]).should.be.true any([ data[i]['allegation']['id'] == allegation2.id for i in range(result_count) ]).should.be.true any([ data[i]['allegation']['id'] == allegation3.id for i in range(result_count) ]).should.be.true
def test_null_last_order_by(self): OfficerAllegationFactory(start_date=datetime.datetime(2010, 11, 1)) OfficerAllegationFactory(start_date=None) [ o.start_date for o in OfficerAllegation.objects.all().order_by('-start_date') ].index(None).should.equal(1) [ o.start_date for o in OfficerAllegation.objects.all().order_by('start_date') ].index(None).should.equal(0) OfficerAllegation.objects.all().delete() OfficerAllegationFactory(allegation=AllegationFactory( incident_date=datetime.datetime(2011, 10, 1))) OfficerAllegationFactory(allegation=AllegationFactory( incident_date=None)) [ o.allegation.incident_date for o in OfficerAllegation.objects.all().order_by( '-allegation__incident_date') ].index(None).should.equal(1) [ o.allegation.incident_date for o in OfficerAllegation.objects.all().order_by( 'allegation__incident_date') ].index(None).should.equal(0)
def setUp(self): base = datetime.now() self.dates = [(base + timedelta(days=x)).date() for x in range(0, 2)] OfficerAllegationFactory(allegation=AllegationFactory( incident_date_only=self.dates[1])) allegation = AllegationFactory(incident_date_only=None) OfficerAllegationFactory(allegation=allegation, start_date=self.dates[0])
def test_location_format(self): allegation = AllegationFactory(location=LOCATION_CHOICES[0][0]) output_format = "{id}. {name}".format( id=allegation.location, name=allegation.get_location_display()) OfficerAllegationFactory(allegation=allegation) data = self.fetch_officer_allegations(allegation__crid=allegation.crid) for row in data: row['allegation']['location'].should.equal(output_format)
def test_hour_look_up(self): allegation = AllegationFactory( incident_date=datetime.datetime(2007, 12, 16, 16, 30, 30)) AllegationFactory( incident_date=datetime.datetime(2007, 12, 16, 7, 30, 30)) allegations = Allegation.objects.filter(incident_date__hour__gte=10) allegations.should.have.length_of(1) allegations.first().id.should.be.equal(allegation.id)
def test_create_officer_allegation_with_officer_age(self): officer = OfficerFactory(birth_year=1984) allegation = AllegationFactory( incident_date=datetime.datetime(2010, 1, 1)) officer_allegation = OfficerAllegationFactory(officer=officer, allegation=allegation) officer_allegation.officer_age.should.equal(26) OfficerAllegationFactory(allegation=AllegationFactory( incident_date=None)).officer_age.should.be.none
def test_incident_date_time_of_day(self): self.clean_db() oa1 = OfficerAllegationFactory(allegation=AllegationFactory( incident_date=datetime.datetime(2007, 12, 16, 7, 30, 30) )) OfficerAllegationFactory(allegation=AllegationFactory( incident_date=datetime.datetime(2007, 12, 16, 16, 30, 30) )) self.check_built_query('incident_date_time_of_day=morning', [oa1.id])
def test_has_address(self): expected_allegations = [ OfficerAllegationFactory(allegation=AllegationFactory(add1=100)), OfficerAllegationFactory(allegation=AllegationFactory(add2='100'))] OfficerAllegationFactory() query_string = 'has_address=true' expected_ids = [allegation.id for allegation in expected_allegations] self.check_built_query(query_string, expected_ids)
def test_suggest_zip_code_distinct(self): city1 = 'Chicago IL 60616' city2 = 'CHICAGO, IL 60616' available_zip_code = '60616' AllegationFactory(city=city1) AllegationFactory(city=city2) self.rebuild_index() len(SuggestAllegationCity.query(available_zip_code)).should.equal(1)
def test_adhoc_queries(self): expected_allegations = [ OfficerAllegationFactory( allegation=AllegationFactory(crid='1'), final_outcome='ZZ')] OfficerAllegationFactory(allegation=AllegationFactory(crid='1')) query_string = 'final_outcome=ZZ&allegation__crid=1' expected_ids = [allegation.id for allegation in expected_allegations] self.check_built_query(query_string, expected_ids)
def setUp(self): Allegation.objects.all().delete() today = datetime.datetime.now().strftime(DATE_ONLY_FORMAT) self.foia = AllegationFactory( incident_date=generate_random_date(FOIA_START_DATE, today)) OfficerAllegationFactory(allegation=self.foia) self.pre_foia = AllegationFactory(incident_date=generate_random_date( START_UNIX_TIME_DATE, FOIA_START_DATE)) OfficerAllegationFactory(allegation=self.pre_foia, start_date=generate_random_date( START_UNIX_TIME_DATE, FOIA_START_DATE))
def test_complainant_age_filter(self): allegation_1 = AllegationFactory() allegation_2 = AllegationFactory() ComplainingWitnessFactory(age=36, allegation=allegation_1) ComplainingWitnessFactory(age=23, allegation=allegation_2) oa_1 = OfficerAllegationFactory(allegation=allegation_1) oa_2 = OfficerAllegationFactory(allegation=allegation_2) self.check_built_query('complainant_age=30-40', [oa_1.id]) self.check_built_query('complainant_age=<30', [oa_2.id]) self.check_built_query('complainant_age=>35', [oa_1.id])
def _test_investigator_agency_iad(self): expected_allegations = [ OfficerAllegationFactory(allegation=AllegationFactory( investigator=InvestigatorFactory(agency='IAD')))] OfficerAllegationFactory(allegation=AllegationFactory( investigator=InvestigatorFactory(agency='IPRA'))) query_string = 'allegation__investigator__agency=IAD' expected_ids = [allegation.id for allegation in expected_allegations] self.check_built_query(query_string, expected_ids)
def test_filter_by_has_investigator(self): data = self.fetch_officer_allegations(has_investigator='true') allegations_num = len(data) allegation = AllegationFactory() OfficerAllegationFactory(allegation=allegation) OfficerAllegationFactory(allegation=AllegationFactory( investigator=None)) data = self.fetch_officer_allegations(has_investigator='true') len(data).should.equal(allegations_num + 1) [obj['allegation']['id'] for obj in data].should.contain(allegation.id)
def test_allegation_summary_multiple_word_term(self): expected_allegations = [ OfficerAllegationFactory(allegation=AllegationFactory( summary='some some really long summary'))] OfficerAllegationFactory( allegation=AllegationFactory(summary='I am so sorry some')) rebuild_index() query_string = 'allegation_summary=some some' expected_ids = [allegation.id for allegation in expected_allegations] self.check_built_query(query_string, expected_ids)
def _test_pre_foia(self): expected_allegations = [ OfficerAllegationFactory(allegation=AllegationFactory( incident_date=datetime.datetime.strptime( '2010-12-31', '%Y-%m-%d')))] OfficerAllegationFactory(allegation=AllegationFactory( incident_date=datetime.datetime.strptime( '2011-01-01', '%Y-%m-%d'))) query_string = 'data_source=pre-FOIA' expected_ids = [allegation.id for allegation in expected_allegations] self.check_built_query(query_string, expected_ids)
def test_filter_by_has_map(self): data = self.fetch_officer_allegations(has_map='true') len(data).should.equal(3) allegation = AllegationFactory() allegation.point = None allegation.save() OfficerAllegationFactory(allegation=allegation) data = self.fetch_officer_allegations(has_map='true') len(data).should.equal(3) for i in range(3): data[i]['allegation']['id'].shouldnt.equal(allegation.id)
def test_suggest_allegation_summary_distinct(self): summary_1 = 'some some really long summary I am sorry' summary_2 = 'some some really long summary I am sorry' AllegationFactory(summary=summary_1) AllegationFactory(summary=summary_2) self.rebuild_index() search_term = 'so' results = SuggestAllegationSummary.query(search_term)['Allegation Summary'] suggested_terms = [x['suggest_value'] for x in results] len(suggested_terms).should.equal(4)
def test_suggestion_entry_without_category(self): crid = '1011111' allegation = AllegationFactory(crid=crid) OfficerAllegationFactory(allegation=allegation, cat=None) expected_entry = { 'text': crid, 'resource': 'officer_allegation', 'resource_key': crid, 'meta': { 'allegation': allegation } } allegation.as_suggestion_entry().should.be.equal(expected_entry)
def test_query_area_type(self): self.clean_db() area = AreaFactory(type='school-grounds') allegation = AllegationFactory() allegation.areas = [area] allegation.save() other_allegation = AllegationFactory() other_allegation.areas = [AreaFactory(type='beat')] other_allegation.save() oa1 = OfficerAllegationFactory(allegation=allegation) OfficerAllegationFactory(allegation=other_allegation) self.check_built_query('allegation__areas__type=school-grounds', [oa1.id])
def test_suggest_crid(self): allegation = AllegationFactory(crid='1051333') OfficerAllegationFactory(allegation=allegation) OfficerAllegationFactory(allegation=AllegationFactory(crid='306697')) crid = str(allegation.crid) partial_query = crid[0:3] suggest_crid(partial_query).should.equal([]) expected_allegation = allegation.as_suggestion_entry() allegation_result = suggest_crid(crid) allegation_result[0]['meta']['allegation'].incident_date = \ allegation_result[0]['meta']['allegation'].incident_date.date() allegation_result.should.equal([expected_allegation])
def test_merge_officer_allegations(self): officer_1 = OfficerFactory() officer_2 = OfficerFactory() oa_list_1 = OfficerAllegationFactory.create_batch(2, officer=officer_1) oa_list_2 = OfficerAllegationFactory.create_batch(2, officer=officer_2) allegation = AllegationFactory() oa_1 = OfficerAllegationFactory(allegation=allegation, officer=officer_1, cat=None, recc_finding=None, recc_outcome=None, final_outcome=None, final_outcome_class=None, start_date=None, end_date=None, final_finding=None) oa_2 = OfficerAllegationFactory(allegation=allegation, officer=officer_2) merge_officer_allegation(officer_1, officer_2) oa_1.refresh_from_db() set(oa.pk for oa in officer_1.officerallegation_set.all()).should.equal( set(oa.pk for oa in oa_list_1 + oa_list_2 + [oa_1])) for field in [ 'cat', 'recc_finding', 'recc_outcome', 'final_finding', 'final_outcome', 'final_outcome_class', 'start_date', 'end_date' ]: getattr(oa_1, field).should.equal(getattr(oa_2, field))
def test_circle_color_of_involved_officers(self): crid = '1234' category = AllegationCategoryFactory() allegation = AllegationFactory(crid=crid) allegations_count_color_map = { 'circle-0': 21, 'circle-1': 11, 'circle-2': 4, 'circle-3': 2, 'circle-4': 1 } officers = {} for circle_class, allegations_count in \ allegations_count_color_map.items(): officer = OfficerFactory(allegations_count=allegations_count) officers[circle_class] = officer OfficerAllegationFactory(allegation=allegation, officer=officer, cat=category) self.visit_complaint_page(allegation.crid, category.id) self.until(lambda: self.should_not_see_text(allegation.crid)) for circle_class, allegations_count in \ allegations_count_color_map.items(): selector = '.officer-card.officer-{id} .{circle_class}'.format( id=officers[circle_class].id, circle_class=circle_class ) self.find_all(selector).should.have.length_of(1)
def test_detect_suggest_type_investigator(self): AllegationFactory(investigator__name='Someone Name') rebuild_index() data = self.get_suggestion('Some') data.should.contain('Investigator')
def setUp(self): self.category = AllegationCategoryFactory() self.allegation = AllegationFactory() DocumentFactory(documentcloud_id=None, requested=False, allegation=self.allegation) OfficerAllegationFactory(cat=self.category, allegation=self.allegation)
def test_officer_sheet(self, mock_workbook): allegation = AllegationFactory() officer = OfficerFactory() officer_allegation = OfficerAllegationFactory(allegation=allegation, officer=officer) allegation_download = AllegationsDownload(DownloadFactory().id) allegation_download.officer_allegations = [officer_allegation] allegation_download.update_crids() allegation_download.write_headers = MagicMock() with patch('allegation.services.download_allegations.os'): allegation_download.init_workbook() mock_worksheet = MagicMock() allegation_download.workbook.add_worksheet = MagicMock( return_value=mock_worksheet) allegation_download.write_officer_profile() (sheet, columns), _ = allegation_download.write_headers.call_args sheet.should.equal(mock_worksheet) columns.should.equal([ 'OfficerID', 'OfficerFirst', 'OfficerLast', 'Gender', 'Race', 'ApptDate', 'Unit', 'Rank', 'Star', 'Age' ]) mock_worksheet.write.assert_any_call(1, 0, officer.id) mock_worksheet.write.assert_any_call(1, 1, officer.officer_first) mock_worksheet.write.assert_any_call(1, 2, officer.officer_last) mock_worksheet.write.assert_any_call(1, 3, officer.gender) mock_worksheet.write.assert_any_call(1, 4, officer.race) mock_worksheet.write.assert_any_call(1, 5, officer.appt_date) mock_worksheet.write.assert_any_call(1, 6, officer.unit.unit_name) mock_worksheet.write.assert_any_call(1, 7, officer.rank) mock_worksheet.write.assert_any_call(1, 8, officer.star) mock_worksheet.write.assert_any_call(1, 9, officer.age)
def test_skip_duplicate_crid(self): allegation = AllegationFactory( crid=self.officer_allegations[0].allegation.crid) OfficerAllegationFactory(allegation=allegation) data = self.fetch_gis_allegations(url='/api/officer-allegations/gis/') data['features'].should.be.a(list) data['features'].should.have.length_of(3) # duplicate crid is ignored
def populate_officer_data(self): incident_date = datetime(2000, 1, 1) allegations = AllegationFactory.create_batch(5, incident_date=incident_date) officer_stats = [ {'race': 'White', 'gender': 'M', 'birth_year': incident_date.year - 24}, {'race': 'White', 'gender': 'F', 'birth_year': incident_date.year - 32}, {'race': 'Black', 'gender': 'M', 'birth_year': incident_date.year - 41}, {'race': 'Black', 'gender': 'F', 'birth_year': incident_date.year - 56}, {'race': 'Hispanic', 'gender': 'X', 'birth_year': incident_date.year - 63}, ] officers = [OfficerFactory(**stat) for stat in officer_stats] [OfficerAllegationFactory(officer=officer, allegation=allegations[ind]) for ind, officer in enumerate(officers)]