Refs #24215 -- Improved error message for unhandled lazy model operations.

This commit is contained in:
Alex Hill 2015-03-26 10:09:58 +08:00 committed by Tim Graham
parent 617eff41ac
commit 25c157e4cc
2 changed files with 70 additions and 7 deletions

View file

@ -625,8 +625,16 @@ class StateTests(SimpleTestCase):
app_label = "migrations"
apps = new_apps
class Publisher(models.Model):
name = models.TextField()
class Meta:
app_label = "migrations"
apps = new_apps
class Book(models.Model):
author = models.ForeignKey(Author, models.CASCADE)
publisher = models.ForeignKey(Publisher, models.CASCADE)
class Meta:
app_label = "migrations"
@ -642,20 +650,42 @@ class StateTests(SimpleTestCase):
# Make a valid ProjectState and render it
project_state = ProjectState()
project_state.add_model(ModelState.from_model(Author))
project_state.add_model(ModelState.from_model(Publisher))
project_state.add_model(ModelState.from_model(Book))
project_state.add_model(ModelState.from_model(Magazine))
self.assertEqual(len(project_state.apps.get_models()), 3)
self.assertEqual(len(project_state.apps.get_models()), 4)
# now make an invalid one with a ForeignKey
project_state = ProjectState()
project_state.add_model(ModelState.from_model(Book))
with self.assertRaises(ValueError):
msg = (
"Unhandled pending operations for models:\n"
" migrations.author (referred to by fields: migrations.Book.author)\n"
" migrations.publisher (referred to by fields: migrations.Book.publisher)"
)
with self.assertRaisesMessage(ValueError, msg):
project_state.apps
# and another with ManyToManyField
# And another with ManyToManyField.
project_state = ProjectState()
project_state.add_model(ModelState.from_model(Magazine))
with self.assertRaises(ValueError):
msg = (
"Unhandled pending operations for models:\n"
" migrations.author (referred to by fields: "
"migrations.Magazine.authors, migrations.Magazine_authors.author)"
)
with self.assertRaisesMessage(ValueError, msg):
project_state.apps
# And now with multiple models and multiple fields.
project_state.add_model(ModelState.from_model(Book))
msg = (
"Unhandled pending operations for models:\n"
" migrations.author (referred to by fields: migrations.Book.author, "
"migrations.Magazine.authors, migrations.Magazine_authors.author)\n"
" migrations.publisher (referred to by fields: migrations.Book.publisher)"
)
with self.assertRaisesMessage(ValueError, msg):
project_state.apps
def test_real_apps(self):