diff options
| author | Alex Hill <alex@hill.net.au> | 2016-03-30 16:34:44 +0800 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2016-05-19 21:33:36 -0400 |
| commit | 779bb82f519420418a711f7ae75e71d600de3224 (patch) | |
| tree | 9d74c86b69bce6ee086adf000e5c8893f2ccb359 /tests | |
| parent | 2ff7ef15b0a1d41e3f121e96cb72a383863046c0 (diff) | |
Fixed #26421 -- Refactored ModelSignal to use Apps.lazy_model_operation()
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/model_validation/tests.py | 81 | ||||
| -rw-r--r-- | tests/signals/tests.py | 8 |
2 files changed, 43 insertions, 46 deletions
diff --git a/tests/model_validation/tests.py b/tests/model_validation/tests.py index b98fd2ddac..0e137af5f2 100644 --- a/tests/model_validation/tests.py +++ b/tests/model_validation/tests.py @@ -1,5 +1,5 @@ from django.core import management -from django.core.checks import Error, run_checks +from django.core.checks import Error from django.core.checks.model_checks import _check_lazy_references from django.db import models from django.db.models.signals import post_init @@ -8,19 +8,6 @@ from django.test.utils import isolate_apps, override_settings from django.utils import six -class OnPostInit(object): - def __call__(self, **kwargs): - pass - - -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) @@ -35,32 +22,6 @@ class ModelValidationTest(SimpleTestCase): # See: https://code.djangoproject.com/ticket/21375 management.call_command("check", stdout=six.StringIO()) - def test_model_signal(self): - unresolved_references = post_init.unresolved_references.copy() - post_init.connect(on_post_init, sender='missing-app.Model') - post_init.connect(OnPostInit(), sender='missing-app.Model') - - errors = run_checks() - expected = [ - Error( - "The 'on_post_init' function was connected to the 'post_init' " - "signal with a lazy reference to the 'missing-app.Model' " - "sender, which has not been installed.", - obj='model_validation.tests', - id='signals.E001', - ), - Error( - "An instance of the 'OnPostInit' class was connected to " - "the 'post_init' signal with a lazy reference to the " - "'missing-app.Model' sender, which has not been installed.", - obj='model_validation.tests', - id='signals.E001', - ) - ] - 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): @@ -70,11 +31,24 @@ class ModelValidationTest(SimpleTestCase): class Meta: app_label = "model_validation" + class DummyClass(object): + def __call__(self, **kwargs): + pass + + def dummy_method(self): + pass + + def dummy_function(*args, **kwargs): + pass + apps.lazy_model_operation(dummy_function, ('auth', 'imaginarymodel')) apps.lazy_model_operation(dummy_function, ('fanciful_app', 'imaginarymodel')) - errors = _check_lazy_references(apps) + post_init.connect(dummy_function, sender='missing-app.Model', apps=apps) + post_init.connect(DummyClass(), sender='missing-app.Model', apps=apps) + post_init.connect(DummyClass().dummy_method, sender='missing-app.Model', apps=apps) + errors = _check_lazy_references(apps) expected = [ Error( "%r contains a lazy reference to auth.imaginarymodel, " @@ -89,6 +63,22 @@ class ModelValidationTest(SimpleTestCase): id='models.E022', ), Error( + "An instance of class 'DummyClass' was connected to " + "the 'post_init' signal with a lazy reference to the sender " + "'missing-app.model', but app 'missing-app' isn't installed.", + hint=None, + obj='model_validation.tests', + id='signals.E001', + ), + Error( + "Bound method 'DummyClass.dummy_method' was connected to the " + "'post_init' signal with a lazy reference to the sender " + "'missing-app.model', but app 'missing-app' isn't installed.", + hint=None, + obj='model_validation.tests', + id='signals.E001', + ), + Error( "The field model_validation.DummyModel.author was declared " "with a lazy reference to 'model_validation.author', but app " "'model_validation' isn't installed.", @@ -96,6 +86,13 @@ class ModelValidationTest(SimpleTestCase): obj=DummyModel.author.field, id='fields.E307', ), + Error( + "The function 'dummy_function' was connected to the 'post_init' " + "signal with a lazy reference to the sender " + "'missing-app.model', but app 'missing-app' isn't installed.", + hint=None, + obj='model_validation.tests', + id='signals.E001', + ), ] - self.assertEqual(errors, expected) diff --git a/tests/signals/tests.py b/tests/signals/tests.py index b02b215958..fd18b2191a 100644 --- a/tests/signals/tests.py +++ b/tests/signals/tests.py @@ -267,7 +267,7 @@ class LazyModelRefTest(BaseSignalTest): self.received.append(kwargs) def test_invalid_sender_model_name(self): - msg = "Specified sender must either be a model or a model name of the 'app_label.ModelName' form." + msg = "Invalid model reference 'invalid'. String model references must be of the form 'app_label.ModelName'." with self.assertRaisesMessage(ValueError, msg): signals.post_init.connect(self.receiver, sender='invalid') @@ -285,10 +285,10 @@ class LazyModelRefTest(BaseSignalTest): finally: signals.post_init.disconnect(self.receiver, sender=Book) - @isolate_apps('signals') - def test_not_loaded_model(self): + @isolate_apps('signals', kwarg_name='apps') + def test_not_loaded_model(self, apps): signals.post_init.connect( - self.receiver, sender='signals.Created', weak=False + self.receiver, sender='signals.Created', weak=False, apps=apps ) try: |
