Merge pull request #28 from nephila/feature/more_tests

Add more tests
This commit is contained in:
Iacopo Spalletti 2014-06-11 19:12:17 +02:00
commit 5089e5a0af
8 changed files with 252 additions and 117 deletions

View file

@ -15,7 +15,7 @@ class LatestEntriesFeed(Feed):
def title(self):
return _('Blog articles on %(site_name)s') % {'site_name': Site.objects.get_current().name}
def items(self, obj):
def items(self, obj=None):
return Post.objects.published().order_by('-date_published')[:10]
def item_title(self, item):
@ -28,7 +28,7 @@ class LatestEntriesFeed(Feed):
class TagFeed(LatestEntriesFeed):
def get_object(self, request, tag):
return tag
return tag # pragma: no cover
def items(self, obj):
def items(self, obj=None):
return Post.objects.published().filter(tags__slug=obj)[:10]

View file

@ -6,7 +6,7 @@ from django.core.urlresolvers import resolve
from django.http import Http404
from django.views.generic import ListView, DetailView
from parler.views import ViewUrlMixin
from parler.views import ViewUrlMixin, TranslatableSlugMixin
from .models import Post, BlogCategory, BLOG_CURRENT_POST_IDENTIFIER
from .settings import (BLOG_PAGINATION, BLOG_POSTS_LIST_TRUNCWORDS_COUNT,
@ -40,22 +40,13 @@ class PostListView(BaseBlogView, ListView):
return context
class PostDetailView(BaseBlogView, DetailView):
class PostDetailView(TranslatableSlugMixin, BaseBlogView, DetailView):
model = Post
context_object_name = 'post'
template_name = "djangocms_blog/post_detail.html"
slug_field = 'slug'
view_url_name = 'djangocms_blog:post-detail'
def get_object(self, queryset=None):
try:
qs = self.model._default_manager.active_translations(**{
self.slug_field: self.kwargs.get(self.slug_url_kwarg, None)
}).latest()
except Post.DoesNotExist:
raise Http404()
return qs
def get_context_data(self, **kwargs):
context = super(PostDetailView, self).get_context_data(**kwargs)
context['meta'] = self.get_object().as_meta()

View file

@ -112,7 +112,8 @@ try:
)
from django_nose import NoseTestSuiteRunner
except ImportError:
except ImportError as e:
print(e)
raise ImportError('To fix this error, run: pip install -r requirements-test.txt')

View file

