mirror of
https://github.com/django/django.git
synced 2025-06-05 11:39:13 +00:00
Added subquery test cases
This commit is contained in:
parent
5d2b5e3a25
commit
40161746c0
@ -855,6 +855,24 @@ class BasicExpressionsTests(TestCase):
|
|||||||
).filter(ceo_manager=max_manager)
|
).filter(ceo_manager=max_manager)
|
||||||
self.assertEqual(qs.get(), self.gmbh)
|
self.assertEqual(qs.get(), self.gmbh)
|
||||||
|
|
||||||
|
def test_subquery_with_custom_template(self):
|
||||||
|
companies = Company.objects.annotate(
|
||||||
|
ceo_manager_count=Subquery(
|
||||||
|
Employee.objects.filter(
|
||||||
|
lastname=OuterRef("ceo__lastname"),
|
||||||
|
).values("manager"),
|
||||||
|
template="(SELECT count(*) FROM (%(subquery)s) _count)",
|
||||||
|
)
|
||||||
|
)
|
||||||
|
expected_results = [
|
||||||
|
{"name": "Example Inc.", "ceo_manager_count": 1},
|
||||||
|
{"name": "Foobar Ltd.", "ceo_manager_count": 1},
|
||||||
|
{"name": "Test GmbH", "ceo_manager_count": 1},
|
||||||
|
]
|
||||||
|
self.assertListEqual(
|
||||||
|
list(companies.values("name", "ceo_manager_count")), expected_results
|
||||||
|
)
|
||||||
|
|
||||||
def test_aggregate_subquery_annotation(self):
|
def test_aggregate_subquery_annotation(self):
|
||||||
with self.assertNumQueries(1) as ctx:
|
with self.assertNumQueries(1) as ctx:
|
||||||
aggregate = Company.objects.annotate(
|
aggregate = Company.objects.annotate(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user