diff options
| author | David Wobrock <david.wobrock@gmail.com> | 2022-05-02 17:22:54 +0200 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2022-05-12 20:44:03 +0200 |
| commit | eacd4977f6a4bb038e82796ba79a2f61bae330c6 (patch) | |
| tree | 8d9b1c7d18226a8922826257ed997cee6229af10 /tests | |
| parent | 20e65a34aea0ace077033c84854dcf225e248f8c (diff) | |
Refs #27064 -- Added RenameIndex migration operation.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/migrations/test_operations.py | 143 | ||||
| -rw-r--r-- | tests/migrations/test_optimizer.py | 38 |
2 files changed, 181 insertions, 0 deletions
diff --git a/tests/migrations/test_operations.py b/tests/migrations/test_operations.py index 610a8b53ce..cfd28b1b39 100644 --- a/tests/migrations/test_operations.py +++ b/tests/migrations/test_operations.py @@ -2900,6 +2900,120 @@ class OperationTests(OperationTestBase): self.unapply_operations("test_rmin", project_state, operations=operations) self.assertIndexExists("test_rmin_pony", ["pink", "weight"]) + def test_rename_index(self): + app_label = "test_rnin" + project_state = self.set_up_test_model(app_label, index=True) + table_name = app_label + "_pony" + self.assertIndexNameExists(table_name, "pony_pink_idx") + self.assertIndexNameNotExists(table_name, "new_pony_test_idx") + operation = migrations.RenameIndex( + "Pony", new_name="new_pony_test_idx", old_name="pony_pink_idx" + ) + self.assertEqual( + operation.describe(), + "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", + ) + + new_state = project_state.clone() + operation.state_forwards(app_label, new_state) + # Rename index. + expected_queries = 1 if connection.features.can_rename_index else 2 + with connection.schema_editor() as editor, self.assertNumQueries( + expected_queries + ): + operation.database_forwards(app_label, editor, project_state, new_state) + self.assertIndexNameNotExists(table_name, "pony_pink_idx") + self.assertIndexNameExists(table_name, "new_pony_test_idx") + # Reversal. + with connection.schema_editor() as editor, self.assertNumQueries( + expected_queries + ): + operation.database_backwards(app_label, editor, new_state, project_state) + self.assertIndexNameExists(table_name, "pony_pink_idx") + self.assertIndexNameNotExists(table_name, "new_pony_test_idx") + # Deconstruction. + definition = operation.deconstruct() + self.assertEqual(definition[0], "RenameIndex") + self.assertEqual(definition[1], []) + self.assertEqual( + definition[2], + { + "model_name": "Pony", + "old_name": "pony_pink_idx", + "new_name": "new_pony_test_idx", + }, + ) + + def test_rename_index_arguments(self): + msg = "RenameIndex.old_name and old_fields are mutually exclusive." + with self.assertRaisesMessage(ValueError, msg): + migrations.RenameIndex( + "Pony", + new_name="new_idx_name", + old_name="old_idx_name", + old_fields=("weight", "pink"), + ) + msg = "RenameIndex requires one of old_name and old_fields arguments to be set." + with self.assertRaisesMessage(ValueError, msg): + migrations.RenameIndex("Pony", new_name="new_idx_name") + + def test_rename_index_unnamed_index(self): + app_label = "test_rninui" + project_state = self.set_up_test_model(app_label, index_together=True) + table_name = app_label + "_pony" + self.assertIndexNameNotExists(table_name, "new_pony_test_idx") + operation = migrations.RenameIndex( + "Pony", new_name="new_pony_test_idx", old_fields=("weight", "pink") + ) + self.assertEqual( + operation.describe(), + "Rename unnamed index for ('weight', 'pink') on Pony to new_pony_test_idx", + ) + self.assertEqual( + operation.migration_name_fragment, + "rename_pony_weight_pink_new_pony_test_idx", + ) + + new_state = project_state.clone() + operation.state_forwards(app_label, new_state) + # Rename index. + with connection.schema_editor() as editor: + operation.database_forwards(app_label, editor, project_state, new_state) + self.assertIndexNameExists(table_name, "new_pony_test_idx") + # Reverse is a no-op. + with connection.schema_editor() as editor, self.assertNumQueries(0): + operation.database_backwards(app_label, editor, new_state, project_state) + self.assertIndexNameExists(table_name, "new_pony_test_idx") + # Deconstruction. + definition = operation.deconstruct() + self.assertEqual(definition[0], "RenameIndex") + self.assertEqual(definition[1], []) + self.assertEqual( + definition[2], + { + "model_name": "Pony", + "new_name": "new_pony_test_idx", + "old_fields": ("weight", "pink"), + }, + ) + + def test_rename_index_unknown_unnamed_index(self): + app_label = "test_rninuui" + project_state = self.set_up_test_model(app_label) + operation = migrations.RenameIndex( + "Pony", new_name="new_pony_test_idx", old_fields=("weight", "pink") + ) + new_state = project_state.clone() + operation.state_forwards(app_label, new_state) + msg = "Found wrong number (0) of indexes for test_rninuui_pony(weight, pink)." + with connection.schema_editor() as editor: + with self.assertRaisesMessage(ValueError, msg): + operation.database_forwards(app_label, editor, project_state, new_state) + def test_add_index_state_forwards(self): project_state = self.set_up_test_model("test_adinsf") index = models.Index(fields=["pink"], name="test_adinsf_pony_pink_idx") @@ -2923,6 +3037,35 @@ class OperationTests(OperationTestBase): new_model = new_state.apps.get_model("test_rminsf", "Pony") self.assertIsNot(old_model, new_model) + def test_rename_index_state_forwards(self): + app_label = "test_rnidsf" + project_state = self.set_up_test_model(app_label, index=True) + old_model = project_state.apps.get_model(app_label, "Pony") + new_state = project_state.clone() + + operation = migrations.RenameIndex( + "Pony", new_name="new_pony_pink_idx", old_name="pony_pink_idx" + ) + operation.state_forwards(app_label, new_state) + new_model = new_state.apps.get_model(app_label, "Pony") + self.assertIsNot(old_model, new_model) + self.assertEqual(new_model._meta.indexes[0].name, "new_pony_pink_idx") + + def test_rename_index_state_forwards_unnamed_index(self): + app_label = "test_rnidsfui" + project_state = self.set_up_test_model(app_label, index_together=True) + old_model = project_state.apps.get_model(app_label, "Pony") + new_state = project_state.clone() + + operation = migrations.RenameIndex( + "Pony", new_name="new_pony_pink_idx", old_fields=("weight", "pink") + ) + operation.state_forwards(app_label, new_state) + new_model = new_state.apps.get_model(app_label, "Pony") + self.assertIsNot(old_model, new_model) + self.assertEqual(new_model._meta.index_together, tuple()) + self.assertEqual(new_model._meta.indexes[0].name, "new_pony_pink_idx") + @skipUnlessDBFeature("supports_expression_indexes") def test_add_func_index(self): app_label = "test_addfuncin" diff --git a/tests/migrations/test_optimizer.py b/tests/migrations/test_optimizer.py index 6bde378cd9..6485009eb4 100644 --- a/tests/migrations/test_optimizer.py +++ b/tests/migrations/test_optimizer.py @@ -1114,3 +1114,41 @@ class OptimizerTests(SimpleTestCase): ), ], ) + + def test_rename_index(self): + self.assertOptimizesTo( + [ + migrations.RenameIndex( + "Pony", new_name="mid_name", old_fields=("weight", "pink") + ), + migrations.RenameIndex( + "Pony", new_name="new_name", old_name="mid_name" + ), + ], + [ + migrations.RenameIndex( + "Pony", new_name="new_name", old_fields=("weight", "pink") + ), + ], + ) + self.assertOptimizesTo( + [ + migrations.RenameIndex( + "Pony", new_name="mid_name", old_name="old_name" + ), + migrations.RenameIndex( + "Pony", new_name="new_name", old_name="mid_name" + ), + ], + [migrations.RenameIndex("Pony", new_name="new_name", old_name="old_name")], + ) + self.assertDoesNotOptimize( + [ + migrations.RenameIndex( + "Pony", new_name="mid_name", old_name="old_name" + ), + migrations.RenameIndex( + "Pony", new_name="new_name", old_fields=("weight", "pink") + ), + ] + ) |