@ -36,7 +36,7 @@ setup(
],
include_package_data=True,
install_requires=[
'django-parler>=1.0b1',
'django-parler>=1.0b3',
'django-cms>=3.0',
'django-taggit',
'django-filer',

View file

@ -81,7 +81,6 @@ class BaseTest(TestCase):
post.save()
return post
@classmethod
def tearDownClass(cls):
User.objects.all().delete()
@ -123,3 +122,14 @@ class BaseTest(TestCase):
mid = ToolbarMiddleware()
mid.process_request(request)
return request
def get_posts(self):
post1 = self._get_post(self.data['en'][0])
post1 = self._get_post(self.data['it'][0], post1, 'it')
post1.publish = True
post1.save()
post1.set_current_language('en')
post2 = self._get_post(self.data['en'][1])
post2 = self._get_post(self.data['it'][1], post2, 'it')
post2.set_current_language('en')
return post1, post2

View file

@ -71,8 +71,8 @@ class ModelsTest(BaseTest):
})
def test_manager(self):
post_1 = self._get_post(self.data['en'][0])
post_2 = self._get_post(self.data['en'][1])
post1 = self._get_post(self.data['en'][0])
post2 = self._get_post(self.data['en'][1])
# default queryset, published and unpublished posts
months = Post.objects.get_months()
@ -81,8 +81,8 @@ class ModelsTest(BaseTest):
self.assertEqual(data['count'], 2)
# custom queryset, only published
post_1.publish = True
post_1.save()
post1.publish = True
post1.save()
months = Post.objects.get_months(Post.objects.published())
for data in months:
self.assertEqual(data['date'], date(year=date.today().year, month=date.today().month, day=1))
@ -91,23 +91,23 @@ class ModelsTest(BaseTest):
self.assertEqual(len(Post.objects.available()), 1)
# If post is published but publishing date is in the future
post_2.date_published = date(year=date.today().year+1, month=date.today().month, day=1)
post_2.publish = True
post_2.save()
post2.date_published = date(year=date.today().year+1, month=date.today().month, day=1)
post2.publish = True
post2.save()
self.assertEqual(len(Post.objects.available()), 2)
self.assertEqual(len(Post.objects.published()), 1)
self.assertEqual(len(Post.objects.archived()), 0)
# If post is published but end publishing date is in the past
post_2.date_published = date(year=date.today().year-2, month=date.today().month, day=1)
post_2.date_published_end = date(year=date.today().year-1, month=date.today().month, day=1)
post_2.save()
post2.date_published = date(year=date.today().year-2, month=date.today().month, day=1)
post2.date_published_end = date(year=date.today().year-1, month=date.today().month, day=1)
post2.save()
self.assertEqual(len(Post.objects.available()), 2)
self.assertEqual(len(Post.objects.published()), 1)
self.assertEqual(len(Post.objects.archived()), 1)
# counting with language fallback enabled
post = self._get_post(self.data['it'][0], post_1, 'it')
post = self._get_post(self.data['it'][0], post1, 'it')
self.assertEqual(len(Post.objects.filter_by_language('it')), 2)
# No fallback
@ -116,12 +116,12 @@ class ModelsTest(BaseTest):
parler.appsettings.PARLER_LANGUAGES['default']['hide_untranslated'] = False
def test_tag_cloud(self):
post_1 = self._get_post(self.data['en'][0])
post_2 = self._get_post(self.data['en'][1])
post_1.tags.add('tag 1', 'tag 2', 'tag 3', 'tag 4')
post_1.save()
post_2.tags.add('tag 6', 'tag 2', 'tag 5', 'tag 8')
post_2.save()
post1 = self._get_post(self.data['en'][0])
post2 = self._get_post(self.data['en'][1])
post1.tags.add('tag 1', 'tag 2', 'tag 3', 'tag 4')
post1.save()
post2.tags.add('tag 6', 'tag 2', 'tag 5', 'tag 8')
post2.save()
self.assertEqual(len(Post.objects.tag_cloud()), 0)
@ -145,59 +145,59 @@ class ModelsTest(BaseTest):
tag.count = 1
tags_1.append(tag)
post_1.publish = True
post_1.save()
post1.publish = True
post1.save()
self.assertEqual(set(Post.objects.tag_cloud()), set(tags_1))
self.assertEqual(set(Post.objects.tag_cloud(published=False)), set(tags))
def test_plugin_latest(self):
post_1 = self._get_post(self.data['en'][0])
post_2 = self._get_post(self.data['en'][1])
post_1.tags.add('tag 1')
post_1.save()
plugin = add_plugin(post_1.content, 'BlogLatestEntriesPlugin', language='en')
post1 = self._get_post(self.data['en'][0])
post2 = self._get_post(self.data['en'][1])
post1.tags.add('tag 1')
post1.save()
plugin = add_plugin(post1.content, 'BlogLatestEntriesPlugin', language='en')
tag = Tag.objects.get(slug='tag-1')
plugin.tags.add(tag)
self.assertEqual(len(plugin.get_posts()), 0)
post_1.publish = True
post_1.save()
post1.publish = True
post1.save()
self.assertEqual(len(plugin.get_posts()), 1)
def test_copy_plugin_latest(self):
post_1 = self._get_post(self.data['en'][0])
post_2 = self._get_post(self.data['en'][1])
post1 = self._get_post(self.data['en'][0])
post2 = self._get_post(self.data['en'][1])
tag = Tag.objects.create(name='tag 1')
plugin = add_plugin(post_1.content, 'BlogLatestEntriesPlugin', language='en')
plugin = add_plugin(post1.content, 'BlogLatestEntriesPlugin', language='en')
plugin.tags.add(tag)
plugins = list(post_1.content.cmsplugin_set.filter(language='en').order_by('tree_id', 'level', 'position'))
copy_plugins_to(plugins, post_2.content)
new = downcast_plugins(post_2.content.cmsplugin_set.all())
plugins = list(post1.content.cmsplugin_set.filter(language='en').order_by('tree_id', 'level', 'position'))
copy_plugins_to(plugins, post2.content)
new = downcast_plugins(post2.content.cmsplugin_set.all())
self.assertEqual(set(new[0].tags.all()), set([tag]))
def test_plugin_author(self):
post_1 = self._get_post(self.data['en'][0])
post_2 = self._get_post(self.data['en'][1])
plugin = add_plugin(post_1.content, 'BlogAuthorPostsPlugin', language='en')
post1 = self._get_post(self.data['en'][0])
post2 = self._get_post(self.data['en'][1])
plugin = add_plugin(post1.content, 'BlogAuthorPostsPlugin', language='en')
plugin.authors.add(self.user)
self.assertEqual(len(plugin.get_posts()), 0)
self.assertEqual(plugin.get_authors()[0].count, 0)
post_1.publish = True
post_1.save()
post1.publish = True
post1.save()
self.assertEqual(len(plugin.get_posts()), 1)
self.assertEqual(plugin.get_authors()[0].count, 1)
post_2.publish = True
post_2.save()
post2.publish = True
post2.save()
self.assertEqual(len(plugin.get_posts()), 2)
self.assertEqual(plugin.get_authors()[0].count, 2)
def test_copy_plugin_author(self):
post_1 = self._get_post(self.data['en'][0])
post_2 = self._get_post(self.data['en'][1])
plugin = add_plugin(post_1.content, 'BlogAuthorPostsPlugin', language='en')
post1 = self._get_post(self.data['en'][0])
post2 = self._get_post(self.data['en'][1])
plugin = add_plugin(post1.content, 'BlogAuthorPostsPlugin', language='en')
plugin.authors.add(self.user)
plugins = list(post_1.content.cmsplugin_set.filter(language='en').order_by('tree_id', 'level', 'position'))
copy_plugins_to(plugins, post_2.content)
new = downcast_plugins(post_2.content.cmsplugin_set.all())
plugins = list(post1.content.cmsplugin_set.filter(language='en').order_by('tree_id', 'level', 'position'))
copy_plugins_to(plugins, post2.content)
new = downcast_plugins(post2.content.cmsplugin_set.all())
self.assertEqual(set(new[0].authors.all()), set([self.user]))

