async def test_can_average_city_population(self): results = await 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() 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)
async def test_can_aggregate_number_of_documents(self): result = await User.objects.aggregate.group_by( User.email, Aggregation.avg( User.number_of_documents, alias="number_of_documents" ) ).fetch() expect(result).not_to_be_null() expect(result).to_length(1) expect(result[0].email).to_equal('*****@*****.**') expect(result[0].number_of_documents).to_be_like(4950.0)
async def test_can_aggregate_with_unwind(self): result = await User.objects.aggregate.unwind(User.list_items).group_by( User.email, User.list_items, Aggregation.avg( User.number_of_documents, alias="number_of_documents" ) ).fetch() expect(result).not_to_be_null() expect(result).to_length(99) expect(result[0].email).to_equal('*****@*****.**') expect(result[0].list_items).to_be_numeric() expect(result[0].number_of_documents).to_be_numeric()