def test_complainant_gender_aggregation(self): unit = PoliceUnitFactory() officer = OfficerFactory() allegation1 = AllegationFactory() allegation2 = AllegationFactory() OfficerHistoryFactory(officer=officer, unit=unit) OfficerAllegationFactory(officer=officer, allegation=allegation1, final_finding='SU') OfficerAllegationFactory(officer=officer, allegation=allegation2, final_finding='UN') ComplainantFactory(allegation=allegation1, gender='F') ComplainantFactory(allegation=allegation2, gender='') expect( sorted(unit.complainant_gender_aggregation, key=itemgetter('name'))).to.eq([{ 'name': 'Female', 'count': 1, 'sustained_count': 1 }, { 'name': 'Unknown', 'count': 1, 'sustained_count': 0 }])
def test_serialization_with_non_existing_pinned_item_ids(self): pinned_officer = OfficerFactory(id=1) pinned_allegation = AllegationFactory(crid='123abc') pinboard = PinboardFactory( id='66ef1560', title='Test pinboard', description='Test description', ) pinboard.officers.set([pinned_officer]) pinboard.allegations.set([pinned_allegation]) pinboard_data = { 'id': '123abc', 'title': 'title', 'description': 'description', 'officer_ids': [1, 2, 4, 5], 'crids': ['xyz789', '123abc', '456def'], 'trr_ids': [0, 3, 1], } serializer = PinboardDetailSerializer(data=pinboard_data) expect(serializer.is_valid()).to.be.true() expect(serializer.data).to.eq({ 'title': 'title', 'description': 'description', 'officer_ids': [1], 'crids': ['123abc'], 'trr_ids': [], 'not_found_items': { 'officer_ids': [2, 4, 5], 'crids': ['xyz789', '456def'], 'trr_ids': [0, 3, 1], } })
def test_sustained_count(self): unit = PoliceUnitFactory() officer = OfficerFactory() OfficerHistoryFactory(unit=unit, officer=officer) OfficerAllegationFactory(officer=officer, final_finding='SU') OfficerAllegationFactory(officer=officer, final_finding='UN') expect(unit.sustained_count).to.eq(1)
def test_serialization_with_data(self): pinned_officer = OfficerFactory(id=1) pinned_allegation = AllegationFactory(crid='1') pinboard = PinboardFactory( id='66ef1560', title='Test pinboard', description='Test description', ) pinboard.officers.set([pinned_officer]) pinboard.allegations.set([pinned_allegation]) pinboard_data = { 'id': '123abc', 'title': 'title', 'description': 'description', 'officer_ids': [1], 'crids': ['1'], 'trr_ids': [], } serializer = PinboardDetailSerializer(data=pinboard_data) expect(serializer.is_valid()).to.be.true() expect(serializer.data).to.eq({ 'title': 'title', 'description': 'description', 'officer_ids': [1], 'crids': ['1'], 'trr_ids': [], })
def test_new_timeline_item_with_officer_alias(self): officer = OfficerFactory(id=456, appointed_date=date(2000, 1, 1)) OfficerAliasFactory(old_officer_id=123, new_officer=officer) unit = PoliceUnitFactory(unit_name='001', description='unit_001') OfficerHistoryFactory(officer=officer, unit=unit, effective_date=date(2010, 1, 1), end_date=date(2011, 12, 31)) AwardFactory(officer=officer, start_date=date(2011, 3, 23), award_type='Life Saving Award') self.refresh_index() response = self.client.get(reverse('api-v2:officers-mobile-new-timeline-items', kwargs={'pk': 123})) expect(response.status_code).to.eq(status.HTTP_200_OK) expect(response.data).to.eq([ { 'unit_name': '001', 'kind': 'AWARD', 'unit_description': 'unit_001', 'award_type': 'Life Saving Award', 'date': '2011-03-23' }, { 'unit_name': '001', 'kind': 'UNIT_CHANGE', 'unit_description': 'unit_001', 'date': '2010-01-01' }, { 'unit_name': '', 'kind': 'JOINED', 'unit_description': '', 'date': '2000-01-01' } ])
def test_check_zip_file_exist_raise_exception(self, aws_mock): client_exception = botocore.exceptions.ClientError( error_response={'Error': { 'Code': 'NoSuchBucket' }}, operation_name='get_object') other_exception = Exception('some other exception') aws_mock.s3.get_object.side_effect = [ client_exception, other_exception ] officer = OfficerFactory(first_name='Jerome', last_name='Finnigan') expect(lambda: officer.check_zip_file_exist(with_docs=False)).to.throw( botocore.exceptions.ClientError) expect(lambda: officer.check_zip_file_exist(with_docs=True)).to.throw( Exception)
def test_export_xlsx_empty(self): officer = OfficerFactory(id=1) writer = DocumentsXlsxWriter(officer, self.test_output_dir) writer.export_xlsx() self.covert_xlsx_to_csv('documents.xlsx') self.assert_csv_files_equal( 'empty', ['Complaint Documents', 'Investigation Documents'])
def test_retrieve_with_alias(self): officer = OfficerFactory(id=456) OfficerAliasFactory(old_officer_id=123, new_officer=officer) self.refresh_index() response = self.client.get(reverse('api-v2:officers-mobile-detail', kwargs={'pk': 123})) expect(response.status_code).to.eq(status.HTTP_200_OK) expect(response.data['officer_id']).to.eq(456)
def test_serialization(self): pinned_officer = OfficerFactory( id=1, rank='Police Officer', first_name='Jerome', last_name='Finnigan', trr_percentile='11.11', complaint_percentile='22.22', civilian_allegation_percentile='33.33', internal_allegation_percentile='44.44', allegation_count=2, ) relevant_allegation = AllegationFactory( crid='1', incident_date=datetime(2002, 2, 21, tzinfo=pytz.utc), most_common_category=AllegationCategoryFactory( category='Operation/Personnel Violations', allegation_name='Subcategory'), point=Point([0.01, 0.02])) VictimFactory(gender='M', race='Black', age=35, allegation=relevant_allegation) pinboard = PinboardFactory( id='66ef1560', title='Test pinboard', description='Test description', ) pinboard.officers.set([pinned_officer]) OfficerAllegationFactory(officer=pinned_officer, allegation=relevant_allegation) expect(pinboard.relevant_complaints.count()).to.eq(1) expect( AllegationMobileSerializer( pinboard.relevant_complaints.first()).data).to.eq({ 'crid': '1', 'category': 'Operation/Personnel Violations', 'incident_date': '2002-02-21', 'officers': [{ 'id': 1, 'rank': 'Police Officer', 'full_name': 'Jerome Finnigan', 'percentile_trr': '11.1100', 'percentile_allegation': '22.2200', 'percentile_allegation_civilian': '33.3300', 'percentile_allegation_internal': '44.4400', }], 'point': { 'lon': 0.01, 'lat': 0.02 }, })
def test_extract_datum(self): datum = PoliceUnitFactory(unit_name='011', description='description') officer = OfficerFactory() officer2 = OfficerFactory() OfficerHistoryFactory(officer=officer, unit=datum, end_date=None) OfficerHistoryFactory(officer=officer2, unit=datum) expect( UnitIndexer().extract_datum(datum) ).to.be.eq({ 'name': '011', 'description': 'description', 'url': datum.v1_url, 'to': datum.v2_to, 'active_member_count': 1, 'member_count': 2, 'long_name': 'Unit 011', })
def test_member_race_aggregation_in_case_officer_left_and_rejoin(self): unit1 = PoliceUnitFactory() unit2 = PoliceUnitFactory() officer = OfficerFactory(race='White') OfficerHistoryFactory(officer=officer, unit=unit1) OfficerHistoryFactory(officer=officer, unit=unit2) OfficerHistoryFactory(officer=officer, unit=unit1) OfficerHistoryFactory(officer=OfficerFactory(race='White'), unit=unit1) OfficerHistoryFactory(officer=OfficerFactory(race=''), unit=unit1) expect(sorted(unit1.member_race_aggregation, key=itemgetter('name'))).to.eq([{ 'name': 'Unknown', 'count': 1 }, { 'name': 'White', 'count': 2 }])
def test_query(self): allegation_officer1 = OfficerFactory(race='White', gender='F') allegation_officer2 = OfficerFactory(race='Hispanic', gender='M') trr_officer_1 = OfficerFactory(race='White', gender='M') trr_officer_2 = OfficerFactory(race='', gender='M') pinboard_officer1 = OfficerFactory(race='White', gender='M') pinboard_officer2 = OfficerFactory(race='Black', gender='') pinboard_officer3 = OfficerFactory(race='Asian/Pacific', gender='X') OfficerFactory(race='White', gender='') OfficerFactory(race='Black', gender='') pinboard_allegation = AllegationFactory() OfficerAllegationFactory(allegation=pinboard_allegation, officer=allegation_officer1) OfficerAllegationFactory(allegation=pinboard_allegation, officer=allegation_officer2) pinboard_trr1 = TRRFactory(officer=trr_officer_1) pinboard_trr2 = TRRFactory(officer=trr_officer_2) pinboard = PinboardFactory(trrs=(pinboard_trr1, pinboard_trr2), allegations=(pinboard_allegation, ), officers=(pinboard_officer1, pinboard_officer2, pinboard_officer3)) query_results = dict(OfficersSummaryQuery(pinboard).query()) expect(list(query_results['race'])).to.eq([{ 'race': 'Black', 'percentage': 0.14 }, { 'race': 'White', 'percentage': 0.43 }, { 'race': 'Hispanic', 'percentage': 0.14 }, { 'race': 'Other', 'percentage': 0.29 }]) expect(list(query_results['gender'])).to.eq([{ 'gender': 'M', 'percentage': 0.57 }, { 'gender': 'F', 'percentage': 0.14 }, { 'gender': 'Unknown', 'percentage': 0.29 }])
def test_list(self): OfficerFactory( id=1, first_name='Daryl', last_name='Mack', trr_percentile=12.0000, allegation_count=12, sustained_count=0, civilian_allegation_percentile=99.7840, internal_allegation_percentile=99.7840, complaint_percentile=99.3450, race='White', gender='M', birth_year=1975, ) OfficerFactory( id=2, first_name='Ronald', last_name='Watts', trr_percentile=0.0000, allegation_count=5, sustained_count=None, civilian_allegation_percentile=98.4344, internal_allegation_percentile=None, complaint_percentile=99.5000, race='Black', gender='F', birth_year=1971, ) response = self.client.get(reverse('api-v2:officers-mobile-list'), {'ids': '2,1'}) expect(response.status_code).to.eq(status.HTTP_200_OK) expect(response.data).to.eq([ { 'id': 2, 'full_name': 'Ronald Watts', 'complaint_count': 5, 'percentile_trr': '0.0000', 'percentile_allegation': '99.5000', 'percentile_allegation_civilian': '98.4344', }, { 'id': 1, 'full_name': 'Daryl Mack', 'complaint_count': 12, 'percentile_allegation': '99.3450', 'percentile_allegation_civilian': '99.7840', 'percentile_allegation_internal': '99.7840', 'percentile_trr': '12.0000', } ])