2017-06-18 15:53:40 +00:00
|
|
|
from django.db.models import CharField, Max
|
|
|
|
from django.db.models.functions import Lower
|
2011-12-22 20:42:40 +00:00
|
|
|
from django.test import TestCase, skipUnlessDBFeature
|
|
|
|
|
2015-01-28 12:35:27 +00:00
|
|
|
from .models import Celebrity, Fan, Staff, StaffTag, Tag
|
2011-12-22 20:42:40 +00:00
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2013-09-12 18:32:23 +00:00
|
|
|
@skipUnlessDBFeature('can_distinct_on_fields')
|
2014-05-07 20:03:10 +00:00
|
|
|
@skipUnlessDBFeature('supports_nullable_unique_constraints')
|
2011-12-22 20:42:40 +00:00
|
|
|
class DistinctOnTests(TestCase):
|
|
|
|
def setUp(self):
|
2017-06-18 15:53:40 +00:00
|
|
|
self.t1 = Tag.objects.create(name='t1')
|
|
|
|
self.t2 = Tag.objects.create(name='t2', parent=self.t1)
|
|
|
|
self.t3 = Tag.objects.create(name='t3', parent=self.t1)
|
|
|
|
self.t4 = Tag.objects.create(name='t4', parent=self.t3)
|
|
|
|
self.t5 = Tag.objects.create(name='t5', parent=self.t3)
|
2011-12-22 20:42:40 +00:00
|
|
|
|
2013-10-10 21:12:10 +00:00
|
|
|
self.p1_o1 = Staff.objects.create(id=1, name="p1", organisation="o1")
|
|
|
|
self.p2_o1 = Staff.objects.create(id=2, name="p2", organisation="o1")
|
|
|
|
self.p3_o1 = Staff.objects.create(id=3, name="p3", organisation="o1")
|
|
|
|
self.p1_o2 = Staff.objects.create(id=4, name="p1", organisation="o2")
|
|
|
|
self.p1_o1.coworkers.add(self.p2_o1, self.p3_o1)
|
2017-06-18 15:53:40 +00:00
|
|
|
StaffTag.objects.create(staff=self.p1_o1, tag=self.t1)
|
|
|
|
StaffTag.objects.create(staff=self.p1_o1, tag=self.t1)
|
2011-12-22 20:42:40 +00:00
|
|
|
|
|
|
|
celeb1 = Celebrity.objects.create(name="c1")
|
|
|
|
celeb2 = Celebrity.objects.create(name="c2")
|
|
|
|
|
|
|
|
self.fan1 = Fan.objects.create(fan_of=celeb1)
|
|
|
|
self.fan2 = Fan.objects.create(fan_of=celeb1)
|
|
|
|
self.fan3 = Fan.objects.create(fan_of=celeb2)
|
|
|
|
|
|
|
|
def test_basic_distinct_on(self):
|
|
|
|
"""QuerySet.distinct('field', ...) works"""
|
|
|
|
# (qset, expected) tuples
|
|
|
|
qsets = (
|
|
|
|
(
|
|
|
|
Staff.objects.distinct().order_by('name'),
|
|
|
|
['<Staff: p1>', '<Staff: p1>', '<Staff: p2>', '<Staff: p3>'],
|
|
|
|
),
|
|
|
|
(
|
|
|
|
Staff.objects.distinct('name').order_by('name'),
|
|
|
|
['<Staff: p1>', '<Staff: p2>', '<Staff: p3>'],
|
|
|
|
),
|
|
|
|
(
|
|
|
|
Staff.objects.distinct('organisation').order_by('organisation', 'name'),
|
|
|
|
['<Staff: p1>', '<Staff: p1>'],
|
|
|
|
),
|
|
|
|
(
|
|
|
|
Staff.objects.distinct('name', 'organisation').order_by('name', 'organisation'),
|
|
|
|
['<Staff: p1>', '<Staff: p1>', '<Staff: p2>', '<Staff: p3>'],
|
|
|
|
),
|
|
|
|
(
|
2013-10-19 23:33:10 +00:00
|
|
|
Celebrity.objects.filter(fan__in=[self.fan1, self.fan2, self.fan3]).distinct('name').order_by('name'),
|
2011-12-22 20:42:40 +00:00
|
|
|
['<Celebrity: c1>', '<Celebrity: c2>'],
|
|
|
|
),
|
|
|
|
# Does combining querysets work?
|
|
|
|
(
|
2013-10-18 00:24:41 +00:00
|
|
|
(Celebrity.objects.filter(fan__in=[self.fan1, self.fan2]).
|
2013-10-23 10:09:29 +00:00
|
|
|
distinct('name').order_by('name') |
|
|
|
|
Celebrity.objects.filter(fan__in=[self.fan3]).
|
2011-12-22 20:42:40 +00:00
|
|
|
distinct('name').order_by('name')),
|
|
|
|
['<Celebrity: c1>', '<Celebrity: c2>'],
|
|
|
|
),
|
|
|
|
(
|
2013-10-26 19:15:03 +00:00
|
|
|
StaffTag.objects.distinct('staff', 'tag'),
|
2011-12-22 20:42:40 +00:00
|
|
|
['<StaffTag: t1 -> p1>'],
|
|
|
|
),
|
|
|
|
(
|
|
|
|
Tag.objects.order_by('parent__pk', 'pk').distinct('parent'),
|
|
|
|
['<Tag: t2>', '<Tag: t4>', '<Tag: t1>'],
|
|
|
|
),
|
|
|
|
(
|
|
|
|
StaffTag.objects.select_related('staff').distinct('staff__name').order_by('staff__name'),
|
|
|
|
['<StaffTag: t1 -> p1>'],
|
|
|
|
),
|
|
|
|
# Fetch the alphabetically first coworker for each worker
|
|
|
|
(
|
|
|
|
(Staff.objects.distinct('id').order_by('id', 'coworkers__name').
|
2013-11-03 04:36:09 +00:00
|
|
|
values_list('id', 'coworkers__name')),
|
2017-01-20 14:24:05 +00:00
|
|
|
["(1, 'p2')", "(2, 'p1')", "(3, 'p1')", "(4, None)"]
|
2011-12-22 20:42:40 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
for qset, expected in qsets:
|
|
|
|
self.assertQuerysetEqual(qset, expected)
|
|
|
|
self.assertEqual(qset.count(), len(expected))
|
|
|
|
|
|
|
|
# Combining queries with different distinct_fields is not allowed.
|
|
|
|
base_qs = Celebrity.objects.all()
|
2016-01-04 08:50:08 +00:00
|
|
|
with self.assertRaisesMessage(AssertionError, "Cannot combine queries with different distinct fields."):
|
|
|
|
base_qs.distinct('id') & base_qs.distinct('name')
|
2011-12-22 20:42:40 +00: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))
|
|
|
|
|
2017-06-18 15:53:40 +00:00
|
|
|
def test_transform(self):
|
|
|
|
new_name = self.t1.name.upper()
|
|
|
|
self.assertNotEqual(self.t1.name, new_name)
|
|
|
|
Tag.objects.create(name=new_name)
|
|
|
|
CharField.register_lookup(Lower)
|
|
|
|
try:
|
|
|
|
self.assertCountEqual(
|
|
|
|
Tag.objects.order_by().distinct('name__lower'),
|
|
|
|
[self.t1, self.t2, self.t3, self.t4, self.t5],
|
|
|
|
)
|
|
|
|
finally:
|
|
|
|
CharField._unregister_lookup(Lower)
|
|
|
|
|
2011-12-22 20:42:40 +00:00
|
|
|
def test_distinct_not_implemented_checks(self):
|
|
|
|
# distinct + annotate not allowed
|
2017-05-28 19:37:21 +00:00
|
|
|
msg = 'annotate() + distinct(fields) is not implemented.'
|
|
|
|
with self.assertRaisesMessage(NotImplementedError, msg):
|
2011-12-22 20:42:40 +00:00
|
|
|
Celebrity.objects.annotate(Max('id')).distinct('id')[0]
|
2017-05-28 19:37:21 +00:00
|
|
|
with self.assertRaisesMessage(NotImplementedError, msg):
|
2011-12-22 20:42:40 +00: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-28 19:37:21 +00:00
|
|
|
msg = 'aggregate() + distinct(fields) not implemented.'
|
|
|
|
with self.assertRaisesMessage(NotImplementedError, msg):
|
2011-12-22 20:42:40 +00:00
|
|
|
Celebrity.objects.distinct('id').aggregate(Max('id'))
|
2013-10-10 21:12:10 +00: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 09:36:27 +00:00
|
|
|
self.assertSequenceEqual(qs, [self.p1_o1, self.p2_o1, self.p3_o1])
|
2013-10-10 21:12:10 +00:00
|
|
|
qs = Staff.objects.distinct('name').order_by('name', '-id')
|
|
|
|
qs = Staff.objects.filter(pk__in=qs).order_by('name')
|
2016-09-10 09:36:27 +00:00
|
|
|
self.assertSequenceEqual(qs, [self.p1_o2, self.p2_o1, self.p3_o1])
|
2015-07-09 17:52:32 +00: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')
|
|
|
|
self.assertEqual(staff.organisation, 'o2')
|