Refs #31614 -- Added test for aliases ordering on combined querysets.

This commit is contained in:
Laurent Tramoy 2020-05-26 08:58:44 +02:00 committed by Mariusz Felisiak
parent 50f71ccb36
commit 51ad767d0b
1 changed files with 11 additions and 0 deletions

View File

@ -109,6 +109,15 @@ class QuerySetSetOperationTests(TestCase):
qs2 = Number.objects.filter(num__gte=2, num__lte=3) qs2 = Number.objects.filter(num__gte=2, num__lte=3)
self.assertNumbersEqual(qs1.union(qs2).order_by('-num'), [3, 2, 1, 0]) self.assertNumbersEqual(qs1.union(qs2).order_by('-num'), [3, 2, 1, 0])
def test_ordering_by_alias(self):
qs1 = Number.objects.filter(num__lte=1).values(alias=F('num'))
qs2 = Number.objects.filter(num__gte=2, num__lte=3).values(alias=F('num'))
self.assertQuerysetEqual(
qs1.union(qs2).order_by('-alias'),
[3, 2, 1, 0],
operator.itemgetter('alias'),
)
def test_ordering_by_f_expression(self): def test_ordering_by_f_expression(self):
qs1 = Number.objects.filter(num__lte=1) qs1 = Number.objects.filter(num__lte=1)
qs2 = Number.objects.filter(num__gte=2, num__lte=3) qs2 = Number.objects.filter(num__gte=2, num__lte=3)
@ -242,6 +251,8 @@ class QuerySetSetOperationTests(TestCase):
# 'num' got realiased to num2 # 'num' got realiased to num2
with self.assertRaisesMessage(DatabaseError, msg): with self.assertRaisesMessage(DatabaseError, msg):
list(qs1.union(qs2).order_by('num')) list(qs1.union(qs2).order_by('num'))
with self.assertRaisesMessage(DatabaseError, msg):
list(qs1.union(qs2).order_by(F('num')))
# switched order, now 'exists' again: # switched order, now 'exists' again:
list(qs2.union(qs1).order_by('num')) list(qs2.union(qs1).order_by('num'))