def test_get_officers_most_complaints(self): officer123 = OfficerFactory( id=123, rank='Officer', first_name='Jerome', last_name='Finnigan', allegation_count=2, ) officer456 = OfficerFactory( id=456, rank='Officer', first_name='Ellis', last_name='Skol', allegation_count=1, ) OfficerFactory( id=789, rank='Senior Police Officer', first_name='Raymond', last_name='Piwinicki', allegation_count=0, ) expect(list(Officer.get_officers_most_complaints( rank='Officer'))).to.eq([officer123, officer456])
def test_allegation_attachments(self): allegation = AllegationFactory(crid='123') attachment_1 = AttachmentFileFactory(allegation=allegation, source_type='DOCUMENTCLOUD') attachment_2 = AttachmentFileFactory( allegation=allegation, source_type='PORTAL_COPA_DOCUMENTCLOUD') AttachmentFileFactory(allegation=allegation, source_type='PORTAL_COPA_DOCUMENTCLOUD', show=False) AttachmentFileFactory(allegation=allegation, source_type='COPA') allegation_456 = AllegationFactory(crid='456') AttachmentFileFactory(allegation=allegation_456, source_type='DOCUMENTCLOUD') AttachmentFileFactory(allegation=allegation_456, source_type='PORTAL_COPA_DOCUMENTCLOUD') AttachmentFileFactory(allegation=allegation_456, source_type='PORTAL_COPA_DOCUMENTCLOUD', show=False) officer = OfficerFactory(id=1) OfficerAllegationFactory(officer=officer, allegation=allegation) expect({ attachment.id for attachment in officer.allegation_attachments }).to.eq({attachment_1.id, attachment_2.id})
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_force_types(self): trr = TRRFactory() ActionResponseFactory(trr=trr, force_type='Physical Force - Stunning', action_sub_category='4') ActionResponseFactory(trr=trr, force_type='Taser', action_sub_category='5.1') ActionResponseFactory(trr=trr, force_type='Other', action_sub_category=None, person='Subject Action') ActionResponseFactory(trr=trr, force_type='Impact Weapon', action_sub_category='5.2') ActionResponseFactory(trr=trr, force_type='Taser Display', action_sub_category='3') ActionResponseFactory(trr=trr, force_type='Taser Display', action_sub_category='3') expect(list(trr.force_types)).to.eq([ 'Impact Weapon', 'Taser', 'Physical Force - Stunning', 'Taser Display' ])
def test_log_url_with_empty_log_key(self): document_crawler = DocumentCrawlerFactory( id=1, source_type='SUMMARY_REPORTS_COPA', ) expect(document_crawler.log_url).to.be.none()
def test_investigator_attachments(self): allegation = AllegationFactory(crid='123') allegation_456 = AllegationFactory(crid='456') attachment_1 = AttachmentFileFactory(allegation=allegation, source_type='DOCUMENTCLOUD') attachment_2 = AttachmentFileFactory( allegation=allegation, source_type='PORTAL_COPA_DOCUMENTCLOUD') AttachmentFileFactory(allegation=allegation, source_type='PORTAL_COPA_DOCUMENTCLOUD', show=False) AttachmentFileFactory(allegation=allegation, source_type='COPA') AttachmentFileFactory(allegation=allegation, source_type='COPA', show=False) AttachmentFileFactory(allegation=allegation_456, source_type='DOCUMENTCLOUD') AttachmentFileFactory(allegation=allegation_456, source_type='PORTAL_COPA_DOCUMENTCLOUD') AttachmentFileFactory(allegation=allegation_456, source_type='PORTAL_COPA_DOCUMENTCLOUD', show=False) investigator = InvestigatorFactory(officer=OfficerFactory(id=1)) InvestigatorAllegationFactory(allegation=allegation, investigator=investigator) expect({ attachment.id for attachment in investigator.officer.investigator_attachments }).to.eq({attachment_1.id, attachment_2.id})
def test_complainant_races(self): allegation = AllegationFactory() ComplainantFactory(race='White', allegation=allegation) expect(allegation.complainant_races).to.eq(['White']) ComplainantFactory(race='White/Hispinic', allegation=allegation) expect(allegation.complainant_races).to.eq(['White', 'White/Hispinic'])
def test_unique_trr_and_email(self): trr = TRRFactory() email = '*****@*****.**' TRRAttachmentRequestFactory(email=email, trr=trr) with transaction.atomic(): expect(lambda: TRRAttachmentRequestFactory(email=email, trr=trr)).to.throw(IntegrityError) expect(len(TRRAttachmentRequest.objects.all())).to.eq(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_compute_trr_metric(self): officer = OfficerFactory(id=1, appointed_date=date(2010, 3, 14)) TRRFactory(officer=officer, trr_datetime=datetime(2014, 12, 31, tzinfo=pytz.utc)) TRRFactory(officer=officer, trr_datetime=datetime(2015, 1, 1, tzinfo=pytz.utc)) TRRFactory(officer=officer, trr_datetime=datetime(2016, 1, 1, tzinfo=pytz.utc)) TRRFactory(officer=officer, trr_datetime=datetime(2016, 2, 1, tzinfo=pytz.utc)) TRRFactory(officer=officer, trr_datetime=datetime(2017, 2, 1, tzinfo=pytz.utc)) officers = officer_percentile._compute_metric(2016, PERCENTILE_TRR_GROUP) expect(officers).to.have.length(1) validate_object( officers[0], { 'officer_id': 1, 'year': 2016, 'start_date': date(2015, 1, 1), 'end_date': date(2016, 7, 1), 'service_year': 1.4986, 'num_trr': 3, 'metric_trr': 2.0019 })
def test_compute_honorable_mention_metric_less_than_one_year(self): self._create_dataset_for_honorable_mention_percentile() # expect officer2 to be excluded cause he service less than 1 year new_percentile_map = officer_percentile.create_percentile_map() with patch('data.officer_percentile.PERCENTILE_MAP', new_percentile_map): honorable_mention_metric_2016 = officer_percentile._compute_metric( 2016, PERCENTILE_HONORABLE_MENTION_GROUP) expect(honorable_mention_metric_2016.count()).to.eq(1) validate_object(honorable_mention_metric_2016[0], { 'id': 1, 'metric_honorable_mention': 0.75 }) honorable_mention_metric_2017 = officer_percentile._compute_metric( 2017, PERCENTILE_HONORABLE_MENTION_GROUP) expect(honorable_mention_metric_2017.count()).to.eq(2) validate_object(honorable_mention_metric_2017[0], { 'id': 1, 'metric_honorable_mention': 0.625 }) validate_object(honorable_mention_metric_2017[1], { 'id': 2, 'metric_honorable_mention': 1.875 })
def test_compute_allegation_metric(self): officer = OfficerFactory(id=1, appointed_date=date(2010, 3, 14)) OfficerAllegationFactory( officer=officer, allegation__incident_date=datetime(2014, 12, 31, tzinfo=pytz.utc), ) OfficerAllegationFactory.create_batch( 3, officer=officer, allegation__incident_date=datetime(2016, 1, 16, tzinfo=pytz.utc), ) OfficerAllegationFactory( officer=officer, allegation__incident_date=datetime(2016, 7, 2, tzinfo=pytz.utc), ) officers = officer_percentile._compute_metric( 2016, PERCENTILE_ALLEGATION_GROUP) expect(officers).to.have.length(1) validate_object( officers[0], { 'officer_id': 1, 'year': 2016, 'start_date': date(2015, 1, 1), 'end_date': date(2016, 7, 1), 'service_year': 1.4986, 'num_allegation': 3, 'metric_allegation': 2.0019, })
def test_with_no_params(self): actual = Mock() dummy_matcher = DummyMockMatcher(actual) actual() expect(dummy_matcher.call_args_str).to.eq('no arguments')
def test_is_empty(self): officer_1 = OfficerFactory(id=1) officer_2 = OfficerFactory(id=2) allegation_1 = AllegationFactory(crid='123abc') allegation_2 = AllegationFactory(crid='456def') trr_1 = TRRFactory(id=1, officer=OfficerFactory(id=3)) trr_2 = TRRFactory(id=2, officer=OfficerFactory(id=4)) pinboard_full = PinboardFactory( officers=(officer_1, officer_2), allegations=(allegation_1, allegation_2), trrs=(trr_1, trr_2), ) pinboard_with_officers = PinboardFactory(officers=(officer_1, officer_2), ) pinboard_with_allegations = PinboardFactory( allegations=(allegation_1, allegation_2), ) pinboard_with_trrs = PinboardFactory(trrs=(trr_1, trr_2), ) pinboard_empty = PinboardFactory() expect(pinboard_full.is_empty).to.be.false() expect(pinboard_with_officers.is_empty).to.be.false() expect(pinboard_with_allegations.is_empty).to.be.false() expect(pinboard_with_trrs.is_empty).to.be.false() expect(pinboard_empty.is_empty).to.be.true()
def test_with_args_and_kwargs(self): actual = Mock() dummy_matcher = DummyMockMatcher(actual) actual(1, 'a', 3, a=1, b=2) expect(dummy_matcher.call_args_str).to.eq("1, 'a', 3, a=1, b=2")
def test_complainant_genders(self): allegation = AllegationFactory() ComplainantFactory(gender='F', allegation=allegation) expect(allegation.complainant_genders).to.eq(['Female']) ComplainantFactory(gender='U', allegation=allegation) expect(allegation.complainant_genders).to.eq(['Female', 'Unknown'])
def test_victims(self): allegation = AllegationFactory() victim1 = VictimFactory(allegation=allegation) victim2 = VictimFactory(allegation=allegation) officer_allegation = OfficerAllegationFactory(allegation=allegation) expect(list(officer_allegation.victims)).to.eq([victim1, victim2])
def test_historic_badges(self): officer = OfficerFactory() expect(officer.historic_badges).to.be.empty() OfficerBadgeNumberFactory(officer=officer, star='000', current=True) OfficerBadgeNumberFactory(officer=officer, star='123', current=False) OfficerBadgeNumberFactory(officer=officer, star='456', current=False) expect(list(officer.historic_badges)).to.eq(['123', '456'])
def test_category(self): officer_allegation = OfficerAllegationFactory(allegation_category=None) expect(officer_allegation.category).to.eq(None) allegation_category = AllegationCategoryFactory(category='category') officer_allegation = OfficerAllegationFactory( allegation_category=allegation_category) expect(officer_allegation.category).to.eq('category')
def test_get_zip_filename(self): officer = OfficerFactory(id=1, first_name='Jerome', last_name='Finnigan') expect(officer.get_zip_filename( with_docs=False)).to.eq('zip/Jerome_Finnigan.zip') expect(officer.get_zip_filename(with_docs=True)).to.eq( 'zip_with_docs/Jerome_Finnigan_with_docs.zip')
def test_unique_allegation_and_email(self): allegation = AllegationFactory() email = '*****@*****.**' AttachmentRequestFactory(email=email, allegation=allegation) expect(lambda: AttachmentRequestFactory( email=email, allegation=allegation)).to.throw(ValidationError) expect(len(AttachmentRequest.objects.all())).to.eq(1)
def test_officer_service_year_filter_no_appointed_date(self): OfficerFactory(id=1, appointed_date=date(2010, 3, 14)) OfficerFactory(id=2, appointed_date=None) officers = officer_percentile._officer_service_year( date(2015, 2, 1), date(2016, 7, 1)) expect(officers).to.have.length(1) expect(officers[0].id).to.equal(1)
def test_queryset_with_tag_name_filter(self, mock_value): tag_list_filter = TagListFilter(request=self.request, model_admin=self.attachment_file_admin, model=AttachmentFile, params='Tag') queryset = AttachmentFile.objects.all() expect(list(tag_list_filter.queryset(self.request, queryset))).to.eq( [self.attachment_file_1])
def test_crids(self): pinned_allegation_1 = AllegationFactory(crid='1') pinned_allegation_2 = AllegationFactory(crid='2') pinboard = PinboardFactory( title='Test pinboard', description='Test description', ) pinboard.allegations.set([pinned_allegation_1, pinned_allegation_2]) expect(list(pinboard.crids)).to.eq(['1', '2'])
def test_trr_ids(self): pinned_trr_1 = TRRFactory(id=1) pinned_trr_2 = TRRFactory(id=2) pinboard = PinboardFactory( title='Test pinboard', description='Test description', ) pinboard.trrs.set([pinned_trr_1, pinned_trr_2]) expect(list(pinboard.trr_ids)).to.eq([1, 2])
def test_get_award_dataset_range(self): expect(officer_percentile._get_award_dataset_range()).to.be.empty() self._create_dataset_for_honorable_mention_percentile() expect(officer_percentile._get_award_dataset_range()).to.be.eq( (datetime(2013, 1, 1, tzinfo=pytz.utc), datetime(2017, 10, 19, tzinfo=pytz.utc)))
def test_investigator_names(self): allegation = AllegationFactory() investigator = InvestigatorFactory(first_name='Jerome', last_name='Finnigan') InvestigatorAllegationFactory(allegation=allegation, investigator=investigator) attachment_request = AttachmentRequestFactory(allegation=allegation) expect( attachment_request.investigator_names()).to.eq('Jerome Finnigan')
def test_member_count_in_case_officer_left_and_rejoin(self): unit1 = PoliceUnitFactory() unit2 = PoliceUnitFactory() officer = OfficerFactory() OfficerHistoryFactory(officer=officer, unit=unit1) OfficerHistoryFactory(officer=officer, unit=unit2) OfficerHistoryFactory(officer=officer, unit=unit1) expect(unit1.member_count).to.eq(1) expect(unit2.member_count).to.eq(1)
def test_active_member_count(self): unit1 = PoliceUnitFactory() unit2 = PoliceUnitFactory() officer = OfficerFactory() OfficerHistoryFactory(officer=officer, unit=unit1, end_date=datetime(2011, 1, 1, tzinfo=pytz.utc)) OfficerHistoryFactory(officer=officer, unit=unit2, end_date=datetime(2011, 2, 1, tzinfo=pytz.utc)) OfficerHistoryFactory(officer=officer, unit=unit1, end_date=None) expect(unit1.active_member_count).to.eq(1) expect(unit2.active_member_count).to.eq(0)
def test_subcategory(self): officer_allegation = OfficerAllegationFactory(allegation_category=None) expect(officer_allegation.subcategory).to.eq(None) allegation_category = AllegationCategoryFactory( allegation_name='subcategory') officer_allegation = OfficerAllegationFactory( allegation_category=allegation_category) expect(officer_allegation.subcategory).to.eq('subcategory')
def match(self): if not self.matches(): message = self.message or self.failure_message() raise BadExpectation(message) return expect(self.actual)