Refs #30966 -- Added test for reloading related model state on non-relational changes.

Thanks Markus Holtermann for initial test.

Fixed in 1d16c5d562.
This commit is contained in:
Mariusz Felisiak 2020-04-08 10:43:38 +02:00 committed by GitHub
parent 2e67e80fbe
commit db6933a032
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 31 additions and 0 deletions

View File

@ -532,6 +532,37 @@ class StateTests(SimpleTestCase):
project_state.add_model(ModelState.from_model(B)) project_state.add_model(ModelState.from_model(B))
self.assertEqual(len(project_state.apps.get_models()), 2) self.assertEqual(len(project_state.apps.get_models()), 2)
def test_reload_related_model_on_non_relational_fields(self):
"""
The model is reloaded even on changes that are not involved in
relations. Other models pointing to or from it are also reloaded.
"""
project_state = ProjectState()
project_state.apps # Render project state.
project_state.add_model(ModelState('migrations', 'A', []))
project_state.add_model(ModelState('migrations', 'B', [
('a', models.ForeignKey('A', models.CASCADE)),
]))
project_state.add_model(ModelState('migrations', 'C', [
('b', models.ForeignKey('B', models.CASCADE)),
('name', models.TextField()),
]))
project_state.add_model(ModelState('migrations', 'D', [
('a', models.ForeignKey('A', models.CASCADE)),
]))
operation = AlterField(
model_name='C',
name='name',
field=models.TextField(blank=True),
)
operation.state_forwards('migrations', project_state)
project_state.reload_model('migrations', 'a', delay=True)
A = project_state.apps.get_model('migrations.A')
B = project_state.apps.get_model('migrations.B')
D = project_state.apps.get_model('migrations.D')
self.assertIs(B._meta.get_field('a').related_model, A)
self.assertIs(D._meta.get_field('a').related_model, A)
def test_reload_model_relationship_consistency(self): def test_reload_model_relationship_consistency(self):
project_state = ProjectState() project_state = ProjectState()
project_state.add_model(ModelState('migrations', 'A', [])) project_state.add_model(ModelState('migrations', 'A', []))