diff --git a/tests/aggregation/tests.py b/tests/aggregation/tests.py index 618ab98369..f92c140ca7 100644 --- a/tests/aggregation/tests.py +++ b/tests/aggregation/tests.py @@ -2366,16 +2366,12 @@ class JSONArrayAggTests(TestCase): @classmethod def setUpTestData(cls): cls.a1 = Author.objects.create(name="Adrian Holovaty", age=34) - cls.a2 = Author.objects.create(name="Jacob Kaplan-Moss", age=35) - cls.a3 = Author.objects.create(name="Brad Dayley", age=45) - - cls.p1 = Publisher.objects.create( - name="Apress", num_awards=3, duration=datetime.timedelta(days=1) - ) - + cls.a2 = Author.objects.create(name="Jacob Kaplan-Moss", age=45) + cls.p1 = Publisher.objects.create(num_awards=3) + cls.p2 = Publisher.objects.create(num_awards=1) cls.b1 = Book.objects.create( isbn="159059725", - name="The Definitive Guide to Django: Web Development Done Right", + name="b1", pages=447, rating=4.5, price=Decimal("30.00"), @@ -2383,63 +2379,72 @@ class JSONArrayAggTests(TestCase): publisher=cls.p1, pubdate=datetime.date(2007, 12, 6), ) + cls.b1.authors.add(cls.a1) + cls.b2 = Book.objects.create( + isbn="067232959", + name="b2", + pages=528, + rating=3.0, + price=Decimal("23.09"), + contact=cls.a2, + publisher=cls.p2, + pubdate=datetime.date(2008, 3, 3), + ) + cls.b2.authors.add(cls.a2) - def test_JSONArrayAgg(self): - vals = Author.objects.aggregate(jsonarrayagg=JSONArrayAgg("name")) + def test(self): + vals = Book.objects.aggregate(jsonarrayagg=JSONArrayAgg("contact__name")) self.assertEqual( vals, - {"jsonarrayagg": ["Adrian Holovaty", "Jacob Kaplan-Moss", "Brad Dayley"]}, + {"jsonarrayagg": ["Adrian Holovaty", "Jacob Kaplan-Moss"]}, ) - def test_JSONArrayAgg_datefield(self): - vals = Book.objects.aggregate(jsonarrayagg=JSONArrayAgg("pubdate")) + def test_datefield(self): + vals = Author.objects.aggregate(jsonarrayagg=JSONArrayAgg("book__pubdate")) self.assertEqual( vals, { "jsonarrayagg": [ "2007-12-06", + "2008-03-03", ] }, ) - def test_JSONArrayAgg_decimalfield(self): - vals = Book.objects.aggregate(jsonarrayagg=JSONArrayAgg("price")) - self.assertEqual(vals, {"jsonarrayagg": [30.0]}) + def test_decimalfield(self): + vals = Author.objects.aggregate(jsonarrayagg=JSONArrayAgg("book__price")) + self.assertEqual(vals, {"jsonarrayagg": [30.0, 23.09]}) - def test_JSONArrayAgg_integerfield(self): - vals = Book.objects.aggregate(jsonarrayagg=JSONArrayAgg("pages")) - self.assertEqual(vals, {"jsonarrayagg": [447]}) + def test_integerfield(self): + vals = Author.objects.aggregate(jsonarrayagg=JSONArrayAgg("book__pages")) + self.assertEqual(vals, {"jsonarrayagg": [447, 528]}) @skipUnlessDBFeature("supports_aggregate_filter_clause") - def test_JSONArrayAgg_filter(self): - vals = Author.objects.aggregate( - jsonarrayagg=JSONArrayAgg("age", filter=Q(age__gt=35)) + def test_filter(self): + vals = Book.objects.aggregate( + jsonarrayagg=JSONArrayAgg("contact__age", filter=Q(contact__age__gt=35)) ) - self.assertEqual(vals, {"jsonarrayagg": [45]}) - def test_JSONArrayAgg_empty_result_set(self): + def test_empty_result_set(self): Author.objects.all().delete() - val = Author.objects.aggregate(jsonarrayagg=JSONArrayAgg("age")) - self.assertEqual(val, {"jsonarrayagg": None}) - def test_JSONArrayAgg_default_set(self): + def test_default_set(self): Author.objects.all().delete() - val = Author.objects.aggregate( jsonarrayagg=JSONArrayAgg("name", default=[""]) ) self.assertEqual(val, {"jsonarrayagg": [""]}) - def test_JSONArrayAgg_distinct_true(self): + def test_distinct_true(self): msg = "JSONArrayAgg does not allow distinct." with self.assertRaisesMessage(TypeError, msg): JSONArrayAgg("age", distinct=True) @skipIfDBFeature("supports_aggregate_filter_clause") - def test_JSONArrayAgg_not_supported(self): + def test_not_supported(self): msg = "JSONArrayAgg(filter) is not supported on this database backend." with self.assertRaisesMessage(NotSupportedError, msg): Author.objects.aggregate(arrayagg=JSONArrayAgg("age", filter=Q(age__gt=35)))