-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathtest_schema.py
More file actions
469 lines (413 loc) · 18.3 KB
/
test_schema.py
File metadata and controls
469 lines (413 loc) · 18.3 KB
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
# Copyright 2021 Google LLC
#
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file or at
# https://developers.google.com/open-source/licenses/bsd
from unittest import mock
from django.db import NotSupportedError, connection, connections
from django.db.models import Index
from django.db.models.fields import AutoField, IntegerField
from django_spanner import gen_rand_int64
from django_spanner.schema import DatabaseSchemaEditor
from tests._helpers import HAS_OPENTELEMETRY_INSTALLED
from tests.unit.django_spanner.simple_test import SpannerSimpleTestClass
from tests.unit.django_spanner.test__opentelemetry_tracing import (
DATABASE_ID,
INSTANCE_ID,
PROJECT,
)
from .models import Author
BASE_ATTRIBUTES = {
"db.type": "spanner",
"db.engine": "django_spanner",
"db.project": PROJECT,
"db.instance": INSTANCE_ID,
"db.name": DATABASE_ID,
}
class TestUtils(SpannerSimpleTestClass):
def test_quote_value(self):
"""
Tries quoting input value.
"""
schema_editor = DatabaseSchemaEditor(self.connection)
self.assertEqual(schema_editor.quote_value(value=1.1), "1.1")
def test_skip_default(self):
"""
Tries skipping default as Cloud spanner doesn't support it.
"""
schema_editor = DatabaseSchemaEditor(self.connection)
self.assertTrue(schema_editor.skip_default(field=None))
def test_create_model(self):
"""
Tries creating a model's table.
"""
with DatabaseSchemaEditor(self.connection) as schema_editor:
schema_editor.execute = mock.MagicMock()
schema_editor.create_model(Author)
schema_editor.execute.assert_called_once_with(
"CREATE TABLE tests_author (id INT64 NOT NULL, name STRING(40) "
+ "NOT NULL, last_name STRING(40) NOT NULL, num INT64 NOT "
+ "NULL, created TIMESTAMP NOT NULL, modified TIMESTAMP) "
+ "PRIMARY KEY(id)",
None,
)
if HAS_OPENTELEMETRY_INSTALLED:
span_list = self.ot_exporter.get_finished_spans()
self.assertEqual(len(span_list), 1)
self.assertSpanAttributes(
"CloudSpannerDjango.create_model",
attributes=dict(BASE_ATTRIBUTES, model_name="tests_author"),
span=span_list[0],
)
def test_delete_model(self):
"""
Tests deleting a model
"""
with DatabaseSchemaEditor(self.connection) as schema_editor:
schema_editor.execute = mock.MagicMock()
schema_editor._constraint_names = mock.MagicMock()
schema_editor.delete_model(Author)
schema_editor.execute.assert_called_once_with(
"DROP TABLE tests_author",
)
if HAS_OPENTELEMETRY_INSTALLED:
span_list = self.ot_exporter.get_finished_spans()
self.assertEqual(len(span_list), 1)
self.assertSpanAttributes(
"CloudSpannerDjango.delete_model",
attributes=dict(BASE_ATTRIBUTES, model_name="tests_author"),
span=span_list[0],
)
def test_delete_model_with_index(self):
"""
Tests deleting a model with index
"""
with DatabaseSchemaEditor(self.connection) as schema_editor:
schema_editor.execute = mock.MagicMock()
def delete_index_sql(*args, **kwargs):
# Overriding Statement creation with sql string.
return "DROP INDEX num_unique"
def constraint_names(*args, **kwargs):
return ["num_unique"]
schema_editor._delete_index_sql = delete_index_sql
schema_editor._constraint_names = constraint_names
schema_editor.delete_model(Author)
calls = [
mock.call("DROP INDEX num_unique"),
mock.call("DROP TABLE tests_author"),
]
schema_editor.execute.assert_has_calls(calls)
if HAS_OPENTELEMETRY_INSTALLED:
span_list = self.ot_exporter.get_finished_spans()
self.assertEqual(len(span_list), 2)
self.assertSpanAttributes(
"CloudSpannerDjango.delete_model.delete_index",
attributes=dict(
BASE_ATTRIBUTES,
model_name="tests_author",
index_name="num_unique",
),
span=span_list[0],
)
self.assertSpanAttributes(
"CloudSpannerDjango.delete_model",
attributes=dict(
BASE_ATTRIBUTES,
model_name="tests_author",
),
span=span_list[1],
)
def test_add_field(self):
"""
Tests adding fields to models
"""
with DatabaseSchemaEditor(self.connection) as schema_editor:
schema_editor.execute = mock.MagicMock()
new_field = IntegerField(null=True)
new_field.set_attributes_from_name("age")
schema_editor.add_field(Author, new_field)
schema_editor.execute.assert_called_once_with(
"ALTER TABLE tests_author ADD COLUMN age INT64", []
)
def test_remove_field(self):
"""
Tests remove fields from models
"""
with DatabaseSchemaEditor(self.connection) as schema_editor:
schema_editor.execute = mock.MagicMock()
schema_editor._constraint_names = mock.MagicMock()
remove_field = IntegerField(unique=True)
remove_field.set_attributes_from_name("num")
schema_editor.remove_field(Author, remove_field)
schema_editor.execute.assert_called_once_with(
"ALTER TABLE tests_author DROP COLUMN num"
)
if HAS_OPENTELEMETRY_INSTALLED:
span_list = self.ot_exporter.get_finished_spans()
self.assertEqual(len(span_list), 1)
self.assertSpanAttributes(
"CloudSpannerDjango.remove_field",
attributes=dict(
BASE_ATTRIBUTES,
model_name="tests_author",
field="num",
),
span=span_list[0],
)
def test_remove_field_with_index(self):
"""
Tests remove fields from models
"""
with DatabaseSchemaEditor(self.connection) as schema_editor:
schema_editor.execute = mock.MagicMock()
def delete_index_sql(*args, **kwargs):
# Overriding Statement creation with sql string.
return "DROP INDEX num_unique"
def constraint_names(*args, **kwargs):
return ["num_unique"]
schema_editor._delete_index_sql = delete_index_sql
schema_editor._constraint_names = constraint_names
remove_field = IntegerField(unique=True)
remove_field.set_attributes_from_name("num")
schema_editor.remove_field(Author, remove_field)
calls = [
mock.call("DROP INDEX num_unique"),
mock.call("ALTER TABLE tests_author DROP COLUMN num"),
]
schema_editor.execute.assert_has_calls(calls)
if HAS_OPENTELEMETRY_INSTALLED:
span_list = self.ot_exporter.get_finished_spans()
self.assertEqual(len(span_list), 2)
self.assertSpanAttributes(
"CloudSpannerDjango.remove_field.delete_index",
attributes=dict(
BASE_ATTRIBUTES,
model_name="tests_author",
field="num",
index_name="num_unique",
),
span=span_list[0],
)
self.assertSpanAttributes(
"CloudSpannerDjango.remove_field",
attributes=dict(
BASE_ATTRIBUTES,
model_name="tests_author",
field="num",
),
span=span_list[1],
)
def test_column_sql_not_null_field(self):
"""
Tests column sql for not null field
"""
with DatabaseSchemaEditor(self.connection) as schema_editor:
schema_editor.execute = mock.MagicMock()
new_field = IntegerField()
new_field.set_attributes_from_name("num")
sql, params = schema_editor.column_sql(Author, new_field)
self.assertEqual(sql, "INT64 NOT NULL")
self.assertEqual(params, [])
def test_column_sql_nullable_field(self):
"""
Tests column sql for nullable field
"""
with DatabaseSchemaEditor(self.connection) as schema_editor:
schema_editor.execute = mock.MagicMock()
new_field = IntegerField(null=True)
new_field.set_attributes_from_name("num")
sql, params = schema_editor.column_sql(Author, new_field)
self.assertEqual(sql, "INT64")
self.assertEqual(params, [])
def test_column_add_index(self):
"""
Tests column add index
"""
with DatabaseSchemaEditor(self.connection) as schema_editor:
schema_editor.execute = mock.MagicMock()
index = Index(name="test_author_index_num", fields=["num"])
schema_editor.add_index(Author, index)
name, args, kwargs = schema_editor.execute.mock_calls[0]
self.assertEqual(
str(args[0]),
"CREATE INDEX test_author_index_num ON tests_author (num)",
)
self.assertEqual(kwargs["params"], None)
self.assertEqual(name, "")
if HAS_OPENTELEMETRY_INSTALLED:
span_list = self.ot_exporter.get_finished_spans()
self.assertEqual(len(span_list), 1)
self.assertSpanAttributes(
"CloudSpannerDjango.add_index",
attributes=dict(
BASE_ATTRIBUTES,
model_name="tests_author",
index="num",
),
span=span_list[0],
)
def test_alter_field(self):
"""
Tests altering existing field in table
"""
with DatabaseSchemaEditor(self.connection) as schema_editor:
schema_editor.execute = mock.MagicMock()
old_field = IntegerField()
old_field.set_attributes_from_name("num")
new_field = IntegerField()
new_field.set_attributes_from_name("author_num")
schema_editor.alter_field(Author, old_field, new_field)
schema_editor.execute.assert_called_once_with(
"ALTER TABLE tests_author RENAME COLUMN num TO author_num"
)
def test_alter_field_change_null_with_single_index(self):
"""
Tests altering nullability of field with single index
"""
with DatabaseSchemaEditor(self.connection) as schema_editor:
schema_editor.execute = mock.MagicMock()
def delete_index_sql(*args, **kwargs):
# Overriding Statement creation with sql string.
return "DROP INDEX num_unique"
def create_index_sql(*args, **kwargs):
# Overriding Statement creation with sql string.
return "CREATE INDEX tests_author ON tests_author (author_num)"
def constraint_names(*args, **kwargs):
return ["num_unique"]
schema_editor._delete_index_sql = delete_index_sql
schema_editor._create_index_sql = create_index_sql
schema_editor._constraint_names = constraint_names
old_field = IntegerField(null=True, db_index=True)
old_field.set_attributes_from_name("num")
new_field = IntegerField(db_index=True)
new_field.set_attributes_from_name("author_num")
schema_editor.alter_field(Author, old_field, new_field)
calls = [
mock.call("DROP INDEX num_unique"),
mock.call("ALTER TABLE tests_author RENAME COLUMN num TO author_num"),
mock.call(
"ALTER TABLE tests_author ALTER COLUMN author_num INT64 NOT NULL",
[],
),
mock.call("CREATE INDEX tests_author ON tests_author (author_num)"),
]
schema_editor.execute.assert_has_calls(calls)
if HAS_OPENTELEMETRY_INSTALLED:
span_list = self.ot_exporter.get_finished_spans()
self.assertEqual(len(span_list), 3)
self.assertSpanAttributes(
"CloudSpannerDjango.alter_field.delete_index",
attributes=dict(
BASE_ATTRIBUTES,
model_name="tests_author",
index_name="num_unique",
alter_field="num",
),
span=span_list[0],
)
self.assertSpanAttributes(
"CloudSpannerDjango.alter_field",
attributes=dict(
BASE_ATTRIBUTES,
model_name="tests_author",
alter_field="num",
),
span=span_list[1],
)
self.assertSpanAttributes(
"CloudSpannerDjango.alter_field.recreate_index",
attributes=dict(
BASE_ATTRIBUTES,
model_name="tests_author",
alter_field="author_num",
),
span=span_list[2],
)
def test_alter_field_nullability_change_raise_not_support_error(self):
"""
Tests altering nullability of existing field in table
"""
with DatabaseSchemaEditor(self.connection) as schema_editor:
schema_editor.execute = mock.MagicMock()
def constraint_names(*args, **kwargs):
return ["num_unique"]
schema_editor._constraint_names = constraint_names
old_field = IntegerField(null=True)
old_field.set_attributes_from_name("num")
new_field = IntegerField()
new_field.set_attributes_from_name("author_num")
with self.assertRaises(NotSupportedError):
schema_editor.alter_field(Author, old_field, new_field)
def test_alter_field_change_null_with_multiple_index_error(self):
"""
Tests altering nullability of field with multiple index not supported
"""
with DatabaseSchemaEditor(self.connection) as schema_editor:
schema_editor.execute = mock.MagicMock()
def constraint_names(*args, **kwargs):
return ["num_unique", "dummy_index"]
schema_editor._constraint_names = constraint_names
old_field = IntegerField(null=True, db_index=True)
old_field.set_attributes_from_name("num")
new_field = IntegerField()
new_field.set_attributes_from_name("author_num")
with self.assertRaises(NotSupportedError):
schema_editor.alter_field(Author, old_field, new_field)
def test_autofield_no_default(self):
"""Spanner, default is not provided."""
field = AutoField(name="field_name")
assert gen_rand_int64 == field.default
# db_returning must be explicitly False because Spanner is handling ID generation client-side
assert getattr(field, "db_returning", True) is False
def test_autofield_default(self):
"""Spanner, default provided."""
mock_func = mock.Mock()
field = AutoField(name="field_name", default=mock_func)
assert gen_rand_int64 != field.default
assert mock_func == field.default
# A default was already provided, so Spanner does not generate random IDs client-side.
# Therefore, db_returning does not need to be overridden to False.
assert field.db_returning is True
def test_autofield_not_spanner(self):
"""Not Spanner, default not provided."""
connection.settings_dict["ENGINE"] = "another_db"
field = AutoField(name="field_name")
assert gen_rand_int64 != field.default
# db_returning should remain untouched (implicitly True) for non-Spanner databases
# so that Django retrieves the auto-increment ID correctly.
assert field.db_returning is True
connection.settings_dict["ENGINE"] = "django_spanner"
def test_autofield_not_spanner_w_default(self):
"""Not Spanner, default provided."""
connection.settings_dict["ENGINE"] = "another_db"
mock_func = mock.Mock()
field = AutoField(name="field_name", default=mock_func)
assert gen_rand_int64 != field.default
assert mock_func == field.default
# Because it's not a Spanner database, the behavior shouldn't be altered in any way.
assert field.db_returning is True
connection.settings_dict["ENGINE"] = "django_spanner"
def test_autofield_spanner_as_non_default_db_random_generation_enabled(
self,
):
"""Not Spanner as the default db, default for field not provided."""
connections.settings["default"]["ENGINE"] = "another_db"
connections.settings["secondary"]["ENGINE"] = "django_spanner"
connections.settings["secondary"]["RANDOM_ID_GENERATION_ENABLED"] = "true"
field = AutoField(name="field_name")
assert gen_rand_int64 == field.default
# Since this specific connection explicitly enables client-side random generation,
# we must tell Django not to attempt retrieving the DB's returned ID.
assert getattr(field, "db_returning", True) is False
connections.settings["default"]["ENGINE"] = "django_spanner"
connections.settings["secondary"]["ENGINE"] = "django_spanner"
del connections.settings["secondary"]["RANDOM_ID_GENERATION_ENABLED"]
def test_autofield_random_generation_disabled(self):
"""Spanner, default is not provided."""
connections.settings["default"]["RANDOM_ID_GENERATION_ENABLED"] = "false"
field = AutoField(name="field_name")
assert gen_rand_int64 != field.default
# Because we're delegating ID generation back to the database backend,
# Django needs to be able to retrieve the assigned ID.
assert field.db_returning is True
del connections.settings["default"]["RANDOM_ID_GENERATION_ENABLED"]