MCPcopy
hub / github.com/django/django / test_add_index

Method test_add_index

tests/migrations/test_operations.py:3889–3932  ·  view source on GitHub ↗

Test the AddIndex operation.

(self)

Source from the content-addressed store, hash-verified

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 """

Callers

nothing calls this directly

Calls 12

describeMethod · 0.95
state_forwardsMethod · 0.95
database_forwardsMethod · 0.95
database_backwardsMethod · 0.95
deconstructMethod · 0.95
assertRaisesMessageMethod · 0.80
formatted_descriptionMethod · 0.80
assertIndexNotExistsMethod · 0.80
schema_editorMethod · 0.80
assertIndexExistsMethod · 0.80
set_up_test_modelMethod · 0.45
cloneMethod · 0.45

Tested by

no test coverage detected