summaryrefslogtreecommitdiff
path: root/tests/migrations/test_commands.py
diff options
context:
space:
mode:
authorAdam Johnson <me@adamj.eu>2022-04-20 21:04:36 +0100
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2022-04-21 12:21:46 +0200
commitf15f7d395c99e3c1194c37eaa4f5958392c1ee07 (patch)
treefad0358a6945d9a8d34221a20062d8a7094aaa71 /tests/migrations/test_commands.py
parentcd4da34fc1f1df08f593e461b2f670bfd61d0d2f (diff)
Refs #33509 -- Made sqlmigrate tests stricter and improved isolation.
Diffstat (limited to 'tests/migrations/test_commands.py')
-rw-r--r--tests/migrations/test_commands.py177
1 files changed, 104 insertions, 73 deletions
diff --git a/tests/migrations/test_commands.py b/tests/migrations/test_commands.py
index 30136aab6b..7ea8267247 100644
--- a/tests/migrations/test_commands.py
+++ b/tests/migrations/test_commands.py
@@ -828,46 +828,56 @@ class MigrateTests(MigrationTestBase):
"""
out = io.StringIO()
call_command("sqlmigrate", "migrations", "0001", stdout=out)
- output = out.getvalue().lower()
- index_tx_start = output.find(connection.ops.start_transaction_sql().lower())
- 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())
+ lines = out.getvalue().splitlines()
if connection.features.can_rollback_ddl:
- self.assertGreater(index_tx_start, -1, "Transaction start not found")
- self.assertGreater(
- index_tx_end,
- index_op_desc_unique_together,
- "Transaction end not found or found before operation description "
- "(unique_together)",
- )
+ self.assertEqual(lines[0], connection.ops.start_transaction_sql())
+ self.assertEqual(lines[-1], connection.ops.end_transaction_sql())
+ lines = lines[1:-1]
- self.assertGreater(
- index_op_desc_author,
- index_tx_start,
- "Operation description (author) not found or found before transaction "
- "start",
+ self.assertEqual(
+ lines[:3],
+ [
+ "--",
+ "-- Create model Author",
+ "--",
+ ],
)
- self.assertGreater(
- index_create_table,
- index_op_desc_author,
- "CREATE TABLE not found or found before operation description (author)",
+ self.assertIn(
+ "create table %s" % connection.ops.quote_name("migrations_author").lower(),
+ lines[3].lower(),
)
- self.assertGreater(
- index_op_desc_tribble,
- index_create_table,
- "Operation description (tribble) not found or found before CREATE TABLE "
- "(author)",
+ pos = lines.index("--", 3)
+ self.assertEqual(
+ lines[pos : pos + 3],
+ [
+ "--",
+ "-- Create model Tribble",
+ "--",
+ ],
+ )
+ self.assertIn(
+ "create table %s" % connection.ops.quote_name("migrations_tribble").lower(),
+ lines[pos + 3].lower(),
+ )
+ pos = lines.index("--", pos + 3)
+ self.assertEqual(
+ lines[pos : pos + 3],
+ [
+ "--",
+ "-- Add field bool to tribble",
+ "--",
+ ],
)
- self.assertGreater(
- index_op_desc_unique_together,
- index_op_desc_tribble,
- "Operation description (unique_together) not found or found before "
- "operation description (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"})
@@ -880,49 +890,70 @@ class MigrateTests(MigrationTestBase):
out = io.StringIO()
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:
+ self.assertEqual(lines[0], connection.ops.start_transaction_sql())
+ self.assertEqual(lines[-1], connection.ops.end_transaction_sql())
+ lines = lines[1:-1]
- if connection.features.can_rollback_ddl:
- self.assertGreater(index_tx_start, -1, "Transaction start not found")
- self.assertGreater(
- index_tx_end,
- index_op_desc_unique_together,
- "Transaction end not found or found before DROP TABLE",
+ self.assertEqual(
+ lines[:3],
+ [
+ "--",
+ "-- Alter unique_together for author (1 constraint(s))",
+ "--",
+ ],
)
- 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(
- index_drop_table,
- index_op_desc_author,
- "DROP TABLE not found or found before operation description (author)",
- )
-
- # Cleanup by unmigrating everything
- call_command("migrate", "migrations", "zero", verbosity=0)
+ pos = lines.index("--", 3)
+ 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)
@override_settings(
MIGRATION_MODULES={"migrations": "migrations.test_migrations_non_atomic"}