summaryrefslogtreecommitdiff
path: root/tests/distinct_on_fields
diff options
context:
space:
mode:
authorHasan Ramezani <hasan.r67@gmail.com>2020-10-18 18:29:52 +0200
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2020-11-06 09:24:50 +0100
commit3f7b3275627385f8f7531fca01cdda50d4ec6b6e (patch)
treefb082d40e73f6c877911eab92229ac21cdfaa5bc /tests/distinct_on_fields
parent13b6fff11703a694e155b84d41d02822bbc0aaa0 (diff)
downloaddjango-3f7b3275627385f8f7531fca01cdda50d4ec6b6e.tar.gz
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>
Diffstat (limited to 'tests/distinct_on_fields')
-rw-r--r--tests/distinct_on_fields/tests.py39
1 files changed, 18 insertions, 21 deletions
diff --git a/tests/distinct_on_fields/tests.py b/tests/distinct_on_fields/tests.py
index e19c3c1b65..34d4509cec 100644
--- a/tests/distinct_on_fields/tests.py
+++ b/tests/distinct_on_fields/tests.py
@@ -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.