summaryrefslogtreecommitdiff
path: root/tests/db_functions
diff options
context:
space:
mode:
authorNick Pope <nick.pope@flightdataservices.com>2019-01-09 00:12:10 +0000
committerTim Graham <timograham@gmail.com>2019-01-12 10:45:48 -0500
commitabf8e390a4161a051ed1c4be11b9447866b06ba8 (patch)
tree5ca4e52efcb4a230d1d5259aa8aba95fca7d972f /tests/db_functions
parentb69f8eb04cc8762d3dfd5af5ea1fc58e3f2ebcc3 (diff)
Refs #28643 -- Added Reverse database function.
Thanks Mariusz Felisiak for Oracle advice and review.
Diffstat (limited to 'tests/db_functions')
-rw-r--r--tests/db_functions/text/test_reverse.py46
1 files changed, 46 insertions, 0 deletions
diff --git a/tests/db_functions/text/test_reverse.py b/tests/db_functions/text/test_reverse.py
new file mode 100644
index 0000000000..1cc1045b04
--- /dev/null
+++ b/tests/db_functions/text/test_reverse.py
@@ -0,0 +1,46 @@
+from django.db import connection
+from django.db.models import CharField
+from django.db.models.functions import Length, Reverse, Trim
+from django.test import TestCase
+from django.test.utils import register_lookup
+
+from ..models import Author
+
+
+class ReverseTests(TestCase):
+ @classmethod
+ def setUpTestData(cls):
+ cls.john = Author.objects.create(name='John Smith', alias='smithj')
+ cls.elena = Author.objects.create(name='Élena Jordan', alias='elena')
+ cls.python = Author.objects.create(name='パイソン')
+
+ def test_null(self):
+ author = Author.objects.annotate(backward=Reverse('alias')).get(pk=self.python.pk)
+ self.assertEqual(author.backward, '' if connection.features.interprets_empty_strings_as_nulls else None)
+
+ def test_basic(self):
+ authors = Author.objects.annotate(backward=Reverse('name'))
+ self.assertQuerysetEqual(
+ authors,
+ [
+ ('John Smith', 'htimS nhoJ'),
+ ('Élena Jordan', 'nadroJ anelÉ'),
+ ('パイソン', 'ンソイパ'),
+ ],
+ lambda a: (a.name, a.backward),
+ ordered=False,
+ )
+
+ def test_transform(self):
+ with register_lookup(CharField, Reverse):
+ authors = Author.objects.all()
+ self.assertCountEqual(authors.filter(name__reverse=self.john.name[::-1]), [self.john])
+ self.assertCountEqual(authors.exclude(name__reverse=self.john.name[::-1]), [self.elena, self.python])
+
+ def test_expressions(self):
+ author = Author.objects.annotate(backward=Reverse(Trim('name'))).get(pk=self.john.pk)
+ self.assertEqual(author.backward, self.john.name[::-1])
+ with register_lookup(CharField, Reverse), register_lookup(CharField, Length):
+ authors = Author.objects.all()
+ self.assertCountEqual(authors.filter(name__reverse__length__gt=7), [self.john, self.elena])
+ self.assertCountEqual(authors.exclude(name__reverse__length__gt=7), [self.python])