View file

@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
from datetime import date
import re
from cms.api import add_plugin
from django.core.urlresolvers import reverse
@ -12,11 +13,11 @@ class PluginTest(BaseTest):
def test_plugin_latest(self):
page1, page2 = self.get_pages()
post_1 = self._get_post(self.data['en'][0])
post_2 = self._get_post(self.data['en'][1])
post_1.tags.add('tag 1')
post_1.publish = True
post_1.save()
post1 = self._get_post(self.data['en'][0])
post2 = self._get_post(self.data['en'][1])
post1.tags.add('tag 1')
post1.publish = True
post1.save()
ph = page1.placeholders.get(slot='placeholder')
plugin = add_plugin(ph, 'BlogLatestEntriesPlugin', language='en')
tag = Tag.objects.get(slug='tag-1')
@ -28,16 +29,16 @@ class PluginTest(BaseTest):
self.assertTrue(rendered.find(reverse('djangocms_blog:posts-tagged', kwargs={'tag': tag.slug})) > -1)
self.assertTrue(rendered.find('<p>first line</p>') > -1)
self.assertTrue(rendered.find('<article id="post-first-post"') > -1)
self.assertTrue(rendered.find(post_1.get_absolute_url()) > -1)
self.assertTrue(rendered.find(post1.get_absolute_url()) > -1)
def test_plugin_authors(self):
page1, page2 = self.get_pages()
post_1 = self._get_post(self.data['en'][0])
post_2 = self._get_post(self.data['en'][1])
post_1.publish = True
post_1.save()
post_2.publish = True
post_2.save()
post1 = self._get_post(self.data['en'][0])
post2 = self._get_post(self.data['en'][1])
post1.publish = True
post1.save()
post2.publish = True
post2.save()
ph = page1.placeholders.get(slot='placeholder')
plugin = add_plugin(ph, 'BlogAuthorPostsPlugin', language='en')
plugin.authors.add(self.user)
@ -49,14 +50,14 @@ class PluginTest(BaseTest):
def test_plugin_tags(self):
page1, page2 = self.get_pages()
post_1 = self._get_post(self.data['en'][0])
post_2 = self._get_post(self.data['en'][1])
post_1.tags.add('tag 1', 'tag 2', 'test tag')
post_1.publish = True
post_1.save()
post_2.tags.add('test tag', 'another tag')
post_2.publish = True
post_2.save()
post1 = self._get_post(self.data['en'][0])
post2 = self._get_post(self.data['en'][1])
post1.tags.add('tag 1', 'tag 2', 'test tag')
post1.publish = True
post1.save()
post2.tags.add('test tag', 'another tag')
post2.publish = True
post2.save()
ph = page1.placeholders.get(slot='placeholder')
plugin = add_plugin(ph, 'BlogTagsPlugin', language='en')
request = self.get_page_request(page1, self.user, r'/en/blog/', lang_code='en', edit=True)
@ -70,3 +71,37 @@ class PluginTest(BaseTest):
rf = '\s+%s\s+<span>\(\s+%s article' % (tag.name, 1)
rx = re.compile(rf)
self.assertEqual(len(rx.findall(rendered)), 1)
def test_blog_category_plugin(self):
page1, page2 = self.get_pages()
post1, post2 = self.get_posts()
post1.publish = True
post1.save()
post2.publish = True
post2.save()
ph = page1.placeholders.get(slot='placeholder')
plugin = add_plugin(ph, 'BlogCategoryPlugin', language='en')
request = self.get_page_request(page1, self.user, r'/en/blog/', lang_code='en', edit=True)
plugin_class = plugin.get_plugin_class_instance()
context = plugin_class.render(RequestContext(request, {}), plugin, ph)
self.assertTrue(context['categories'])
self.assertEqual(list(context['categories']), [self.category_1])
def test_blog_archive_plugin(self):
page1, page2 = self.get_pages()
post1, post2 = self.get_posts()
post1.publish = True
post1.save()
post2.publish = True
post2.save()
ph = page1.placeholders.get(slot='placeholder')
plugin = add_plugin(ph, 'BlogArchivePlugin', language='en')
request = self.get_page_request(page1, self.user, r'/en/blog/', lang_code='en', edit=True)
plugin_class = plugin.get_plugin_class_instance()
context = plugin_class.render(RequestContext(request, {}), plugin, ph)
self.assertEqual(context['dates'][0], {'date': date(year=date.today().year, month=date.today().month, day=1), 'count': 2})
post2.publish = False
post2.save()
context = plugin_class.render(RequestContext(request, {}), plugin, ph)
self.assertEqual(context['dates'][0], {'date': date(year=date.today().year, month=date.today().month, day=1), 'count': 1})

