Skip to content

Conversation

@WaVEV
Copy link
Collaborator

@WaVEV WaVEV commented Oct 21, 2025

This increases the test coverage for query refactor (#396).

@WaVEV WaVEV force-pushed the direct-query-lookup-tests branch 4 times, most recently from 9bf4025 to 804b0c0 Compare October 27, 2025 21:19
Comment on lines 103 to 149
def test_union_simple_conditions(self):
with self.assertNumQueries(1) as ctx:
list(Book.objects.filter(title="star wars").union(Book.objects.filter(isbn__in="1234")))
self.assertAggregateQuery(
ctx.captured_queries[0]["sql"],
"lookup__book",
[
{"$match": {"title": "star wars"}},
{"$project": {"_id": 1, "title": 1, "isbn": 1}},
{
"$unionWith": {
"coll": "lookup__book",
"pipeline": [
{"$match": {"isbn": {"$in": ("1", "2", "3", "4")}}},
{"$project": {"_id": 1, "title": 1, "isbn": 1}},
],
}
},
{"$group": {"_id": {"_id": "$_id", "title": "$title", "isbn": "$isbn"}}},
{"$addFields": {"_id": "$_id._id", "title": "$_id.title", "isbn": "$_id.isbn"}},
],
)

def test_union_all_simple_conditions(self):
with self.assertNumQueries(1) as ctx:
list(
Book.objects.filter(title="star wars").union(
Book.objects.filter(isbn="1234"), all=True
)
)
self.assertAggregateQuery(
ctx.captured_queries[0]["sql"],
"lookup__book",
[
{"$match": {"title": "star wars"}},
{"$project": {"_id": 1, "title": 1, "isbn": 1}},
{
"$unionWith": {
"coll": "lookup__book",
"pipeline": [
{"$match": {"isbn": "1234"}},
{"$project": {"_id": 1, "title": 1, "isbn": 1}},
],
}
},
],
)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved these to queries_/test_qs_combinators.py to match the location of Django's tests for union(). Similarly, test_subquery_filter_constant may be more complicated than most lookup tests.

@WaVEV WaVEV force-pushed the direct-query-lookup-tests branch from 804b0c0 to 6ee279f Compare October 29, 2025 23:41
@WaVEV WaVEV force-pushed the direct-query-lookup-tests branch from 6ee279f to 1c6e93d Compare October 29, 2025 23:43
@WaVEV WaVEV marked this pull request as ready for review October 31, 2025 22:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants