8000 test: add unit test for schema (#637) · asthamohta/python-spanner-django@eeaa047 · GitHub
[go: up one dir, main page]

Skip to content

Commit eeaa047

Browse files

File tree

1 file changed

+126
-0
lines changed

1 file changed

+126
-0
lines changed
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
# Copyright 2021 Google LLC
2+
#
3+
# Use of this source code is governed by a BSD-style
4+
# license that can be found in the LICENSE file or at
5+
# https://developers.google.com/open-source/licenses/bsd
6+
7+
8+
from django.db.models import Index
9+
from django.db.models.fields import IntegerField
10+
from django_spanner.schema import DatabaseSchemaEditor
11+
from tests.unit.django_spanner.simple_test import SpannerSimpleTestClass
12+
from unittest import mock
13+
from .models import Author
14+
15+
16+
class TestUtils(SpannerSimpleTestClass):
17+
def test_quote_value(self):
18+
"""
19+
Tries quoting input value.
20+
"""
21+
schema_editor = DatabaseSchemaEditor(self.connection)
22+
self.assertEqual(schema_editor.quote_value(value=1.1), "1.1")
23+
24+
def test_skip_default(self):
25+
"""
26+
Tries skipping default as Cloud spanner doesn't support it.
27+
"""
28+
schema_editor = DatabaseSchemaEditor(self.connection)
29+
self.assertTrue(schema_editor.skip_default(field=None))
30+
31+
def test_create_model(self):
32+
"""
33+
Tries creating a model's table.
34+
"""
35+
with DatabaseSchemaEditor(self.connection) as schema_editor:
36+
schema_editor.execute = mock.MagicMock()
37+
schema_editor.create_model(Author)
38+
39+
schema_editor.execute.assert_called_once_with(
40+
"CREATE TABLE tests_author (id INT64 NOT NULL, name STRING(40) "
41+
+ "NOT NULL, last_name STRING(40) NOT NULL, num INT64 NOT "
42+
+ "NULL, created TIMESTAMP NOT NULL, modified TIMESTAMP) "
43+
+ "PRIMARY KEY(id)",
44+
None,
45+
)
46+
47+
def test_delete_model(self):
48+
"""
49+
Tests deleting a model
50+
"""
51+
with DatabaseSchemaEditor(self.connection) as schema_editor:
52+
schema_editor.execute = mock.MagicMock()
53+
schema_editor._constraint_names = mock.MagicMock()
54+
schema_editor.delete_model(Author)
55+
56+
schema_editor.execute.assert_called_once_with(
57+
"DROP TABLE tests_author",
58+
)
59+
60+
def test_add_field(self):
61+
"""
62+
Tests adding fields to models
63+
"""
64+
with DatabaseSchemaEditor(self.connection) as schema_editor:
65+
schema_editor.execute = mock.MagicMock()
66+
new_field = IntegerField(null=True)
67+
new_field.set_attributes_from_name("age")
68+
schema_editor.add_field(Author, new_field)
69+
70+
schema_editor.execute.assert_called_once_with(
71+
"ALTER TABLE tests_author ADD COLUMN age INT64", []
72+
)
73+
74+
def test_column_sql_not_null_field(self):
75+
"""
76+
Tests column sql for not null field
77+
"""
78+
with DatabaseSchemaEditor(self.connection) as schema_editor:
79+
schema_editor.execute = mock.MagicMock()
80+
new_field = IntegerField()
81+
new_field.set_attributes_from_name("num")
82+
sql, params = schema_editor.column_sql(Author, new_field)
83+
self.assertEqual(sql, "INT64 NOT NULL")
84+
85+
def test_column_sql_nullable_field(self):
86+
"""
87+
Tests column sql for nullable field
88+
"""
89+
with DatabaseSchemaEditor(self.connection) as schema_editor:
90+
schema_editor.execute = mock.MagicMock()
91+
new_field = IntegerField(null=True)
92+
new_field.set_attributes_from_name("num")
93+
sql, params = schema_editor.column_sql(Author, new_field)
94+
self.assertEqual(sql, "INT64")
95+
96+
def test_column_add_index(self):
97+
"""
98+
Tests column add index
99+
"""
100+
with DatabaseSchemaEditor(self.connection) as schema_editor:
101+
schema_editor.execute = mock.MagicMock()
102+
index = Index(name="test_author_index_num", fields=["num"])
103+
schema_editor.add_index(Author, index)
104+
name, args, kwargs = schema_editor.execute.mock_calls[0]
105+
106+
self.assertEqual(
107+
str(args[0]),
108+
"CREATE INDEX test_author_index_num ON tests_author (num)",
109+
)
110+
self.assertEqual(kwargs["params"], None)
111+
112+
def test_alter_field(self):
113+
"""
114+
Tests altering existing field in table
115+
"""
116+
with DatabaseSchemaEditor(self.connection) as schema_editor:
117+
schema_editor.execute = mock.MagicMock()
118+
old_field = IntegerField()
119+
old_field.set_attributes_from_name("num")
120+
new_field = IntegerField()
121+
new_field.set_attributes_from_name("author_num")
122+
schema_editor.alter_field(Author, old_field, new_field)
123+
124+
schema_editor.execute.assert_called_once_with(
125+
"ALTER TABLE tests_author RENAME COLUMN num TO author_num"
126+
)

0 commit comments

Comments
 (0)
0