def test_searches(self): """Test Agency search counts""" agency = factories.AgencyFactory() # Create the following racial data for 2015: 1 black p1 = factories.PersonFactory(race="B", ethnicity="N", stop__agency=agency, stop__year=2015) s1 = factories.SearchFactory(person=p1, stop=p1.stop) # Create the following racial data for 2016: 1 native american, 3 hispanic p2 = factories.PersonFactory(race="W", ethnicity="H", stop__agency=agency, stop__year=2016) s2 = factories.SearchFactory(person=p2, stop=p2.stop) p3 = factories.PersonFactory(race="B", ethnicity="H", stop__agency=agency, stop__year=2016) factories.SearchFactory(person=p3, stop=p3.stop) p4 = factories.PersonFactory(race="B", ethnicity="H", stop__agency=agency, stop__year=2016) factories.SearchFactory(person=p4, stop=p4.stop) p5 = factories.PersonFactory(race="I", ethnicity="N", stop__agency=agency, stop__year=2016) factories.SearchFactory(person=p5, stop=p5.stop) url = reverse("nc:agency-api-searches", args=[agency.pk]) response = self.client.get(url, format="json") self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(len(response.data), 2) # Everyone got searched, so the expected racial data for 2015 are: 1 black, # and for 2016 are: 1 native american, 3 hispanic self.assertEqual(response.data[0]["year"], s1.stop.date.year) self.assertEqual(response.data[0]["black"], 1) self.assertEqual(response.data[1]["year"], s2.stop.date.year) self.assertEqual(response.data[1]["black"], 0) self.assertEqual(response.data[1]["native_american"], 1) self.assertEqual(response.data[1]["hispanic"], 3)
def test_searches_by_reason(self): agency = factories.AgencyFactory() url = reverse("nc:agency-api-searches-by-type", args=[agency.pk]) type_code, type_label = SEARCH_TYPE_CHOICES[2] # Create the following racial data for 2015: 1 black p1 = factories.PersonFactory(race="B", ethnicity="N", stop__agency=agency, stop__year=2015) factories.SearchFactory( person=p1, stop=p1.stop, type=type_code, ) # Create the following racial data for 2016: 1 native american, 3 hispanic p2 = factories.PersonFactory(race="W", ethnicity="H", stop__agency=agency, stop__year=2016) factories.SearchFactory( person=p2, stop=p2.stop, type=type_code, ) p3 = factories.PersonFactory(race="B", ethnicity="H", stop__agency=agency, stop__year=2016) factories.SearchFactory( person=p3, stop=p3.stop, type=type_code, ) p4 = factories.PersonFactory(race="B", ethnicity="H", stop__agency=agency, stop__year=2016) factories.SearchFactory( person=p4, stop=p4.stop, type=type_code, ) p5 = factories.PersonFactory(race="I", ethnicity="N", stop__agency=agency, stop__year=2016) factories.SearchFactory( person=p5, stop=p5.stop, type=type_code, ) response = self.client.get(url, format="json") self.assertEqual(response.status_code, status.HTTP_200_OK) # Two years = two items self.assertEqual(len(response.data), 2) searches = response.data self.assertEqual( searches[0]["year"], 2015, ) self.assertEqual( searches[0]["black"], 1, ) self.assertEqual( searches[0]["search_type"], type_label, ) self.assertEqual( searches[1]["year"], 2016, ) self.assertEqual( searches[1]["hispanic"], 3, ) self.assertEqual( searches[1]["native_american"], 1, ) self.assertEqual( searches[1]["search_type"], type_label, )
def test_contraband_hit_rate(self): agency = factories.AgencyFactory() # Create the following racial data for 2010: # 1 black, 1 native american, 3 hispanic p1 = factories.PersonFactory(race="B", stop__agency=agency, ethnicity="N", stop__year=2010) p2 = factories.PersonFactory(race="B", stop__agency=agency, ethnicity="H", stop__year=2010) p3 = factories.PersonFactory(race="I", stop__agency=agency, ethnicity="N", stop__year=2010) p4 = factories.PersonFactory(race="I", stop__agency=agency, ethnicity="H", stop__year=2010) p5 = factories.PersonFactory(race="I", stop__agency=agency, ethnicity="H", stop__year=2010) # Create the following racial data for 2012: 1 black p6 = factories.PersonFactory(race="B", stop__agency=agency, ethnicity="N", stop__year=2012) s1 = factories.SearchFactory(stop=p1.stop) factories.SearchFactory(stop=p2.stop) s3 = factories.SearchFactory(stop=p3.stop) s4 = factories.SearchFactory(stop=p4.stop) s5 = factories.SearchFactory(stop=p5.stop) s6 = factories.SearchFactory(stop=p6.stop) factories.ContrabandFactory(search=s1, person=p1, stop=p1.stop) factories.ContrabandFactory(search=s3, person=p3, stop=p3.stop) factories.ContrabandFactory(search=s4, person=p4, stop=p4.stop) factories.ContrabandFactory(search=s5, person=p5, stop=p5.stop) factories.ContrabandFactory(search=s6, person=p6, stop=p6.stop) url = reverse("nc:agency-api-contraband-hit-rate", args=[agency.pk]) response = self.client.get(url, format="json") self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(len(response.data.keys()), 2) searches = response.data["searches"] # The expected search data matches the created data, since each of the # people were searched self.assertEqual(searches[0]["year"], 2010) self.assertEqual(searches[0]["black"], 1) self.assertEqual(searches[0]["native_american"], 1) self.assertEqual(searches[0]["hispanic"], 3) self.assertEqual(searches[1]["year"], 2012) self.assertEqual(searches[1]["black"], 1) contraband = response.data["contraband"] # Everyone had contraband, except for p2, so the expected contraband data # for 2010 are: 1 black, 1 native american, 2 hispanic, and for 2012 # are: 1 black self.assertEqual(contraband[0]["year"], 2010) self.assertEqual(contraband[0]["black"], 1) self.assertEqual(contraband[0]["native_american"], 1) self.assertEqual(contraband[0]["hispanic"], 2) self.assertEqual(contraband[1]["year"], 2012) self.assertEqual(contraband[1]["black"], 1)
def test_stops_by_reason(self): """Test Agency stops_by_reason API endpoint""" agency = factories.AgencyFactory() url = reverse('nc:agency-api-stops-by-reason', args=[agency.pk]) purpose_code, purpose_label = PURPOSE_CHOICES[4] # Create the following racial data for 2010: 2 black, 3 hispanic factories.PersonFactory(race='B', stop__agency=agency, ethnicity='N', stop__year=2010, stop__purpose=purpose_code) p2 = factories.PersonFactory(race='B', stop__agency=agency, ethnicity='H', stop__year=2010, stop__purpose=purpose_code) p3 = factories.PersonFactory(race='I', stop__agency=agency, ethnicity='H', stop__year=2010, stop__purpose=purpose_code) p4 = factories.PersonFactory(race='I', stop__agency=agency, ethnicity='H', stop__year=2010, stop__purpose=purpose_code) # Create the following racial data for 2012: 1 black p5 = factories.PersonFactory(race='B', stop__agency=agency, ethnicity='N', stop__year=2012, stop__purpose=purpose_code) # Everyone except for the first person got searched, so the expected # search data for 2010 are: 1 black, 3 hispanic, and for 2012 is: 1 black factories.SearchFactory(stop=p2.stop) factories.SearchFactory(stop=p3.stop) factories.SearchFactory(stop=p4.stop) factories.SearchFactory(stop=p5.stop) response = self.client.get(url, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(len(response.data.keys()), 2) searches = response.data['searches'] self.assertEqual(searches[0]['year'], 2010) self.assertEqual(searches[0]['black'], 0) self.assertEqual(searches[0]['hispanic'], 3) self.assertEqual(searches[0]['purpose'], purpose_label) self.assertEqual(searches[1]['year'], 2012) self.assertEqual(searches[1]['black'], 1) self.assertEqual(searches[1]['purpose'], purpose_label) stops = response.data['stops'] self.assertEqual(stops[0]['year'], 2010) self.assertEqual(stops[0]['black'], 1) self.assertEqual(stops[0]['hispanic'], 3) self.assertEqual(stops[0]['purpose'], purpose_label) self.assertEqual(stops[1]['year'], 2012) self.assertEqual(stops[1]['black'], 1) self.assertEqual(stops[1]['purpose'], purpose_label)