summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorAlex Hill <alex@hill.net.au>2016-05-18 23:18:40 +0800
committerTim Graham <timograham@gmail.com>2016-05-19 21:33:36 -0400
commit2ff7ef15b0a1d41e3f121e96cb72a383863046c0 (patch)
treeaebb17708c121fc4acf3611f9d23c69ccdc3dc7c /tests
parent0eac5535f7afd2295b1db978dffb97d79030807e (diff)
Refs #26421 -- Refactored Apps.lazy_model_operation() for better checks and tests
Diffstat (limited to 'tests')
-rw-r--r--tests/migrations/test_commands.py6
-rw-r--r--tests/migrations/test_state.py26
-rw-r--r--tests/model_validation/tests.py47
3 files changed, 68 insertions, 11 deletions
diff --git a/tests/migrations/test_commands.py b/tests/migrations/test_commands.py
index cc655f112f..c9efa09667 100644
--- a/tests/migrations/test_commands.py
+++ b/tests/migrations/test_commands.py
@@ -449,6 +449,12 @@ class MigrateTests(MigrationTestBase):
"""
call_command("migrate", "migrated_unapplied_app", stdout=six.StringIO())
+ # unmigrated_app.SillyModel has a foreign key to 'migrations.Tribble',
+ # but that model is only defined in a migration, so the global app
+ # registry never sees it and the reference is left dangling. Remove it
+ # to avoid problems in subsequent tests.
+ del apps._pending_operations[('migrations', 'tribble')]
+
@override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations_squashed"})
def test_migrate_record_replaced(self):
"""
diff --git a/tests/migrations/test_state.py b/tests/migrations/test_state.py
index f2e91267e9..2e53e4f825 100644
--- a/tests/migrations/test_state.py
+++ b/tests/migrations/test_state.py
@@ -661,9 +661,10 @@ class StateTests(SimpleTestCase):
project_state = ProjectState()
project_state.add_model(ModelState.from_model(Book))
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)"
+ "The field migrations.Book.author was declared with a lazy reference "
+ "to 'migrations.author', but app 'migrations' doesn't provide model 'author'.\n"
+ "The field migrations.Book.publisher was declared with a lazy reference "
+ "to 'migrations.publisher', but app 'migrations' doesn't provide model 'publisher'."
)
with self.assertRaisesMessage(ValueError, msg):
project_state.apps
@@ -672,9 +673,10 @@ class StateTests(SimpleTestCase):
project_state = ProjectState()
project_state.add_model(ModelState.from_model(Magazine))
msg = (
- "Unhandled pending operations for models:\n"
- " migrations.author (referred to by fields: "
- "migrations.Magazine.authors, migrations.Magazine_authors.author)"
+ "The field migrations.Magazine.authors was declared with a lazy reference "
+ "to 'migrations.author\', but app 'migrations' doesn't provide model 'author'.\n"
+ "The field migrations.Magazine_authors.author was declared with a lazy reference "
+ "to \'migrations.author\', but app 'migrations' doesn't provide model 'author'."
)
with self.assertRaisesMessage(ValueError, msg):
project_state.apps
@@ -682,10 +684,14 @@ class StateTests(SimpleTestCase):
# 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)"
+ "The field migrations.Book.author was declared with a lazy reference "
+ "to 'migrations.author', but app 'migrations' doesn't provide model 'author'.\n"
+ "The field migrations.Book.publisher was declared with a lazy reference "
+ "to 'migrations.publisher', but app 'migrations' doesn't provide model 'publisher'.\n"
+ "The field migrations.Magazine.authors was declared with a lazy reference "
+ "to 'migrations.author', but app 'migrations' doesn't provide model 'author'.\n"
+ "The field migrations.Magazine_authors.author was declared with a lazy reference "
+ "to 'migrations.author', but app 'migrations' doesn't provide model 'author'."
)
with self.assertRaisesMessage(ValueError, msg):
project_state.apps
diff --git a/tests/model_validation/tests.py b/tests/model_validation/tests.py
index 3d079d5bef..b98fd2ddac 100644
--- a/tests/model_validation/tests.py
+++ b/tests/model_validation/tests.py
@@ -1,8 +1,10 @@
from django.core import management
from django.core.checks import Error, run_checks
+from django.core.checks.model_checks import _check_lazy_references
+from django.db import models
from django.db.models.signals import post_init
from django.test import SimpleTestCase
-from django.test.utils import override_settings
+from django.test.utils import isolate_apps, override_settings
from django.utils import six
@@ -15,6 +17,10 @@ def on_post_init(**kwargs):
pass
+def dummy_function(model):
+ pass
+
+
@override_settings(
INSTALLED_APPS=['django.contrib.auth', 'django.contrib.contenttypes'],
SILENCED_SYSTEM_CHECKS=['fields.W342'], # ForeignKey(unique=True)
@@ -54,3 +60,42 @@ class ModelValidationTest(SimpleTestCase):
self.assertEqual(errors, expected)
post_init.unresolved_references = unresolved_references
+
+ @isolate_apps('django.contrib.auth', kwarg_name='apps')
+ def test_lazy_reference_checks(self, apps):
+
+ class DummyModel(models.Model):
+ author = models.ForeignKey('Author', models.CASCADE)
+
+ class Meta:
+ app_label = "model_validation"
+
+ apps.lazy_model_operation(dummy_function, ('auth', 'imaginarymodel'))
+ apps.lazy_model_operation(dummy_function, ('fanciful_app', 'imaginarymodel'))
+
+ errors = _check_lazy_references(apps)
+
+ expected = [
+ Error(
+ "%r contains a lazy reference to auth.imaginarymodel, "
+ "but app 'auth' doesn't provide model 'imaginarymodel'." % dummy_function,
+ obj=dummy_function,
+ id='models.E022',
+ ),
+ Error(
+ "%r contains a lazy reference to fanciful_app.imaginarymodel, "
+ "but app 'fanciful_app' isn't installed." % dummy_function,
+ obj=dummy_function,
+ id='models.E022',
+ ),
+ Error(
+ "The field model_validation.DummyModel.author was declared "
+ "with a lazy reference to 'model_validation.author', but app "
+ "'model_validation' isn't installed.",
+ hint=None,
+ obj=DummyModel.author.field,
+ id='fields.E307',
+ ),
+ ]
+
+ self.assertEqual(errors, expected)