View file

@ -3,7 +3,10 @@ from datetime import date
from django.contrib.auth.models import AnonymousUser
from django.http import Http404
from django.utils.translation import activate
from djangocms_blog.views import PostListView, PostDetailView, PostArchiveView
from djangocms_blog.feeds import LatestEntriesFeed, TagFeed
from djangocms_blog.sitemaps import BlogSitemap
from djangocms_blog.views import PostListView, PostDetailView, PostArchiveView, \
CategoryEntriesView, AuthorEntriesView, TaggedListView
from . import BaseTest
@ -12,32 +15,25 @@ class ViewTest(BaseTest):
def test_post_list_view(self):
page1, page2 = self.get_pages()
post_1 = self._get_post(self.data['en'][0])
post_1 = self._get_post(self.data['it'][0], post_1, 'it')
post_1.publish = True
post_1.save()
post_1.set_current_language('en')
post_2 = self._get_post(self.data['en'][1])
post_2 = self._get_post(self.data['it'][1], post_2, 'it')
post_2.set_current_language('en')
post1, post2 = self.get_posts()
request = self.get_page_request(page1, AnonymousUser(), r'/en/blog/', edit=False)
activate('en')
view_obj = PostListView()
view_obj.request = request
self.assertEqual(list(view_obj.get_queryset()), [post_1])
self.assertEqual(list(view_obj.get_queryset()), [post1])
request = self.get_page_request(page1, self.user, r'/en/blog/', edit=False)
view_obj.request = request
self.assertEqual(set(view_obj.get_queryset()), set([post_1, post_2]))
self.assertEqual(set(view_obj.get_queryset()), set([post1, post2]))
view_obj.kwargs = {}
view_obj.object_list = view_obj.get_queryset()
view_obj.paginate_by = 1
context = view_obj.get_context_data(object_list=view_obj.object_list)
self.assertTrue(context['is_paginated'])
self.assertEqual(list(context['post_list']), [post_2])
self.assertEqual(list(context['post_list']), [post2])
self.assertEqual(context['paginator'].count, 2)
self.assertEqual(context['post_list'][0].title, 'Second post')
response = view_obj.render_to_response(context)
@ -54,14 +50,7 @@ class ViewTest(BaseTest):
def test_post_detail_view(self):
page1, page2 = self.get_pages()
post_1 = self._get_post(self.data['en'][0])
post_1 = self._get_post(self.data['it'][0], post_1, 'it')
post_1.publish = True
post_1.save()
post_1.set_current_language('en')
post_2 = self._get_post(self.data['en'][1])
post_2 = self._get_post(self.data['it'][1], post_2, 'it')
post_2.set_current_language('en')
post1, post2 = self.get_posts()
request = self.get_page_request(page1, AnonymousUser(), r'/en/blog/', edit=False)
activate('en')
@ -72,34 +61,29 @@ class ViewTest(BaseTest):
view_obj.kwargs = {'slug': 'not-existing'}
post_obj = view_obj.get_object()
view_obj.kwargs = {'slug': post_1.slug}
view_obj.kwargs = {'slug': post1.slug}
post_obj = view_obj.get_object()
self.assertEqual(post_obj, post_1)
self.assertEqual(post_obj, post1)
self.assertEqual(post_obj.language_code, 'en')
request = self.get_page_request(page1, AnonymousUser(), r'/it/blog/', lang_code='it', edit=False)
activate('it')
post1.set_current_language('it')
view_obj.request = request
view_obj.kwargs = {'slug': post1.slug}
post_obj = view_obj.get_object()
self.assertEqual(post_obj, post_1)
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'], post_1)
self.assertEqual(context['post'], post1)
self.assertEqual(context['post'].language_code, 'it')
self.assertTrue(context['meta'])
def test_post_archive_view(self):
page1, page2 = self.get_pages()
post_1 = self._get_post(self.data['en'][0])
post_1 = self._get_post(self.data['it'][0], post_1, 'it')
post_1.publish = True
post_1.save()
post_1.set_current_language('en')
post_2 = self._get_post(self.data['en'][1])
post_2 = self._get_post(self.data['it'][1], post_2, 'it')
post_2.set_current_language('en')
post1, post2 = self.get_posts()
request = self.get_page_request(page1, AnonymousUser(), r'/en/blog/', edit=False)
activate('en')
@ -110,8 +94,122 @@ class ViewTest(BaseTest):
# One post only, anonymous request
qs = view_obj.get_queryset()
self.assertEqual(qs.count(), 1)
self.assertEqual(list(qs), [post_1])
self.assertEqual(list(qs), [post1])
view_obj.object_list = qs
context = view_obj.get_context_data(object_list=view_obj.object_list)
self.assertEqual(context['archive_date'], date(year=date.today().year, month=date.today().month, day=1))
def test_category_entries_view(self):
page1, page2 = self.get_pages()
post1, post2 = self.get_posts()
request = self.get_page_request(page1, self.user, r'/en/blog/', edit=False)
activate('en')
view_obj = CategoryEntriesView()
view_obj.request = request
view_obj.kwargs = {'category': 'category-1'}
qs = view_obj.get_queryset()
self.assertEqual(qs.count(), 2)
self.assertEqual(set(qs), set([post1, post2]))
view_obj.paginate_by = 1
view_obj.object_list = qs
context = view_obj.get_context_data(object_list=view_obj.object_list)
self.assertTrue(context['category'])
self.assertEqual(context['category'], self.category_1)
self.assertTrue(context['is_paginated'])
self.assertEqual(list(context['post_list']), [post2])
self.assertEqual(context['paginator'].count, 2)
self.assertEqual(context['post_list'][0].title, 'Second post')
def test_author_entries_view(self):
page1, page2 = self.get_pages()
post1, post2 = self.get_posts()
request = self.get_page_request(page1, self.user, r'/en/blog/', edit=False)
activate('en')
view_obj = AuthorEntriesView()
view_obj.request = request
view_obj.kwargs = {'username': 'admin'}
qs = view_obj.get_queryset()
self.assertEqual(qs.count(), 2)
self.assertEqual(set(qs), set([post1, post2]))
view_obj.paginate_by = 1
view_obj.object_list = qs
context = view_obj.get_context_data(object_list=view_obj.object_list)
self.assertTrue(context['author'])
self.assertEqual(context['author'], self.user)
self.assertTrue(context['is_paginated'])
self.assertEqual(list(context['post_list']), [post2])
self.assertEqual(context['paginator'].count, 2)
self.assertEqual(context['post_list'][0].title, 'Second post')
def test_taggedlist_view(self):
page1, page2 = self.get_pages()
post1, post2 = self.get_posts()
post1.tags.add('tag 1', 'tag 2', 'tag 3', 'tag 4')
post1.save()
post2.tags.add('tag 6', 'tag 2', 'tag 5', 'tag 8')
post2.save()
request = self.get_page_request(page1, self.user, r'/en/blog/', edit=False)
activate('en')
view_obj = TaggedListView()
view_obj.request = request
view_obj.kwargs = {'tag': 'tag-2'}
qs = view_obj.get_queryset()
self.assertEqual(qs.count(), 2)
self.assertEqual(set(qs), set([post1, post2]))
view_obj.paginate_by = 1
view_obj.object_list = qs
context = view_obj.get_context_data(object_list=view_obj.object_list)
self.assertTrue(context['tagged_entries'], 'tag-2')
self.assertTrue(context['is_paginated'])
self.assertEqual(list(context['post_list']), [post2])
self.assertEqual(context['paginator'].count, 2)
self.assertEqual(context['post_list'][0].title, 'Second post')
def test_feed(self):
page1, page2 = self.get_pages()
post1, post2 = self.get_posts()
post1.tags.add('tag 1', 'tag 2', 'tag 3', 'tag 4')
post1.save()
post2.tags.add('tag 6', 'tag 2', 'tag 5', 'tag 8')
post2.save()
post1.set_current_language('en')
feed = LatestEntriesFeed()
self.assertEqual(list(feed.items()), [post1])
request = self.get_page_request(page1, self.user, r'/en/blog/', lang_code='en', edit=False)
xml = feed(request)
self.assertContains(xml, post1.get_absolute_url())
self.assertContains(xml, 'Blog articles on example.com')
activate('it')
post1.set_current_language('it')
feed = LatestEntriesFeed()
self.assertEqual(list(feed.items()), [post1])
request = self.get_page_request(page1, self.user, r'/it/blog/', lang_code='it', edit=False)
xml = feed(request)
self.assertContains(xml, post1.get_absolute_url())
self.assertContains(xml, 'Articoli del blog su example.com')
feed = TagFeed()
self.assertEqual(list(feed.items('tag-2')), [post1])
def test_sitemap(self):
post1, post2 = self.get_posts()
post1.tags.add('tag 1', 'tag 2', 'tag 3', 'tag 4')
post1.save()
post2.tags.add('tag 6', 'tag 2', 'tag 5', 'tag 8')
post2.publish = True
post2.save()
post1.set_current_language('en')
sitemap = BlogSitemap()
self.assertEqual(sitemap.items().count(), 2)
for item in sitemap.items():
self.assertTrue(sitemap.lastmod(item).date(), date.today())