Debian Codegraph @

tests/inspectdb/tests.py

raw
1 import re
2 from io import StringIO
3 from unittest import mock, skipUnless
4
5 from django.core.management import call_command
6 from django.core.management.commands import inspectdb
7 from django.db import connection
8 from django.db.backends.base.introspection import TableInfo
9 from django.test import TestCase, TransactionTestCase, skipUnlessDBFeature
10
11 from .models import PeopleMoreData, test_collation
12
13
14 def inspectdb_tables_only(table_name):
15 """
16 Limit introspection to tables created for models of this app.
17 Some databases such as Oracle are extremely slow at introspection.
18 """
19 return table_name.startswith("inspectdb_")
20
21
22 def inspectdb_views_only(table_name):
23 return table_name.startswith("inspectdb_") and table_name.endswith(
24 ("_materialized", "_view")
25 )
26
27
28 def special_table_only(table_name):
29 return table_name.startswith("inspectdb_special")
30
31
32 def cursor_execute(*queries):
33 """
34 Execute SQL queries using a new cursor on the default database connection.
35 """
36 results = []
37 with connection.cursor() as cursor:
38 for q in queries:
39 results.append(cursor.execute(q))
40 return results
41
42
43 class InspectDBTestCase(TestCase):
44 unique_re = re.compile(r".*unique_together = \((.+),\).*")
45
46 def test_stealth_table_name_filter_option(self):
47 out = StringIO()
48 call_command("inspectdb", table_name_filter=inspectdb_tables_only, stdout=out)
49 error_message = (
50 "inspectdb has examined a table that should have been filtered out."
51 )
52 # contrib.contenttypes is one of the apps always installed when running
53 # the Django test suite, check that one of its tables hasn't been
54 # inspected
55 self.assertNotIn(
56 "class DjangoContentType(models.Model):", out.getvalue(), msg=error_message
57 )
58
59 def test_table_option(self):
60 """
61 inspectdb can inspect a subset of tables by passing the table names as
62 arguments.
63 """
64 out = StringIO()
65 call_command("inspectdb", "inspectdb_people", stdout=out)
66 output = out.getvalue()
67 self.assertIn("class InspectdbPeople(models.Model):", output)
68 self.assertNotIn("InspectdbPeopledata", output)
69
70 def make_field_type_asserter(self):
71 """
72 Call inspectdb and return a function to validate a field type in its
73 output.
74 """
75 out = StringIO()
76 call_command("inspectdb", "inspectdb_columntypes", stdout=out)
77 output = out.getvalue()
78
79 def assertFieldType(name, definition):
80 out_def = re.search(r"^\s*%s = (models.*)$" % name, output, re.MULTILINE)[1]
81 self.assertEqual(definition, out_def)
82
83 return assertFieldType
84
85 def test_field_types(self):
86 """Test introspection of various Django field types"""
87 assertFieldType = self.make_field_type_asserter()
88 introspected_field_types = connection.features.introspected_field_types
89 char_field_type = introspected_field_types["CharField"]
90 # Inspecting Oracle DB doesn't produce correct results (#19884):
91 # - it reports fields as blank=True when they aren't.
92 if (
93 not connection.features.interprets_empty_strings_as_nulls
94 and char_field_type == "CharField"
95 ):
96 assertFieldType("char_field", "models.CharField(max_length=10)")
97 assertFieldType(
98 "null_char_field",
99 "models.CharField(max_length=10, blank=True, null=True)",
100 )
101 assertFieldType("email_field", "models.CharField(max_length=254)")
102 assertFieldType("file_field", "models.CharField(max_length=100)")
103 assertFieldType("file_path_field", "models.CharField(max_length=100)")
104 assertFieldType("slug_field", "models.CharField(max_length=50)")
105 assertFieldType("text_field", "models.TextField()")
106 assertFieldType("url_field", "models.CharField(max_length=200)")
107 if char_field_type == "TextField":
108 assertFieldType("char_field", "models.TextField()")
109 assertFieldType(
110 "null_char_field", "models.TextField(blank=True, null=True)"
111 )
112 assertFieldType("email_field", "models.TextField()")
113 assertFieldType("file_field", "models.TextField()")
114 assertFieldType("file_path_field", "models.TextField()")
115 assertFieldType("slug_field", "models.TextField()")
116 assertFieldType("text_field", "models.TextField()")
117 assertFieldType("url_field", "models.TextField()")
118 assertFieldType("date_field", "models.DateField()")
119 assertFieldType("date_time_field", "models.DateTimeField()")
120 if introspected_field_types["GenericIPAddressField"] == "GenericIPAddressField":
121 assertFieldType("gen_ip_address_field", "models.GenericIPAddressField()")
122 elif not connection.features.interprets_empty_strings_as_nulls:
123 assertFieldType("gen_ip_address_field", "models.CharField(max_length=39)")
124 assertFieldType(
125 "time_field", "models.%s()" % introspected_field_types["TimeField"]
126 )
127 if connection.features.has_native_uuid_field:
128 assertFieldType("uuid_field", "models.UUIDField()")
129 elif not connection.features.interprets_empty_strings_as_nulls:
130 assertFieldType("uuid_field", "models.CharField(max_length=32)")
131
132 @skipUnlessDBFeature("can_introspect_json_field", "supports_json_field")
133 def test_json_field(self):
134 out = StringIO()
135 call_command("inspectdb", "inspectdb_jsonfieldcolumntype", stdout=out)
136 output = out.getvalue()
137 if not connection.features.interprets_empty_strings_as_nulls:
138 self.assertIn("json_field = models.JSONField()", output)
139 self.assertIn(
140 "null_json_field = models.JSONField(blank=True, null=True)", output
141 )
142
143 @skipUnlessDBFeature("supports_comments")
144 def test_db_comments(self):
145 out = StringIO()
146 call_command("inspectdb", "inspectdb_dbcomment", stdout=out)
147 output = out.getvalue()
148 integer_field_type = connection.features.introspected_field_types[
149 "IntegerField"
150 ]
151 self.assertIn(
152 f"rank = models.{integer_field_type}("
153 f"db_comment=\"'Rank' column comment\")",
154 output,
155 )
156 self.assertIn(
157 " db_table_comment = 'Custom table comment'",
158 output,
159 )
160
161 @skipUnlessDBFeature("supports_collation_on_charfield")
162 @skipUnless(test_collation, "Language collations are not supported.")
163 def test_char_field_db_collation(self):
164 out = StringIO()
165 call_command("inspectdb", "inspectdb_charfielddbcollation", stdout=out)
166 output = out.getvalue()
167 if not connection.features.interprets_empty_strings_as_nulls:
168 self.assertIn(
169 "char_field = models.CharField(max_length=10, "
170 "db_collation='%s')" % test_collation,
171 output,
172 )
173 else:
174 self.assertIn(
175 "char_field = models.CharField(max_length=10, "
176 "db_collation='%s', blank=True, null=True)" % test_collation,
177 output,
178 )
179
180 @skipUnlessDBFeature("supports_collation_on_textfield")
181 @skipUnless(test_collation, "Language collations are not supported.")
182 def test_text_field_db_collation(self):
183 out = StringIO()
184 call_command("inspectdb", "inspectdb_textfielddbcollation", stdout=out)
185 output = out.getvalue()
186 if not connection.features.interprets_empty_strings_as_nulls:
187 self.assertIn(
188 "text_field = models.TextField(db_collation='%s')" % test_collation,
189 output,
190 )
191 else:
192 self.assertIn(
193 "text_field = models.TextField(db_collation='%s, blank=True, "
194 "null=True)" % test_collation,
195 output,
196 )
197
198 @skipUnlessDBFeature("supports_unlimited_charfield")
199 def test_char_field_unlimited(self):
200 out = StringIO()
201 call_command("inspectdb", "inspectdb_charfieldunlimited", stdout=out)
202 output = out.getvalue()
203 self.assertIn("char_field = models.CharField()", output)
204
205 def test_number_field_types(self):
206 """Test introspection of various Django field types"""
207 assertFieldType = self.make_field_type_asserter()
208 introspected_field_types = connection.features.introspected_field_types
209
210 auto_field_type = connection.features.introspected_field_types["AutoField"]
211 if auto_field_type != "AutoField":
212 assertFieldType(
213 "id", "models.%s(primary_key=True) # AutoField?" % auto_field_type
214 )
215
216 assertFieldType(
217 "big_int_field", "models.%s()" % introspected_field_types["BigIntegerField"]
218 )
219
220 bool_field_type = introspected_field_types["BooleanField"]
221 assertFieldType("bool_field", "models.{}()".format(bool_field_type))
222 assertFieldType(
223 "null_bool_field",
224 "models.{}(blank=True, null=True)".format(bool_field_type),
225 )
226
227 if connection.vendor != "sqlite":
228 assertFieldType(
229 "decimal_field", "models.DecimalField(max_digits=6, decimal_places=1)"
230 )
231 else: # Guessed arguments on SQLite, see #5014
232 assertFieldType(
233 "decimal_field",
234 "models.DecimalField(max_digits=10, decimal_places=5) "
235 "# max_digits and decimal_places have been guessed, "
236 "as this database handles decimal fields as float",
237 )
238
239 assertFieldType("float_field", "models.FloatField()")
240 assertFieldType(
241 "int_field", "models.%s()" % introspected_field_types["IntegerField"]
242 )
243 assertFieldType(
244 "pos_int_field",
245 "models.%s()" % introspected_field_types["PositiveIntegerField"],
246 )
247 assertFieldType(
248 "pos_big_int_field",
249 "models.%s()" % introspected_field_types["PositiveBigIntegerField"],
250 )
251 assertFieldType(
252 "pos_small_int_field",
253 "models.%s()" % introspected_field_types["PositiveSmallIntegerField"],
254 )
255 assertFieldType(
256 "small_int_field",
257 "models.%s()" % introspected_field_types["SmallIntegerField"],
258 )
259
260 @skipUnlessDBFeature("can_introspect_foreign_keys")
261 def test_attribute_name_not_python_keyword(self):
262 out = StringIO()
263 call_command("inspectdb", table_name_filter=inspectdb_tables_only, stdout=out)
264 output = out.getvalue()
265 error_message = (
266 "inspectdb generated an attribute name which is a Python keyword"
267 )
268 # Recursive foreign keys should be set to 'self'
269 self.assertIn("parent = models.ForeignKey('self', models.DO_NOTHING)", output)
270 self.assertNotIn(
271 "from = models.ForeignKey(InspectdbPeople, models.DO_NOTHING)",
272 output,
273 msg=error_message,
274 )
275 # As InspectdbPeople model is defined after InspectdbMessage, it should
276 # be quoted.
277 self.assertIn(
278 "from_field = models.ForeignKey('InspectdbPeople', models.DO_NOTHING, "
279 "db_column='from_id')",
280 output,
281 )
282 self.assertIn(
283 "people_pk = models.OneToOneField(InspectdbPeople, models.DO_NOTHING, "
284 "primary_key=True)",
285 output,
286 )
287 self.assertIn(
288 "people_unique = models.OneToOneField(InspectdbPeople, models.DO_NOTHING)",
289 output,
290 )
291
292 @skipUnlessDBFeature("can_introspect_foreign_keys")
293 def test_foreign_key_to_field(self):
294 out = StringIO()
295 call_command("inspectdb", "inspectdb_foreignkeytofield", stdout=out)
296 self.assertIn(
297 "to_field_fk = models.ForeignKey('InspectdbPeoplemoredata', "
298 "models.DO_NOTHING, to_field='people_unique_id')",
299 out.getvalue(),
300 )
301
302 def test_digits_column_name_introspection(self):
303 """Introspection of column names consist/start with digits (#16536/#17676)"""
304 char_field_type = connection.features.introspected_field_types["CharField"]
305 out = StringIO()
306 call_command("inspectdb", "inspectdb_digitsincolumnname", stdout=out)
307 output = out.getvalue()
308 error_message = "inspectdb generated a model field name which is a number"
309 self.assertNotIn(
310 " 123 = models.%s" % char_field_type, output, msg=error_message
311 )
312 self.assertIn("number_123 = models.%s" % char_field_type, output)
313
314 error_message = (
315 "inspectdb generated a model field name which starts with a digit"
316 )
317 self.assertNotIn(
318 " 4extra = models.%s" % char_field_type, output, msg=error_message
319 )
320 self.assertIn("number_4extra = models.%s" % char_field_type, output)
321
322 self.assertNotIn(
323 " 45extra = models.%s" % char_field_type, output, msg=error_message
324 )
325 self.assertIn("number_45extra = models.%s" % char_field_type, output)
326
327 def test_special_column_name_introspection(self):
328 """
329 Introspection of column names containing special characters,
330 unsuitable for Python identifiers
331 """
332 out = StringIO()
333 call_command("inspectdb", table_name_filter=special_table_only, stdout=out)
334 output = out.getvalue()
335 base_name = connection.introspection.identifier_converter("Field")
336 integer_field_type = connection.features.introspected_field_types[
337 "IntegerField"
338 ]
339 self.assertIn("field = models.%s()" % integer_field_type, output)
340 self.assertIn(
341 "field_field = models.%s(db_column='%s_')"
342 % (integer_field_type, base_name),
343 output,
344 )
345 self.assertIn(
346 "field_field_0 = models.%s(db_column='%s__')"
347 % (integer_field_type, base_name),
348 output,
349 )
350 self.assertIn(
351 "field_field_1 = models.%s(db_column='__field')" % integer_field_type,
352 output,
353 )
354 self.assertIn(
355 "prc_x = models.{}(db_column='prc(%) x')".format(integer_field_type), output
356 )
357 self.assertIn("tamaƱo = models.%s()" % integer_field_type, output)
358
359 def test_table_name_introspection(self):
360 """
361 Introspection of table names containing special characters,
362 unsuitable for Python identifiers
363 """
364 out = StringIO()
365 call_command("inspectdb", table_name_filter=special_table_only, stdout=out)
366 output = out.getvalue()
367 self.assertIn("class InspectdbSpecialTableName(models.Model):", output)
368
369 def test_custom_normalize_table_name(self):
370 def pascal_case_table_only(table_name):
371 return table_name.startswith("inspectdb_pascal")
372
373 class MyCommand(inspectdb.Command):
374 def normalize_table_name(self, table_name):
375 normalized_name = table_name.split(".")[1]
376 if connection.features.ignores_table_name_case:
377 normalized_name = normalized_name.lower()
378 return normalized_name
379
380 out = StringIO()
381 call_command(MyCommand(), table_name_filter=pascal_case_table_only, stdout=out)
382 if connection.features.ignores_table_name_case:
383 expected_model_name = "pascalcase"
384 else:
385 expected_model_name = "PascalCase"
386 self.assertIn(f"class {expected_model_name}(models.Model):", out.getvalue())
387
388 @skipUnlessDBFeature("supports_expression_indexes")
389 def test_table_with_func_unique_constraint(self):
390 out = StringIO()
391 call_command("inspectdb", "inspectdb_funcuniqueconstraint", stdout=out)
392 output = out.getvalue()
393 self.assertIn("class InspectdbFuncuniqueconstraint(models.Model):", output)
394
395 def test_managed_models(self):
396 """
397 By default the command generates models with `Meta.managed = False`.
398 """
399 out = StringIO()
400 call_command("inspectdb", "inspectdb_columntypes", stdout=out)
401 output = out.getvalue()
402 self.longMessage = False
403 self.assertIn(
404 " managed = False",
405 output,
406 msg="inspectdb should generate unmanaged models.",
407 )
408
409 def test_unique_together_meta(self):
410 out = StringIO()
411 call_command("inspectdb", "inspectdb_uniquetogether", stdout=out)
412 output = out.getvalue()
413 self.assertIn(" unique_together = (('", output)
414 unique_together_match = self.unique_re.findall(output)
415 # There should be one unique_together tuple.
416 self.assertEqual(len(unique_together_match), 1)
417 fields = unique_together_match[0]
418 # Fields with db_column = field name.
419 self.assertIn("('field1', 'field2')", fields)
420 # Fields from columns whose names are Python keywords.
421 self.assertIn("('field1', 'field2')", fields)
422 # Fields whose names normalize to the same Python field name and hence
423 # are given an integer suffix.
424 self.assertIn("('non_unique_column', 'non_unique_column_0')", fields)
425
426 @skipUnless(connection.vendor == "postgresql", "PostgreSQL specific SQL")
427 def test_unsupported_unique_together(self):
428 """Unsupported index types (COALESCE here) are skipped."""
429 cursor_execute(
430 "CREATE UNIQUE INDEX Findex ON %s "
431 "(id, people_unique_id, COALESCE(message_id, -1))"
432 % PeopleMoreData._meta.db_table
433 )
434 self.addCleanup(cursor_execute, "DROP INDEX Findex")
435 out = StringIO()
436 call_command(
437 "inspectdb",
438 table_name_filter=lambda tn: tn.startswith(PeopleMoreData._meta.db_table),
439 stdout=out,
440 )
441 output = out.getvalue()
442 self.assertIn("# A unique constraint could not be introspected.", output)
443 self.assertEqual(self.unique_re.findall(output), ["('id', 'people_unique')"])
444
445 @skipUnless(
446 connection.vendor == "sqlite",
447 "Only patched sqlite's DatabaseIntrospection.data_types_reverse for this test",
448 )
449 def test_custom_fields(self):
450 """
451 Introspection of columns with a custom field (#21090)
452 """
453 out = StringIO()
454 with mock.patch(
455 "django.db.connection.introspection.data_types_reverse."
456 "base_data_types_reverse",
457 {
458 "text": "myfields.TextField",
459 "bigint": "BigIntegerField",
460 },
461 ):
462 call_command("inspectdb", "inspectdb_columntypes", stdout=out)
463 output = out.getvalue()
464 self.assertIn("text_field = myfields.TextField()", output)
465 self.assertIn("big_int_field = models.BigIntegerField()", output)
466
467 def test_introspection_errors(self):
468 """
469 Introspection errors should not crash the command, and the error should
470 be visible in the output.
471 """
472 out = StringIO()
473 with mock.patch(
474 "django.db.connection.introspection.get_table_list",
475 return_value=[TableInfo(name="nonexistent", type="t")],
476 ):
477 call_command("inspectdb", stdout=out)
478 output = out.getvalue()
479 self.assertIn("# Unable to inspect table 'nonexistent'", output)
480 # The error message depends on the backend
481 self.assertIn("# The error was:", output)
482
483 def test_same_relations(self):
484 out = StringIO()
485 call_command("inspectdb", "inspectdb_message", stdout=out)
486 self.assertIn(
487 "author = models.ForeignKey('InspectdbPeople', models.DO_NOTHING, "
488 "related_name='inspectdbmessage_author_set')",
489 out.getvalue(),
490 )
491
492
493 class InspectDBTransactionalTests(TransactionTestCase):
494 available_apps = ["inspectdb"]
495
496 def test_include_views(self):
497 """inspectdb --include-views creates models for database views."""
498 cursor_execute(
499 "CREATE VIEW inspectdb_people_view AS "
500 "SELECT id, name FROM inspectdb_people"
501 )
502 self.addCleanup(cursor_execute, "DROP VIEW inspectdb_people_view")
503 out = StringIO()
504 view_model = "class InspectdbPeopleView(models.Model):"
505 view_managed = "managed = False # Created from a view."
506 call_command(
507 "inspectdb",
508 table_name_filter=inspectdb_views_only,
509 stdout=out,
510 )
511 no_views_output = out.getvalue()
512 self.assertNotIn(view_model, no_views_output)
513 self.assertNotIn(view_managed, no_views_output)
514 call_command(
515 "inspectdb",
516 table_name_filter=inspectdb_views_only,
517 include_views=True,
518 stdout=out,
519 )
520 with_views_output = out.getvalue()
521 self.assertIn(view_model, with_views_output)
522 self.assertIn(view_managed, with_views_output)
523
524 @skipUnlessDBFeature("can_introspect_materialized_views")
525 def test_include_materialized_views(self):
526 """inspectdb --include-views creates models for materialized views."""
527 cursor_execute(
528 "CREATE MATERIALIZED VIEW inspectdb_people_materialized AS "
529 "SELECT id, name FROM inspectdb_people"
530 )
531 self.addCleanup(
532 cursor_execute, "DROP MATERIALIZED VIEW inspectdb_people_materialized"
533 )
534 out = StringIO()
535 view_model = "class InspectdbPeopleMaterialized(models.Model):"
536 view_managed = "managed = False # Created from a view."
537 call_command(
538 "inspectdb",
539 table_name_filter=inspectdb_views_only,
540 stdout=out,
541 )
542 no_views_output = out.getvalue()
543 self.assertNotIn(view_model, no_views_output)
544 self.assertNotIn(view_managed, no_views_output)
545 call_command(
546 "inspectdb",
547 table_name_filter=inspectdb_views_only,
548 include_views=True,
549 stdout=out,
550 )
551 with_views_output = out.getvalue()
552 self.assertIn(view_model, with_views_output)
553 self.assertIn(view_managed, with_views_output)
554
555 @skipUnless(connection.vendor == "postgresql", "PostgreSQL specific SQL")
556 def test_include_partitions(self):
557 """inspectdb --include-partitions creates models for partitions."""
558 cursor_execute(
559 """
560 CREATE TABLE inspectdb_partition_parent (name text not null)
561 PARTITION BY LIST (left(upper(name), 1))
562 """,
563 """
564 CREATE TABLE inspectdb_partition_child
565 PARTITION OF inspectdb_partition_parent
566 FOR VALUES IN ('A', 'B', 'C')
567 """,
568 )
569 self.addCleanup(
570 cursor_execute,
571 "DROP TABLE IF EXISTS inspectdb_partition_child",
572 "DROP TABLE IF EXISTS inspectdb_partition_parent",
573 )
574 out = StringIO()
575 partition_model_parent = "class InspectdbPartitionParent(models.Model):"
576 partition_model_child = "class InspectdbPartitionChild(models.Model):"
577 partition_managed = "managed = False # Created from a partition."
578 call_command("inspectdb", table_name_filter=inspectdb_tables_only, stdout=out)
579 no_partitions_output = out.getvalue()
580 self.assertIn(partition_model_parent, no_partitions_output)
581 self.assertNotIn(partition_model_child, no_partitions_output)
582 self.assertNotIn(partition_managed, no_partitions_output)
583 call_command(
584 "inspectdb",
585 table_name_filter=inspectdb_tables_only,
586 include_partitions=True,
587 stdout=out,
588 )
589 with_partitions_output = out.getvalue()
590 self.assertIn(partition_model_parent, with_partitions_output)
591 self.assertIn(partition_model_child, with_partitions_output)
592 self.assertIn(partition_managed, with_partitions_output)
593
594 @skipUnless(connection.vendor == "postgresql", "PostgreSQL specific SQL")
595 def test_foreign_data_wrapper(self):
596 cursor_execute(
597 "CREATE EXTENSION IF NOT EXISTS file_fdw",
598 "CREATE SERVER inspectdb_server FOREIGN DATA WRAPPER file_fdw",
599 """
600 CREATE FOREIGN TABLE inspectdb_iris_foreign_table (
601 petal_length real,
602 petal_width real,
603 sepal_length real,
604 sepal_width real
605 ) SERVER inspectdb_server OPTIONS (
606 program 'echo 1,2,3,4',
607 format 'csv'
608 )
609 """,
610 )
611 self.addCleanup(
612 cursor_execute,
613 "DROP FOREIGN TABLE IF EXISTS inspectdb_iris_foreign_table",
614 "DROP SERVER IF EXISTS inspectdb_server",
615 "DROP EXTENSION IF EXISTS file_fdw",
616 )
617 out = StringIO()
618 foreign_table_model = "class InspectdbIrisForeignTable(models.Model):"
619 foreign_table_managed = "managed = False"
620 call_command(
621 "inspectdb",
622 table_name_filter=inspectdb_tables_only,
623 stdout=out,
624 )
625 output = out.getvalue()
626 self.assertIn(foreign_table_model, output)
627 self.assertIn(foreign_table_managed, output)
628
629 def test_composite_primary_key(self):
630 out = StringIO()
631 field_type = connection.features.introspected_field_types["IntegerField"]
632 call_command("inspectdb", "inspectdb_compositepkmodel", stdout=out)
633 output = out.getvalue()
634 self.assertIn(
635 "pk = models.CompositePrimaryKey('column_1', 'column_2')",
636 output,
637 )
638 self.assertIn(f"column_1 = models.{field_type}()", output)
639 self.assertIn(f"column_2 = models.{field_type}()", output)
640
641 def test_composite_primary_key_not_unique_together(self):
642 out = StringIO()
643 call_command("inspectdb", "inspectdb_compositepkmodel", stdout=out)
644 self.assertNotIn("unique_together", out.getvalue())