summaryrefslogtreecommitdiff
path: root/tests/check_framework/tests.py
diff options
context:
space:
mode:
authorRigel Di Scala <rigel.discala@propylon.com>2014-10-14 14:10:27 +0100
committerLoic Bistuer <loic.bistuer@gmail.com>2014-10-16 23:49:21 +0700
commita5c77417a651c93036cf963e6da518653115be7e (patch)
treebffcc1d05dae9573b5de308bab0335b733ce1110 /tests/check_framework/tests.py
parent157f9cf240427bd52aa09a895ac4456da167f876 (diff)
Fixed #23615 -- Validate that a Model instance's "check" attribute is a method.
The "check" name is a reserved word used by Django's check framework, and cannot be redefined as something else other than a method, or the check framework will raise an error. This change amends the django.core.checks.model_check.check_all_models() function, so that it verifies that a model instance's attribute "check" is actually a method. This new check is assigned the id "models.E020".
Diffstat (limited to 'tests/check_framework/tests.py')
-rw-r--r--tests/check_framework/tests.py55
1 files changed, 55 insertions, 0 deletions
diff --git a/tests/check_framework/tests.py b/tests/check_framework/tests.py
index c1059de604..d603cf926e 100644
--- a/tests/check_framework/tests.py
+++ b/tests/check_framework/tests.py
@@ -8,11 +8,13 @@ from django.apps import apps
from django.conf import settings
from django.core import checks
from django.core.checks import Error, Warning
+from django.core.checks.model_checks import check_all_models
from django.core.checks.registry import CheckRegistry
from django.core.checks.compatibility.django_1_6_0 import check_1_6_compatibility
from django.core.checks.compatibility.django_1_7_0 import check_1_7_compatibility
from django.core.management.base import CommandError
from django.core.management import call_command
+from django.db import models
from django.db.models.fields import NOT_PROVIDED
from django.test import TestCase
from django.test.utils import override_settings, override_system_checks
@@ -327,3 +329,56 @@ class SilencingCheckTests(TestCase):
self.assertEqual(out.getvalue(), 'System check identified no issues (1 silenced).\n')
self.assertEqual(err.getvalue(), '')
+
+
+class CheckFrameworkReservedNamesTests(TestCase):
+
+ def setUp(self):
+ self.current_models = apps.all_models[__package__]
+ self.saved_models = set(self.current_models)
+
+ def tearDown(self):
+ for model in (set(self.current_models) - self.saved_models):
+ del self.current_models[model]
+ apps.clear_cache()
+
+ @override_settings(SILENCED_SYSTEM_CHECKS=['models.E020'])
+ def test_model_check_method_not_shadowed(self):
+ class ModelWithAttributeCalledCheck(models.Model):
+ check = 42
+
+ class ModelWithFieldCalledCheck(models.Model):
+ check = models.IntegerField()
+
+ class ModelWithRelatedManagerCalledCheck(models.Model):
+ pass
+
+ class ModelWithDescriptorCalledCheck(models.Model):
+ check = models.ForeignKey(ModelWithRelatedManagerCalledCheck)
+ article = models.ForeignKey(ModelWithRelatedManagerCalledCheck, related_name='check')
+
+ expected = [
+ Error(
+ "The 'ModelWithAttributeCalledCheck.check()' class method is "
+ "currently overridden by 42.",
+ hint=None,
+ obj=ModelWithAttributeCalledCheck,
+ id='models.E020'
+ ),
+ Error(
+ "The 'ModelWithRelatedManagerCalledCheck.check()' class method is "
+ "currently overridden by %r." % ModelWithRelatedManagerCalledCheck.check,
+ hint=None,
+ obj=ModelWithRelatedManagerCalledCheck,
+ id='models.E020'
+ ),
+ Error(
+ "The 'ModelWithDescriptorCalledCheck.check()' class method is "
+ "currently overridden by %r." % ModelWithDescriptorCalledCheck.check,
+ hint=None,
+ obj=ModelWithDescriptorCalledCheck,
+ id='models.E020'
+ ),
+ ]
+
+ self.assertEqual(check_all_models(), expected)