MCPcopy
hub / github.com/django/django / test_rename_model

Method test_rename_model

tests/migrations/test_operations.py:788–858  ·  view source on GitHub ↗

Tests the RenameModel operation.

(self)

Source from the content-addressed store, hash-verified

786 self.assertColumnExists("test_dlmtimo_shetlandpony", "pony_ptr_id")
787
788 def test_rename_model(self):
789 """
790 Tests the RenameModel operation.
791 """
792 project_state = self.set_up_test_model("test_rnmo", related_model=True)
793 # Test the state alteration
794 operation = migrations.RenameModel("Pony", "Horse")
795 self.assertEqual(operation.describe(), "Rename model Pony to Horse")
796 self.assertEqual(
797 operation.formatted_description(), "~ Rename model Pony to Horse"
798 )
799 self.assertEqual(operation.migration_name_fragment, "rename_pony_horse")
800 # Test initial state and database
801 self.assertIn(("test_rnmo", "pony"), project_state.models)
802 self.assertNotIn(("test_rnmo", "horse"), project_state.models)
803 self.assertTableExists("test_rnmo_pony")
804 self.assertTableNotExists("test_rnmo_horse")
805 if connection.features.supports_foreign_keys:
806 self.assertFKExists(
807 "test_rnmo_rider", ["pony_id"], ("test_rnmo_pony", "id")
808 )
809 self.assertFKNotExists(
810 "test_rnmo_rider", ["pony_id"], ("test_rnmo_horse", "id")
811 )
812 # Migrate forwards
813 new_state = project_state.clone()
814 new_state = self.apply_operations("test_rnmo", new_state, [operation])
815 # Test new state and database
816 self.assertNotIn(("test_rnmo", "pony"), new_state.models)
817 self.assertIn(("test_rnmo", "horse"), new_state.models)
818 # RenameModel also repoints all incoming FKs and M2Ms
819 self.assertEqual(
820 new_state.models["test_rnmo", "rider"].fields["pony"].remote_field.model,
821 "test_rnmo.Horse",
822 )
823 self.assertTableNotExists("test_rnmo_pony")
824 self.assertTableExists("test_rnmo_horse")
825 if connection.features.supports_foreign_keys:
826 self.assertFKNotExists(
827 "test_rnmo_rider", ["pony_id"], ("test_rnmo_pony", "id")
828 )
829 self.assertFKExists(
830 "test_rnmo_rider", ["pony_id"], ("test_rnmo_horse", "id")
831 )
832 # Migrate backwards
833 original_state = self.unapply_operations(
834 "test_rnmo", project_state, [operation]
835 )
836 # Test original state and database
837 self.assertIn(("test_rnmo", "pony"), original_state.models)
838 self.assertNotIn(("test_rnmo", "horse"), original_state.models)
839 self.assertEqual(
840 original_state.models["test_rnmo", "rider"]
841 .fields["pony"]
842 .remote_field.model,
843 "Pony",
844 )
845 self.assertTableExists("test_rnmo_pony")

Callers

nothing calls this directly

Calls 11

describeMethod · 0.95
deconstructMethod · 0.95
formatted_descriptionMethod · 0.80
assertFKExistsMethod · 0.80
assertFKNotExistsMethod · 0.80
unapply_operationsMethod · 0.80
set_up_test_modelMethod · 0.45
assertTableExistsMethod · 0.45
assertTableNotExistsMethod · 0.45
cloneMethod · 0.45
apply_operationsMethod · 0.45

Tested by

no test coverage detected