def test_cache_all(self, salary_cache_mock, officer_cache_mock, allegation_cache_mock, activity_pair_card_cache_mock): cache_managers.cache_all() expect(salary_cache_mock).to.be.called_once() expect(officer_cache_mock).to.be.called_once() expect(allegation_cache_mock).to.be.called_once() expect(activity_pair_card_cache_mock).to.be.called_once() expect(len(cache_managers.managers)).to.eq(4)
def test_new_timeline_item_no_join(self): officer = OfficerFactory(id=123, appointed_date=None, rank='Police 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)) allegation = AllegationFactory( crid='123456', coaccused_count=4, incident_date=datetime(2011, 8, 23, tzinfo=pytz.utc) ) OfficerAllegationFactory( final_finding='UN', final_outcome='Unknown', officer=officer, allegation=allegation, allegation_category=AllegationCategoryFactory(category='category', allegation_name='sub category') ) OfficerAllegationFactory.create_batch(3, allegation=allegation) SalaryFactory(officer=officer, year=2001, rank='Police Officer', spp_date=date(2001, 9, 23)) cache_managers.cache_all() response = self.client.get(reverse('api-v2:officers-new-timeline-items', kwargs={'pk': 123})) expect(response.status_code).to.eq(status.HTTP_200_OK) expect(response.data).to.eq([ { 'date': '2011-08-23', 'kind': 'CR', 'crid': '123456', 'category': 'category', 'subcategory': 'sub category', 'finding': 'Unfounded', 'outcome': 'Unknown', 'coaccused': 4, 'unit_name': '001', 'unit_description': 'unit_001', 'rank': 'Police Officer', 'victims': [], 'attachments': [], }, { 'date': '2010-01-01', 'kind': 'UNIT_CHANGE', 'unit_name': '001', 'unit_description': 'unit_001', 'rank': 'Police Officer', }, { 'date': '2001-09-23', 'kind': 'RANK_CHANGE', 'unit_name': '', 'unit_description': '', 'rank': 'Police Officer', } ])
def test_list_order(self): ActivityCardFactory.create_batch(3, important=True) ActivityCardFactory.create_batch(10, last_activity=datetime(2017, 5, 21, tzinfo=pytz.utc)) ActivityCardFactory.create_batch(10) ActivityCardFactory.create_batch(17, last_activity=datetime(2017, 7, 21, tzinfo=pytz.utc)) ActivityPairCardFactory.create_batch(3, important=True) ActivityPairCardFactory.create_batch(10, last_activity=datetime(2017, 5, 20, tzinfo=pytz.utc)) ActivityPairCardFactory.create_batch(10) ActivityPairCardFactory.create_batch(17, last_activity=datetime(2017, 7, 20, tzinfo=pytz.utc)) url = reverse('api-v2:activity-grid-list') cache_all() response = self.client.get(url) expect(response.status_code).to.eq(status.HTTP_200_OK) expect(response.data).to.have.length(80) for item in response.data[:3]: activity_card = Officer.objects.get(pk=item['id']).activity_card expect(activity_card.important).to.be.true() for item in response.data[3:6]: pair_card = ActivityPairCard.objects.get( officer1_id=item['officer1']['id'], officer2_id=item['officer2']['id'] ) expect(pair_card.important).to.be.true() for item in response.data[6:23]: activity_card = Officer.objects.get(pk=item['id']).activity_card expect(activity_card.last_activity).to.eq(datetime(2017, 7, 21, tzinfo=pytz.utc)) for item in response.data[23:40]: pair_card = ActivityPairCard.objects.get( officer1_id=item['officer1']['id'], officer2_id=item['officer2']['id'] ) expect(pair_card.last_activity).to.eq(datetime(2017, 7, 20, tzinfo=pytz.utc)) for item in response.data[40:50]: activity_card = Officer.objects.get(pk=item['id']).activity_card expect(activity_card.last_activity).to.eq(datetime(2017, 5, 21, tzinfo=pytz.utc)) for item in response.data[50:60]: pair_card = ActivityPairCard.objects.get( officer1_id=item['officer1']['id'], officer2_id=item['officer2']['id'] ) expect(pair_card.last_activity).to.eq(datetime(2017, 5, 20, tzinfo=pytz.utc))
def test_new_timeline_item_merge_rank_and_unit_change_join(self): officer = OfficerFactory(id=123, appointed_date=date(2000, 1, 1), rank='Police Officer') first_unit = PoliceUnitFactory(unit_name='001', description='unit_001') unit = PoliceUnitFactory(unit_name='002', description='unit_002') OfficerHistoryFactory( officer=officer, unit=first_unit, effective_date=date(2000, 1, 1), end_date=date(2009, 12, 31)) OfficerHistoryFactory( officer=officer, unit=unit, effective_date=date(2010, 1, 1), end_date=date(2011, 12, 31)) SalaryFactory(officer=officer, year=2001, rank='Police Officer', spp_date=date(2001, 9, 23)) SalaryFactory(officer=officer, year=2000, rank='Junior Police Officer', spp_date=date(2000, 1, 1)) cache_managers.cache_all() response = self.client.get(reverse('api-v2:officers-new-timeline-items', kwargs={'pk': 123})) expect(response.status_code).to.eq(status.HTTP_200_OK) expect(response.data).to.eq([ { 'date': '2010-01-01', 'kind': 'UNIT_CHANGE', 'unit_name': '002', 'unit_description': 'unit_002', 'rank': 'Police Officer', }, { 'date': '2001-09-23', 'kind': 'RANK_CHANGE', 'unit_name': '001', 'unit_description': 'unit_001', 'rank': 'Police Officer', }, { 'date': '2000-01-01', 'kind': 'JOINED', 'unit_name': '001', 'unit_description': 'unit_001', 'rank': 'Junior Police Officer', } ])
def handle(self, *args, **kwargs): start_time = time.time() cache_managers.cache_all() self.stdout.write(f'Finished on --- {time.time() - start_time} seconds ---')
def test_new_timeline_item(self): officer = OfficerFactory(id=123, appointed_date=date(2000, 1, 1), rank='Police Officer') unit1 = PoliceUnitFactory(unit_name='001', description='unit_001') unit2 = PoliceUnitFactory(unit_name='002', description='unit_002') OfficerHistoryFactory(officer=officer, unit=unit1, effective_date=date(2010, 1, 1), end_date=date(2011, 12, 31)) OfficerHistoryFactory(officer=officer, unit=unit2, effective_date=date(2012, 1, 1), end_date=None) AwardFactory(officer=officer, start_date=date(2011, 3, 23), award_type='Honorable Mention') AwardFactory(officer=officer, start_date=date(2015, 3, 23), award_type='Complimentary Letter') AwardFactory(officer=officer, start_date=date(2011, 3, 23), award_type='Life Saving Award') allegation = AllegationFactory( crid='123456', coaccused_count=4, incident_date=datetime(2011, 8, 23, tzinfo=pytz.utc) ) VictimFactory(allegation=allegation, gender='M', race='White', age=34) OfficerAllegationFactory( final_finding='UN', final_outcome='Unknown', officer=officer, allegation=allegation, allegation_category=AllegationCategoryFactory(category='category', allegation_name='sub category') ) OfficerAllegationFactory.create_batch(3, allegation=allegation) allegation2 = AllegationFactory( crid='654321', point=Point(35.5, 68.9), coaccused_count=1, incident_date=datetime(2015, 8, 23, tzinfo=pytz.utc) ) OfficerAllegationFactory( final_finding='UN', final_outcome='9 Day Suspension', officer=officer, allegation=allegation2, allegation_category=AllegationCategoryFactory(category='Use of Force', allegation_name='sub category') ) trr2011 = TRRFactory( officer=officer, trr_datetime=datetime(2011, 9, 23, tzinfo=pytz.utc), taser=True, firearm_used=False ) trr2015 = TRRFactory( officer=officer, trr_datetime=datetime(2015, 9, 23, tzinfo=pytz.utc), taser=False, firearm_used=False ) SalaryFactory(officer=officer, year=2001, rank='Police Officer', spp_date=date(2001, 9, 23)) SalaryFactory(officer=officer, year=2000, rank='Junior Police Officer', spp_date=date(2000, 1, 1)) cache_managers.cache_all() response = self.client.get(reverse('api-v2:officers-new-timeline-items', kwargs={'pk': 123})) expect(response.status_code).to.eq(status.HTTP_200_OK) expect(response.data).to.eq([ { 'trr_id': trr2015.id, 'date': '2015-09-23', 'kind': 'FORCE', 'taser': False, 'firearm_used': False, 'unit_name': '002', 'unit_description': 'unit_002', 'rank': 'Police Officer', }, { 'date': '2015-08-23', 'kind': 'CR', 'crid': '654321', 'category': 'Use of Force', 'subcategory': 'sub category', 'finding': 'Unfounded', 'outcome': '9 Day Suspension', 'coaccused': 1, 'unit_name': '002', 'unit_description': 'unit_002', 'rank': 'Police Officer', 'point': { 'lon': 35.5, 'lat': 68.9 }, 'victims': [], 'attachments': [] }, { 'date': '2012-01-01', 'kind': 'UNIT_CHANGE', 'unit_name': '002', 'unit_description': 'unit_002', 'rank': 'Police Officer', }, { 'trr_id': trr2011.id, 'date': '2011-09-23', 'kind': 'FORCE', 'taser': True, 'firearm_used': False, 'unit_name': '001', 'unit_description': 'unit_001', 'rank': 'Police Officer', }, { 'date': '2011-08-23', 'kind': 'CR', 'crid': '123456', 'category': 'category', 'subcategory': 'sub category', 'finding': 'Unfounded', 'outcome': 'Unknown', 'coaccused': 4, 'unit_name': '001', 'unit_description': 'unit_001', 'rank': 'Police Officer', 'victims': [ { 'race': 'White', 'age': 34, 'gender': 'Male', } ], 'attachments': [], }, { 'date': '2011-03-23', 'kind': 'AWARD', 'award_type': 'Life Saving Award', 'unit_name': '001', 'unit_description': 'unit_001', 'rank': 'Police Officer', }, { 'date': '2010-01-01', 'kind': 'UNIT_CHANGE', 'unit_name': '001', 'unit_description': 'unit_001', 'rank': 'Police Officer', }, { 'date': '2001-09-23', 'kind': 'RANK_CHANGE', 'unit_name': '', 'unit_description': '', 'rank': 'Police Officer', }, { 'date': '2000-01-01', 'kind': 'JOINED', 'unit_name': '', 'unit_description': '', 'rank': 'Junior Police Officer', }, ])
def test_list_item_content(self): officer1 = OfficerFactory( first_name='Jerome', last_name='Finnigan', birth_year=1950, race='Asian', gender='M', appointed_date=datetime(2011, 1, 1, tzinfo=pytz.utc), complaint_percentile=50.0, allegation_count=6, sustained_count=2, rank='Police Officer', ) officer2 = OfficerFactory( first_name='Raymond', last_name='Piwinicki', birth_year=1960, race='White', gender='M', appointed_date=datetime(2012, 1, 1, tzinfo=pytz.utc), complaint_percentile=0.0, allegation_count=1, sustained_count=1, rank='Police Officer', ) allegation = AllegationFactory(incident_date=datetime(2014, 1, 1, tzinfo=pytz.utc)) OfficerAllegationFactory( officer=officer1, allegation=allegation, final_finding='SU', start_date=date(2014, 1, 1), ) OfficerAllegationFactory( officer=officer2, allegation=allegation, final_finding='SU', start_date=date(2014, 1, 1), ) OfficerAllegationFactory( officer=officer1, final_finding='SU', allegation__incident_date=datetime(2016, 1, 1, tzinfo=pytz.utc), start_date=date(2016, 1, 1) ) OfficerAllegationFactory.create_batch( 4, officer=officer1, final_finding='NS', start_date=date(2015, 1, 1), allegation__incident_date=datetime(2015, 2, 20, tzinfo=pytz.utc) ) ActivityCardFactory(officer=officer1, last_activity=datetime(2018, 12, 22, tzinfo=pytz.utc)) ActivityCardFactory(officer=officer2, last_activity=datetime(2018, 10, 15, tzinfo=pytz.utc)) ActivityPairCardFactory( officer1=officer1, officer2=officer2, last_activity=datetime(2018, 5, 20, tzinfo=pytz.utc) ) cache_all() url = reverse('api-v2:activity-grid-list') response = self.client.get(url) expect(response.status_code).to.eq(status.HTTP_200_OK) expect(response.data).to.eq([{ 'id': officer1.id, 'full_name': 'Jerome Finnigan', 'complaint_count': 6, 'sustained_count': 2, 'birth_year': 1950, 'race': 'Asian', 'gender': 'Male', 'rank': 'Police Officer', 'percentile_trr': '0.0000', 'percentile_allegation_internal': '0.0000', 'percentile_allegation_civilian': '50.0000', 'percentile_allegation': '50.0000', 'kind': 'single_officer', }, { 'id': officer2.id, 'full_name': 'Raymond Piwinicki', 'complaint_count': 1, 'sustained_count': 1, 'birth_year': 1960, 'race': 'White', 'gender': 'Male', 'rank': 'Police Officer', 'percentile_trr': '0.0000', 'percentile_allegation_internal': '0.0000', 'percentile_allegation_civilian': '0.0000', 'percentile_allegation': '0.0000', 'kind': 'single_officer', }, { 'officer1': { 'id': officer1.id, 'full_name': 'Jerome Finnigan', 'birth_year': 1950, 'race': 'Asian', 'gender': 'Male', 'rank': 'Police Officer', 'percentile_trr': '0.0000', 'percentile_allegation_internal': '0.0000', 'percentile_allegation_civilian': '50.0000', 'percentile_allegation': '50.0000', 'complaint_count': 6, 'sustained_count': 2, }, 'officer2': { 'id': officer2.id, 'full_name': 'Raymond Piwinicki', 'birth_year': 1960, 'race': 'White', 'gender': 'Male', 'rank': 'Police Officer', 'percentile_trr': '0.0000', 'percentile_allegation_internal': '0.0000', 'percentile_allegation_civilian': '0.0000', 'percentile_allegation': '0.0000', 'complaint_count': 1, 'sustained_count': 1, }, 'coaccusal_count': 1, 'kind': 'coaccused_pair', }])