mirror of
https://github.com/django/django.git
synced 2025-10-26 07:06:08 +00:00
Fixed #31235 -- Made assertQuerysetEqual() compare querysets directly.
This also replaces assertQuerysetEqual() to assertSequenceEqual()/assertCountEqual() where appropriate. Co-authored-by: Peter Inglesby <peter.inglesby@gmail.com> Co-authored-by: Mariusz Felisiak <felisiak.mariusz@gmail.com>
This commit is contained in:
committed by
Mariusz Felisiak
parent
13b6fff117
commit
3f7b327562
@@ -23,15 +23,15 @@ class DistinctOnTests(TestCase):
|
||||
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)
|
||||
StaffTag.objects.create(staff=cls.p1_o1, tag=cls.t1)
|
||||
cls.st1 = StaffTag.objects.create(staff=cls.p1_o1, tag=cls.t1)
|
||||
StaffTag.objects.create(staff=cls.p1_o1, tag=cls.t1)
|
||||
|
||||
celeb1 = Celebrity.objects.create(name="c1")
|
||||
celeb2 = Celebrity.objects.create(name="c2")
|
||||
cls.celeb1 = Celebrity.objects.create(name="c1")
|
||||
cls.celeb2 = Celebrity.objects.create(name="c2")
|
||||
|
||||
cls.fan1 = Fan.objects.create(fan_of=celeb1)
|
||||
cls.fan2 = Fan.objects.create(fan_of=celeb1)
|
||||
cls.fan3 = Fan.objects.create(fan_of=celeb2)
|
||||
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)
|
||||
|
||||
def test_basic_distinct_on(self):
|
||||
"""QuerySet.distinct('field', ...) works"""
|
||||
@@ -39,23 +39,23 @@ class DistinctOnTests(TestCase):
|
||||
qsets = (
|
||||
(
|
||||
Staff.objects.distinct().order_by('name'),
|
||||
['<Staff: p1>', '<Staff: p1>', '<Staff: p2>', '<Staff: p3>'],
|
||||
[self.p1_o1, self.p1_o2, self.p2_o1, self.p3_o1],
|
||||
),
|
||||
(
|
||||
Staff.objects.distinct('name').order_by('name'),
|
||||
['<Staff: p1>', '<Staff: p2>', '<Staff: p3>'],
|
||||
[self.p1_o1, self.p2_o1, self.p3_o1],
|
||||
),
|
||||
(
|
||||
Staff.objects.distinct('organisation').order_by('organisation', 'name'),
|
||||
['<Staff: p1>', '<Staff: p1>'],
|
||||
[self.p1_o1, self.p1_o2],
|
||||
),
|
||||
(
|
||||
Staff.objects.distinct('name', 'organisation').order_by('name', 'organisation'),
|
||||
['<Staff: p1>', '<Staff: p1>', '<Staff: p2>', '<Staff: p3>'],
|
||||
[self.p1_o1, self.p1_o2, self.p2_o1, self.p3_o1],
|
||||
),
|
||||
(
|
||||
Celebrity.objects.filter(fan__in=[self.fan1, self.fan2, self.fan3]).distinct('name').order_by('name'),
|
||||
['<Celebrity: c1>', '<Celebrity: c2>'],
|
||||
[self.celeb1, self.celeb2],
|
||||
),
|
||||
# Does combining querysets work?
|
||||
(
|
||||
@@ -63,31 +63,28 @@ class DistinctOnTests(TestCase):
|
||||
distinct('name').order_by('name') |
|
||||
Celebrity.objects.filter(fan__in=[self.fan3]).
|
||||
distinct('name').order_by('name')),
|
||||
['<Celebrity: c1>', '<Celebrity: c2>'],
|
||||
),
|
||||
(
|
||||
StaffTag.objects.distinct('staff', 'tag'),
|
||||
['<StaffTag: t1 -> p1>'],
|
||||
[self.celeb1, self.celeb2],
|
||||
),
|
||||
(StaffTag.objects.distinct('staff', 'tag'), [self.st1]),
|
||||
(
|
||||
Tag.objects.order_by('parent__pk', 'pk').distinct('parent'),
|
||||
['<Tag: t2>', '<Tag: t4>', '<Tag: t1>']
|
||||
[self.t2, self.t4, self.t1]
|
||||
if connection.features.nulls_order_largest
|
||||
else ['<Tag: t1>', '<Tag: t2>', '<Tag: t4>'],
|
||||
else [self.t1, self.t2, self.t4],
|
||||
),
|
||||
(
|
||||
StaffTag.objects.select_related('staff').distinct('staff__name').order_by('staff__name'),
|
||||
['<StaffTag: t1 -> p1>'],
|
||||
[self.st1],
|
||||
),
|
||||
# Fetch the alphabetically first coworker for each worker
|
||||
(
|
||||
(Staff.objects.distinct('id').order_by('id', 'coworkers__name').
|
||||
values_list('id', 'coworkers__name')),
|
||||
["(1, 'p2')", "(2, 'p1')", "(3, 'p1')", "(4, None)"]
|
||||
[(1, 'p2'), (2, 'p1'), (3, 'p1'), (4, None)],
|
||||
),
|
||||
)
|
||||
for qset, expected in qsets:
|
||||
self.assertQuerysetEqual(qset, expected)
|
||||
self.assertSequenceEqual(qset, expected)
|
||||
self.assertEqual(qset.count(), len(expected))
|
||||
|
||||
# Combining queries with different distinct_fields is not allowed.
|
||||
|
||||
Reference in New Issue
Block a user