Repository: django/django
File: tests/queries/test_qs_combinators.py (Python)

Code before:
            ordered=False,
        )

    def test_count_union(self):
        qs1 = Number.objects.filter(num__lte=1).values("num")
        qs2 = Number.objects.filter(num__gte=2, num__lte=3).values("num")
        self.assertEqual(qs1.union(qs2).count(), 4)

    def test_count_union_empty_result(self):
        qs = Number.objects.filter(pk__in=[])
        self.assertEqual(qs.union(qs).count(), 0)

    def test_count_union_with_select_related(self):
        e1 = ExtraInfo.objects.create(value=1, info="e1")
        Author.objects.create(name="a1", num=1, extra=e1)
        qs = Author.objects.select_related("extra").order_by()
        self.assertEqual(qs.union(qs).count(), 1)

    @skipUnlessDBFeature("supports_slicing_ordering_in_compound")
    def test_count_union_with_select_related_in_values(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.assertCountEqual(
            qs.union(qs), {"pk": a1.id, "name": "a1", "extra__value": 1}
        )

    @skipUnlessDBFeature("supports_select_difference")
    def test_count_difference(self):
        qs1 = Number.objects.filter(num__lt=10)
        qs2 = Number.objects.filter(num__lt=9)
        self.assertEqual(qs1.difference(qs2).count(), 1)

    @skipUnlessDBFeature("supports_select_intersection")
    def test_count_intersection(self):
        qs1 = Number.objects.filter(num__gte=5)
        qs2 = Number.objects.filter(num__lte=5)
        self.assertEqual(qs1.intersection(qs2).count(), 1)

    def test_exists_union(self):
        qs1 = Number.objects.filter(num__gte=5)
        qs2 = Number.objects.filter(num__lte=5)
        with CaptureQueriesContext(connection) as context:
            self.assertIs(qs1.union(qs2).exists(), True)
        captured_queries = context.captured_queries
        self.assertEqual(len(captured_queries), 1)
        captured_sql = captured_queries[0]["sql"]
        self.assertNotIn(
            connection.ops.quote_name(Number._meta.pk.column),
            captured_sql,
        )

Review comment (bug):
The qs has one element, not three:
```py
======================================================================
FAIL: test_count_union_with_select_related_in_values (queries.test_qs_combinators.QuerySetSetOperationTests.test_count_union_with_select_related_in_values)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/django/source/tests/queries/test_qs_combinators.py", line 615, in test_count_union_with_select_related_in_values
    self.assertCountEqual(
    ~~~~~~~~~~~~~~~~~~~~~^
        qs.union(qs), {"pk": a1.id, "name": "a1", "extra__value": 1}
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
AssertionError: Element counts were not equal:
First has 1, Second has 0:  {'pk': 2, 'name': 'a1', 'extra__value': 1}
First has 0, Second has 1:  'pk'
First has 0, Second has 1:  'name'
First has 0, Second has 1:  'extra__value'

----------------------------------------------------------------------
Ran 71 tests in 0.218s
```

Code after:
            ordered=False,
        )

    def test_count_union(self):
        qs1 = Number.objects.filter(num__lte=1).values("num")
        qs2 = Number.objects.filter(num__gte=2, num__lte=3).values("num")
        self.assertEqual(qs1.union(qs2).count(), 4)

    def test_count_union_empty_result(self):
        qs = Number.objects.filter(pk__in=[])
        self.assertEqual(qs.union(qs).count(), 0)

    def test_count_union_with_select_related(self):
        e1 = ExtraInfo.objects.create(value=1, info="e1")
        Author.objects.create(name="a1", num=1, extra=e1)
        qs = Author.objects.select_related("extra").order_by()
        self.assertEqual(qs.union(qs).count(), 1)

    @skipUnlessDBFeature("supports_slicing_ordering_in_compound")
    def test_count_union_with_select_related_in_values(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.assertCountEqual(
            qs.union(qs), [{"pk": a1.id, "name": "a1", "extra__value": 1}]
        )

    @skipUnlessDBFeature("supports_select_difference")
    def test_count_difference(self):
        qs1 = Number.objects.filter(num__lt=10)
        qs2 = Number.objects.filter(num__lt=9)
        self.assertEqual(qs1.difference(qs2).count(), 1)

    @skipUnlessDBFeature("supports_select_intersection")
    def test_count_intersection(self):
        qs1 = Number.objects.filter(num__gte=5)
        qs2 = Number.objects.filter(num__lte=5)
        self.assertEqual(qs1.intersection(qs2).count(), 1)

    def test_exists_union(self):
        qs1 = Number.objects.filter(num__gte=5)
        qs2 = Number.objects.filter(num__lte=5)
        with CaptureQueriesContext(connection) as context:
            self.assertIs(qs1.union(qs2).exists(), True)
        captured_queries = context.captured_queries
        self.assertEqual(len(captured_queries), 1)
        captured_sql = captured_queries[0]["sql"]
        self.assertNotIn(
            connection.ops.quote_name(Number._meta.pk.column),
            captured_sql,
        )