diff options
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/migrations/test_autodetector.py | 45 | ||||
| -rw-r--r-- | tests/migrations/test_base.py | 7 | ||||
| -rw-r--r-- | tests/migrations/test_operations.py | 21 |
3 files changed, 62 insertions, 11 deletions
diff --git a/tests/migrations/test_autodetector.py b/tests/migrations/test_autodetector.py index e333621855..7a66e500cb 100644 --- a/tests/migrations/test_autodetector.py +++ b/tests/migrations/test_autodetector.py @@ -5590,6 +5590,51 @@ class AutodetectorTests(BaseAutodetectorTests): preserve_default=True, ) + def test_does_not_crash_after_rename_on_unique_together(self): + fields = ("first", "second") + before = self.make_project_state( + [ + ModelState( + "app", + "Foo", + [ + ("id", models.AutoField(primary_key=True)), + ("first", models.IntegerField()), + ("second", models.IntegerField()), + ], + options={"unique_together": {fields}}, + ), + ] + ) + after = before.clone() + after.rename_field("app", "foo", "first", "first_renamed") + + changes = MigrationAutodetector( + before, after, MigrationQuestioner({"ask_rename": True}) + )._detect_changes() + + self.assertNumberMigrations(changes, "app", 1) + self.assertOperationTypes( + changes, "app", 0, ["RenameField", "AlterUniqueTogether"] + ) + self.assertOperationAttributes( + changes, + "app", + 0, + 0, + model_name="foo", + old_name="first", + new_name="first_renamed", + ) + self.assertOperationAttributes( + changes, + "app", + 0, + 1, + name="foo", + unique_together={("first_renamed", "second")}, + ) + class MigrationSuggestNameTests(SimpleTestCase): def test_no_operations(self): diff --git a/tests/migrations/test_base.py b/tests/migrations/test_base.py index 24ae59ca1b..31967f9ed4 100644 --- a/tests/migrations/test_base.py +++ b/tests/migrations/test_base.py @@ -290,14 +290,13 @@ class OperationTestBase(MigrationTestBase): ): """Creates a test model state and database table.""" # Make the "current" state. - model_options = { - "swappable": "TEST_SWAP_MODEL", - "unique_together": [["pink", "weight"]] if unique_together else [], - } + model_options = {"swappable": "TEST_SWAP_MODEL"} if options: model_options["permissions"] = [("can_groom", "Can groom")] if db_table: model_options["db_table"] = db_table + if unique_together: + model_options["unique_together"] = {("pink", "weight")} operations = [ migrations.CreateModel( "Pony", diff --git a/tests/migrations/test_operations.py b/tests/migrations/test_operations.py index 00c76538e0..e859b62a10 100644 --- a/tests/migrations/test_operations.py +++ b/tests/migrations/test_operations.py @@ -3336,11 +3336,11 @@ class OperationTests(OperationTestBase): # unique_together has the renamed column. self.assertIn( "blue", - new_state.models["test_rnflut", "pony"].options["unique_together"][0], + list(new_state.models["test_rnflut", "pony"].options["unique_together"])[0], ) self.assertNotIn( "pink", - new_state.models["test_rnflut", "pony"].options["unique_together"][0], + list(new_state.models["test_rnflut", "pony"].options["unique_together"])[0], ) # Rename field. self.assertColumnExists("test_rnflut_pony", "pink") @@ -3377,7 +3377,7 @@ class OperationTests(OperationTestBase): ("weight", models.FloatField()), ], options={ - "index_together": [("weight", "pink")], + "index_together": {("weight", "pink")}, }, ), ] @@ -3390,10 +3390,12 @@ class OperationTests(OperationTestBase): self.assertNotIn("pink", new_state.models["test_rnflit", "pony"].fields) # index_together has the renamed column. self.assertIn( - "blue", new_state.models["test_rnflit", "pony"].options["index_together"][0] + "blue", + list(new_state.models["test_rnflit", "pony"].options["index_together"])[0], ) self.assertNotIn( - "pink", new_state.models["test_rnflit", "pony"].options["index_together"][0] + "pink", + list(new_state.models["test_rnflit", "pony"].options["index_together"])[0], ) # Rename field. @@ -3952,7 +3954,7 @@ class OperationTests(OperationTestBase): ("weight", models.FloatField()), ], options={ - "index_together": [("weight", "pink")], + "index_together": {("weight", "pink")}, }, ), ] @@ -3972,6 +3974,11 @@ class OperationTests(OperationTestBase): ) new_state = project_state.clone() operation.state_forwards(app_label, new_state) + # Ensure the model state has the correct type for the index_together + # option. + self.assertIsInstance( + new_state.models[app_label, "pony"].options["index_together"], set + ) # Rename index. with connection.schema_editor() as editor: operation.database_forwards(app_label, editor, project_state, new_state) @@ -4079,7 +4086,7 @@ class OperationTests(OperationTestBase): ("weight", models.FloatField()), ], options={ - "index_together": [("weight", "pink")], + "index_together": {("weight", "pink")}, }, ), ] |
