summaryrefslogtreecommitdiff
path: root/tests/postgres_tests/test_citext.py
blob: 2abb56b39f8a8637a2e90a49f785694c651e4782 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# RemovedInDjango51Warning.
"""
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
from django.utils.deprecation import RemovedInDjango51Warning

from . import PostgreSQLTestCase
from .models import CITestModel


class CITextTestCase(PostgreSQLTestCase):
    case_sensitive_lookups = ("contains", "startswith", "endswith", "regex")

    @classmethod
    def setUpTestData(cls):
        cls.john = CITestModel.objects.create(
            name="JoHn",
            email="joHn@johN.com",
            description="Average Joe named JoHn",
            array_field=["JoE", "jOhn"],
        )

    def test_equal_lowercase(self):
        """
        citext removes the need for iexact as the index is case-insensitive.
        """
        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,
        )

    def test_fail_citext_primary_key(self):
        """
        Creating an entry for a citext field used as a primary key which
        clashes with an existing value isn't allowed.
        """
        with self.assertRaises(IntegrityError):
            CITestModel.objects.create(name="John")

    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()
        )

    def test_lookups_name_char(self):
        for lookup in self.case_sensitive_lookups:
            with self.subTest(lookup=lookup):
                query = {"name__{}".format(lookup): "john"}
                self.assertSequenceEqual(
                    CITestModel.objects.filter(**query), [self.john]
                )

    def test_lookups_description_text(self):
        for lookup, string in zip(
            self.case_sensitive_lookups, ("average", "average joe", "john", "Joe.named")
        ):
            with self.subTest(lookup=lookup, string=string):
                query = {"description__{}".format(lookup): string}
                self.assertSequenceEqual(
                    CITestModel.objects.filter(**query), [self.john]
                )

    def test_lookups_email(self):
        for lookup, string in zip(
            self.case_sensitive_lookups, ("john", "john", "john.com", "john.com")
        ):
            with self.subTest(lookup=lookup, string=string):
                query = {"email__{}".format(lookup): string}
                self.assertSequenceEqual(
                    CITestModel.objects.filter(**query), [self.john]
                )

    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()