diff options
| author | Amir Karimi <amk9978@gmail.com> | 2023-09-16 05:41:22 +0330 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2024-01-17 14:00:02 +0100 |
| commit | 27a3eee72170f1eb994a213db985f42c6cf5f994 (patch) | |
| tree | d3522cf986d3ce3c1147063c5dbd1805c95843fa /tests | |
| parent | c7e986fc9f4848bd757d4b9b70a40586d2cee9fb (diff) | |
Fixed #31700 -- Made makemigrations command display meaningful symbols for each operation.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/migrations/test_commands.py | 12 | ||||
| -rw-r--r-- | tests/migrations/test_operations.py | 72 | ||||
| -rw-r--r-- | tests/postgres_tests/test_operations.py | 21 |
3 files changed, 99 insertions, 6 deletions
diff --git a/tests/migrations/test_commands.py b/tests/migrations/test_commands.py index a9c1cdf893..263b25ab61 100644 --- a/tests/migrations/test_commands.py +++ b/tests/migrations/test_commands.py @@ -2141,7 +2141,7 @@ class MakeMigrationsTests(MigrationTestBase): ) # Normal --dry-run output - self.assertIn("- Add field silly_char to sillymodel", out.getvalue()) + self.assertIn("+ Add field silly_char to sillymodel", out.getvalue()) # Additional output caused by verbosity 3 # The complete migrations file that would be written @@ -2171,7 +2171,7 @@ class MakeMigrationsTests(MigrationTestBase): ) initial_file = os.path.join(migration_dir, "0001_initial.py") self.assertEqual(out.getvalue(), f"{initial_file}\n") - self.assertIn(" - Create model ModelWithCustomBase\n", err.getvalue()) + self.assertIn(" + Create model ModelWithCustomBase\n", err.getvalue()) @mock.patch("builtins.input", return_value="Y") def test_makemigrations_scriptable_merge(self, mock_input): @@ -2216,7 +2216,7 @@ class MakeMigrationsTests(MigrationTestBase): self.assertTrue(os.path.exists(initial_file)) # Command output indicates the migration is created. - self.assertIn(" - Create model SillyModel", out.getvalue()) + self.assertIn(" + Create model SillyModel", out.getvalue()) @override_settings(MIGRATION_MODULES={"migrations": "some.nonexistent.path"}) def test_makemigrations_migrations_modules_nonexistent_toplevel_package(self): @@ -2321,12 +2321,12 @@ class MakeMigrationsTests(MigrationTestBase): out.getvalue().lower(), "merging conflicting_app_with_dependencies\n" " branch 0002_conflicting_second\n" - " - create model something\n" + " + create model something\n" " branch 0002_second\n" " - delete model tribble\n" " - remove field silly_field from author\n" - " - add field rating to author\n" - " - create model book\n" + " + add field rating to author\n" + " + create model book\n" "\n" "merging will only work if the operations printed above do not " "conflict\n" diff --git a/tests/migrations/test_operations.py b/tests/migrations/test_operations.py index 2d9d3a38f0..52e43d20f9 100644 --- a/tests/migrations/test_operations.py +++ b/tests/migrations/test_operations.py @@ -4,6 +4,7 @@ from decimal import Decimal from django.core.exceptions import FieldDoesNotExist from django.db import IntegrityError, connection, migrations, models, transaction from django.db.migrations.migration import Migration +from django.db.migrations.operations.base import Operation from django.db.migrations.operations.fields import FieldOperation from django.db.migrations.state import ModelState, ProjectState from django.db.models import F @@ -47,6 +48,7 @@ class OperationTests(OperationTestBase): ], ) self.assertEqual(operation.describe(), "Create model Pony") + self.assertEqual(operation.formatted_description(), "+ Create model Pony") self.assertEqual(operation.migration_name_fragment, "pony") # Test the state alteration project_state = ProjectState() @@ -710,6 +712,7 @@ class OperationTests(OperationTestBase): # Test the state alteration operation = migrations.DeleteModel("Pony") self.assertEqual(operation.describe(), "Delete model Pony") + self.assertEqual(operation.formatted_description(), "- Delete model Pony") self.assertEqual(operation.migration_name_fragment, "delete_pony") new_state = project_state.clone() operation.state_forwards("test_dlmo", new_state) @@ -790,6 +793,9 @@ class OperationTests(OperationTestBase): # Test the state alteration operation = migrations.RenameModel("Pony", "Horse") self.assertEqual(operation.describe(), "Rename model Pony to Horse") + self.assertEqual( + operation.formatted_description(), "~ Rename model Pony to Horse" + ) self.assertEqual(operation.migration_name_fragment, "rename_pony_horse") # Test initial state and database self.assertIn(("test_rnmo", "pony"), project_state.models) @@ -1350,6 +1356,9 @@ class OperationTests(OperationTestBase): models.FloatField(null=True, default=5), ) self.assertEqual(operation.describe(), "Add field height to Pony") + self.assertEqual( + operation.formatted_description(), "+ Add field height to Pony" + ) self.assertEqual(operation.migration_name_fragment, "pony_height") project_state, new_state = self.make_test_state("test_adfl", operation) self.assertEqual(len(new_state.models["test_adfl", "pony"].fields), 6) @@ -1906,6 +1915,9 @@ class OperationTests(OperationTestBase): # Test the state alteration operation = migrations.RemoveField("Pony", "pink") self.assertEqual(operation.describe(), "Remove field pink from Pony") + self.assertEqual( + operation.formatted_description(), "- Remove field pink from Pony" + ) self.assertEqual(operation.migration_name_fragment, "remove_pony_pink") new_state = project_state.clone() operation.state_forwards("test_rmfl", new_state) @@ -1952,6 +1964,10 @@ class OperationTests(OperationTestBase): self.assertEqual( operation.describe(), "Rename table for Pony to test_almota_pony_2" ) + self.assertEqual( + operation.formatted_description(), + "~ Rename table for Pony to test_almota_pony_2", + ) self.assertEqual(operation.migration_name_fragment, "alter_pony_table") new_state = project_state.clone() operation.state_forwards("test_almota", new_state) @@ -2093,6 +2109,9 @@ class OperationTests(OperationTestBase): "Pony", "pink", models.IntegerField(null=True) ) self.assertEqual(operation.describe(), "Alter field pink on Pony") + self.assertEqual( + operation.formatted_description(), "~ Alter field pink on Pony" + ) self.assertEqual(operation.migration_name_fragment, "alter_pony_pink") new_state = project_state.clone() operation.state_forwards("test_alfl", new_state) @@ -2403,6 +2422,9 @@ class OperationTests(OperationTestBase): # Add table comment. operation = migrations.AlterModelTableComment("Pony", "Custom pony comment") self.assertEqual(operation.describe(), "Alter Pony table comment") + self.assertEqual( + operation.formatted_description(), "~ Alter Pony table comment" + ) self.assertEqual(operation.migration_name_fragment, "alter_pony_table_comment") new_state = project_state.clone() operation.state_forwards(app_label, new_state) @@ -3073,6 +3095,9 @@ class OperationTests(OperationTestBase): project_state = self.set_up_test_model("test_rnfl") operation = migrations.RenameField("Pony", "pink", "blue") self.assertEqual(operation.describe(), "Rename field pink on Pony to blue") + self.assertEqual( + operation.formatted_description(), "~ Rename field pink on Pony to blue" + ) self.assertEqual(operation.migration_name_fragment, "rename_pink_pony_blue") new_state = project_state.clone() operation.state_forwards("test_rnfl", new_state) @@ -3327,6 +3352,10 @@ class OperationTests(OperationTestBase): operation.describe(), "Alter unique_together for Pony (1 constraint(s))" ) self.assertEqual( + operation.formatted_description(), + "~ Alter unique_together for Pony (1 constraint(s))", + ) + self.assertEqual( operation.migration_name_fragment, "alter_pony_unique_together", ) @@ -3479,6 +3508,10 @@ class OperationTests(OperationTestBase): "Create index test_adin_pony_pink_idx on field(s) pink of model Pony", ) self.assertEqual( + operation.formatted_description(), + "+ Create index test_adin_pony_pink_idx on field(s) pink of model Pony", + ) + self.assertEqual( operation.migration_name_fragment, "pony_test_adin_pony_pink_idx", ) @@ -3512,6 +3545,9 @@ class OperationTests(OperationTestBase): operation = migrations.RemoveIndex("Pony", "pony_test_idx") self.assertEqual(operation.describe(), "Remove index pony_test_idx from Pony") self.assertEqual( + operation.formatted_description(), "- Remove index pony_test_idx from Pony" + ) + self.assertEqual( operation.migration_name_fragment, "remove_pony_pony_test_idx", ) @@ -3566,6 +3602,10 @@ class OperationTests(OperationTestBase): "Rename index pony_pink_idx on Pony to new_pony_test_idx", ) self.assertEqual( + operation.formatted_description(), + "~ Rename index pony_pink_idx on Pony to new_pony_test_idx", + ) + self.assertEqual( operation.migration_name_fragment, "rename_pony_pink_idx_new_pony_test_idx", ) @@ -3807,6 +3847,10 @@ class OperationTests(OperationTestBase): self.assertEqual( operation.describe(), "Alter index_together for Pony (0 constraint(s))" ) + self.assertEqual( + operation.formatted_description(), + "~ Alter index_together for Pony (0 constraint(s))", + ) def test_add_constraint(self): project_state = self.set_up_test_model("test_addconstraint") @@ -3820,6 +3864,10 @@ class OperationTests(OperationTestBase): "Create constraint test_add_constraint_pony_pink_gt_2 on model Pony", ) self.assertEqual( + gt_operation.formatted_description(), + "+ Create constraint test_add_constraint_pony_pink_gt_2 on model Pony", + ) + self.assertEqual( gt_operation.migration_name_fragment, "pony_test_add_constraint_pony_pink_gt_2", ) @@ -4025,6 +4073,10 @@ class OperationTests(OperationTestBase): "Remove constraint test_remove_constraint_pony_pink_gt_2 from model Pony", ) self.assertEqual( + gt_operation.formatted_description(), + "- Remove constraint test_remove_constraint_pony_pink_gt_2 from model Pony", + ) + self.assertEqual( gt_operation.migration_name_fragment, "remove_pony_test_remove_constraint_pony_pink_gt_2", ) @@ -4564,6 +4616,9 @@ class OperationTests(OperationTestBase): "Pony", {"permissions": [("can_groom", "Can groom")]} ) self.assertEqual(operation.describe(), "Change Meta options on Pony") + self.assertEqual( + operation.formatted_description(), "~ Change Meta options on Pony" + ) self.assertEqual(operation.migration_name_fragment, "alter_pony_options") new_state = project_state.clone() operation.state_forwards("test_almoop", new_state) @@ -4631,6 +4686,10 @@ class OperationTests(OperationTestBase): operation.describe(), "Set order_with_respect_to on Rider to pony" ) self.assertEqual( + operation.formatted_description(), + "~ Set order_with_respect_to on Rider to pony", + ) + self.assertEqual( operation.migration_name_fragment, "alter_rider_order_with_respect_to", ) @@ -4705,6 +4764,7 @@ class OperationTests(OperationTestBase): ], ) self.assertEqual(operation.describe(), "Change managers on Pony") + self.assertEqual(operation.formatted_description(), "~ Change managers on Pony") self.assertEqual(operation.migration_name_fragment, "alter_pony_managers") managers = project_state.models["test_almoma", "pony"].managers self.assertEqual(managers, []) @@ -4840,6 +4900,7 @@ class OperationTests(OperationTestBase): ], ) self.assertEqual(operation.describe(), "Raw SQL operation") + self.assertEqual(operation.formatted_description(), "s Raw SQL operation") # Test the state alteration new_state = project_state.clone() operation.state_forwards("test_runsql", new_state) @@ -5034,6 +5095,7 @@ class OperationTests(OperationTestBase): inner_method, reverse_code=inner_method_reverse ) self.assertEqual(operation.describe(), "Raw Python operation") + self.assertEqual(operation.formatted_description(), "p Raw Python operation") # Test the state alteration does nothing new_state = project_state.clone() operation.state_forwards("test_runpython", new_state) @@ -5565,6 +5627,10 @@ class OperationTests(OperationTestBase): self.assertEqual( operation.describe(), "Custom state/database change combination" ) + self.assertEqual( + operation.formatted_description(), + "? Custom state/database change combination", + ) # Test the state alteration new_state = project_state.clone() operation.state_forwards("test_separatedatabaseandstate", new_state) @@ -6073,3 +6139,9 @@ class FieldOperationTests(SimpleTestCase): self.assertIs( operation.references_field("Through", "second", "migrations"), True ) + + +class BaseOperationTests(SimpleTestCase): + def test_formatted_description_no_category(self): + operation = Operation() + self.assertEqual(operation.formatted_description(), "? Operation: ((), {})") diff --git a/tests/postgres_tests/test_operations.py b/tests/postgres_tests/test_operations.py index f395198533..ff344e3cb0 100644 --- a/tests/postgres_tests/test_operations.py +++ b/tests/postgres_tests/test_operations.py @@ -59,6 +59,10 @@ class AddIndexConcurrentlyTests(OperationTestBase): operation.describe(), "Concurrently create index pony_pink_idx on field(s) pink of model Pony", ) + self.assertEqual( + operation.formatted_description(), + "+ Concurrently create index pony_pink_idx on field(s) pink of model Pony", + ) operation.state_forwards(self.app_label, new_state) self.assertEqual( len(new_state.models[self.app_label, "pony"].options["indexes"]), 1 @@ -154,6 +158,10 @@ class RemoveIndexConcurrentlyTests(OperationTestBase): operation.describe(), "Concurrently remove index pony_pink_idx from Pony", ) + self.assertEqual( + operation.formatted_description(), + "- Concurrently remove index pony_pink_idx from Pony", + ) operation.state_forwards(self.app_label, new_state) self.assertEqual( len(new_state.models[self.app_label, "pony"].options["indexes"]), 0 @@ -190,6 +198,9 @@ class CreateExtensionTests(PostgreSQLTestCase): @override_settings(DATABASE_ROUTERS=[NoMigrationRouter()]) def test_no_allow_migrate(self): operation = CreateExtension("tablefunc") + self.assertEqual( + operation.formatted_description(), "+ Creates extension tablefunc" + ) project_state = ProjectState() new_state = project_state.clone() # Don't create an extension. @@ -287,6 +298,7 @@ class CreateCollationTests(PostgreSQLTestCase): operation = CreateCollation("C_test", locale="C") self.assertEqual(operation.migration_name_fragment, "create_collation_c_test") self.assertEqual(operation.describe(), "Create collation C_test") + self.assertEqual(operation.formatted_description(), "+ Create collation C_test") project_state = ProjectState() new_state = project_state.clone() # Create a collation. @@ -418,6 +430,7 @@ class RemoveCollationTests(PostgreSQLTestCase): operation = RemoveCollation("C_test", locale="C") self.assertEqual(operation.migration_name_fragment, "remove_collation_c_test") self.assertEqual(operation.describe(), "Remove collation C_test") + self.assertEqual(operation.formatted_description(), "- Remove collation C_test") project_state = ProjectState() new_state = project_state.clone() # Remove a collation. @@ -471,6 +484,10 @@ class AddConstraintNotValidTests(OperationTestBase): f"Create not valid constraint {constraint_name} on model Pony", ) self.assertEqual( + operation.formatted_description(), + f"+ Create not valid constraint {constraint_name} on model Pony", + ) + self.assertEqual( operation.migration_name_fragment, f"pony_{constraint_name}_not_valid", ) @@ -531,6 +548,10 @@ class ValidateConstraintTests(OperationTestBase): f"Validate constraint {constraint_name} on model Pony", ) self.assertEqual( + operation.formatted_description(), + f"~ Validate constraint {constraint_name} on model Pony", + ) + self.assertEqual( operation.migration_name_fragment, f"pony_validate_{constraint_name}", ) |
