def test_can_aggregate_city_data(self): City.objects.aggregate.group_by( City.state, Aggregation.sum(City.pop, alias="totalPop") ).match( totalPop__gte=1000 * 1000 ).fetch(callback=self.stop) result = self.wait() expect(result).not_to_be_null() expect(result).to_length(4)
def test_can_average_city_population(self): City.objects.aggregate.group_by( City.state, City.city, Aggregation.sum(City.pop, alias="total_pop")).group_by( City.state, Aggregation.avg("total_pop", alias="avg_pop")).fetch(callback=self.stop) results = self.wait() expect(results).not_to_be_null() expect(results).to_length(4) states = [result.state for result in results] expect(states).to_be_like(['ny', 'ca', 'wa', 'fl']) for state in results: expect(state.avg_pop).to_be_greater_than(2000000)
def test_can_average_city_population(self): City.objects.aggregate.group_by( City.state, City.city, Aggregation.sum(City.pop, alias="total_pop") ).group_by( City.state, Aggregation.avg("total_pop", alias="avg_pop") ).fetch(callback=self.stop) results = self.wait() expect(results).not_to_be_null() expect(results).to_length(4) states = [result.state for result in results] expect(states).to_be_like(['ny', 'ca', 'wa', 'fl']) for state in results: expect(state.avg_pop).to_be_greater_than(2000000)