Test the AddIndex operation.
(self)
| 3887 | self.assertConstraintNotExists(table_name, unique_together_constraint_name) |
| 3888 | |
| 3889 | def test_add_index(self): |
| 3890 | """ |
| 3891 | Test the AddIndex operation. |
| 3892 | """ |
| 3893 | project_state = self.set_up_test_model("test_adin") |
| 3894 | msg = ( |
| 3895 | "Indexes passed to AddIndex operations require a name argument. " |
| 3896 | "<Index: fields=['pink']> doesn't have one." |
| 3897 | ) |
| 3898 | with self.assertRaisesMessage(ValueError, msg): |
| 3899 | migrations.AddIndex("Pony", models.Index(fields=["pink"])) |
| 3900 | index = models.Index(fields=["pink"], name="test_adin_pony_pink_idx") |
| 3901 | operation = migrations.AddIndex("Pony", index) |
| 3902 | self.assertEqual( |
| 3903 | operation.describe(), |
| 3904 | "Create index test_adin_pony_pink_idx on field(s) pink of model Pony", |
| 3905 | ) |
| 3906 | self.assertEqual( |
| 3907 | operation.formatted_description(), |
| 3908 | "+ Create index test_adin_pony_pink_idx on field(s) pink of model Pony", |
| 3909 | ) |
| 3910 | self.assertEqual( |
| 3911 | operation.migration_name_fragment, |
| 3912 | "pony_test_adin_pony_pink_idx", |
| 3913 | ) |
| 3914 | new_state = project_state.clone() |
| 3915 | operation.state_forwards("test_adin", new_state) |
| 3916 | # Test the database alteration |
| 3917 | self.assertEqual( |
| 3918 | len(new_state.models["test_adin", "pony"].options["indexes"]), 1 |
| 3919 | ) |
| 3920 | self.assertIndexNotExists("test_adin_pony", ["pink"]) |
| 3921 | with connection.schema_editor() as editor: |
| 3922 | operation.database_forwards("test_adin", editor, project_state, new_state) |
| 3923 | self.assertIndexExists("test_adin_pony", ["pink"]) |
| 3924 | # And test reversal |
| 3925 | with connection.schema_editor() as editor: |
| 3926 | operation.database_backwards("test_adin", editor, new_state, project_state) |
| 3927 | self.assertIndexNotExists("test_adin_pony", ["pink"]) |
| 3928 | # And deconstruction |
| 3929 | definition = operation.deconstruct() |
| 3930 | self.assertEqual(definition[0], "AddIndex") |
| 3931 | self.assertEqual(definition[1], []) |
| 3932 | self.assertEqual(definition[2], {"model_name": "Pony", "index": index}) |
| 3933 | |
| 3934 | def test_remove_index(self): |
| 3935 | """ |
nothing calls this directly
no test coverage detected