Add tests for admin

This commit is contained in:
Iacopo Spalletti 2014-10-05 08:52:04 +02:00
parent eabc809e72
commit 150817a041
4 changed files with 51 additions and 23 deletions

View file

@ -7,7 +7,6 @@ from django.conf import settings
from parler.admin import TranslatableAdmin
from .models import Post, BlogCategory
from .settings import BLOG_USE_PLACEHOLDER, BLOG_MULTISITE
class BlogCategoryAdmin(EnhancedModelAdminMixin, TranslatableAdmin):
@ -48,6 +47,7 @@ class PostAdmin(EnhancedModelAdminMixin, FrontendEditableAdmin,
]
def get_fieldsets(self, request, obj=None):
from .settings import BLOG_USE_PLACEHOLDER, BLOG_MULTISITE
fsets = deepcopy(self._fieldsets)
if not BLOG_USE_PLACEHOLDER:
fsets[0][1]['fields'].append('post_text')

View file

@ -123,7 +123,7 @@ class GenericDateTaggedManager(TaggedFilterItem, TranslationManager):
def get_queryset(self, *args, **kwargs):
try:
return super(GenericDateTaggedManager, self).get_queryset(*args, **kwargs)
except AttributeError:
except AttributeError: # pragma: no cover
return super(GenericDateTaggedManager, self).get_query_set(*args, **kwargs)
if django.VERSION < (1, 8):
get_query_set = get_queryset

View file

@ -12,7 +12,7 @@ from django.contrib.sites.models import Site
from django.core.files import File as DjangoFile
from django.http import SimpleCookie
from django.test import TestCase, RequestFactory
from django.utils.translation import activate, override
from django.utils.translation import activate
from filer.models import File, Image
from PIL import Image as PilImage, ImageDraw
from six import StringIO
@ -164,7 +164,6 @@ class BaseTest(TestCase):
return request
def get_posts(self, sites=None):
with override('en'):
post1 = self._get_post(self.data['en'][0], sites=sites)
post1 = self._get_post(self.data['it'][0], post1, 'it')
post1.publish = True

View file

@ -2,6 +2,7 @@
from cms.api import add_plugin
from cms.utils.copy_plugins import copy_plugins_to
from cms.utils.plugins import downcast_plugins
from django.contrib import admin
from django.contrib.sites.models import Site
from django.core.urlresolvers import reverse
from django.utils.timezone import now
@ -16,6 +17,33 @@ from djangocms_blog import settings
from . import BaseTest
class AdminTest(BaseTest):
def test_admin_fieldsets(self):
request = self.get_page_request('/', self.user_staff, r'/en/blog/', edit=False)
post_admin = admin.site._registry[Post]
settings.BLOG_USE_PLACEHOLDER = True
fsets = post_admin.get_fieldsets(request)
self.assertFalse('post_text' in fsets[0][1]['fields'])
settings.BLOG_USE_PLACEHOLDER = False
fsets = post_admin.get_fieldsets(request)
self.assertTrue('post_text' in fsets[0][1]['fields'])
settings.BLOG_USE_PLACEHOLDER = True
settings.BLOG_MULTISITE = True
fsets = post_admin.get_fieldsets(request)
self.assertTrue('sites' in fsets[1][1]['fields'][0])
settings.BLOG_MULTISITE = False
fsets = post_admin.get_fieldsets(request)
self.assertFalse('sites' in fsets[1][1]['fields'][0])
settings.BLOG_MULTISITE = True
request = self.get_page_request('/', self.user, r'/en/blog/', edit=False)
fsets = post_admin.get_fieldsets(request)
self.assertTrue('author' in fsets[1][1]['fields'][0])
class ModelsTest(BaseTest):
def test_model_attributes(self):
@ -219,14 +247,15 @@ class ModelsTest(BaseTest):
self.assertEqual(set(new[0].authors.all()), set([self.user]))
def test_multisite(self):
with override('en'):
post1 = self._get_post(self.data['en'][0], sites=(self.site_1,))
post2 = self._get_post(self.data['en'][1], sites=(self.site_2,))
post3 = self._get_post(self.data['en'][2], sites=(self.site_2, self.site_1))
self.assertEqual(len(Post.objects.all()), 3)
with self.settings(**{'SITE_ID': 1}):
with self.settings(**{'SITE_ID': self.site_1.pk}):
self.assertEqual(len(Post.objects.all().on_site()), 2)
self.assertEqual(set(list(Post.objects.all().on_site())), set([post1, post3]))
with self.settings(**{'SITE_ID': 2}):
with self.settings(**{'SITE_ID': self.site_2.pk}):
self.assertEqual(len(Post.objects.all().on_site()), 2)
self.assertEqual(set(list(Post.objects.all().on_site())), set([post2, post3]))