mirror of
https://github.com/django/django.git
synced 2025-08-03 18:38:50 +00:00
Fixed #21181 -- Added Collate database function.
Thanks Simon Charette for reviews.
This commit is contained in:
parent
60626162f7
commit
63300f7e68
10 changed files with 121 additions and 2 deletions
56
tests/db_functions/comparison/test_collate.py
Normal file
56
tests/db_functions/comparison/test_collate.py
Normal file
|
@ -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)
|
Loading…
Add table
Add a link
Reference in a new issue