1
0
mirror of https://github.com/django/django.git synced 2025-05-04 22:17:34 +00:00

Refs #33509 -- Made sqlmigrate tests stricter and improved isolation.

This commit is contained in:
Adam Johnson 2022-04-20 21:04:36 +01:00 committed by Mariusz Felisiak
parent cd4da34fc1
commit f15f7d395c

View File

@ -828,46 +828,56 @@ class MigrateTests(MigrationTestBase):
""" """
out = io.StringIO() out = io.StringIO()
call_command("sqlmigrate", "migrations", "0001", stdout=out) call_command("sqlmigrate", "migrations", "0001", stdout=out)
output = out.getvalue().lower()
index_tx_start = output.find(connection.ops.start_transaction_sql().lower()) lines = out.getvalue().splitlines()
index_op_desc_author = output.find("-- create model author")
index_create_table = output.find("create table")
index_op_desc_tribble = output.find("-- create model tribble")
index_op_desc_unique_together = output.find("-- alter unique_together")
index_tx_end = output.find(connection.ops.end_transaction_sql().lower())
if connection.features.can_rollback_ddl: if connection.features.can_rollback_ddl:
self.assertGreater(index_tx_start, -1, "Transaction start not found") self.assertEqual(lines[0], connection.ops.start_transaction_sql())
self.assertGreater( self.assertEqual(lines[-1], connection.ops.end_transaction_sql())
index_tx_end, lines = lines[1:-1]
index_op_desc_unique_together,
"Transaction end not found or found before operation description "
"(unique_together)",
)
self.assertGreater( self.assertEqual(
index_op_desc_author, lines[:3],
index_tx_start, [
"Operation description (author) not found or found before transaction " "--",
"start", "-- Create model Author",
"--",
],
) )
self.assertGreater( self.assertIn(
index_create_table, "create table %s" % connection.ops.quote_name("migrations_author").lower(),
index_op_desc_author, lines[3].lower(),
"CREATE TABLE not found or found before operation description (author)",
) )
self.assertGreater( pos = lines.index("--", 3)
index_op_desc_tribble, self.assertEqual(
index_create_table, lines[pos : pos + 3],
"Operation description (tribble) not found or found before CREATE TABLE " [
"(author)", "--",
"-- Create model Tribble",
"--",
],
) )
self.assertGreater( self.assertIn(
index_op_desc_unique_together, "create table %s" % connection.ops.quote_name("migrations_tribble").lower(),
index_op_desc_tribble, lines[pos + 3].lower(),
"Operation description (unique_together) not found or found before " )
"operation description (tribble)", pos = lines.index("--", pos + 3)
self.assertEqual(
lines[pos : pos + 3],
[
"--",
"-- Add field bool to tribble",
"--",
],
)
pos = lines.index("--", pos + 3)
self.assertEqual(
lines[pos : pos + 3],
[
"--",
"-- Alter unique_together for author (1 constraint(s))",
"--",
],
) )
@override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations"}) @override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations"})
@ -880,48 +890,69 @@ class MigrateTests(MigrationTestBase):
out = io.StringIO() out = io.StringIO()
call_command("sqlmigrate", "migrations", "0001", stdout=out, backwards=True) call_command("sqlmigrate", "migrations", "0001", stdout=out, backwards=True)
output = out.getvalue().lower()
index_tx_start = output.find(connection.ops.start_transaction_sql().lower())
index_op_desc_unique_together = output.find("-- alter unique_together")
index_op_desc_tribble = output.find("-- create model tribble")
index_op_desc_author = output.find("-- create model author")
index_drop_table = output.rfind("drop table")
index_tx_end = output.find(connection.ops.end_transaction_sql().lower())
lines = out.getvalue().splitlines()
try:
if connection.features.can_rollback_ddl: if connection.features.can_rollback_ddl:
self.assertGreater(index_tx_start, -1, "Transaction start not found") self.assertEqual(lines[0], connection.ops.start_transaction_sql())
self.assertGreater( self.assertEqual(lines[-1], connection.ops.end_transaction_sql())
index_tx_end, lines = lines[1:-1]
index_op_desc_unique_together,
"Transaction end not found or found before DROP TABLE",
)
self.assertGreater(
index_op_desc_unique_together,
index_tx_start,
"Operation description (unique_together) not found or found before "
"transaction start",
)
self.assertGreater(
index_op_desc_tribble,
index_op_desc_unique_together,
"Operation description (tribble) not found or found before operation "
"description (unique_together)",
)
self.assertGreater(
index_op_desc_author,
index_op_desc_tribble,
"Operation description (author) not found or found before operation "
"description (tribble)",
)
self.assertGreater( self.assertEqual(
index_drop_table, lines[:3],
index_op_desc_author, [
"DROP TABLE not found or found before operation description (author)", "--",
"-- Alter unique_together for author (1 constraint(s))",
"--",
],
) )
pos = lines.index("--", 3)
# Cleanup by unmigrating everything self.assertEqual(
lines[pos : pos + 3],
[
"--",
"-- Add field bool to tribble",
"--",
],
)
pos = lines.index("--", pos + 3)
self.assertEqual(
lines[pos : pos + 3],
[
"--",
"-- Create model Tribble",
"--",
],
)
next_pos = lines.index("--", pos + 3)
drop_table_sql = (
"drop table %s"
% connection.ops.quote_name("migrations_tribble").lower()
)
for line in lines[pos + 3 : next_pos]:
if drop_table_sql in line.lower():
break
else:
self.fail("DROP TABLE (tribble) not found.")
pos = next_pos
self.assertEqual(
lines[pos : pos + 3],
[
"--",
"-- Create model Author",
"--",
],
)
drop_table_sql = (
"drop table %s" % connection.ops.quote_name("migrations_author").lower()
)
for line in lines[pos + 3 :]:
if drop_table_sql in line.lower():
break
else:
self.fail("DROP TABLE (author) not found.")
finally:
# Unmigrate everything.
call_command("migrate", "migrations", "zero", verbosity=0) call_command("migrate", "migrations", "zero", verbosity=0)
@override_settings( @override_settings(