2016-06-02 05:43:59 +08: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
|
2017-05-03 13:25:30 +08:00
|
|
|
from django.test.utils import modify_settings
|
2016-06-02 05:43:59 +08:00
|
|
|
|
|
|
|
from . import PostgreSQLTestCase
|
2017-02-11 20:16:35 +08:00
|
|
|
from .models import CITestModel
|
2016-06-02 05:43:59 +08:00
|
|
|
|
|
|
|
|
2017-05-03 13:25:30 +08:00
|
|
|
@modify_settings(INSTALLED_APPS={'append': 'django.contrib.postgres'})
|
2016-06-02 05:43:59 +08:00
|
|
|
class CITextTestCase(PostgreSQLTestCase):
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
2017-02-11 20:16:35 +08:00
|
|
|
cls.john = CITestModel.objects.create(
|
|
|
|
name='JoHn',
|
|
|
|
email='joHn@johN.com',
|
|
|
|
description='Average Joe named JoHn',
|
2017-05-03 13:25:30 +08:00
|
|
|
array_field=['JoE', 'jOhn'],
|
2017-02-11 20:16:35 +08:00
|
|
|
)
|
2016-06-02 05:43:59 +08:00
|
|
|
|
|
|
|
def test_equal_lowercase(self):
|
|
|
|
"""
|
|
|
|
citext removes the need for iexact as the index is case-insensitive.
|
|
|
|
"""
|
2017-02-11 20:16:35 +08: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-02 05:43:59 +08:00
|
|
|
|
2017-02-11 20:16:35 +08:00
|
|
|
def test_fail_citext_primary_key(self):
|
2016-06-02 05:43:59 +08:00
|
|
|
"""
|
2017-02-11 20:16:35 +08:00
|
|
|
Creating an entry for a citext field used as a primary key which
|
|
|
|
clashes with an existing value isn't allowed.
|
2016-06-02 05:43:59 +08:00
|
|
|
"""
|
|
|
|
with self.assertRaises(IntegrityError):
|
2017-02-11 20:16:35 +08:00
|
|
|
CITestModel.objects.create(name='John')
|
2017-05-03 13:25:30 +08:00
|
|
|
|
|
|
|
def test_array_field(self):
|
|
|
|
instance = CITestModel.objects.get()
|
|
|
|
self.assertEqual(instance.array_field, self.john.array_field)
|
|
|
|
self.assertTrue(CITestModel.objects.filter(array_field__contains=['joe']).exists())
|