File: test_indexes.py

package info (click to toggle)
python-django 3%3A5.2.5-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 61,236 kB
  • sloc: python: 361,585; javascript: 19,250; xml: 211; makefile: 182; sh: 28
file content (77 lines) | stat: -rw-r--r-- 2,804 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
71
72
73
74
75
76
77
from django.contrib.gis.db import models
from django.db import connection
from django.db.models import Index
from django.test import TransactionTestCase
from django.test.utils import isolate_apps

from .models import City


class SchemaIndexesTests(TransactionTestCase):
    available_apps = []
    models = [City]

    def get_indexes(self, table):
        with connection.cursor() as cursor:
            constraints = connection.introspection.get_constraints(cursor, table)
            return {
                name: constraint["columns"]
                for name, constraint in constraints.items()
                if constraint["index"]
            }

    def has_spatial_indexes(self, table):
        if connection.ops.mysql:
            with connection.cursor() as cursor:
                return connection.introspection.supports_spatial_index(cursor, table)
        elif connection.ops.oracle:
            # Spatial indexes in Meta.indexes are not supported by the Oracle
            # backend (see #31252).
            return False
        return True

    def test_using_sql(self):
        if not connection.ops.postgis:
            self.skipTest("This is a PostGIS-specific test.")
        index = Index(fields=["point"])
        editor = connection.schema_editor()
        self.assertIn(
            "%s USING " % editor.quote_name(City._meta.db_table),
            str(index.create_sql(City, editor)),
        )

    @isolate_apps("gis_tests.geoapp")
    def test_namespaced_db_table(self):
        if not connection.ops.postgis:
            self.skipTest("PostGIS-specific test.")

        class SchemaCity(models.Model):
            point = models.PointField()

            class Meta:
                app_label = "geoapp"
                db_table = 'django_schema"."geoapp_schema_city'

        index = Index(fields=["point"])
        editor = connection.schema_editor()
        create_index_sql = str(index.create_sql(SchemaCity, editor))
        self.assertIn(
            "%s USING " % editor.quote_name(SchemaCity._meta.db_table),
            create_index_sql,
        )
        self.assertIn(
            'CREATE INDEX "geoapp_schema_city_point_9ed70651_id" ',
            create_index_sql,
        )

    def test_index_name(self):
        if not self.has_spatial_indexes(City._meta.db_table):
            self.skipTest("Spatial indexes in Meta.indexes are not supported.")
        index_name = "custom_point_index_name"
        index = Index(fields=["point"], name=index_name)
        with connection.schema_editor() as editor:
            editor.add_index(City, index)
            indexes = self.get_indexes(City._meta.db_table)
            self.assertIn(index_name, indexes)
            self.assertEqual(indexes[index_name], ["point"])
            editor.remove_index(City, index)