1
0
mirror of https://github.com/django/django.git synced 2024-12-23 01:25:58 +00:00

Added subquery test cases

This commit is contained in:
priyank.panchal 2024-08-10 00:22:02 +05:30
parent 5d2b5e3a25
commit 40161746c0

View File

@ -855,6 +855,24 @@ class BasicExpressionsTests(TestCase):
).filter(ceo_manager=max_manager)
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):
with self.assertNumQueries(1) as ctx:
aggregate = Company.objects.annotate(