1
from __future__ import absolute_import
3
from django.db.models import Max
4
from django.test import TestCase, skipUnlessDBFeature
5
from django.test.utils import str_prefix
7
from .models import Tag, Celebrity, Fan, Staff, StaffTag
9
class DistinctOnTests(TestCase):
11
t1 = Tag.objects.create(name='t1')
12
t2 = Tag.objects.create(name='t2', parent=t1)
13
t3 = Tag.objects.create(name='t3', parent=t1)
14
t4 = Tag.objects.create(name='t4', parent=t3)
15
t5 = Tag.objects.create(name='t5', parent=t3)
17
p1_o1 = Staff.objects.create(id=1, name="p1", organisation="o1")
18
p2_o1 = Staff.objects.create(id=2, name="p2", organisation="o1")
19
p3_o1 = Staff.objects.create(id=3, name="p3", organisation="o1")
20
p1_o2 = Staff.objects.create(id=4, name="p1", organisation="o2")
21
p1_o1.coworkers.add(p2_o1, p3_o1)
22
StaffTag.objects.create(staff=p1_o1, tag=t1)
23
StaffTag.objects.create(staff=p1_o1, tag=t1)
25
celeb1 = Celebrity.objects.create(name="c1")
26
celeb2 = Celebrity.objects.create(name="c2")
28
self.fan1 = Fan.objects.create(fan_of=celeb1)
29
self.fan2 = Fan.objects.create(fan_of=celeb1)
30
self.fan3 = Fan.objects.create(fan_of=celeb2)
32
@skipUnlessDBFeature('can_distinct_on_fields')
33
def test_basic_distinct_on(self):
34
"""QuerySet.distinct('field', ...) works"""
35
# (qset, expected) tuples
38
Staff.objects.distinct().order_by('name'),
39
['<Staff: p1>', '<Staff: p1>', '<Staff: p2>', '<Staff: p3>'],
42
Staff.objects.distinct('name').order_by('name'),
43
['<Staff: p1>', '<Staff: p2>', '<Staff: p3>'],
46
Staff.objects.distinct('organisation').order_by('organisation', 'name'),
47
['<Staff: p1>', '<Staff: p1>'],
50
Staff.objects.distinct('name', 'organisation').order_by('name', 'organisation'),
51
['<Staff: p1>', '<Staff: p1>', '<Staff: p2>', '<Staff: p3>'],
54
Celebrity.objects.filter(fan__in=[self.fan1, self.fan2, self.fan3]).\
55
distinct('name').order_by('name'),
56
['<Celebrity: c1>', '<Celebrity: c2>'],
58
# Does combining querysets work?
60
(Celebrity.objects.filter(fan__in=[self.fan1, self.fan2]).\
61
distinct('name').order_by('name')
62
|Celebrity.objects.filter(fan__in=[self.fan3]).\
63
distinct('name').order_by('name')),
64
['<Celebrity: c1>', '<Celebrity: c2>'],
67
StaffTag.objects.distinct('staff','tag'),
68
['<StaffTag: t1 -> p1>'],
71
Tag.objects.order_by('parent__pk', 'pk').distinct('parent'),
72
['<Tag: t2>', '<Tag: t4>', '<Tag: t1>'],
75
StaffTag.objects.select_related('staff').distinct('staff__name').order_by('staff__name'),
76
['<StaffTag: t1 -> p1>'],
78
# Fetch the alphabetically first coworker for each worker
80
(Staff.objects.distinct('id').order_by('id', 'coworkers__name').
81
values_list('id', 'coworkers__name')),
82
[str_prefix("(1, %(_)s'p2')"), str_prefix("(2, %(_)s'p1')"),
83
str_prefix("(3, %(_)s'p1')"), "(4, None)"]
86
for qset, expected in qsets:
87
self.assertQuerysetEqual(qset, expected)
88
self.assertEqual(qset.count(), len(expected))
90
# Combining queries with different distinct_fields is not allowed.
91
base_qs = Celebrity.objects.all()
92
self.assertRaisesMessage(
94
"Cannot combine queries with different distinct fields.",
95
lambda: (base_qs.distinct('id') & base_qs.distinct('name'))
99
c1 = Celebrity.objects.distinct('greatest_fan__id', 'greatest_fan__fan_of')
100
self.assertIn('OUTER JOIN', str(c1.query))
101
c2 = c1.distinct('pk')
102
self.assertNotIn('OUTER JOIN', str(c2.query))
104
@skipUnlessDBFeature('can_distinct_on_fields')
105
def test_distinct_not_implemented_checks(self):
106
# distinct + annotate not allowed
107
with self.assertRaises(NotImplementedError):
108
Celebrity.objects.annotate(Max('id')).distinct('id')[0]
109
with self.assertRaises(NotImplementedError):
110
Celebrity.objects.distinct('id').annotate(Max('id'))[0]
112
# However this check is done only when the query executes, so you
113
# can use distinct() to remove the fields before execution.
114
Celebrity.objects.distinct('id').annotate(Max('id')).distinct()[0]
115
# distinct + aggregate not allowed
116
with self.assertRaises(NotImplementedError):
117
Celebrity.objects.distinct('id').aggregate(Max('id'))