Fixed #24420 -- Allowed ordering by case expressions

This commit is contained in:
Josh Smeaton 2015-03-05 17:10:48 +11:00
parent 82f7bee1d5
commit ceaf31adff
3 changed files with 47 additions and 0 deletions

View file

@ -980,6 +980,30 @@ class CaseExpressionTests(TestCase):
transform=attrgetter('integer', 'integer2', 'test')
)
def test_order_by_conditional_implicit(self):
self.assertQuerysetEqual(
CaseTestModel.objects.filter(integer__lte=2).annotate(test=Case(
When(integer=1, then=2),
When(integer=2, then=1),
default=3,
output_field=models.IntegerField(),
)).order_by('test', 'pk'),
[(2, 1), (2, 1), (1, 2)],
transform=attrgetter('integer', 'test')
)
def test_order_by_conditional_explicit(self):
self.assertQuerysetEqual(
CaseTestModel.objects.filter(integer__lte=2).annotate(test=Case(
When(integer=1, then=2),
When(integer=2, then=1),
default=3,
output_field=models.IntegerField(),
)).order_by(F('test').asc(), 'pk'),
[(2, 1), (2, 1), (1, 2)],
transform=attrgetter('integer', 'test')
)
class CaseDocumentationExamples(TestCase):
@classmethod