test_citext.py 3.3 KB

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