Fix tests

This commit is contained in:
Iacopo Spalletti 2014-10-05 08:25:18 +02:00
parent 51a1913f4c
commit eabc809e72
5 changed files with 63 additions and 63 deletions

View file

@ -1,4 +1,5 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import django
from django.contrib.sites.models import Site from django.contrib.sites.models import Site
from django.db.models import Q from django.db.models import Q
@ -124,9 +125,8 @@ class GenericDateTaggedManager(TaggedFilterItem, TranslationManager):
return super(GenericDateTaggedManager, self).get_queryset(*args, **kwargs) return super(GenericDateTaggedManager, self).get_queryset(*args, **kwargs)
except AttributeError: except AttributeError:
return super(GenericDateTaggedManager, self).get_query_set(*args, **kwargs) return super(GenericDateTaggedManager, self).get_query_set(*args, **kwargs)
if django.VERSION < (1, 8):
def get_query_set(self, *args, **kwargs): get_query_set = get_queryset
return self.get_queryset(*args, **kwargs)
def published(self): def published(self):
return self.get_queryset().published() return self.get_queryset().published()

View file

@ -18,7 +18,7 @@ class BaseBlogView(ViewUrlMixin):
def get_queryset(self): def get_queryset(self):
language = get_language() language = get_language()
queryset = self.model._default_manager.active_translations(language_code=language) queryset = self.model._default_manager.language(language_code=language)
if not self.request.user.is_staff: if not self.request.user.is_staff:
queryset = queryset.published() queryset = queryset.published()
return queryset.on_site() return queryset.on_site()
@ -128,10 +128,7 @@ class CategoryEntriesView(BaseBlogView, ListView):
@property @property
def category(self): def category(self):
if not self._category: if not self._category:
language = get_language() self._category = BlogCategory.objects.active_translations(get_language(), slug=self.kwargs['category']).last()
self._category = BlogCategory._default_manager.language(language).get(
translations__language_code=language,
translations__slug=self.kwargs['category'])
return self._category return self._category
def get_queryset(self): def get_queryset(self):

View file

@ -2,23 +2,23 @@
""" """
Tests for `djangocms_blog` module. Tests for `djangocms_blog` module.
""" """
from django.contrib.sites.models import Site
import os import os
from cms.utils.i18n import get_language_list from cms.utils.i18n import get_language_list
from cmsplugin_filer_image.models import ThumbnailOption from cmsplugin_filer_image.models import ThumbnailOption
from django.conf import settings from django.conf import settings
from django.contrib.auth import get_user_model from django.contrib.auth import get_user_model
from django.contrib.sites.models import Site
from django.core.files import File as DjangoFile from django.core.files import File as DjangoFile
from django.http import SimpleCookie from django.http import SimpleCookie
from django.test import TestCase, RequestFactory from django.test import TestCase, RequestFactory
from django.utils.translation import activate from django.utils.translation import activate, override
from filer.models import File, Image from filer.models import File, Image
from PIL import Image as PilImage, ImageDraw from PIL import Image as PilImage, ImageDraw
from six import StringIO from six import StringIO
from djangocms_blog.models import BlogCategory, Post
from djangocms_helper.utils import create_user from djangocms_helper.utils import create_user
from djangocms_blog.models import BlogCategory, Post
User = get_user_model() User = get_user_model()
@ -71,13 +71,10 @@ class BaseTest(TestCase):
def setUp(self): def setUp(self):
activate('en') activate('en')
super(BaseTest, self).setUp() super(BaseTest, self).setUp()
self.category_1 = BlogCategory.objects.create() self.category_1 = BlogCategory.objects.create(name=u'category 1')
self.category_1.name = u'category 1' self.category_1.set_current_language('it', initialize=True)
self.category_1.save()
self.category_1.set_current_language('it')
self.category_1.name = u'categoria 1' self.category_1.name = u'categoria 1'
self.category_1.save() self.category_1.save()
self.category_1.set_current_language('en')
self.thumb_1 = ThumbnailOption.objects.create( self.thumb_1 = ThumbnailOption.objects.create(
name='base', width=100, height=100, crop=True, upscale=False name='base', width=100, height=100, crop=True, upscale=False
) )
@ -96,19 +93,25 @@ class BaseTest(TestCase):
def _get_post(self, data, post=None, lang='en', sites=None): def _get_post(self, data, post=None, lang='en', sites=None):
if not post: if not post:
post = Post() post_data = {
post.set_current_language(lang) 'author': self.user,
post.author = self.user 'title': data['title'],
post.title = data['title'] 'abstract': data['abstract'],
post.abstract = data['abstract'] 'meta_description': data['description'],
post.meta_description = data['description'] 'meta_keywords': data['keywords'],
post.meta_keywords = data['keywords'] }
post.save() post = Post.objects.create(**post_data)
else:
post.set_current_language(lang)
post.title = data['title']
post.abstract = data['abstract']
post.meta_description = data['description']
post.meta_keywords = data['keywords']
post.save()
post.categories.add(self.category_1) post.categories.add(self.category_1)
if sites: if sites:
for site in sites: for site in sites:
post.sites.add(site) post.sites.add(site)
post.save()
return post return post
@classmethod @classmethod
@ -116,6 +119,8 @@ class BaseTest(TestCase):
User.objects.all().delete() User.objects.all().delete()
def tearDown(self): def tearDown(self):
for post in Post.objects.all():
post.delete()
os.remove(self.filename) os.remove(self.filename)
for f in File.objects.all(): for f in File.objects.all():
f.delete() f.delete()
@ -159,18 +164,16 @@ class BaseTest(TestCase):
return request return request
def get_posts(self, sites=None): def get_posts(self, sites=None):
post1 = self._get_post(self.data['en'][0], sites=sites) with override('en'):
post1 = self._get_post(self.data['it'][0], post1, 'it') post1 = self._get_post(self.data['en'][0], sites=sites)
post1.publish = True post1 = self._get_post(self.data['it'][0], post1, 'it')
post1.main_image = self.img post1.publish = True
post1.save() post1.main_image = self.img
post1.set_current_language('en') post1.save()
post2 = self._get_post(self.data['en'][1], sites=sites) post2 = self._get_post(self.data['en'][1], sites=sites)
post2 = self._get_post(self.data['it'][1], post2, 'it') post2 = self._get_post(self.data['it'][1], post2, 'it')
post2.set_current_language('en') post2.main_image = self.img
post2.main_image = self.img post2.save()
post2.save()
post2.set_current_language('en')
return post1, post2 return post1, post2

