mirror of
https://github.com/django/django.git
synced 2024-11-24 20:07:01 +01:00
Removed JSONArrayAgg on test names and modified some tests to use relationships.
This commit is contained in:
parent
1f0221bbf5
commit
140ae867ee
@ -2366,16 +2366,12 @@ class JSONArrayAggTests(TestCase):
|
|||||||
@classmethod
|
@classmethod
|
||||||
def setUpTestData(cls):
|
def setUpTestData(cls):
|
||||||
cls.a1 = Author.objects.create(name="Adrian Holovaty", age=34)
|
cls.a1 = Author.objects.create(name="Adrian Holovaty", age=34)
|
||||||
cls.a2 = Author.objects.create(name="Jacob Kaplan-Moss", age=35)
|
cls.a2 = Author.objects.create(name="Jacob Kaplan-Moss", age=45)
|
||||||
cls.a3 = Author.objects.create(name="Brad Dayley", age=45)
|
cls.p1 = Publisher.objects.create(num_awards=3)
|
||||||
|
cls.p2 = Publisher.objects.create(num_awards=1)
|
||||||
cls.p1 = Publisher.objects.create(
|
|
||||||
name="Apress", num_awards=3, duration=datetime.timedelta(days=1)
|
|
||||||
)
|
|
||||||
|
|
||||||
cls.b1 = Book.objects.create(
|
cls.b1 = Book.objects.create(
|
||||||
isbn="159059725",
|
isbn="159059725",
|
||||||
name="The Definitive Guide to Django: Web Development Done Right",
|
name="b1",
|
||||||
pages=447,
|
pages=447,
|
||||||
rating=4.5,
|
rating=4.5,
|
||||||
price=Decimal("30.00"),
|
price=Decimal("30.00"),
|
||||||
@ -2383,63 +2379,72 @@ class JSONArrayAggTests(TestCase):
|
|||||||
publisher=cls.p1,
|
publisher=cls.p1,
|
||||||
pubdate=datetime.date(2007, 12, 6),
|
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):
|
def test(self):
|
||||||
vals = Author.objects.aggregate(jsonarrayagg=JSONArrayAgg("name"))
|
vals = Book.objects.aggregate(jsonarrayagg=JSONArrayAgg("contact__name"))
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
vals,
|
vals,
|
||||||
{"jsonarrayagg": ["Adrian Holovaty", "Jacob Kaplan-Moss", "Brad Dayley"]},
|
{"jsonarrayagg": ["Adrian Holovaty", "Jacob Kaplan-Moss"]},
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_JSONArrayAgg_datefield(self):
|
def test_datefield(self):
|
||||||
vals = Book.objects.aggregate(jsonarrayagg=JSONArrayAgg("pubdate"))
|
vals = Author.objects.aggregate(jsonarrayagg=JSONArrayAgg("book__pubdate"))
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
vals,
|
vals,
|
||||||
{
|
{
|
||||||
"jsonarrayagg": [
|
"jsonarrayagg": [
|
||||||
"2007-12-06",
|
"2007-12-06",
|
||||||
|
"2008-03-03",
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_JSONArrayAgg_decimalfield(self):
|
def test_decimalfield(self):
|
||||||
vals = Book.objects.aggregate(jsonarrayagg=JSONArrayAgg("price"))
|
vals = Author.objects.aggregate(jsonarrayagg=JSONArrayAgg("book__price"))
|
||||||
self.assertEqual(vals, {"jsonarrayagg": [30.0]})
|
self.assertEqual(vals, {"jsonarrayagg": [30.0, 23.09]})
|
||||||
|
|
||||||
def test_JSONArrayAgg_integerfield(self):
|
def test_integerfield(self):
|
||||||
vals = Book.objects.aggregate(jsonarrayagg=JSONArrayAgg("pages"))
|
vals = Author.objects.aggregate(jsonarrayagg=JSONArrayAgg("book__pages"))
|
||||||
self.assertEqual(vals, {"jsonarrayagg": [447]})
|
self.assertEqual(vals, {"jsonarrayagg": [447, 528]})
|
||||||
|
|
||||||
@skipUnlessDBFeature("supports_aggregate_filter_clause")
|
@skipUnlessDBFeature("supports_aggregate_filter_clause")
|
||||||
def test_JSONArrayAgg_filter(self):
|
def test_filter(self):
|
||||||
vals = Author.objects.aggregate(
|
vals = Book.objects.aggregate(
|
||||||
jsonarrayagg=JSONArrayAgg("age", filter=Q(age__gt=35))
|
jsonarrayagg=JSONArrayAgg("contact__age", filter=Q(contact__age__gt=35))
|
||||||
)
|
)
|
||||||
|
|
||||||
self.assertEqual(vals, {"jsonarrayagg": [45]})
|
self.assertEqual(vals, {"jsonarrayagg": [45]})
|
||||||
|
|
||||||
def test_JSONArrayAgg_empty_result_set(self):
|
def test_empty_result_set(self):
|
||||||
Author.objects.all().delete()
|
Author.objects.all().delete()
|
||||||
|
|
||||||
val = Author.objects.aggregate(jsonarrayagg=JSONArrayAgg("age"))
|
val = Author.objects.aggregate(jsonarrayagg=JSONArrayAgg("age"))
|
||||||
|
|
||||||
self.assertEqual(val, {"jsonarrayagg": None})
|
self.assertEqual(val, {"jsonarrayagg": None})
|
||||||
|
|
||||||
def test_JSONArrayAgg_default_set(self):
|
def test_default_set(self):
|
||||||
Author.objects.all().delete()
|
Author.objects.all().delete()
|
||||||
|
|
||||||
val = Author.objects.aggregate(
|
val = Author.objects.aggregate(
|
||||||
jsonarrayagg=JSONArrayAgg("name", default=["<empty>"])
|
jsonarrayagg=JSONArrayAgg("name", default=["<empty>"])
|
||||||
)
|
)
|
||||||
self.assertEqual(val, {"jsonarrayagg": ["<empty>"]})
|
self.assertEqual(val, {"jsonarrayagg": ["<empty>"]})
|
||||||
|
|
||||||
def test_JSONArrayAgg_distinct_true(self):
|
def test_distinct_true(self):
|
||||||
msg = "JSONArrayAgg does not allow distinct."
|
msg = "JSONArrayAgg does not allow distinct."
|
||||||
with self.assertRaisesMessage(TypeError, msg):
|
with self.assertRaisesMessage(TypeError, msg):
|
||||||
JSONArrayAgg("age", distinct=True)
|
JSONArrayAgg("age", distinct=True)
|
||||||
|
|
||||||
@skipIfDBFeature("supports_aggregate_filter_clause")
|
@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."
|
msg = "JSONArrayAgg(filter) is not supported on this database backend."
|
||||||
with self.assertRaisesMessage(NotSupportedError, msg):
|
with self.assertRaisesMessage(NotSupportedError, msg):
|
||||||
Author.objects.aggregate(arrayagg=JSONArrayAgg("age", filter=Q(age__gt=35)))
|
Author.objects.aggregate(arrayagg=JSONArrayAgg("age", filter=Q(age__gt=35)))
|
||||||
|
Loading…
Reference in New Issue
Block a user