File: test_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 (33 lines) | stat: -rw-r--r-- 964 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
from django.db import models
from django.db.migrations import AddIndex, CreateModel

from psqlextra.indexes import UniqueIndex

from .migrations import apply_migration, filtered_schema_editor


def test_unique_index_migrations():
    index = UniqueIndex(fields=["name", "other_name"], name="index1")

    ops = [
        CreateModel(
            name="mymodel",
            fields=[
                ("name", models.TextField()),
                ("other_name", models.TextField()),
            ],
            options={
                # "indexes": [index],
            },
        ),
        AddIndex(model_name="mymodel", index=index),
    ]

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

    calls = [call[0] for _, call, _ in calls["CREATE UNIQUE INDEX"]]

    db_table = "tests_mymodel"
    query = 'CREATE UNIQUE INDEX "index1" ON "{0}" ("name", "other_name")'
    assert str(calls[0]) == query.format(db_table)