summaryrefslogtreecommitdiff
path: root/tests/backends/test_ddl_references.py
blob: bd4036ee3358a283cb582527fd51de6f52a98885 (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
from django.db import connection
from django.db.backends.ddl_references import (
    Columns, Expressions, ForeignKeyName, IndexName, Statement, Table,
)
from django.db.models import ExpressionList, F
from django.db.models.functions import Upper
from django.db.models.indexes import IndexExpression
from django.test import SimpleTestCase, TransactionTestCase

from .models import Person


class TableTests(SimpleTestCase):
    def setUp(self):
        self.reference = Table('table', lambda table: table.upper())

    def test_references_table(self):
        self.assertIs(self.reference.references_table('table'), True)
        self.assertIs(self.reference.references_table('other'), False)

    def test_rename_table_references(self):
        self.reference.rename_table_references('other', 'table')
        self.assertIs(self.reference.references_table('table'), True)
        self.assertIs(self.reference.references_table('other'), False)
        self.reference.rename_table_references('table', 'other')
        self.assertIs(self.reference.references_table('table'), False)
        self.assertIs(self.reference.references_table('other'), True)

    def test_repr(self):
        self.assertEqual(repr(self.reference), "<Table 'TABLE'>")

    def test_str(self):
        self.assertEqual(str(self.reference), 'TABLE')


class ColumnsTests(TableTests):
    def setUp(self):
        self.reference = Columns(
            'table', ['first_column', 'second_column'], lambda column: column.upper()
        )

    def test_references_column(self):
        self.assertIs(self.reference.references_column('other', 'first_column'), False)
        self.assertIs(self.reference.references_column('table', 'third_column'), False)
        self.assertIs(self.reference.references_column('table', 'first_column'), True)

    def test_rename_column_references(self):
        self.reference.rename_column_references('other', 'first_column', 'third_column')
        self.assertIs(self.reference.references_column('table', 'first_column'), True)
        self.assertIs(self.reference.references_column('table', 'third_column'), False)
        self.assertIs(self.reference.references_column('other', 'third_column'), False)
        self.reference.rename_column_references('table', 'third_column', 'first_column')
        self.assertIs(self.reference.references_column('table', 'first_column'), True)
        self.assertIs(self.reference.references_column('table', 'third_column'), False)
        self.reference.rename_column_references('table', 'first_column', 'third_column')
        self.assertIs(self.reference.references_column('table', 'first_column'), False)
        self.assertIs(self.reference.references_column('table', 'third_column'), True)

    def test_repr(self):
        self.assertEqual(repr(self.reference), "<Columns 'FIRST_COLUMN, SECOND_COLUMN'>")

    def test_str(self):
        self.assertEqual(str(self.reference), 'FIRST_COLUMN, SECOND_COLUMN')


class IndexNameTests(ColumnsTests):
    def setUp(self):
        def create_index_name(table_name, column_names, suffix):
            return ', '.join("%s_%s_%s" % (table_name, column_name, suffix) for column_name in column_names)
        self.reference = IndexName(
            'table', ['first_column', 'second_column'], 'suffix', create_index_name
        )

    def test_repr(self):
        self.assertEqual(repr(self.reference), "<IndexName 'table_first_column_suffix, table_second_column_suffix'>")

    def test_str(self):
        self.assertEqual(str(self.reference), 'table_first_column_suffix, table_second_column_suffix')


class ForeignKeyNameTests(IndexNameTests):
    def setUp(self):
        def create_foreign_key_name(table_name, column_names, suffix):
            return ', '.join("%s_%s_%s" % (table_name, column_name, suffix) for column_name in column_names)
        self.reference = ForeignKeyName(
            'table', ['first_column', 'second_column'],
            'to_table', ['to_first_column', 'to_second_column'],
            '%(to_table)s_%(to_column)s_fk',
            create_foreign_key_name,
        )

    def test_references_table(self):
        super().test_references_table()
        self.assertIs(self.reference.references_table('to_table'), True)

    def test_references_column(self):
        super().test_references_column()
        self.assertIs(self.reference.references_column('to_table', 'second_column'), False)
        self.assertIs(self.reference.references_column('to_table', 'to_second_column'), True)

    def test_rename_table_references(self):
        super().test_rename_table_references()
        self.reference.rename_table_references('to_table', 'other_to_table')
        self.assertIs(self.reference.references_table('other_to_table'), True)
        self.assertIs(self.reference.references_table('to_table'), False)

    def test_rename_column_references(self):
        super().test_rename_column_references()
        self.reference.rename_column_references('to_table', 'second_column', 'third_column')
        self.assertIs(self.reference.references_column('table', 'second_column'), True)
        self.assertIs(self.reference.references_column('to_table', 'to_second_column'), True)
        self.reference.rename_column_references('to_table', 'to_first_column', 'to_third_column')
        self.assertIs(self.reference.references_column('to_table', 'to_first_column'), False)
        self.assertIs(self.reference.references_column('to_table', 'to_third_column'), True)

    def test_repr(self):
        self.assertEqual(
            repr(self.reference),
            "<ForeignKeyName 'table_first_column_to_table_to_first_column_fk, "
            "table_second_column_to_table_to_first_column_fk'>"
        )

    def test_str(self):
        self.assertEqual(
            str(self.reference),
            'table_first_column_to_table_to_first_column_fk, '
            'table_second_column_to_table_to_first_column_fk'
        )


class MockReference:
    def __init__(self, representation, referenced_tables, referenced_columns):
        self.representation = representation
        self.referenced_tables = referenced_tables
        self.referenced_columns = referenced_columns

    def references_table(self, table):
        return table in self.referenced_tables

    def references_column(self, table, column):
        return (table, column) in self.referenced_columns

    def rename_table_references(self, old_table, new_table):
        if old_table in self.referenced_tables:
            self.referenced_tables.remove(old_table)
            self.referenced_tables.add(new_table)

    def rename_column_references(self, table, old_column, new_column):
        column = (table, old_column)
        if column in self.referenced_columns:
            self.referenced_columns.remove(column)
            self.referenced_columns.add((table, new_column))

    def __str__(self):
        return self.representation


class StatementTests(SimpleTestCase):
    def test_references_table(self):
        statement = Statement('', reference=MockReference('', {'table'}, {}), non_reference='')
        self.assertIs(statement.references_table('table'), True)
        self.assertIs(statement.references_table('other'), False)

    def test_references_column(self):
        statement = Statement('', reference=MockReference('', {}, {('table', 'column')}), non_reference='')
        self.assertIs(statement.references_column('table', 'column'), True)
        self.assertIs(statement.references_column('other', 'column'), False)

    def test_rename_table_references(self):
        reference = MockReference('', {'table'}, {})
        statement = Statement('', reference=reference, non_reference='')
        statement.rename_table_references('table', 'other')
        self.assertEqual(reference.referenced_tables, {'other'})

    def test_rename_column_references(self):
        reference = MockReference('', {}, {('table', 'column')})
        statement = Statement('', reference=reference, non_reference='')
        statement.rename_column_references('table', 'column', 'other')
        self.assertEqual(reference.referenced_columns, {('table', 'other')})

    def test_repr(self):
        reference = MockReference('reference', {}, {})
        statement = Statement("%(reference)s - %(non_reference)s", reference=reference, non_reference='non_reference')
        self.assertEqual(repr(statement), "<Statement 'reference - non_reference'>")

    def test_str(self):
        reference = MockReference('reference', {}, {})
        statement = Statement("%(reference)s - %(non_reference)s", reference=reference, non_reference='non_reference')
        self.assertEqual(str(statement), 'reference - non_reference')


class ExpressionsTests(TransactionTestCase):
    available_apps = []

    def setUp(self):
        compiler = Person.objects.all().query.get_compiler(connection.alias)
        self.editor = connection.schema_editor()
        self.expressions = Expressions(
            table=Person._meta.db_table,
            expressions=ExpressionList(
                IndexExpression(F('first_name')),
                IndexExpression(F('last_name').desc()),
                IndexExpression(Upper('last_name')),
            ).resolve_expression(compiler.query),
            compiler=compiler,
            quote_value=self.editor.quote_value,
        )

    def test_references_table(self):
        self.assertIs(self.expressions.references_table(Person._meta.db_table), True)
        self.assertIs(self.expressions.references_table('other'), False)

    def test_references_column(self):
        table = Person._meta.db_table
        self.assertIs(self.expressions.references_column(table, 'first_name'), True)
        self.assertIs(self.expressions.references_column(table, 'last_name'), True)
        self.assertIs(self.expressions.references_column(table, 'other'), False)

    def test_rename_table_references(self):
        table = Person._meta.db_table
        self.expressions.rename_table_references(table, 'other')
        self.assertIs(self.expressions.references_table(table), False)
        self.assertIs(self.expressions.references_table('other'), True)
        self.assertIn(
            '%s.%s' % (
                self.editor.quote_name('other'),
                self.editor.quote_name('first_name'),
            ),
            str(self.expressions),
        )

    def test_rename_column_references(self):
        table = Person._meta.db_table
        self.expressions.rename_column_references(table, 'first_name', 'other')
        self.assertIs(self.expressions.references_column(table, 'other'), True)
        self.assertIs(self.expressions.references_column(table, 'first_name'), False)
        self.assertIn(
            '%s.%s' % (self.editor.quote_name(table), self.editor.quote_name('other')),
            str(self.expressions),
        )

    def test_str(self):
        table_name = self.editor.quote_name(Person._meta.db_table)
        expected_str = '%s.%s, %s.%s DESC, (UPPER(%s.%s))' % (
            table_name,
            self.editor.quote_name('first_name'),
            table_name,
            self.editor.quote_name('last_name'),
            table_name,
            self.editor.quote_name('last_name'),
        )
        self.assertEqual(str(self.expressions), expected_str)