File: test_case_insensitive_unique_index.py

package info (click to toggle)
python-django-postgres-extra 2.0.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,096 kB
  • sloc: python: 9,057; makefile: 17; sh: 7; sql: 1
file content (70 lines) | stat: -rw-r--r-- 2,093 bytes parent folder | download | duplicates (3)
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
import pytest

from django.db import IntegrityError, connection, models
from django.db.migrations import AddIndex, CreateModel

from psqlextra.indexes import CaseInsensitiveUniqueIndex
from psqlextra.models import PostgresModel

from .fake_model import get_fake_model
from .migrations import apply_migration, filtered_schema_editor


def test_ciui_migrations():
    """Tests whether migrations for case sensitive indexes are being created as
    expected."""

    index_1 = CaseInsensitiveUniqueIndex(
        fields=["name", "other_name"], name="index1"
    )

    ops = [
        CreateModel(
            name="mymodel",
            fields=[
                ("name", models.CharField(max_length=255)),
                ("other_name", models.CharField(max_length=255)),
            ],
        ),
        AddIndex(model_name="mymodel", index=index_1),
    ]

    with filtered_schema_editor("CREATE UNIQUE INDEX") as calls:
        apply_migration(ops)

    sql = str([call[0] for _, call, _ in calls["CREATE UNIQUE INDEX"]][0])
    expected_sql = 'CREATE UNIQUE INDEX "index1" ON "tests_mymodel" (LOWER("name"), LOWER("other_name"))'
    assert sql == expected_sql


def test_ciui():
    """Tests whether the case insensitive unique index works as expected."""

    index_1 = CaseInsensitiveUniqueIndex(fields=["name"], name="index1")

    model = get_fake_model(
        {"name": models.CharField(max_length=255)}, PostgresModel
    )

    with connection.schema_editor() as schema_editor:
        schema_editor.add_index(model, index_1)

    model.objects.create(name="henk")

    with pytest.raises(IntegrityError):
        model.objects.create(name="Henk")


def test_ciui_on_conflict():
    """Tests wether fields with a :see:CaseInsensitiveUniqueIndex can be used
    as a conflict target."""

    index_1 = CaseInsensitiveUniqueIndex(fields=["name"], name="index1")

    model = get_fake_model(
        {"name": models.CharField(max_length=255)},
        PostgresModel,
        {"indexes": [index_1]},
    )

    model.objects.upsert(conflict_target=["name"], fields=dict(name="henk"))