|
| 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 | +from django.db.backends.base.introspection import TableInfo, FieldInfo |
| 8 | +from django_spanner.introspection import DatabaseIntrospection |
| 9 | +from google.cloud.spanner_dbapi._helpers import ColumnInfo |
| 10 | +from google.cloud.spanner_dbapi.cursor import ColumnDetails |
| 11 | +from google.cloud.spanner_v1 import TypeCode |
| 12 | +from tests.unit.django_spanner.simple_test import SpannerSimpleTestClass |
| 13 | +from unittest import mock |
| 14 | + |
| 15 | + |
| 16 | +class TestUtils(SpannerSimpleTestClass): |
| 17 | + def test_get_field_type_boolean(self): |
| 18 | + """ |
| 19 | + Tests get field type for boolean field. |
| 20 | + """ |
| 21 | + db_introspection = DatabaseIntrospection(self.connection) |
| 22 | + self.assertEqual( |
| 23 | + db_introspection.get_field_type(TypeCode.BOOL, description=None), |
| 24 | + "BooleanField", |
| 25 | + ) |
| 26 | + |
| 27 | + def test_get_field_type_text_field(self): |
| 28 | + """ |
| 29 | + Tests get field type for text field. |
| 30 | + """ |
| 31 | + db_introspection = DatabaseIntrospection(self.connection) |
| 32 | + self.assertEqual( |
| 33 | + db_introspection.get_field_type( |
| 34 | + TypeCode.STRING, |
| 35 | + description=ColumnInfo( |
| 36 | + name="name", |
| 37 | + type_code=TypeCode.STRING, |
| 38 | + internal_size="MAX", |
| 39 | + ), |
| 40 | + ), |
| 41 | + "TextField", |
| 42 | + ) |
| 43 | + |
| 44 | + def test_get_table_list(self): |
| 45 | + """ |
| 46 | + Tests get table list method. |
| 47 | + """ |
| 48 | + db_introspection = DatabaseIntrospection(self.connection) |
| 49 | + cursor = mock.MagicMock() |
| 50 | + |
| 51 | + def list_tables(*args, **kwargs): |
| 52 | + return [["Table_1"], ["Table_2"]] |
| 53 | + |
| 54 | + cursor.list_tables = list_tables |
| 55 | + table_list = db_introspection.get_table_list(cursor=cursor) |
| 56 | + self.assertEqual( |
| 57 | + table_list, |
| 58 | + [ |
| 59 | + TableInfo(name="Table_1", type="t"), |
| 60 | + TableInfo(name="Table_2", type="t"), |
| 61 | + ], |
| 62 | + ) |
| 63 | + |
| 64 | + def test_get_table_description(self): |
| 65 | + """ |
| 66 | + Tests get table description method. |
| 67 | + """ |
| 68 | + db_introspection = DatabaseIntrospection(self.connection) |
| 69 | + cursor = mock.MagicMock() |
| 70 | + |
| 71 | + def description(*args, **kwargs): |
| 72 | + return [["name", TypeCode.STRING], ["age", TypeCode.INT64]] |
| 73 | + |
| 74 | + def get_table_column_schema(*args, **kwargs): |
| 75 | + column_details = {} |
| 76 | + column_details["name"] = ColumnDetails( |
| 77 | + null_ok=False, spanner_type="STRING(10)" |
| 78 | + ) |
| 79 | + column_details["age"] = ColumnDetails( |
| 80 | + null_ok=True, spanner_type="INT64" |
| 81 | + ) |
| 82 | + return column_details |
| 83 | + |
| 84 | + cursor.get_table_column_schema = get_table_column_schema |
| 85 | + cursor.description = description() |
| 86 | + table_description = db_introspection.get_table_description( |
| 87 | + cursor=cursor, table_name="Table_1" |
| 88 | + ) |
| 89 | + self.assertEqual( |
| 90 | + table_description, |
| 91 | + [ |
| 92 | + FieldInfo( |
| 93 | + name="name", |
| 94 | + type_code=TypeCode.STRING, |
| 95 | + display_size=None, |
| 96 | + internal_size=10, |
| 97 | + precision=None, |
| 98 | + scale=None, |
| 99 | + null_ok=False, |
| 100 | + default=None, |
| 101 | + ), |
| 102 | + FieldInfo( |
| 103 | + name="age", |
| 104 | + type_code=TypeCode.INT64, |
| 105 | + display_size=None, |
| 106 | + internal_size=None, |
| 107 | + precision=None, |
| 108 | + scale=None, |
| 109 | + null_ok=True, |
| 110 | + default=None, |
| 111 | + ), |
| 112 | + ], |
| 113 | + ) |
| 114 | + |
| 115 | + def test_get_primary_key_column(self): |
| 116 | + """ |
| 117 | + Tests get primary column of table. |
| 118 | + """ |
| 119 | + db_introspection = DatabaseIntrospection(self.connection) |
| 120 | + cursor = mock.MagicMock() |
| 121 | + |
| 122 | + def run_sql_in_snapshot(*args, **kwargs): |
| 123 | + return [["PK_column"]] |
| 124 | + |
| 125 | + cursor.run_sql_in_snapshot = run_sql_in_snapshot |
| 126 | + primary_key = db_introspection.get_primary_key_column( |
| 127 | + cursor=cursor, table_name="Table_1" |
| 128 | + ) |
| 129 | + self.assertEqual( |
| 130 | + primary_key, "PK_column", |
| 131 | + ) |
| 132 | + |
| 133 | + def test_get_primary_key_column_returns_none(self): |
| 134 | + """ |
| 135 | + Tests get primary column of table when key is None. |
| 136 | + """ |
| 137 | + db_introspection = DatabaseIntrospection(self.connection) |
| 138 | + cursor = mock.MagicMock() |
| 139 | + |
| 140 | + def run_sql_in_snapshot(*args, **kwargs): |
| 141 | + return None |
| 142 | + |
| 143 | + cursor.run_sql_in_snapshot = run_sql_in_snapshot |
| 144 | + primary_key = db_introspection.get_primary_key_column( |
| 145 | + cursor=cursor, table_name="Table_1" |
| 146 | + ) |
| 147 | + self.assertIsNone(primary_key,) |
| 148 | + |
| 149 | + def test_get_constraints(self): |
| 150 | + """ |
| 151 | + Tests get constraints applied on table columns. |
| 152 | + """ |
| 153 | + db_introspection = DatabaseIntrospection(self.connection) |
| 154 | + cursor = mock.MagicMock() |
| 155 | + |
| 156 | + def run_sql_in_snapshot(*args, **kwargs): |
| 157 | + # returns dummy data for 'CONSTRAINT_NAME, COLUMN_NAME' query. |
| 158 | + if "CONSTRAINT_NAME, COLUMN_NAME" in args[0]: |
| 159 | + return [["pk_constraint", "id"], ["name_constraint", "name"]] |
| 160 | + # returns dummy data for 'CONSTRAINT_NAME, CONSTRAINT_TYPE' query. |
| 161 | + if "CONSTRAINT_NAME, CONSTRAINT_TYPE" in args[0]: |
| 162 | + return [ |
| 163 | + ["pk_constraint", "PRIMARY KEY"], |
| 164 | + ["FOREIGN KEY", "dept_id"], |
| 165 | + ] |
| 166 | + # returns dummy data for 'INFORMATION_SCHEMA.INDEXES' table query. |
| 167 | + return [["pk_index", "id", "ASCENDING", "PRIMARY_KEY", True]] |
| 168 | + |
| 169 | + cursor.run_sql_in_snapshot = run_sql_in_snapshot |
| 170 | + constraints = db_introspection.get_constraints( |
| 171 | + cursor=cursor, table_name="Table_1" |
| 172 | + ) |
| 173 | + |
| 174 | + self.assertEqual( |
| 175 | + constraints, |
| 176 | + { |
| 177 | + "pk_constraint": { |
| 178 | + "check": False, |
| 179 | + "columns": ["id"], |
| 180 | + "foreign_key": None, |
| 181 | + "index": False, |
| 182 | + "orders": [], |
| 183 | + "primary_key": True, |
| 184 | + "type": None, |
| 185 | + "unique": True, |
| 186 | + }, |
| 187 | + "name_constraint": { |
| 188 | + "check": False, |
| 189 | + "columns": ["name"], |
| 190 | + "foreign_key": None, |
| 191 | + "index": False, |
| 192 | + "orders": [], |
| 193 | + "primary_key": False, |
| 194 | + "type": None, |
| 195 | + "unique": False, |
| 196 | + }, |
| 197 | + "FOREIGN KEY": { |
| 198 | + "check": False, |
| 199 | + "columns": [], |
| 200 | + "foreign_key": None, |
| 201 | + "index": False, |
| 202 | + "orders": [], |
| 203 | + "primary_key": False, |
| 204 | + "type": None, |
| 205 | + "unique": False, |
| 206 | + }, |
| 207 | + "pk_index": { |
| 208 | + "check": False, |
| 209 | + "columns": ["id"], |
| 210 | + "foreign_key": None, |
| 211 | + "index": True, |
| 212 | + "orders": ["ASCENDING"], |
| 213 | + "primary_key": True, |
| 214 | + "type": "PRIMARY_KEY", |
| 215 | + "unique": True, |
| 216 | + }, |
| 217 | + }, |
| 218 | + ) |
0 commit comments