summaryrefslogtreecommitdiff
path: root/tests/db_functions
diff options
context:
space:
mode:
authorTom Carrick <tom@carrick.eu>2020-08-08 13:37:06 +0200
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2020-08-11 22:21:08 +0200
commit63300f7e686c2c452763cb512df9abf7734fd588 (patch)
tree79526757ff463d5706ee879f06b3dd46780f6c19 /tests/db_functions
parent60626162f76f26d32a38d18151700cb041201fb3 (diff)
downloaddjango-63300f7e686c2c452763cb512df9abf7734fd588.tar.gz
Fixed #21181 -- Added Collate database function.
Thanks Simon Charette for reviews.
Diffstat (limited to 'tests/db_functions')
-rw-r--r--tests/db_functions/comparison/test_collate.py56
1 files changed, 56 insertions, 0 deletions
diff --git a/tests/db_functions/comparison/test_collate.py b/tests/db_functions/comparison/test_collate.py
new file mode 100644
index 0000000000..6507c904bc
--- /dev/null
+++ b/tests/db_functions/comparison/test_collate.py
@@ -0,0 +1,56 @@
+from django.db import connection
+from django.db.models import F, Value
+from django.db.models.functions import Collate
+from django.test import TestCase
+
+from ..models import Author
+
+
+class CollateTests(TestCase):
+ @classmethod
+ def setUpTestData(cls):
+ cls.author1 = Author.objects.create(alias='a', name='Jones 1')
+ cls.author2 = Author.objects.create(alias='A', name='Jones 2')
+
+ def test_collate_filter_ci(self):
+ collation = connection.features.test_collations.get('ci')
+ if not collation:
+ self.skipTest(
+ 'This backend does not support case-insensitive collations.'
+ )
+ qs = Author.objects.filter(alias=Collate(Value('a'), collation))
+ self.assertEqual(qs.count(), 2)
+
+ def test_collate_order_by_cs(self):
+ collation = connection.features.test_collations.get('cs')
+ if not collation:
+ self.skipTest(
+ 'This backend does not support case-sensitive collations.'
+ )
+ qs = Author.objects.order_by(Collate('alias', collation))
+ self.assertSequenceEqual(qs, [self.author2, self.author1])
+
+ def test_language_collation_order_by(self):
+ collation = connection.features.test_collations.get('swedish-ci')
+ if not collation:
+ self.skipTest('This backend does not support language collations.')
+ author3 = Author.objects.create(alias='O', name='Jones')
+ author4 = Author.objects.create(alias='Ö', name='Jones')
+ author5 = Author.objects.create(alias='P', name='Jones')
+ qs = Author.objects.order_by(Collate(F('alias'), collation), 'name')
+ self.assertSequenceEqual(
+ qs,
+ [self.author1, self.author2, author3, author5, author4],
+ )
+
+ def test_invalid_collation(self):
+ tests = [
+ None,
+ '',
+ 'et-x-icu" OR ',
+ '"schema"."collation"',
+ ]
+ msg = "Invalid collation name: %r."
+ for value in tests:
+ with self.subTest(value), self.assertRaisesMessage(ValueError, msg % value):
+ Collate(F('alias'), value)