def test_functions(self): aggregations = [ Aggregation("population", "population_max", models.Max), Aggregation("population", "population_min", models.Min), Aggregation("population", "population_avg", models.Avg), Aggregation("population", "population_sum", models.Sum), Aggregation("population", "population_cnt", models.Count), ] expected = Country.objects.all().aggregate( population_max=models.Max("population"), population_min=models.Min("population"), population_avg=models.Avg("population"), population_sum=models.Sum("population"), population_cnt=models.Count("population"), ) if version.parse(".".join(map(str, django.VERSION))) >= version.parse("2.2.0"): aggregations.append(Aggregation("population", "population_stddev", models.StdDev)) aggregations.append(Aggregation("population", "population_var", models.Variance), ) expected = expected.aggregate( population_stddev=models.StdDev("population"), population_var=models.Variance("population") ) aggregations = [a.get() for a in aggregations] kwargs = dict(aggregations) query = Country.objects.all().aggregate(**kwargs) self.assertEqual(expected, query)
def test_from_query_value(self): query_string = ( "field=population|func=avg|to=population_avg," "field=population|func=max|to=population_max," "field=population|func=min|to=population_min" ) aggregations = utils.split_list_values([query_string], ",") aggregations = [Aggregation.from_query_value(a, Country, self.censor) for a in aggregations] aggregations = [a.get() for a in aggregations] kwargs = dict(aggregations) query = Country.objects.all().aggregate(**kwargs) expected = Country.objects.all().aggregate( population_max=models.Max("population"), population_min=models.Min("population"), population_avg=models.Avg("population"), ) self.assertEqual(expected, query)
def test_missing_field(self): query_string = "to=population_max|func=max" aggregations = utils.split_list_values([query_string], ",") with self.assertRaises(InvalidCommandError): [Aggregation.from_query_value(a, Country, self.censor) for a in aggregations]
def test_no_equal_subquery_string(self): query_string = "population" aggregations = utils.split_list_values([query_string], ",") with self.assertRaises(InvalidCommandError): [Aggregation.from_query_value(a, Country, self.censor) for a in aggregations]
def test_invalid_to(self): query_string = "field=population|func=max|to=1notvalid" aggregations = utils.split_list_values([query_string], ",") with self.assertRaises(InvalidCommandError): [Aggregation.from_query_value(a, Country, self.censor) for a in aggregations]