From 33511662ddbff0ca22cf5a0b7fdeca2f6764759f Mon Sep 17 00:00:00 2001 From: Simon Charette Date: Thu, 22 May 2014 18:51:11 -0400 Subject: [1.7.x] Fixed #22659 -- Prevent model states from sharing field instances. Thanks to Trac alias tbartelmess for the report and the test project. Backport of 7a38f88922 from master --- tests/migrations/test_autodetector.py | 4 ++-- tests/migrations/test_state.py | 19 ++++++++++++++++++- 2 files changed, 20 insertions(+), 3 deletions(-) (limited to 'tests') diff --git a/tests/migrations/test_autodetector.py b/tests/migrations/test_autodetector.py index 54361f00ed..cceb450452 100644 --- a/tests/migrations/test_autodetector.py +++ b/tests/migrations/test_autodetector.py @@ -239,7 +239,7 @@ class AutodetectorTests(TestCase): action = migration.operations[0] self.assertEqual(action.__class__.__name__, "AlterField") self.assertEqual(action.name, "author") - self.assertEqual(action.field.rel.to.__name__, "Writer") + self.assertEqual(action.field.rel.to, "testapp.Writer") def test_rename_model_with_renamed_rel_field(self): """ @@ -276,7 +276,7 @@ class AutodetectorTests(TestCase): action = migration.operations[1] self.assertEqual(action.__class__.__name__, "AlterField") self.assertEqual(action.name, "writer") - self.assertEqual(action.field.rel.to.__name__, "Writer") + self.assertEqual(action.field.rel.to, "testapp.Writer") def test_fk_dependency(self): "Tests that having a ForeignKey automatically adds a dependency" diff --git a/tests/migrations/test_state.py b/tests/migrations/test_state.py index ab65630105..9be04f3dfc 100644 --- a/tests/migrations/test_state.py +++ b/tests/migrations/test_state.py @@ -356,7 +356,7 @@ class StateTests(TestCase): project_state = ProjectState() project_state.add_model_state(ModelState.from_model(TestModel)) with self.assertRaises(ValueError): - rendered_state = project_state.render() + project_state.render() # If we include the real app it should succeed project_state = ProjectState(real_apps=["contenttypes"]) @@ -372,3 +372,20 @@ class ModelStateTests(TestCase): def test_custom_model_base(self): state = ModelState.from_model(ModelWithCustomBase) self.assertEqual(state.bases, (models.Model,)) + + def test_bound_field_sanity_check(self): + field = models.CharField(max_length=1) + field.model = models.Model + with self.assertRaisesMessage(ValueError, + 'ModelState.fields cannot be bound to a model - "field" is.'): + ModelState('app', 'Model', [('field', field)]) + + def test_fields_immutability(self): + """ + Tests that rendering a model state doesn't alter its internal fields. + """ + apps = Apps() + field = models.CharField(max_length=1) + state = ModelState('app', 'Model', [('name', field)]) + Model = state.render(apps) + self.assertNotEqual(Model._meta.get_field('name'), field) -- cgit v1.3