diff options
author | Matthew Wilkes <git@matthewwilkes.name> | 2017-06-18 16:53:40 +0100 |
---|---|---|
committer | Tim Graham <timograham@gmail.com> | 2018-02-10 19:08:55 -0500 |
commit | 2162f0983de0dfe2178531638ce7ea56f54dd4e7 (patch) | |
tree | bb1e859159200fa7ebeeaa02ec3908e1cf5d2655 /tests/custom_lookups | |
parent | bf26f66029bca94b007a2452679ac004598364a6 (diff) | |
download | django-2162f0983de0dfe2178531638ce7ea56f54dd4e7.tar.gz |
Fixed #24747 -- Allowed transforms in QuerySet.order_by() and distinct(*fields).
Diffstat (limited to 'tests/custom_lookups')
-rw-r--r-- | tests/custom_lookups/tests.py | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/tests/custom_lookups/tests.py b/tests/custom_lookups/tests.py index 9661aebc49..418525c3ed 100644 --- a/tests/custom_lookups/tests.py +++ b/tests/custom_lookups/tests.py @@ -63,6 +63,14 @@ class Mult3BilateralTransform(models.Transform): return '3 * (%s)' % lhs, lhs_params +class LastDigitTransform(models.Transform): + lookup_name = 'lastdigit' + + def as_sql(self, compiler, connection): + lhs, lhs_params = compiler.compile(self.lhs) + return 'SUBSTR(CAST(%s AS CHAR(2)), 2, 1)' % lhs, lhs_params + + class UpperBilateralTransform(models.Transform): bilateral = True lookup_name = 'upper' @@ -379,6 +387,15 @@ class BilateralTransformTests(TestCase): self.assertSequenceEqual(baseqs.filter(age__mult3__div3=42), [a1, a2, a3, a4]) self.assertSequenceEqual(baseqs.filter(age__div3__mult3=42), [a3]) + def test_transform_order_by(self): + with register_lookup(models.IntegerField, LastDigitTransform): + a1 = Author.objects.create(name='a1', age=11) + a2 = Author.objects.create(name='a2', age=23) + a3 = Author.objects.create(name='a3', age=32) + a4 = Author.objects.create(name='a4', age=40) + qs = Author.objects.order_by('age__lastdigit') + self.assertSequenceEqual(qs, [a4, a1, a3, a2]) + def test_bilateral_fexpr(self): with register_lookup(models.IntegerField, Mult3BilateralTransform): a1 = Author.objects.create(name='a1', age=1, average_rating=3.2) |