def test_group_by_and_count(self): results = self.query_agg_api( self.car_api_url, 'group_by[manufacturer__name]', 'group_by[manufacturer__id]', 'aggregate[Count]=id', ) for row in results: count_cars = len(filter_models( self.cars, manufacturer_id=row['manufacturer__id'] )) self.assertEqual(count_cars, row['count_id'])
def test_count(self): results = self.query_agg_api(self.car_api_url, 'aggregate[Count]=id') self.assertIn('count_id', results) self.assertEqual(results['count_id'], len(self.cars)) # we test that filtering works in conjuction with our aggregates classification = Car.CLASSIFICATION[0][0] results = self.query_agg_api( self.car_api_url, f'classification={classification}', 'aggregate[Count]=id', ) self.assertIn('count_id', results) self.assertEqual( results['count_id'], len(filter_models(self.cars, classification=classification)))
def test_group_by_and_choice_fields(self): results = self.query_agg_api( self.car_api_url, 'group_by[classification]', 'aggregate[Count]=id', ) # choice fields both return the database key and the display value self.assertIn('classification', results[0]) self.assertIn('classification_display', results[0]) # check that the group by count worked for row in results: count_cars = len(filter_models( self.cars, classification=row['classification'] )) self.assertEqual(count_cars, row['count_id']) # check that the display value is correct self.assertEqual( row['classification_display'], dict(Car.CLASSIFICATION).get(row['classification']) )
def test_countiffalse(self): results = self.query_agg_api( self.car_api_url, 'aggregate[CountIfFalse]=is_bullet_proof') self.assertIn('countiffalse_is_bullet_proof', results) self.assertEqual(results['countiffalse_is_bullet_proof'], len(filter_models(self.cars, is_bullet_proof=False)))