def test_default_values(self): """Verify group by with two items""" stops = GroupedData('year', defaults={'race1': 0, 'race2': 0}) stops.add(year=2010, race1=10) stops.add(year=2011, race2=10) expected = [{'year': 2010, 'race1': 10, 'race2': 0}, {'year': 2011, 'race1': 0, 'race2': 10}] self.assertEqual(stops.flatten(), expected)
def stops_by_reason(self, request, pk=None): response = {} # stops results = GroupedData(by=("purpose", "year"), defaults=GROUP_DEFAULTS) self.query(results, group_by=("purpose", "year", "person__race")) self.query(results, group_by=("purpose", "year", "person__ethnicity")) response["stops"] = results.flatten() # searches results = GroupedData(by=("purpose", "year"), defaults=GROUP_DEFAULTS) self.query(results, group_by=("purpose", "year", "person__race"), filter_=Q(search__isnull=False)) self.query(results, group_by=("purpose", "year", "person__ethnicity"), filter_=Q(search__isnull=False)) response["searches"] = results.flatten() return Response(response)
def test_default_values(self): """Verify group by with two items""" stops = GroupedData('year', defaults={'race1': 0, 'race2': 0}) stops.add(year=2010, race1=10) stops.add(year=2011, race2=10) expected = [{ 'year': 2010, 'race1': 10, 'race2': 0 }, { 'year': 2011, 'race1': 0, 'race2': 10 }] self.assertEqual(stops.flatten(), expected)
def contraband_hit_rate(self, request, pk=None): response = {} # searches results = GroupedData(by="year", defaults=GROUP_DEFAULTS) q = Q(search__isnull=False) self.query(results, group_by=("year", "person__race"), filter_=q) self.query(results, group_by=("year", "person__ethnicity"), filter_=q) response["searches"] = results.flatten() # searches results = GroupedData(by="year", defaults=GROUP_DEFAULTS) q = Q(search__contraband__isnull=False) self.query(results, group_by=("year", "person__race"), filter_=q) self.query(results, group_by=("year", "person__ethnicity"), filter_=q) response["contraband"] = results.flatten() return Response(response)
def test_single_item_group(self): """Verify group by with single item""" stops = GroupedData(by='year') stops.add(year=2010, race1=10) stops.add(year=2010, race2=10) stops.add(year=2011, race1=5) stops.add(year=2011, race2=5) expected = [{'year': 2010, 'race1': 10, 'race2': 10}, {'year': 2011, 'race1': 5, 'race2': 5}] self.assertEqual(stops.flatten(), expected)
def test_pair_item_group(self): """Verify group by with two items""" stops = GroupedData(by=('purpose', 'year')) stops.add(purpose='Checkpoint', year=2010, race1=10) stops.add(purpose='Checkpoint', year=2010, race2=10) stops.add(purpose='Checkpoint', year=2011, race1=5) stops.add(purpose='Checkpoint', year=2011, race2=5) expected = [{'purpose': 'Checkpoint', 'year': 2010, 'race1': 10, 'race2': 10}, {'purpose': 'Checkpoint', 'year': 2011, 'race1': 5, 'race2': 5}] self.assertEqual(stops.flatten(), expected)
def test_single_item_group(self): """Verify group by with single item""" stops = GroupedData(by='year') stops.add(year=2010, race1=10) stops.add(year=2010, race2=10) stops.add(year=2011, race1=5) stops.add(year=2011, race2=5) expected = [{ 'year': 2010, 'race1': 10, 'race2': 10 }, { 'year': 2011, 'race1': 5, 'race2': 5 }] self.assertEqual(stops.flatten(), expected)
def contraband_hit_rate(self, request, pk=None): response = {} # searches results = GroupedData(by='year', defaults=GROUP_DEFAULTS) q = Q(search__isnull=False) self.query(results, group_by=('year', 'person__race'), filter_=q) self.query(results, group_by=('year', 'person__ethnicity'), filter_=q) response['searches'] = results.flatten() # searches results = GroupedData(by='year', defaults=GROUP_DEFAULTS) q = Q(search__contraband__isnull=False) self.query(results, group_by=('year', 'person__race'), filter_=q) self.query(results, group_by=('year', 'person__ethnicity'), filter_=q) response['contraband'] = results.flatten() return Response(response)
def test_pair_item_group(self): """Verify group by with two items""" stops = GroupedData(by=('purpose', 'year')) stops.add(purpose='Checkpoint', year=2010, race1=10) stops.add(purpose='Checkpoint', year=2010, race2=10) stops.add(purpose='Checkpoint', year=2011, race1=5) stops.add(purpose='Checkpoint', year=2011, race2=5) expected = [{ 'purpose': 'Checkpoint', 'year': 2010, 'race1': 10, 'race2': 10 }, { 'purpose': 'Checkpoint', 'year': 2011, 'race1': 5, 'race2': 5 }] self.assertEqual(stops.flatten(), expected)
def stops_by_reason(self, request, pk=None): response = {} # stops results = GroupedData(by=('purpose', 'year'), defaults=GROUP_DEFAULTS) self.query(results, group_by=('purpose', 'year', 'person__race')) self.query(results, group_by=('purpose', 'year', 'person__ethnicity')) response['stops'] = results.flatten() # searches results = GroupedData(by=('purpose', 'year'), defaults=GROUP_DEFAULTS) self.query(results, group_by=('purpose', 'year', 'person__race'), filter_=Q(search__isnull=False)) self.query(results, group_by=('purpose', 'year', 'person__ethnicity'), filter_=Q(search__isnull=False)) response['searches'] = results.flatten() return Response(response)
def stops(self, request, pk=None): results = GroupedData(by='year', defaults=GROUP_DEFAULTS) self.query(results, group_by=('year', 'person__race')) self.query(results, group_by=('year', 'person__ethnicity')) return Response(results.flatten())
def searches(self, request, pk=None): results = GroupedData(by='year', defaults=GROUP_DEFAULTS) q = Q(search__isnull=False) self.query(results, group_by=('year', 'person__race'), filter_=q) self.query(results, group_by=('year', 'person__ethnicity'), filter_=q) return Response(results.flatten())
def stops(self, request, pk=None): results = GroupedData(by="year", defaults=GROUP_DEFAULTS) self.query(results, group_by=("year", "person__race")) self.query(results, group_by=("year", "person__ethnicity")) return Response(results.flatten())
def searches(self, request, pk=None): results = GroupedData(by="year", defaults=GROUP_DEFAULTS) q = Q(search__isnull=False) self.query(results, group_by=("year", "person__race"), filter_=q) self.query(results, group_by=("year", "person__ethnicity"), filter_=q) return Response(results.flatten())