Skip to content

Commit

Permalink
Added QuerySet.union() test with renames.
Browse files Browse the repository at this point in the history
  • Loading branch information
WaVEV authored and timgraham committed Sep 13, 2024
1 parent e486811 commit ab7e2e4
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion tests/queries/test_qs_combinators.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from django.test import TestCase, skipIfDBFeature, skipUnlessDBFeature
from django.test.utils import CaptureQueriesContext

from .models import Author, Celebrity, ExtraInfo, Number, ReservedName
from .models import Author, Celebrity, ExtraInfo, Number, Report, ReservedName


@skipUnlessDBFeature("supports_select_union")
Expand Down Expand Up @@ -123,6 +123,31 @@ def test_union_nested(self):
ordered=False,
)

def test_union_with_different_models(self):
expected_result = {
"Angel",
"Lionel",
"Emiliano",
"Demetrio",
"Daniel",
"Javier",
}
Celebrity.objects.create(name="Angel")
Celebrity.objects.create(name="Lionel")
Celebrity.objects.create(name="Emiliano")
Celebrity.objects.create(name="Demetrio")
Report.objects.create(name="Demetrio")
Report.objects.create(name="Daniel")
Report.objects.create(name="Javier")
qs1 = Celebrity.objects.values(alias=F("name"))
qs2 = Report.objects.values(alias_author=F("name"))
qs3 = qs1.union(qs2).values("name")
self.assertCountEqual((e["name"] for e in qs3), expected_result)
qs4 = qs1.union(qs2)
self.assertCountEqual((e["alias"] for e in qs4), expected_result)
qs5 = qs2.union(qs1)
self.assertCountEqual((e["alias_author"] for e in qs5), expected_result)

@skipUnlessDBFeature("supports_select_intersection")
def test_intersection_with_empty_qs(self):
qs1 = Number.objects.all()
Expand Down Expand Up @@ -474,6 +499,16 @@ def test_count_intersection(self):
qs2 = Number.objects.filter(num__lte=5)
self.assertEqual(qs1.intersection(qs2).count(), 1)

@skipUnlessDBFeature("supports_slicing_ordering_in_compound")
def test_count_union_with_select_related_projected(self):
e1 = ExtraInfo.objects.create(value=1, info="e1")
a1 = Author.objects.create(name="a1", num=1, extra=e1)
qs = Author.objects.select_related("extra").values("pk", "name", "extra__value")
self.assertEqual(len(qs.union(qs)), 1)
self.assertEqual(
qs.union(qs).first(), {"pk": a1.id, "name": "a1", "extra__value": 1}
)

def test_exists_union(self):
qs1 = Number.objects.filter(num__gte=5)
qs2 = Number.objects.filter(num__lte=5)
Expand Down

0 comments on commit ab7e2e4

Please sign in to comment.