MCPcopy
hub / github.com/django/django / test_add_relations

Method test_add_relations

tests/migrations/test_state.py:686–748  ·  view source on GitHub ↗

#24573 - Adding relations to existing models should reload the referenced models too.

(self)

Source from the content-addressed store, hash-verified

684 self.assertEqual([r.related_model for r in C._meta.related_objects], [])
685
686 def test_add_relations(self):
687 """
688 #24573 - Adding relations to existing models should reload the
689 referenced models too.
690 """
691 new_apps = Apps()
692
693 class A(models.Model):
694 class Meta:
695 app_label = "something"
696 apps = new_apps
697
698 class B(A):
699 class Meta:
700 app_label = "something"
701 apps = new_apps
702
703 class C(models.Model):
704 class Meta:
705 app_label = "something"
706 apps = new_apps
707
708 project_state = ProjectState()
709 project_state.add_model(ModelState.from_model(A))
710 project_state.add_model(ModelState.from_model(B))
711 project_state.add_model(ModelState.from_model(C))
712
713 project_state.apps # We need to work with rendered models
714
715 old_state = project_state.clone()
716 model_a_old = old_state.apps.get_model("something", "A")
717 model_b_old = old_state.apps.get_model("something", "B")
718 model_c_old = old_state.apps.get_model("something", "C")
719 # The relations between the old models are correct
720 self.assertIs(model_a_old._meta.get_field("b").related_model, model_b_old)
721 self.assertIs(model_b_old._meta.get_field("a_ptr").related_model, model_a_old)
722
723 operation = AddField(
724 "c",
725 "to_a",
726 models.OneToOneField(
727 "something.A",
728 models.CASCADE,
729 related_name="from_c",
730 ),
731 )
732 operation.state_forwards("something", project_state)
733 model_a_new = project_state.apps.get_model("something", "A")
734 model_b_new = project_state.apps.get_model("something", "B")
735 model_c_new = project_state.apps.get_model("something", "C")
736
737 # All models have changed
738 self.assertIsNot(model_a_old, model_a_new)
739 self.assertIsNot(model_b_old, model_b_new)
740 self.assertIsNot(model_c_old, model_c_new)
741 # The relations between the old models still hold
742 self.assertIs(model_a_old._meta.get_field("b").related_model, model_b_old)
743 self.assertIs(model_b_old._meta.get_field("a_ptr").related_model, model_a_old)

Callers

nothing calls this directly

Calls 9

add_modelMethod · 0.95
cloneMethod · 0.95
state_forwardsMethod · 0.95
AppsClass · 0.90
ProjectStateClass · 0.90
AddFieldClass · 0.90
from_modelMethod · 0.80
get_modelMethod · 0.45
get_fieldMethod · 0.45

Tested by

no test coverage detected