2020-11-02 23:47:42 +08:00
|
|
|
from django.db import connection
|
2021-11-24 01:22:52 +08:00
|
|
|
from django.db.models import CharField, F, Max
|
2017-06-18 23:53:40 +08:00
|
|
|
from django.db.models.functions import Lower
|
2011-12-23 04:42:40 +08:00
|
|
|
from django.test import TestCase, skipUnlessDBFeature
|
2018-08-22 00:17:46 +08:00
|
|
|
from django.test.utils import register_lookup
|
2011-12-23 04:42:40 +08:00
|
|
|
|
2015-01-28 20:35:27 +08:00
|
|
|
from .models import Celebrity, Fan, Staff, StaffTag, Tag
|
2011-12-23 04:42:40 +08:00
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2013-09-13 02:32:23 +08:00
|
|
|
@skipUnlessDBFeature("can_distinct_on_fields")
|
2014-05-08 04:03:10 +08:00
|
|
|
@skipUnlessDBFeature("supports_nullable_unique_constraints")
|
2011-12-23 04:42:40 +08:00
|
|
|
class DistinctOnTests(TestCase):
|
2018-11-24 09:59:38 +08:00
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
|
|
|
cls.t1 = Tag.objects.create(name="t1")
|
|
|
|
cls.t2 = Tag.objects.create(name="t2", parent=cls.t1)
|
|
|
|
cls.t3 = Tag.objects.create(name="t3", parent=cls.t1)
|
|
|
|
cls.t4 = Tag.objects.create(name="t4", parent=cls.t3)
|
|
|
|
cls.t5 = Tag.objects.create(name="t5", parent=cls.t3)
|
2011-12-23 04:42:40 +08:00
|
|
|
|
2018-11-24 09:59:38 +08:00
|
|
|
cls.p1_o1 = Staff.objects.create(id=1, name="p1", organisation="o1")
|
|
|
|
cls.p2_o1 = Staff.objects.create(id=2, name="p2", organisation="o1")
|
|
|
|
cls.p3_o1 = Staff.objects.create(id=3, name="p3", organisation="o1")
|
|
|
|
cls.p1_o2 = Staff.objects.create(id=4, name="p1", organisation="o2")
|
|
|
|
cls.p1_o1.coworkers.add(cls.p2_o1, cls.p3_o1)
|
2020-10-19 00:29:52 +08:00
|
|
|
cls.st1 = StaffTag.objects.create(staff=cls.p1_o1, tag=cls.t1)
|
2018-11-24 09:59:38 +08:00
|
|
|
StaffTag.objects.create(staff=cls.p1_o1, tag=cls.t1)
|
2011-12-23 04:42:40 +08:00
|
|
|
|
2020-10-19 00:29:52 +08:00
|
|
|
cls.celeb1 = Celebrity.objects.create(name="c1")
|
|
|
|
cls.celeb2 = Celebrity.objects.create(name="c2")
|
2011-12-23 04:42:40 +08:00
|
|
|
|
2020-10-19 00:29:52 +08:00
|
|
|
cls.fan1 = Fan.objects.create(fan_of=cls.celeb1)
|
|
|
|
cls.fan2 = Fan.objects.create(fan_of=cls.celeb1)
|
|
|
|
cls.fan3 = Fan.objects.create(fan_of=cls.celeb2)
|
2011-12-23 04:42:40 +08:00
|
|
|
|
|
|
|
def test_basic_distinct_on(self):
|
|
|
|
"""QuerySet.distinct('field', ...) works"""
|
|
|
|
# (qset, expected) tuples
|
|
|
|
qsets = (
|
|
|
|
(
|
|
|
|
Staff.objects.distinct().order_by("name"),
|
2020-10-19 00:29:52 +08:00
|
|
|
[self.p1_o1, self.p1_o2, self.p2_o1, self.p3_o1],
|
2011-12-23 04:42:40 +08:00
|
|
|
),
|
|
|
|
(
|
|
|
|
Staff.objects.distinct("name").order_by("name"),
|
2020-10-19 00:29:52 +08:00
|
|
|
[self.p1_o1, self.p2_o1, self.p3_o1],
|
2011-12-23 04:42:40 +08:00
|
|
|
),
|
|
|
|
(
|
|
|
|
Staff.objects.distinct("organisation").order_by("organisation", "name"),
|
2020-10-19 00:29:52 +08:00
|
|
|
[self.p1_o1, self.p1_o2],
|
2011-12-23 04:42:40 +08:00
|
|
|
),
|
|
|
|
(
|
|
|
|
Staff.objects.distinct("name", "organisation").order_by(
|
|
|
|
"name", "organisation"
|
|
|
|
),
|
2020-10-19 00:29:52 +08:00
|
|
|
[self.p1_o1, self.p1_o2, self.p2_o1, self.p3_o1],
|
2011-12-23 04:42:40 +08:00
|
|
|
),
|
|
|
|
(
|
2013-10-20 07:33:10 +08:00
|
|
|
Celebrity.objects.filter(fan__in=[self.fan1, self.fan2, self.fan3])
|
|
|
|
.distinct("name")
|
|
|
|
.order_by("name"),
|
2020-10-19 00:29:52 +08:00
|
|
|
[self.celeb1, self.celeb2],
|
2011-12-23 04:42:40 +08:00
|
|
|
),
|
|
|
|
# Does combining querysets work?
|
|
|
|
(
|
2013-10-18 08:24:41 +08:00
|
|
|
(
|
|
|
|
Celebrity.objects.filter(fan__in=[self.fan1, self.fan2])
|
2013-10-23 18:09:29 +08:00
|
|
|
.distinct("name")
|
|
|
|
.order_by("name")
|
|
|
|
| Celebrity.objects.filter(fan__in=[self.fan3])
|
2011-12-23 04:42:40 +08:00
|
|
|
.distinct("name")
|
|
|
|
.order_by("name")
|
|
|
|
),
|
2020-10-19 00:29:52 +08:00
|
|
|
[self.celeb1, self.celeb2],
|
2011-12-23 04:42:40 +08:00
|
|
|
),
|
2020-10-19 00:29:52 +08:00
|
|
|
(StaffTag.objects.distinct("staff", "tag"), [self.st1]),
|
2011-12-23 04:42:40 +08:00
|
|
|
(
|
|
|
|
Tag.objects.order_by("parent__pk", "pk").distinct("parent"),
|
2020-10-19 00:29:52 +08:00
|
|
|
[self.t2, self.t4, self.t1]
|
2020-11-02 23:47:42 +08:00
|
|
|
if connection.features.nulls_order_largest
|
2020-10-19 00:29:52 +08:00
|
|
|
else [self.t1, self.t2, self.t4],
|
2011-12-23 04:42:40 +08:00
|
|
|
),
|
|
|
|
(
|
|
|
|
StaffTag.objects.select_related("staff")
|
|
|
|
.distinct("staff__name")
|
|
|
|
.order_by("staff__name"),
|
2020-10-19 00:29:52 +08:00
|
|
|
[self.st1],
|
2011-12-23 04:42:40 +08:00
|
|
|
),
|
|
|
|
# Fetch the alphabetically first coworker for each worker
|
|
|
|
(
|
|
|
|
(
|
|
|
|
Staff.objects.distinct("id")
|
|
|
|
.order_by("id", "coworkers__name")
|
2013-11-03 12:36:09 +08:00
|
|
|
.values_list("id", "coworkers__name")
|
|
|
|
),
|
2020-10-19 00:29:52 +08:00
|
|
|
[(1, "p2"), (2, "p1"), (3, "p1"), (4, None)],
|
2011-12-23 04:42:40 +08:00
|
|
|
),
|
|
|
|
)
|
|
|
|
for qset, expected in qsets:
|
2020-10-19 00:29:52 +08:00
|
|
|
self.assertSequenceEqual(qset, expected)
|
2011-12-23 04:42:40 +08:00
|
|
|
self.assertEqual(qset.count(), len(expected))
|
|
|
|
|
2021-03-24 13:45:08 +08:00
|
|
|
# Combining queries with non-unique query is not allowed.
|
2011-12-23 04:42:40 +08:00
|
|
|
base_qs = Celebrity.objects.all()
|
2021-03-24 13:45:08 +08:00
|
|
|
msg = "Cannot combine a unique query with a non-unique query."
|
|
|
|
with self.assertRaisesMessage(TypeError, msg):
|
|
|
|
base_qs.distinct("id") & base_qs
|
|
|
|
# Combining queries with different distinct_fields is not allowed.
|
|
|
|
msg = "Cannot combine queries with different distinct fields."
|
|
|
|
with self.assertRaisesMessage(TypeError, msg):
|
2016-01-04 16:50:08 +08:00
|
|
|
base_qs.distinct("id") & base_qs.distinct("name")
|
2011-12-23 04:42:40 +08:00
|
|
|
|
|
|
|
# Test join unreffing
|
|
|
|
c1 = Celebrity.objects.distinct("greatest_fan__id", "greatest_fan__fan_of")
|
|
|
|
self.assertIn("OUTER JOIN", str(c1.query))
|
|
|
|
c2 = c1.distinct("pk")
|
|
|
|
self.assertNotIn("OUTER JOIN", str(c2.query))
|
|
|
|
|
2021-03-10 16:16:28 +08:00
|
|
|
def test_sliced_queryset(self):
|
|
|
|
msg = "Cannot create distinct fields once a slice has been taken."
|
|
|
|
with self.assertRaisesMessage(TypeError, msg):
|
|
|
|
Staff.objects.all()[0:5].distinct("name")
|
|
|
|
|
2017-06-18 23:53:40 +08:00
|
|
|
def test_transform(self):
|
|
|
|
new_name = self.t1.name.upper()
|
|
|
|
self.assertNotEqual(self.t1.name, new_name)
|
|
|
|
Tag.objects.create(name=new_name)
|
2018-08-22 00:17:46 +08:00
|
|
|
with register_lookup(CharField, Lower):
|
2017-06-18 23:53:40 +08:00
|
|
|
self.assertCountEqual(
|
|
|
|
Tag.objects.order_by().distinct("name__lower"),
|
|
|
|
[self.t1, self.t2, self.t3, self.t4, self.t5],
|
|
|
|
)
|
|
|
|
|
2011-12-23 04:42:40 +08:00
|
|
|
def test_distinct_not_implemented_checks(self):
|
|
|
|
# distinct + annotate not allowed
|
2017-05-29 03:37:21 +08:00
|
|
|
msg = "annotate() + distinct(fields) is not implemented."
|
|
|
|
with self.assertRaisesMessage(NotImplementedError, msg):
|
2011-12-23 04:42:40 +08:00
|
|
|
Celebrity.objects.annotate(Max("id")).distinct("id")[0]
|
2017-05-29 03:37:21 +08:00
|
|
|
with self.assertRaisesMessage(NotImplementedError, msg):
|
2011-12-23 04:42:40 +08:00
|
|
|
Celebrity.objects.distinct("id").annotate(Max("id"))[0]
|
|
|
|
|
|
|
|
# However this check is done only when the query executes, so you
|
|
|
|
# can use distinct() to remove the fields before execution.
|
|
|
|
Celebrity.objects.distinct("id").annotate(Max("id")).distinct()[0]
|
|
|
|
# distinct + aggregate not allowed
|
2017-05-29 03:37:21 +08:00
|
|
|
msg = "aggregate() + distinct(fields) not implemented."
|
|
|
|
with self.assertRaisesMessage(NotImplementedError, msg):
|
2011-12-23 04:42:40 +08:00
|
|
|
Celebrity.objects.distinct("id").aggregate(Max("id"))
|
2013-10-11 05:12:10 +08:00
|
|
|
|
|
|
|
def test_distinct_on_in_ordered_subquery(self):
|
|
|
|
qs = Staff.objects.distinct("name").order_by("name", "id")
|
|
|
|
qs = Staff.objects.filter(pk__in=qs).order_by("name")
|
2016-09-10 17:36:27 +08:00
|
|
|
self.assertSequenceEqual(qs, [self.p1_o1, self.p2_o1, self.p3_o1])
|
2013-10-11 05:12:10 +08:00
|
|
|
qs = Staff.objects.distinct("name").order_by("name", "-id")
|
|
|
|
qs = Staff.objects.filter(pk__in=qs).order_by("name")
|
2016-09-10 17:36:27 +08:00
|
|
|
self.assertSequenceEqual(qs, [self.p1_o2, self.p2_o1, self.p3_o1])
|
2015-07-10 01:52:32 +08:00
|
|
|
|
|
|
|
def test_distinct_on_get_ordering_preserved(self):
|
|
|
|
"""
|
|
|
|
Ordering shouldn't be cleared when distinct on fields are specified.
|
|
|
|
refs #25081
|
|
|
|
"""
|
|
|
|
staff = (
|
|
|
|
Staff.objects.distinct("name")
|
|
|
|
.order_by("name", "-organisation")
|
|
|
|
.get(name="p1")
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2015-07-10 01:52:32 +08:00
|
|
|
self.assertEqual(staff.organisation, "o2")
|
2021-11-24 01:22:52 +08:00
|
|
|
|
|
|
|
def test_distinct_on_mixed_case_annotation(self):
|
|
|
|
qs = (
|
|
|
|
Staff.objects.annotate(
|
|
|
|
nAmEAlIaS=F("name"),
|
|
|
|
)
|
|
|
|
.distinct("nAmEAlIaS")
|
|
|
|
.order_by("nAmEAlIaS")
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2021-11-24 01:22:52 +08:00
|
|
|
self.assertSequenceEqual(qs, [self.p1_o1, self.p2_o1, self.p3_o1])
|