mirror of
https://github.com/django/django.git
synced 2025-05-15 19:36:30 +00:00
[5.2.x] Used addCleanup() instead of try-finally blocks in inspectdb tests.
Backport of 2722cb61ccae84f593e6d2c28814e3c628743994 from main.
This commit is contained in:
parent
231eb7acc3
commit
af241dfa7d
@ -29,6 +29,17 @@ def special_table_only(table_name):
|
|||||||
return table_name.startswith("inspectdb_special")
|
return table_name.startswith("inspectdb_special")
|
||||||
|
|
||||||
|
|
||||||
|
def cursor_execute(*queries):
|
||||||
|
"""
|
||||||
|
Execute SQL queries using a new cursor on the default database connection.
|
||||||
|
"""
|
||||||
|
results = []
|
||||||
|
with connection.cursor() as cursor:
|
||||||
|
for q in queries:
|
||||||
|
results.append(cursor.execute(q))
|
||||||
|
return results
|
||||||
|
|
||||||
|
|
||||||
class InspectDBTestCase(TestCase):
|
class InspectDBTestCase(TestCase):
|
||||||
unique_re = re.compile(r".*unique_together = \((.+),\).*")
|
unique_re = re.compile(r".*unique_together = \((.+),\).*")
|
||||||
|
|
||||||
@ -415,29 +426,21 @@ class InspectDBTestCase(TestCase):
|
|||||||
@skipUnless(connection.vendor == "postgresql", "PostgreSQL specific SQL")
|
@skipUnless(connection.vendor == "postgresql", "PostgreSQL specific SQL")
|
||||||
def test_unsupported_unique_together(self):
|
def test_unsupported_unique_together(self):
|
||||||
"""Unsupported index types (COALESCE here) are skipped."""
|
"""Unsupported index types (COALESCE here) are skipped."""
|
||||||
with connection.cursor() as c:
|
cursor_execute(
|
||||||
c.execute(
|
|
||||||
"CREATE UNIQUE INDEX Findex ON %s "
|
"CREATE UNIQUE INDEX Findex ON %s "
|
||||||
"(id, people_unique_id, COALESCE(message_id, -1))"
|
"(id, people_unique_id, COALESCE(message_id, -1))"
|
||||||
% PeopleMoreData._meta.db_table
|
% PeopleMoreData._meta.db_table
|
||||||
)
|
)
|
||||||
try:
|
self.addCleanup(cursor_execute, "DROP INDEX Findex")
|
||||||
out = StringIO()
|
out = StringIO()
|
||||||
call_command(
|
call_command(
|
||||||
"inspectdb",
|
"inspectdb",
|
||||||
table_name_filter=lambda tn: tn.startswith(
|
table_name_filter=lambda tn: tn.startswith(PeopleMoreData._meta.db_table),
|
||||||
PeopleMoreData._meta.db_table
|
|
||||||
),
|
|
||||||
stdout=out,
|
stdout=out,
|
||||||
)
|
)
|
||||||
output = out.getvalue()
|
output = out.getvalue()
|
||||||
self.assertIn("# A unique constraint could not be introspected.", output)
|
self.assertIn("# A unique constraint could not be introspected.", output)
|
||||||
self.assertEqual(
|
self.assertEqual(self.unique_re.findall(output), ["('id', 'people_unique')"])
|
||||||
self.unique_re.findall(output), ["('id', 'people_unique')"]
|
|
||||||
)
|
|
||||||
finally:
|
|
||||||
with connection.cursor() as c:
|
|
||||||
c.execute("DROP INDEX Findex")
|
|
||||||
|
|
||||||
@skipUnless(
|
@skipUnless(
|
||||||
connection.vendor == "sqlite",
|
connection.vendor == "sqlite",
|
||||||
@ -492,15 +495,14 @@ class InspectDBTransactionalTests(TransactionTestCase):
|
|||||||
|
|
||||||
def test_include_views(self):
|
def test_include_views(self):
|
||||||
"""inspectdb --include-views creates models for database views."""
|
"""inspectdb --include-views creates models for database views."""
|
||||||
with connection.cursor() as cursor:
|
cursor_execute(
|
||||||
cursor.execute(
|
|
||||||
"CREATE VIEW inspectdb_people_view AS "
|
"CREATE VIEW inspectdb_people_view AS "
|
||||||
"SELECT id, name FROM inspectdb_people"
|
"SELECT id, name FROM inspectdb_people"
|
||||||
)
|
)
|
||||||
|
self.addCleanup(cursor_execute, "DROP VIEW inspectdb_people_view")
|
||||||
out = StringIO()
|
out = StringIO()
|
||||||
view_model = "class InspectdbPeopleView(models.Model):"
|
view_model = "class InspectdbPeopleView(models.Model):"
|
||||||
view_managed = "managed = False # Created from a view."
|
view_managed = "managed = False # Created from a view."
|
||||||
try:
|
|
||||||
call_command(
|
call_command(
|
||||||
"inspectdb",
|
"inspectdb",
|
||||||
table_name_filter=inspectdb_views_only,
|
table_name_filter=inspectdb_views_only,
|
||||||
@ -518,22 +520,20 @@ class InspectDBTransactionalTests(TransactionTestCase):
|
|||||||
with_views_output = out.getvalue()
|
with_views_output = out.getvalue()
|
||||||
self.assertIn(view_model, with_views_output)
|
self.assertIn(view_model, with_views_output)
|
||||||
self.assertIn(view_managed, with_views_output)
|
self.assertIn(view_managed, with_views_output)
|
||||||
finally:
|
|
||||||
with connection.cursor() as cursor:
|
|
||||||
cursor.execute("DROP VIEW inspectdb_people_view")
|
|
||||||
|
|
||||||
@skipUnlessDBFeature("can_introspect_materialized_views")
|
@skipUnlessDBFeature("can_introspect_materialized_views")
|
||||||
def test_include_materialized_views(self):
|
def test_include_materialized_views(self):
|
||||||
"""inspectdb --include-views creates models for materialized views."""
|
"""inspectdb --include-views creates models for materialized views."""
|
||||||
with connection.cursor() as cursor:
|
cursor_execute(
|
||||||
cursor.execute(
|
|
||||||
"CREATE MATERIALIZED VIEW inspectdb_people_materialized AS "
|
"CREATE MATERIALIZED VIEW inspectdb_people_materialized AS "
|
||||||
"SELECT id, name FROM inspectdb_people"
|
"SELECT id, name FROM inspectdb_people"
|
||||||
)
|
)
|
||||||
|
self.addCleanup(
|
||||||
|
cursor_execute, "DROP MATERIALIZED VIEW inspectdb_people_materialized"
|
||||||
|
)
|
||||||
out = StringIO()
|
out = StringIO()
|
||||||
view_model = "class InspectdbPeopleMaterialized(models.Model):"
|
view_model = "class InspectdbPeopleMaterialized(models.Model):"
|
||||||
view_managed = "managed = False # Created from a view."
|
view_managed = "managed = False # Created from a view."
|
||||||
try:
|
|
||||||
call_command(
|
call_command(
|
||||||
"inspectdb",
|
"inspectdb",
|
||||||
table_name_filter=inspectdb_views_only,
|
table_name_filter=inspectdb_views_only,
|
||||||
@ -551,35 +551,31 @@ class InspectDBTransactionalTests(TransactionTestCase):
|
|||||||
with_views_output = out.getvalue()
|
with_views_output = out.getvalue()
|
||||||
self.assertIn(view_model, with_views_output)
|
self.assertIn(view_model, with_views_output)
|
||||||
self.assertIn(view_managed, with_views_output)
|
self.assertIn(view_managed, with_views_output)
|
||||||
finally:
|
|
||||||
with connection.cursor() as cursor:
|
|
||||||
cursor.execute("DROP MATERIALIZED VIEW inspectdb_people_materialized")
|
|
||||||
|
|
||||||
@skipUnless(connection.vendor == "postgresql", "PostgreSQL specific SQL")
|
@skipUnless(connection.vendor == "postgresql", "PostgreSQL specific SQL")
|
||||||
def test_include_partitions(self):
|
def test_include_partitions(self):
|
||||||
"""inspectdb --include-partitions creates models for partitions."""
|
"""inspectdb --include-partitions creates models for partitions."""
|
||||||
with connection.cursor() as cursor:
|
cursor_execute(
|
||||||
cursor.execute(
|
"""
|
||||||
"""\
|
|
||||||
CREATE TABLE inspectdb_partition_parent (name text not null)
|
CREATE TABLE inspectdb_partition_parent (name text not null)
|
||||||
PARTITION BY LIST (left(upper(name), 1))
|
PARTITION BY LIST (left(upper(name), 1))
|
||||||
|
""",
|
||||||
"""
|
"""
|
||||||
)
|
|
||||||
cursor.execute(
|
|
||||||
"""\
|
|
||||||
CREATE TABLE inspectdb_partition_child
|
CREATE TABLE inspectdb_partition_child
|
||||||
PARTITION OF inspectdb_partition_parent
|
PARTITION OF inspectdb_partition_parent
|
||||||
FOR VALUES IN ('A', 'B', 'C')
|
FOR VALUES IN ('A', 'B', 'C')
|
||||||
"""
|
""",
|
||||||
|
)
|
||||||
|
self.addCleanup(
|
||||||
|
cursor_execute,
|
||||||
|
"DROP TABLE IF EXISTS inspectdb_partition_child",
|
||||||
|
"DROP TABLE IF EXISTS inspectdb_partition_parent",
|
||||||
)
|
)
|
||||||
out = StringIO()
|
out = StringIO()
|
||||||
partition_model_parent = "class InspectdbPartitionParent(models.Model):"
|
partition_model_parent = "class InspectdbPartitionParent(models.Model):"
|
||||||
partition_model_child = "class InspectdbPartitionChild(models.Model):"
|
partition_model_child = "class InspectdbPartitionChild(models.Model):"
|
||||||
partition_managed = "managed = False # Created from a partition."
|
partition_managed = "managed = False # Created from a partition."
|
||||||
try:
|
call_command("inspectdb", table_name_filter=inspectdb_tables_only, stdout=out)
|
||||||
call_command(
|
|
||||||
"inspectdb", table_name_filter=inspectdb_tables_only, stdout=out
|
|
||||||
)
|
|
||||||
no_partitions_output = out.getvalue()
|
no_partitions_output = out.getvalue()
|
||||||
self.assertIn(partition_model_parent, no_partitions_output)
|
self.assertIn(partition_model_parent, no_partitions_output)
|
||||||
self.assertNotIn(partition_model_child, no_partitions_output)
|
self.assertNotIn(partition_model_child, no_partitions_output)
|
||||||
@ -594,19 +590,12 @@ class InspectDBTransactionalTests(TransactionTestCase):
|
|||||||
self.assertIn(partition_model_parent, with_partitions_output)
|
self.assertIn(partition_model_parent, with_partitions_output)
|
||||||
self.assertIn(partition_model_child, with_partitions_output)
|
self.assertIn(partition_model_child, with_partitions_output)
|
||||||
self.assertIn(partition_managed, with_partitions_output)
|
self.assertIn(partition_managed, with_partitions_output)
|
||||||
finally:
|
|
||||||
with connection.cursor() as cursor:
|
|
||||||
cursor.execute("DROP TABLE IF EXISTS inspectdb_partition_child")
|
|
||||||
cursor.execute("DROP TABLE IF EXISTS inspectdb_partition_parent")
|
|
||||||
|
|
||||||
@skipUnless(connection.vendor == "postgresql", "PostgreSQL specific SQL")
|
@skipUnless(connection.vendor == "postgresql", "PostgreSQL specific SQL")
|
||||||
def test_foreign_data_wrapper(self):
|
def test_foreign_data_wrapper(self):
|
||||||
with connection.cursor() as cursor:
|
cursor_execute(
|
||||||
cursor.execute("CREATE EXTENSION IF NOT EXISTS file_fdw")
|
"CREATE EXTENSION IF NOT EXISTS file_fdw",
|
||||||
cursor.execute(
|
"CREATE SERVER inspectdb_server FOREIGN DATA WRAPPER file_fdw",
|
||||||
"CREATE SERVER inspectdb_server FOREIGN DATA WRAPPER file_fdw"
|
|
||||||
)
|
|
||||||
cursor.execute(
|
|
||||||
"""
|
"""
|
||||||
CREATE FOREIGN TABLE inspectdb_iris_foreign_table (
|
CREATE FOREIGN TABLE inspectdb_iris_foreign_table (
|
||||||
petal_length real,
|
petal_length real,
|
||||||
@ -617,12 +606,17 @@ class InspectDBTransactionalTests(TransactionTestCase):
|
|||||||
program 'echo 1,2,3,4',
|
program 'echo 1,2,3,4',
|
||||||
format 'csv'
|
format 'csv'
|
||||||
)
|
)
|
||||||
"""
|
""",
|
||||||
|
)
|
||||||
|
self.addCleanup(
|
||||||
|
cursor_execute,
|
||||||
|
"DROP FOREIGN TABLE IF EXISTS inspectdb_iris_foreign_table",
|
||||||
|
"DROP SERVER IF EXISTS inspectdb_server",
|
||||||
|
"DROP EXTENSION IF EXISTS file_fdw",
|
||||||
)
|
)
|
||||||
out = StringIO()
|
out = StringIO()
|
||||||
foreign_table_model = "class InspectdbIrisForeignTable(models.Model):"
|
foreign_table_model = "class InspectdbIrisForeignTable(models.Model):"
|
||||||
foreign_table_managed = "managed = False"
|
foreign_table_managed = "managed = False"
|
||||||
try:
|
|
||||||
call_command(
|
call_command(
|
||||||
"inspectdb",
|
"inspectdb",
|
||||||
table_name_filter=inspectdb_tables_only,
|
table_name_filter=inspectdb_tables_only,
|
||||||
@ -631,27 +625,17 @@ class InspectDBTransactionalTests(TransactionTestCase):
|
|||||||
output = out.getvalue()
|
output = out.getvalue()
|
||||||
self.assertIn(foreign_table_model, output)
|
self.assertIn(foreign_table_model, output)
|
||||||
self.assertIn(foreign_table_managed, output)
|
self.assertIn(foreign_table_managed, output)
|
||||||
finally:
|
|
||||||
with connection.cursor() as cursor:
|
|
||||||
cursor.execute(
|
|
||||||
"DROP FOREIGN TABLE IF EXISTS inspectdb_iris_foreign_table"
|
|
||||||
)
|
|
||||||
cursor.execute("DROP SERVER IF EXISTS inspectdb_server")
|
|
||||||
cursor.execute("DROP EXTENSION IF EXISTS file_fdw")
|
|
||||||
|
|
||||||
@skipUnlessDBFeature("create_test_table_with_composite_primary_key")
|
@skipUnlessDBFeature("create_test_table_with_composite_primary_key")
|
||||||
def test_composite_primary_key(self):
|
def test_composite_primary_key(self):
|
||||||
table_name = "test_table_composite_pk"
|
table_name = "test_table_composite_pk"
|
||||||
with connection.cursor() as cursor:
|
cursor_execute(connection.features.create_test_table_with_composite_primary_key)
|
||||||
cursor.execute(
|
self.addCleanup(cursor_execute, "DROP TABLE %s" % table_name)
|
||||||
connection.features.create_test_table_with_composite_primary_key
|
|
||||||
)
|
|
||||||
out = StringIO()
|
out = StringIO()
|
||||||
if connection.vendor == "sqlite":
|
if connection.vendor == "sqlite":
|
||||||
field_type = connection.features.introspected_field_types["AutoField"]
|
field_type = connection.features.introspected_field_types["AutoField"]
|
||||||
else:
|
else:
|
||||||
field_type = connection.features.introspected_field_types["IntegerField"]
|
field_type = connection.features.introspected_field_types["IntegerField"]
|
||||||
try:
|
|
||||||
call_command("inspectdb", table_name, stdout=out)
|
call_command("inspectdb", table_name, stdout=out)
|
||||||
output = out.getvalue()
|
output = out.getvalue()
|
||||||
self.assertIn(
|
self.assertIn(
|
||||||
@ -664,6 +648,3 @@ class InspectDBTransactionalTests(TransactionTestCase):
|
|||||||
% connection.features.introspected_field_types["IntegerField"],
|
% connection.features.introspected_field_types["IntegerField"],
|
||||||
output,
|
output,
|
||||||
)
|
)
|
||||||
finally:
|
|
||||||
with connection.cursor() as cursor:
|
|
||||||
cursor.execute("DROP TABLE %s" % table_name)
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user