summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorbobort <4dbobort@msn.com>2018-02-23 09:23:22 -0600
committerTim Graham <timograham@gmail.com>2018-02-23 10:23:22 -0500
commitf82de6bfb1c3dc468f6eb7472b292cc432d00338 (patch)
treeee3a835949807a1cd550ab1a8aa2dd2bbbc27d87 /tests
parentc412926a2e359afb40738d8177c9f3bef80ee04e (diff)
Refs #28643 -- Added Ord, Chr, Left, and Right database functions.
Diffstat (limited to 'tests')
-rw-r--r--tests/db_functions/test_chr.py32
-rw-r--r--tests/db_functions/test_left.py27
-rw-r--r--tests/db_functions/test_ord.py27
-rw-r--r--tests/db_functions/test_right.py27
4 files changed, 113 insertions, 0 deletions
diff --git a/tests/db_functions/test_chr.py b/tests/db_functions/test_chr.py
new file mode 100644
index 0000000000..0b4b0cc77c
--- /dev/null
+++ b/tests/db_functions/test_chr.py
@@ -0,0 +1,32 @@
+from django.db.models import IntegerField
+from django.db.models.functions import Chr, Left, Ord
+from django.test import TestCase
+
+from .models import Author
+
+
+class ChrTests(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.rhonda = Author.objects.create(name='Rhonda')
+
+ def test_basic(self):
+ authors = Author.objects.annotate(first_initial=Left('name', 1))
+ self.assertCountEqual(authors.filter(first_initial=Chr(ord('J'))), [self.john])
+ self.assertCountEqual(authors.exclude(first_initial=Chr(ord('J'))), [self.elena, self.rhonda])
+
+ def test_non_ascii(self):
+ authors = Author.objects.annotate(first_initial=Left('name', 1))
+ self.assertCountEqual(authors.filter(first_initial=Chr(ord('É'))), [self.elena])
+ self.assertCountEqual(authors.exclude(first_initial=Chr(ord('É'))), [self.john, self.rhonda])
+
+ def test_transform(self):
+ try:
+ IntegerField.register_lookup(Chr)
+ authors = Author.objects.annotate(name_code_point=Ord('name'))
+ self.assertCountEqual(authors.filter(name_code_point__chr=Chr(ord('J'))), [self.john])
+ self.assertCountEqual(authors.exclude(name_code_point__chr=Chr(ord('J'))), [self.elena, self.rhonda])
+ finally:
+ IntegerField._unregister_lookup(Chr)
diff --git a/tests/db_functions/test_left.py b/tests/db_functions/test_left.py
new file mode 100644
index 0000000000..f853ac21ac
--- /dev/null
+++ b/tests/db_functions/test_left.py
@@ -0,0 +1,27 @@
+from django.db.models import CharField, Value
+from django.db.models.functions import Left, Lower
+from django.test import TestCase
+
+from .models import Author
+
+
+class LeftTests(TestCase):
+ @classmethod
+ def setUpTestData(cls):
+ Author.objects.create(name='John Smith', alias='smithj')
+ Author.objects.create(name='Rhonda')
+
+ def test_basic(self):
+ authors = Author.objects.annotate(name_part=Left('name', 5))
+ self.assertQuerysetEqual(authors.order_by('name'), ['John ', 'Rhond'], lambda a: a.name_part)
+ # If alias is null, set it to the first 2 lower characters of the name.
+ Author.objects.filter(alias__isnull=True).update(alias=Lower(Left('name', 2)))
+ self.assertQuerysetEqual(authors.order_by('name'), ['smithj', 'rh'], lambda a: a.alias)
+
+ def test_invalid_length(self):
+ with self.assertRaisesMessage(ValueError, "'length' must be greater than 0"):
+ Author.objects.annotate(raises=Left('name', 0))
+
+ def test_expressions(self):
+ authors = Author.objects.annotate(name_part=Left('name', Value(3), output_field=CharField()))
+ self.assertQuerysetEqual(authors.order_by('name'), ['Joh', 'Rho'], lambda a: a.name_part)
diff --git a/tests/db_functions/test_ord.py b/tests/db_functions/test_ord.py
new file mode 100644
index 0000000000..93ec38b076
--- /dev/null
+++ b/tests/db_functions/test_ord.py
@@ -0,0 +1,27 @@
+from django.db.models import CharField, Value
+from django.db.models.functions import Left, Ord
+from django.test import TestCase
+
+from .models import Author
+
+
+class OrdTests(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.rhonda = Author.objects.create(name='Rhonda')
+
+ def test_basic(self):
+ authors = Author.objects.annotate(name_part=Ord('name'))
+ self.assertCountEqual(authors.filter(name_part__gt=Ord(Value('John'))), [self.elena, self.rhonda])
+ self.assertCountEqual(authors.exclude(name_part__gt=Ord(Value('John'))), [self.john])
+
+ def test_transform(self):
+ try:
+ CharField.register_lookup(Ord)
+ authors = Author.objects.annotate(first_initial=Left('name', 1))
+ self.assertCountEqual(authors.filter(first_initial__ord=ord('J')), [self.john])
+ self.assertCountEqual(authors.exclude(first_initial__ord=ord('J')), [self.elena, self.rhonda])
+ finally:
+ CharField._unregister_lookup(Ord)
diff --git a/tests/db_functions/test_right.py b/tests/db_functions/test_right.py
new file mode 100644
index 0000000000..b75bfd5155
--- /dev/null
+++ b/tests/db_functions/test_right.py
@@ -0,0 +1,27 @@
+from django.db.models import CharField, Value
+from django.db.models.functions import Lower, Right
+from django.test import TestCase
+
+from .models import Author
+
+
+class RightTests(TestCase):
+ @classmethod
+ def setUpTestData(cls):
+ Author.objects.create(name='John Smith', alias='smithj')
+ Author.objects.create(name='Rhonda')
+
+ def test_basic(self):
+ authors = Author.objects.annotate(name_part=Right('name', 5))
+ self.assertQuerysetEqual(authors.order_by('name'), ['Smith', 'honda'], lambda a: a.name_part)
+ # If alias is null, set it to the first 2 lower characters of the name.
+ Author.objects.filter(alias__isnull=True).update(alias=Lower(Right('name', 2)))
+ self.assertQuerysetEqual(authors.order_by('name'), ['smithj', 'da'], lambda a: a.alias)
+
+ def test_invalid_length(self):
+ with self.assertRaisesMessage(ValueError, "'length' must be greater than 0"):
+ Author.objects.annotate(raises=Right('name', 0))
+
+ def test_expressions(self):
+ authors = Author.objects.annotate(name_part=Right('name', Value(3), output_field=CharField()))
+ self.assertQuerysetEqual(authors.order_by('name'), ['ith', 'nda'], lambda a: a.name_part)