2022-08-03 09:42:51 +00:00
|
|
|
# RemovedInDjango51Warning.
|
2016-06-01 21:43:59 +00:00
|
|
|
"""
|
|
|
|
The citext PostgreSQL extension supports indexing of case-insensitive text
|
|
|
|
strings and thus eliminates the need for operations such as iexact and other
|
|
|
|
modifiers to enforce use of an index.
|
|
|
|
"""
|
|
|
|
from django.db import IntegrityError
|
2022-08-03 09:42:51 +00:00
|
|
|
from django.utils.deprecation import RemovedInDjango51Warning
|
2016-06-01 21:43:59 +00:00
|
|
|
|
|
|
|
from . import PostgreSQLTestCase
|
2017-02-11 12:16:35 +00:00
|
|
|
from .models import CITestModel
|
2016-06-01 21:43:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
class CITextTestCase(PostgreSQLTestCase):
|
2022-02-03 19:24:19 +00:00
|
|
|
case_sensitive_lookups = ("contains", "startswith", "endswith", "regex")
|
2016-06-01 21:43:59 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
2017-02-11 12:16:35 +00:00
|
|
|
cls.john = CITestModel.objects.create(
|
2022-02-03 19:24:19 +00:00
|
|
|
name="JoHn",
|
|
|
|
email="joHn@johN.com",
|
|
|
|
description="Average Joe named JoHn",
|
|
|
|
array_field=["JoE", "jOhn"],
|
2017-02-11 12:16:35 +00:00
|
|
|
)
|
2016-06-01 21:43:59 +00:00
|
|
|
|
|
|
|
def test_equal_lowercase(self):
|
|
|
|
"""
|
|
|
|
citext removes the need for iexact as the index is case-insensitive.
|
|
|
|
"""
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(
|
|
|
|
CITestModel.objects.filter(name=self.john.name.lower()).count(), 1
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
CITestModel.objects.filter(email=self.john.email.lower()).count(), 1
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
CITestModel.objects.filter(
|
|
|
|
description=self.john.description.lower()
|
|
|
|
).count(),
|
|
|
|
1,
|
|
|
|
)
|
2016-06-01 21:43:59 +00:00
|
|
|
|
2017-02-11 12:16:35 +00:00
|
|
|
def test_fail_citext_primary_key(self):
|
2016-06-01 21:43:59 +00:00
|
|
|
"""
|
2017-02-11 12:16:35 +00:00
|
|
|
Creating an entry for a citext field used as a primary key which
|
|
|
|
clashes with an existing value isn't allowed.
|
2016-06-01 21:43:59 +00:00
|
|
|
"""
|
|
|
|
with self.assertRaises(IntegrityError):
|
2022-02-03 19:24:19 +00:00
|
|
|
CITestModel.objects.create(name="John")
|
2017-05-03 05:25:30 +00:00
|
|
|
|
|
|
|
def test_array_field(self):
|
|
|
|
instance = CITestModel.objects.get()
|
|
|
|
self.assertEqual(instance.array_field, self.john.array_field)
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertTrue(
|
|
|
|
CITestModel.objects.filter(array_field__contains=["joe"]).exists()
|
|
|
|
)
|
2017-11-14 21:51:51 +00:00
|
|
|
|
|
|
|
def test_lookups_name_char(self):
|
|
|
|
for lookup in self.case_sensitive_lookups:
|
|
|
|
with self.subTest(lookup=lookup):
|
2022-02-03 19:24:19 +00:00
|
|
|
query = {"name__{}".format(lookup): "john"}
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
CITestModel.objects.filter(**query), [self.john]
|
|
|
|
)
|
2017-11-14 21:51:51 +00:00
|
|
|
|
|
|
|
def test_lookups_description_text(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
for lookup, string in zip(
|
|
|
|
self.case_sensitive_lookups, ("average", "average joe", "john", "Joe.named")
|
|
|
|
):
|
2017-11-14 21:51:51 +00:00
|
|
|
with self.subTest(lookup=lookup, string=string):
|
2022-02-03 19:24:19 +00:00
|
|
|
query = {"description__{}".format(lookup): string}
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
CITestModel.objects.filter(**query), [self.john]
|
|
|
|
)
|
2017-11-14 21:51:51 +00:00
|
|
|
|
|
|
|
def test_lookups_email(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
for lookup, string in zip(
|
|
|
|
self.case_sensitive_lookups, ("john", "john", "john.com", "john.com")
|
|
|
|
):
|
2017-11-14 21:51:51 +00:00
|
|
|
with self.subTest(lookup=lookup, string=string):
|
2022-02-03 19:24:19 +00:00
|
|
|
query = {"email__{}".format(lookup): string}
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
CITestModel.objects.filter(**query), [self.john]
|
|
|
|
)
|
2022-08-03 09:42:51 +00:00
|
|
|
|
|
|
|
def test_citext_deprecated(self):
|
|
|
|
from django.contrib.postgres.fields import CIText
|
|
|
|
|
|
|
|
msg = "django.contrib.postgres.fields.CIText mixin is deprecated."
|
|
|
|
with self.assertRaisesMessage(RemovedInDjango51Warning, msg):
|
|
|
|
CIText()
|