| 1 |
from django.db import DatabaseError, connection |
| 2 |
from django.db.models import Index |
| 3 |
from django.test import TransactionTestCase, skipUnlessDBFeature |
| 4 |
|
| 5 |
from .models import ( |
| 6 |
Article, |
| 7 |
ArticleReporter, |
| 8 |
CheckConstraintModel, |
| 9 |
City, |
| 10 |
Comment, |
| 11 |
Country, |
| 12 |
DbCommentModel, |
| 13 |
District, |
| 14 |
Reporter, |
| 15 |
UniqueConstraintConditionModel, |
| 16 |
) |
| 17 |
|
| 18 |
|
| 19 |
class IntrospectionTests(TransactionTestCase): |
| 20 |
available_apps = ["introspection"] |
| 21 |
|
| 22 |
def test_table_names(self): |
| 23 |
tl = connection.introspection.table_names() |
| 24 |
self.assertEqual(tl, sorted(tl)) |
| 25 |
self.assertIn( |
| 26 |
Reporter._meta.db_table, |
| 27 |
tl, |
| 28 |
"'%s' isn't in table_list()." % Reporter._meta.db_table, |
| 29 |
) |
| 30 |
self.assertIn( |
| 31 |
Article._meta.db_table, |
| 32 |
tl, |
| 33 |
"'%s' isn't in table_list()." % Article._meta.db_table, |
| 34 |
) |
| 35 |
|
| 36 |
def test_django_table_names(self): |
| 37 |
with connection.cursor() as cursor: |
| 38 |
cursor.execute("CREATE TABLE django_ixn_test_table (id INTEGER);") |
| 39 |
tl = connection.introspection.django_table_names() |
| 40 |
cursor.execute("DROP TABLE django_ixn_test_table;") |
| 41 |
self.assertNotIn( |
| 42 |
"django_ixn_test_table", |
| 43 |
tl, |
| 44 |
"django_table_names() returned a non-Django table", |
| 45 |
) |
| 46 |
|
| 47 |
def test_django_table_names_retval_type(self): |
| 48 |
# Table name is a list #15216 |
| 49 |
tl = connection.introspection.django_table_names(only_existing=True) |
| 50 |
self.assertIs(type(tl), list) |
| 51 |
tl = connection.introspection.django_table_names(only_existing=False) |
| 52 |
self.assertIs(type(tl), list) |
| 53 |
|
| 54 |
def test_table_names_with_views(self): |
| 55 |
with connection.cursor() as cursor: |
| 56 |
try: |
| 57 |
cursor.execute( |
| 58 |
"CREATE VIEW introspection_article_view AS SELECT headline " |
| 59 |
"from introspection_article;" |
| 60 |
) |
| 61 |
except DatabaseError as e: |
| 62 |
if "insufficient privileges" in str(e): |
| 63 |
self.fail("The test user has no CREATE VIEW privileges") |
| 64 |
else: |
| 65 |
raise |
| 66 |
try: |
| 67 |
self.assertIn( |
| 68 |
"introspection_article_view", |
| 69 |
connection.introspection.table_names(include_views=True), |
| 70 |
) |
| 71 |
self.assertNotIn( |
| 72 |
"introspection_article_view", connection.introspection.table_names() |
| 73 |
) |
| 74 |
finally: |
| 75 |
with connection.cursor() as cursor: |
| 76 |
cursor.execute("DROP VIEW introspection_article_view") |
| 77 |
|
| 78 |
def test_unmanaged_through_model(self): |
| 79 |
tables = connection.introspection.django_table_names() |
| 80 |
self.assertNotIn(ArticleReporter._meta.db_table, tables) |
| 81 |
|
| 82 |
def test_installed_models(self): |
| 83 |
tables = [Article._meta.db_table, Reporter._meta.db_table] |
| 84 |
models = connection.introspection.installed_models(tables) |
| 85 |
self.assertEqual(models, {Article, Reporter}) |
| 86 |
|
| 87 |
def test_sequence_list(self): |
| 88 |
sequences = connection.introspection.sequence_list() |
| 89 |
reporter_seqs = [ |
| 90 |
seq for seq in sequences if seq["table"] == Reporter._meta.db_table |
| 91 |
] |
| 92 |
self.assertEqual( |
| 93 |
len(reporter_seqs), 1, "Reporter sequence not found in sequence_list()" |
| 94 |
) |
| 95 |
self.assertEqual(reporter_seqs[0]["column"], "id") |
| 96 |
|
| 97 |
def test_get_table_description_names(self): |
| 98 |
with connection.cursor() as cursor: |
| 99 |
desc = connection.introspection.get_table_description( |
| 100 |
cursor, Reporter._meta.db_table |
| 101 |
) |
| 102 |
self.assertEqual( |
| 103 |
[r[0] for r in desc], [f.column for f in Reporter._meta.fields] |
| 104 |
) |
| 105 |
|
| 106 |
def test_get_table_description_types(self): |
| 107 |
with connection.cursor() as cursor: |
| 108 |
desc = connection.introspection.get_table_description( |
| 109 |
cursor, Reporter._meta.db_table |
| 110 |
) |
| 111 |
self.assertEqual( |
| 112 |
[connection.introspection.get_field_type(r[1], r) for r in desc], |
| 113 |
[ |
| 114 |
connection.features.introspected_field_types[field] |
| 115 |
for field in ( |
| 116 |
"AutoField", |
| 117 |
"CharField", |
| 118 |
"CharField", |
| 119 |
"CharField", |
| 120 |
"BigIntegerField", |
| 121 |
"BinaryField", |
| 122 |
"SmallIntegerField", |
| 123 |
"DurationField", |
| 124 |
) |
| 125 |
], |
| 126 |
) |
| 127 |
|
| 128 |
def test_get_table_description_col_lengths(self): |
| 129 |
with connection.cursor() as cursor: |
| 130 |
desc = connection.introspection.get_table_description( |
| 131 |
cursor, Reporter._meta.db_table |
| 132 |
) |
| 133 |
self.assertEqual( |
| 134 |
[ |
| 135 |
r[2] |
| 136 |
for r in desc |
| 137 |
if connection.introspection.get_field_type(r[1], r) == "CharField" |
| 138 |
], |
| 139 |
[30, 30, 254], |
| 140 |
) |
| 141 |
|
| 142 |
def test_get_table_description_nullable(self): |
| 143 |
with connection.cursor() as cursor: |
| 144 |
desc = connection.introspection.get_table_description( |
| 145 |
cursor, Reporter._meta.db_table |
| 146 |
) |
| 147 |
nullable_by_backend = connection.features.interprets_empty_strings_as_nulls |
| 148 |
self.assertEqual( |
| 149 |
[r[6] for r in desc], |
| 150 |
[ |
| 151 |
False, |
| 152 |
nullable_by_backend, |
| 153 |
nullable_by_backend, |
| 154 |
nullable_by_backend, |
| 155 |
True, |
| 156 |
True, |
| 157 |
False, |
| 158 |
False, |
| 159 |
], |
| 160 |
) |
| 161 |
|
| 162 |
def test_bigautofield(self): |
| 163 |
with connection.cursor() as cursor: |
| 164 |
desc = connection.introspection.get_table_description( |
| 165 |
cursor, City._meta.db_table |
| 166 |
) |
| 167 |
self.assertIn( |
| 168 |
connection.features.introspected_field_types["BigAutoField"], |
| 169 |
[connection.introspection.get_field_type(r[1], r) for r in desc], |
| 170 |
) |
| 171 |
|
| 172 |
def test_smallautofield(self): |
| 173 |
with connection.cursor() as cursor: |
| 174 |
desc = connection.introspection.get_table_description( |
| 175 |
cursor, Country._meta.db_table |
| 176 |
) |
| 177 |
self.assertIn( |
| 178 |
connection.features.introspected_field_types["SmallAutoField"], |
| 179 |
[connection.introspection.get_field_type(r[1], r) for r in desc], |
| 180 |
) |
| 181 |
|
| 182 |
@skipUnlessDBFeature("supports_comments") |
| 183 |
def test_db_comments(self): |
| 184 |
with connection.cursor() as cursor: |
| 185 |
desc = connection.introspection.get_table_description( |
| 186 |
cursor, DbCommentModel._meta.db_table |
| 187 |
) |
| 188 |
table_list = connection.introspection.get_table_list(cursor) |
| 189 |
self.assertEqual( |
| 190 |
["'Name' column comment"], |
| 191 |
[field.comment for field in desc if field.name == "name"], |
| 192 |
) |
| 193 |
self.assertEqual( |
| 194 |
["Custom table comment"], |
| 195 |
[ |
| 196 |
table.comment |
| 197 |
for table in table_list |
| 198 |
if table.name == "introspection_dbcommentmodel" |
| 199 |
], |
| 200 |
) |
| 201 |
|
| 202 |
# Regression test for #9991 - 'real' types in postgres |
| 203 |
@skipUnlessDBFeature("has_real_datatype") |
| 204 |
def test_postgresql_real_type(self): |
| 205 |
with connection.cursor() as cursor: |
| 206 |
cursor.execute("CREATE TABLE django_ixn_real_test_table (number REAL);") |
| 207 |
desc = connection.introspection.get_table_description( |
| 208 |
cursor, "django_ixn_real_test_table" |
| 209 |
) |
| 210 |
cursor.execute("DROP TABLE django_ixn_real_test_table;") |
| 211 |
self.assertEqual( |
| 212 |
connection.introspection.get_field_type(desc[0][1], desc[0]), "FloatField" |
| 213 |
) |
| 214 |
|
| 215 |
@skipUnlessDBFeature("can_introspect_foreign_keys") |
| 216 |
def test_get_relations(self): |
| 217 |
with connection.cursor() as cursor: |
| 218 |
relations = connection.introspection.get_relations( |
| 219 |
cursor, Article._meta.db_table |
| 220 |
) |
| 221 |
|
| 222 |
# That's {field_name: (field_name_other_table, other_table)} |
| 223 |
expected_relations = { |
| 224 |
"reporter_id": ("id", Reporter._meta.db_table), |
| 225 |
"response_to_id": ("id", Article._meta.db_table), |
| 226 |
} |
| 227 |
self.assertEqual(relations, expected_relations) |
| 228 |
|
| 229 |
# Removing a field shouldn't disturb get_relations (#17785) |
| 230 |
body = Article._meta.get_field("body") |
| 231 |
with connection.schema_editor() as editor: |
| 232 |
editor.remove_field(Article, body) |
| 233 |
with connection.cursor() as cursor: |
| 234 |
relations = connection.introspection.get_relations( |
| 235 |
cursor, Article._meta.db_table |
| 236 |
) |
| 237 |
with connection.schema_editor() as editor: |
| 238 |
editor.add_field(Article, body) |
| 239 |
self.assertEqual(relations, expected_relations) |
| 240 |
|
| 241 |
def test_get_primary_key_column(self): |
| 242 |
with connection.cursor() as cursor: |
| 243 |
primary_key_column = connection.introspection.get_primary_key_column( |
| 244 |
cursor, Article._meta.db_table |
| 245 |
) |
| 246 |
pk_fk_column = connection.introspection.get_primary_key_column( |
| 247 |
cursor, District._meta.db_table |
| 248 |
) |
| 249 |
self.assertEqual(primary_key_column, "id") |
| 250 |
self.assertEqual(pk_fk_column, "city_id") |
| 251 |
|
| 252 |
def test_get_constraints_index_types(self): |
| 253 |
with connection.cursor() as cursor: |
| 254 |
constraints = connection.introspection.get_constraints( |
| 255 |
cursor, Article._meta.db_table |
| 256 |
) |
| 257 |
index = {} |
| 258 |
index2 = {} |
| 259 |
for val in constraints.values(): |
| 260 |
if val["columns"] == ["headline", "pub_date"]: |
| 261 |
index = val |
| 262 |
if val["columns"] == [ |
| 263 |
"headline", |
| 264 |
"response_to_id", |
| 265 |
"pub_date", |
| 266 |
"reporter_id", |
| 267 |
]: |
| 268 |
index2 = val |
| 269 |
self.assertEqual(index["type"], Index.suffix) |
| 270 |
self.assertEqual(index2["type"], Index.suffix) |
| 271 |
|
| 272 |
@skipUnlessDBFeature("supports_index_column_ordering") |
| 273 |
def test_get_constraints_indexes_orders(self): |
| 274 |
""" |
| 275 |
Indexes have the 'orders' key with a list of 'ASC'/'DESC' values. |
| 276 |
""" |
| 277 |
with connection.cursor() as cursor: |
| 278 |
constraints = connection.introspection.get_constraints( |
| 279 |
cursor, Article._meta.db_table |
| 280 |
) |
| 281 |
indexes_verified = 0 |
| 282 |
expected_columns = [ |
| 283 |
["headline", "pub_date"], |
| 284 |
["headline", "response_to_id", "pub_date", "reporter_id"], |
| 285 |
] |
| 286 |
if connection.features.indexes_foreign_keys: |
| 287 |
expected_columns += [ |
| 288 |
["reporter_id"], |
| 289 |
["response_to_id"], |
| 290 |
] |
| 291 |
for val in constraints.values(): |
| 292 |
if val["index"] and not (val["primary_key"] or val["unique"]): |
| 293 |
self.assertIn(val["columns"], expected_columns) |
| 294 |
self.assertEqual(val["orders"], ["ASC"] * len(val["columns"])) |
| 295 |
indexes_verified += 1 |
| 296 |
self.assertEqual(indexes_verified, len(expected_columns)) |
| 297 |
|
| 298 |
@skipUnlessDBFeature("supports_index_column_ordering", "supports_partial_indexes") |
| 299 |
def test_get_constraints_unique_indexes_orders(self): |
| 300 |
with connection.cursor() as cursor: |
| 301 |
constraints = connection.introspection.get_constraints( |
| 302 |
cursor, |
| 303 |
UniqueConstraintConditionModel._meta.db_table, |
| 304 |
) |
| 305 |
self.assertIn("cond_name_without_color_uniq", constraints) |
| 306 |
constraint = constraints["cond_name_without_color_uniq"] |
| 307 |
self.assertIs(constraint["unique"], True) |
| 308 |
self.assertEqual(constraint["columns"], ["name"]) |
| 309 |
self.assertEqual(constraint["orders"], ["ASC"]) |
| 310 |
|
| 311 |
def test_get_constraints(self): |
| 312 |
def assertDetails( |
| 313 |
details, |
| 314 |
cols, |
| 315 |
primary_key=False, |
| 316 |
unique=False, |
| 317 |
index=False, |
| 318 |
check=False, |
| 319 |
foreign_key=None, |
| 320 |
): |
| 321 |
# Different backends have different values for same constraints: |
| 322 |
# PRIMARY KEY UNIQUE CONSTRAINT UNIQUE INDEX |
| 323 |
# MySQL pk=1 uniq=1 idx=1 pk=0 uniq=1 idx=1 pk=0 uniq=1 idx=1 |
| 324 |
# PostgreSQL pk=1 uniq=1 idx=0 pk=0 uniq=1 idx=0 pk=0 uniq=1 idx=1 |
| 325 |
# SQLite pk=1 uniq=0 idx=0 pk=0 uniq=1 idx=0 pk=0 uniq=1 idx=1 |
| 326 |
if details["primary_key"]: |
| 327 |
details["unique"] = True |
| 328 |
if details["unique"]: |
| 329 |
details["index"] = False |
| 330 |
self.assertEqual(details["columns"], cols) |
| 331 |
self.assertEqual(details["primary_key"], primary_key) |
| 332 |
self.assertEqual(details["unique"], unique) |
| 333 |
self.assertEqual(details["index"], index) |
| 334 |
self.assertEqual(details["check"], check) |
| 335 |
self.assertEqual(details["foreign_key"], foreign_key) |
| 336 |
|
| 337 |
# Test custom constraints |
| 338 |
custom_constraints = { |
| 339 |
"article_email_pub_date_uniq", |
| 340 |
"email_pub_date_idx", |
| 341 |
} |
| 342 |
with connection.cursor() as cursor: |
| 343 |
constraints = connection.introspection.get_constraints( |
| 344 |
cursor, Comment._meta.db_table |
| 345 |
) |
| 346 |
if ( |
| 347 |
connection.features.supports_column_check_constraints |
| 348 |
and connection.features.can_introspect_check_constraints |
| 349 |
): |
| 350 |
constraints.update( |
| 351 |
connection.introspection.get_constraints( |
| 352 |
cursor, CheckConstraintModel._meta.db_table |
| 353 |
) |
| 354 |
) |
| 355 |
custom_constraints.add("up_votes_gte_0_check") |
| 356 |
assertDetails( |
| 357 |
constraints["up_votes_gte_0_check"], ["up_votes"], check=True |
| 358 |
) |
| 359 |
assertDetails( |
| 360 |
constraints["article_email_pub_date_uniq"], |
| 361 |
["article_id", "email", "pub_date"], |
| 362 |
unique=True, |
| 363 |
) |
| 364 |
assertDetails( |
| 365 |
constraints["email_pub_date_idx"], ["email", "pub_date"], index=True |
| 366 |
) |
| 367 |
# Test field constraints |
| 368 |
field_constraints = set() |
| 369 |
for name, details in constraints.items(): |
| 370 |
if name in custom_constraints: |
| 371 |
continue |
| 372 |
elif details["columns"] == ["up_votes"] and details["check"]: |
| 373 |
assertDetails(details, ["up_votes"], check=True) |
| 374 |
field_constraints.add(name) |
| 375 |
elif details["columns"] == ["voting_number"] and details["check"]: |
| 376 |
assertDetails(details, ["voting_number"], check=True) |
| 377 |
field_constraints.add(name) |
| 378 |
elif details["columns"] == ["ref"] and details["unique"]: |
| 379 |
assertDetails(details, ["ref"], unique=True) |
| 380 |
field_constraints.add(name) |
| 381 |
elif details["columns"] == ["voting_number"] and details["unique"]: |
| 382 |
assertDetails(details, ["voting_number"], unique=True) |
| 383 |
field_constraints.add(name) |
| 384 |
elif details["columns"] == ["article_id"] and details["index"]: |
| 385 |
assertDetails(details, ["article_id"], index=True) |
| 386 |
field_constraints.add(name) |
| 387 |
elif details["columns"] == ["id"] and details["primary_key"]: |
| 388 |
assertDetails(details, ["id"], primary_key=True, unique=True) |
| 389 |
field_constraints.add(name) |
| 390 |
elif details["columns"] == ["article_id"] and details["foreign_key"]: |
| 391 |
assertDetails( |
| 392 |
details, ["article_id"], foreign_key=("introspection_article", "id") |
| 393 |
) |
| 394 |
field_constraints.add(name) |
| 395 |
elif details["check"]: |
| 396 |
# Some databases (e.g. Oracle) include additional check |
| 397 |
# constraints. |
| 398 |
field_constraints.add(name) |
| 399 |
# All constraints are accounted for. |
| 400 |
self.assertEqual( |
| 401 |
constraints.keys() ^ (custom_constraints | field_constraints), set() |
| 402 |
) |