Refs #23622 -- Added tests to ensure ordering is retained for distinct on fields subqueries.

The ticket was already fixed by
b68212f539.

Thanks to Beauhurst for commissioning the work on this ticket.
This commit is contained in:
Anssi Kääriäinen 2015-07-17 09:26:43 +03:00 committed by Tim Graham
parent 6a9a9e50f2
commit baa732ac9f
1 changed files with 68 additions and 0 deletions

View File

@ -3732,3 +3732,71 @@ class TestTicket24605(TestCase):
).order_by('pk'),
[i1, i2, i3], lambda x: x
)
class Ticket23622Tests(TestCase):
@skipUnlessDBFeature('can_distinct_on_fields')
def test_ticket_23622(self):
"""
Make sure __pk__in and __in work the same for related fields when
using a distinct on subquery.
"""
a1 = Ticket23605A.objects.create()
a2 = Ticket23605A.objects.create()
c1 = Ticket23605C.objects.create(field_c0=0.0)
Ticket23605B.objects.create(
modela_fk=a1, field_b0=123,
field_b1=datetime.date(2013, 1, 6),
modelc_fk=c1,
)
Ticket23605B.objects.create(
modela_fk=a1, field_b0=23,
field_b1=datetime.date(2011, 6, 6),
modelc_fk=c1,
)
Ticket23605B.objects.create(
modela_fk=a1, field_b0=234,
field_b1=datetime.date(2011, 9, 2),
modelc_fk=c1,
)
Ticket23605B.objects.create(
modela_fk=a1, field_b0=12,
field_b1=datetime.date(2012, 9, 15),
modelc_fk=c1,
)
Ticket23605B.objects.create(
modela_fk=a2, field_b0=567,
field_b1=datetime.date(2014, 3, 1),
modelc_fk=c1,
)
Ticket23605B.objects.create(
modela_fk=a2, field_b0=76,
field_b1=datetime.date(2011, 3, 3),
modelc_fk=c1,
)
Ticket23605B.objects.create(
modela_fk=a2, field_b0=7,
field_b1=datetime.date(2012, 10, 20),
modelc_fk=c1,
)
Ticket23605B.objects.create(
modela_fk=a2, field_b0=56,
field_b1=datetime.date(2011, 1, 27),
modelc_fk=c1,
)
qx = (
Q(ticket23605b__pk__in=Ticket23605B.objects.order_by('modela_fk', '-field_b1').distinct('modela_fk'))
& Q(ticket23605b__field_b0__gte=300)
)
qy = (
Q(ticket23605b__in=Ticket23605B.objects.order_by('modela_fk', '-field_b1').distinct('modela_fk'))
& Q(ticket23605b__field_b0__gte=300)
)
self.assertEqual(
set(Ticket23605A.objects.filter(qx).values_list('pk', flat=True)),
set(Ticket23605A.objects.filter(qy).values_list('pk', flat=True))
)
self.assertQuerysetEqual(
Ticket23605A.objects.filter(qx),
[a2], lambda x: x
)