test_citext.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. # RemovedInDjango51Warning.
  2. """
  3. The citext PostgreSQL extension supports indexing of case-insensitive text
  4. strings and thus eliminates the need for operations such as iexact and other
  5. modifiers to enforce use of an index.
  6. """
  7. from django.db import IntegrityError
  8. from django.utils.deprecation import RemovedInDjango51Warning
  9. from . import PostgreSQLTestCase
  10. from .models import CITestModel
  11. class CITextTestCase(PostgreSQLTestCase):
  12. case_sensitive_lookups = ("contains", "startswith", "endswith", "regex")
  13. @classmethod
  14. def setUpTestData(cls):
  15. cls.john = CITestModel.objects.create(
  16. name="JoHn",
  17. email="joHn@johN.com",
  18. description="Average Joe named JoHn",
  19. array_field=["JoE", "jOhn"],
  20. )
  21. def test_equal_lowercase(self):
  22. """
  23. citext removes the need for iexact as the index is case-insensitive.
  24. """
  25. self.assertEqual(
  26. CITestModel.objects.filter(name=self.john.name.lower()).count(), 1
  27. )
  28. self.assertEqual(
  29. CITestModel.objects.filter(email=self.john.email.lower()).count(), 1
  30. )
  31. self.assertEqual(
  32. CITestModel.objects.filter(
  33. description=self.john.description.lower()
  34. ).count(),
  35. 1,
  36. )
  37. def test_fail_citext_primary_key(self):
  38. """
  39. Creating an entry for a citext field used as a primary key which
  40. clashes with an existing value isn't allowed.
  41. """
  42. with self.assertRaises(IntegrityError):
  43. CITestModel.objects.create(name="John")
  44. def test_array_field(self):
  45. instance = CITestModel.objects.get()
  46. self.assertEqual(instance.array_field, self.john.array_field)
  47. self.assertTrue(
  48. CITestModel.objects.filter(array_field__contains=["joe"]).exists()
  49. )
  50. def test_lookups_name_char(self):
  51. for lookup in self.case_sensitive_lookups:
  52. with self.subTest(lookup=lookup):
  53. query = {"name__{}".format(lookup): "john"}
  54. self.assertSequenceEqual(
  55. CITestModel.objects.filter(**query), [self.john]
  56. )
  57. def test_lookups_description_text(self):
  58. for lookup, string in zip(
  59. self.case_sensitive_lookups, ("average", "average joe", "john", "Joe.named")
  60. ):
  61. with self.subTest(lookup=lookup, string=string):
  62. query = {"description__{}".format(lookup): string}
  63. self.assertSequenceEqual(
  64. CITestModel.objects.filter(**query), [self.john]
  65. )
  66. def test_lookups_email(self):
  67. for lookup, string in zip(
  68. self.case_sensitive_lookups, ("john", "john", "john.com", "john.com")
  69. ):
  70. with self.subTest(lookup=lookup, string=string):
  71. query = {"email__{}".format(lookup): string}
  72. self.assertSequenceEqual(
  73. CITestModel.objects.filter(**query), [self.john]
  74. )
  75. def test_citext_deprecated(self):
  76. from django.contrib.postgres.fields import CIText
  77. msg = "django.contrib.postgres.fields.CIText mixin is deprecated."
  78. with self.assertRaisesMessage(RemovedInDjango51Warning, msg):
  79. CIText()