test_citext.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536
  1. """
  2. The citext PostgreSQL extension supports indexing of case-insensitive text
  3. strings and thus eliminates the need for operations such as iexact and other
  4. modifiers to enforce use of an index.
  5. """
  6. from django.db import IntegrityError
  7. from . import PostgreSQLTestCase
  8. from .models import CITestModel
  9. class CITextTestCase(PostgreSQLTestCase):
  10. @classmethod
  11. def setUpTestData(cls):
  12. cls.john = CITestModel.objects.create(
  13. name='JoHn',
  14. email='joHn@johN.com',
  15. description='Average Joe named JoHn',
  16. )
  17. def test_equal_lowercase(self):
  18. """
  19. citext removes the need for iexact as the index is case-insensitive.
  20. """
  21. self.assertEqual(CITestModel.objects.filter(name=self.john.name.lower()).count(), 1)
  22. self.assertEqual(CITestModel.objects.filter(email=self.john.email.lower()).count(), 1)
  23. self.assertEqual(CITestModel.objects.filter(description=self.john.description.lower()).count(), 1)
  24. def test_fail_citext_primary_key(self):
  25. """
  26. Creating an entry for a citext field used as a primary key which
  27. clashes with an existing value isn't allowed.
  28. """
  29. with self.assertRaises(IntegrityError):
  30. CITestModel.objects.create(name='John')