2014-01-20 10:45:21 +08:00
|
|
|
from django.apps import apps
|
2010-10-18 12:56:27 +08:00
|
|
|
from django.conf import settings
|
2014-01-20 10:45:21 +08:00
|
|
|
from django.contrib.sites.managers import CurrentSiteManager
|
2010-10-18 12:56:27 +08:00
|
|
|
from django.contrib.sites.models import Site
|
2014-01-20 10:45:21 +08:00
|
|
|
from django.core import checks
|
|
|
|
from django.db import models
|
2010-10-18 12:56:27 +08:00
|
|
|
from django.test import TestCase
|
|
|
|
|
2015-01-28 20:35:27 +08:00
|
|
|
from .models import (
|
|
|
|
AbstractArticle, CustomArticle, ExclusiveArticle, SyndicatedArticle,
|
|
|
|
)
|
2011-10-14 05:34:56 +08:00
|
|
|
|
2010-10-18 12:56:27 +08:00
|
|
|
|
|
|
|
class SitesFrameworkTestCase(TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
Site.objects.get_or_create(id=settings.SITE_ID, domain="example.com", name="example.com")
|
2013-11-04 02:08:55 +08:00
|
|
|
Site.objects.create(id=settings.SITE_ID + 1, domain="example2.com", name="example2.com")
|
2011-10-14 05:34:56 +08:00
|
|
|
|
2014-01-20 10:45:21 +08:00
|
|
|
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()
|
|
|
|
|
2010-10-18 12:56:27 +08:00
|
|
|
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)
|
2011-10-14 05:34:56 +08:00
|
|
|
|
2010-10-18 12:56:27 +08:00
|
|
|
def test_sites_m2m(self):
|
|
|
|
article = SyndicatedArticle.objects.create(title="Fresh News!")
|
|
|
|
article.sites.add(Site.objects.get(id=settings.SITE_ID))
|
2013-11-04 02:08:55 +08:00
|
|
|
article.sites.add(Site.objects.get(id=settings.SITE_ID + 1))
|
2010-10-18 12:56:27 +08:00
|
|
|
article2 = SyndicatedArticle.objects.create(title="More News!")
|
2013-11-04 02:08:55 +08:00
|
|
|
article2.sites.add(Site.objects.get(id=settings.SITE_ID + 1))
|
2010-10-18 12:56:27 +08:00
|
|
|
self.assertEqual(SyndicatedArticle.on_site.all().get(), article)
|
2011-10-14 05:34:56 +08:00
|
|
|
|
2010-10-18 12:56:27 +08:00
|
|
|
def test_custom_named_field(self):
|
2015-09-12 07:33:12 +08:00
|
|
|
article = CustomArticle.objects.create(
|
|
|
|
title="Tantalizing News!",
|
|
|
|
places_this_article_should_appear_id=settings.SITE_ID,
|
|
|
|
)
|
2010-10-18 12:56:27 +08:00
|
|
|
self.assertEqual(CustomArticle.on_site.all().get(), article)
|
2011-10-14 05:34:56 +08:00
|
|
|
|
2010-10-18 12:56:27 +08:00
|
|
|
def test_invalid_name(self):
|
2014-01-20 10:45:21 +08:00
|
|
|
|
|
|
|
class InvalidArticle(AbstractArticle):
|
2015-07-22 22:43:21 +08:00
|
|
|
site = models.ForeignKey(Site, models.CASCADE)
|
2014-01-20 10:45:21 +08:00
|
|
|
|
|
|
|
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'."),
|
2014-03-03 14:27:22 +08:00
|
|
|
hint=None,
|
2014-01-20 10:45:21 +08:00
|
|
|
obj=InvalidArticle.on_site,
|
|
|
|
id='sites.E001',
|
|
|
|
)
|
|
|
|
]
|
|
|
|
self.assertEqual(errors, expected)
|
2011-10-14 05:34:56 +08:00
|
|
|
|
2010-10-18 12:56:27 +08:00
|
|
|
def test_invalid_field_type(self):
|
2014-01-20 10:45:21 +08:00
|
|
|
|
|
|
|
class ConfusedArticle(AbstractArticle):
|
|
|
|
site = models.IntegerField()
|
|
|
|
|
|
|
|
errors = ConfusedArticle.check()
|
|
|
|
expected = [
|
|
|
|
checks.Error(
|
2014-03-03 14:27:22 +08:00
|
|
|
"CurrentSiteManager cannot use 'ConfusedArticle.site' as it is not a ForeignKey or ManyToManyField.",
|
2014-01-20 10:45:21 +08:00
|
|
|
hint=None,
|
|
|
|
obj=ConfusedArticle.on_site,
|
|
|
|
id='sites.E002',
|
|
|
|
)
|
|
|
|
]
|
|
|
|
self.assertEqual(errors, expected)
|