View file

@ -5,7 +5,7 @@ from cms.utils.plugins import downcast_plugins
from django.contrib.sites.models import Site from django.contrib.sites.models import Site
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from django.utils.timezone import now from django.utils.timezone import now
from django.utils.translation import get_language, activate, override from django.utils.translation import get_language, override
import parler import parler
from taggit.models import Tag from taggit.models import Tag

View file

@ -3,7 +3,8 @@ from django.contrib.auth.models import AnonymousUser
from django.http import Http404 from django.http import Http404
from django.utils.translation import activate from django.utils.translation import activate
from django.utils.timezone import now from django.utils.timezone import now
from djangocms_blog.models import Post from parler.utils.context import switch_language
from djangocms_blog.models import Post, BlogCategory
from djangocms_blog.feeds import LatestEntriesFeed, TagFeed from djangocms_blog.feeds import LatestEntriesFeed, TagFeed
from djangocms_blog.sitemaps import BlogSitemap from djangocms_blog.sitemaps import BlogSitemap
from djangocms_blog.views import (PostListView, PostDetailView, from djangocms_blog.views import (PostListView, PostDetailView,
@ -54,34 +55,33 @@ class ViewTest(BaseTest):
page1, page2 = self.get_pages() page1, page2 = self.get_pages()
post1, post2 = self.get_posts() post1, post2 = self.get_posts()
request = self.get_page_request(page1, AnonymousUser(), r'/en/blog/', edit=False) with switch_language(post1, 'en'):
activate('en') request = self.get_page_request(page1, AnonymousUser(), r'/en/blog/', edit=False)
view_obj = PostDetailView() view_obj = PostDetailView()
view_obj.request = request view_obj.request = request
with self.assertRaises(Http404): with self.assertRaises(Http404):
view_obj.kwargs = {'slug': 'not-existing'} view_obj.kwargs = {'slug': 'not-existing'}
post_obj = view_obj.get_object()
view_obj.kwargs = {'slug': post1.slug}
post_obj = view_obj.get_object() post_obj = view_obj.get_object()
self.assertEqual(post_obj, post1)
self.assertEqual(post_obj.language_code, 'en')
view_obj.kwargs = {'slug': post1.slug} with switch_language(post1, 'it'):
post_obj = view_obj.get_object() request = self.get_page_request(page1, AnonymousUser(), r'/it/blog/', lang_code='it', edit=False)
self.assertEqual(post_obj, post1) view_obj.request = request
self.assertEqual(post_obj.language_code, 'en') view_obj.kwargs = {'slug': post1.slug}
post_obj = view_obj.get_object()
self.assertEqual(post_obj, post1)
self.assertEqual(post_obj.language_code, 'it')
request = self.get_page_request(page1, AnonymousUser(), r'/it/blog/', lang_code='it', edit=False) view_obj.object = post_obj
activate('it') context = view_obj.get_context_data()
post1.set_current_language('it') self.assertEqual(context['post'], post1)
view_obj.request = request self.assertEqual(context['post'].language_code, 'it')
view_obj.kwargs = {'slug': post1.slug} self.assertTrue(context['meta'])
post_obj = view_obj.get_object()
self.assertEqual(post_obj, post1)
self.assertEqual(post_obj.language_code, 'it')
view_obj.object = post_obj
context = view_obj.get_context_data()
self.assertEqual(context['post'], post1)
self.assertEqual(context['post'].language_code, 'it')
self.assertTrue(context['meta'])
def test_post_archive_view(self): def test_post_archive_view(self):
page1, page2 = self.get_pages() page1, page2 = self.get_pages()