From d818e0c9b2b88276cc499974f9eee893170bf0a8 Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Mon, 20 Jan 2014 10:45:21 +0800 Subject: Fixed #16905 -- Added extensible checks (nee validation) framework MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is the result of Christopher Medrela's 2013 Summer of Code project. Thanks also to Preston Holmes, Tim Graham, Anssi Kääriäinen, Florian Apolloner, and Alex Gaynor for review notes along the way. Also: Fixes #8579, fixes #3055, fixes #19844. --- tests/sites_framework/models.py | 11 --------- tests/sites_framework/tests.py | 50 ++++++++++++++++++++++++++++++++++++----- 2 files changed, 45 insertions(+), 16 deletions(-) (limited to 'tests/sites_framework') diff --git a/tests/sites_framework/models.py b/tests/sites_framework/models.py index 12b1d08dd8..23d39cec14 100644 --- a/tests/sites_framework/models.py +++ b/tests/sites_framework/models.py @@ -31,14 +31,3 @@ class CustomArticle(AbstractArticle): objects = models.Manager() on_site = CurrentSiteManager("places_this_article_should_appear") - - -class InvalidArticle(AbstractArticle): - site = models.ForeignKey(Site) - - objects = models.Manager() - on_site = CurrentSiteManager("places_this_article_should_appear") - - -class ConfusedArticle(AbstractArticle): - site = models.IntegerField() diff --git a/tests/sites_framework/tests.py b/tests/sites_framework/tests.py index fb62c28c14..680d5d4fb2 100644 --- a/tests/sites_framework/tests.py +++ b/tests/sites_framework/tests.py @@ -1,9 +1,13 @@ +from django.apps import apps from django.conf import settings +from django.contrib.sites.managers import CurrentSiteManager from django.contrib.sites.models import Site +from django.core import checks +from django.db import models from django.test import TestCase from .models import (SyndicatedArticle, ExclusiveArticle, CustomArticle, - InvalidArticle, ConfusedArticle) + AbstractArticle) class SitesFrameworkTestCase(TestCase): @@ -11,6 +15,13 @@ class SitesFrameworkTestCase(TestCase): Site.objects.get_or_create(id=settings.SITE_ID, domain="example.com", name="example.com") Site.objects.create(id=settings.SITE_ID + 1, domain="example2.com", name="example2.com") + self._old_models = apps.app_configs['sites_framework'].models.copy() + + def tearDown(self): + apps.app_configs['sites_framework'].models = self._old_models + apps.all_models['sites_framework'] = self._old_models + apps.clear_cache() + def test_site_fk(self): article = ExclusiveArticle.objects.create(title="Breaking News!", site_id=settings.SITE_ID) self.assertEqual(ExclusiveArticle.on_site.all().get(), article) @@ -28,9 +39,38 @@ class SitesFrameworkTestCase(TestCase): self.assertEqual(CustomArticle.on_site.all().get(), article) def test_invalid_name(self): - InvalidArticle.objects.create(title="Bad News!", site_id=settings.SITE_ID) - self.assertRaises(ValueError, InvalidArticle.on_site.all) + + class InvalidArticle(AbstractArticle): + site = models.ForeignKey(Site) + + objects = models.Manager() + on_site = CurrentSiteManager("places_this_article_should_appear") + + errors = InvalidArticle.check() + expected = [ + checks.Error( + ("CurrentSiteManager could not find a field named " + "'places_this_article_should_appear'."), + hint=('Ensure that you did not misspell the field name. ' + 'Does the field exist?'), + obj=InvalidArticle.on_site, + id='sites.E001', + ) + ] + self.assertEqual(errors, expected) def test_invalid_field_type(self): - ConfusedArticle.objects.create(title="More Bad News!", site=settings.SITE_ID) - self.assertRaises(TypeError, ConfusedArticle.on_site.all) + + class ConfusedArticle(AbstractArticle): + site = models.IntegerField() + + errors = ConfusedArticle.check() + expected = [ + checks.Error( + "CurrentSiteManager requires that 'ConfusedArticle.site' must be a ForeignKey or ManyToManyField.", + hint=None, + obj=ConfusedArticle.on_site, + id='sites.E002', + ) + ] + self.assertEqual(errors, expected) -- cgit v1.3