Better RequestContext handling in Django 1.8 during tests

This commit is contained in:
Iacopo Spalletti 2015-09-01 01:05:17 +02:00
parent 87ffaf6d3f
commit 98c0e1964d

View file

@ -4,6 +4,7 @@ import re
from cms.api import add_plugin
from django.core.urlresolvers import reverse
from django.template import RequestContext
from django.template.loader import get_template
from django.utils.timezone import now
from djangocms_blog.models import BlogCategory
from taggit.models import Tag
@ -26,8 +27,14 @@ class PluginTest(BaseTest):
tag = Tag.objects.get(slug='tag-1')
plugin.tags.add(tag)
request = self.get_page_request(page1, self.user, r'/en/blog/', lang_code='en', edit=True)
context = RequestContext(request, {'request': request})
rendered = plugin.render_plugin(context, ph)
context = RequestContext(request)
try:
template = get_template('page.html').template
with context.bind_template(template):
rendered = plugin.render_plugin(context, ph)
except AttributeError:
rendered = plugin.render_plugin(context, ph)
self.assertTrue(rendered.find('cms_plugin-djangocms_blog-post-abstract-1') > -1)
self.assertTrue(rendered.find(reverse('djangocms_blog:posts-tagged', kwargs={'tag': tag.slug})) > -1)
self.assertTrue(rendered.find('<p>first line</p>') > -1)
@ -43,8 +50,13 @@ class PluginTest(BaseTest):
plugin = add_plugin(ph, 'BlogLatestEntriesPlugin', language='en')
plugin.categories.add(category_2)
request = self.get_page_request(page1, self.user, r'/en/blog/', lang_code='en', edit=True)
context = RequestContext(request, {'request': request})
rendered = plugin.render_plugin(context, ph)
context = RequestContext(request)
try:
template = get_template('page.html').template
with context.bind_template(template):
rendered = plugin.render_plugin(context, ph)
except AttributeError:
rendered = plugin.render_plugin(context, ph)
self.assertTrue(rendered.find('cms_plugin-djangocms_blog-post-abstract-2') > -1)
self.assertTrue(rendered.find(reverse('djangocms_blog:posts-category', kwargs={'category': category_2.slug})) > -1)
self.assertTrue(rendered.find('<p>second post first line</p>') > -1)
@ -63,8 +75,13 @@ class PluginTest(BaseTest):
plugin = add_plugin(ph, 'BlogAuthorPostsPlugin', language='en')
plugin.authors.add(self.user)
request = self.get_page_request(page1, self.user, r'/en/blog/', lang_code='en', edit=True)
context = RequestContext(request, {'request': request})
rendered = plugin.render_plugin(context, ph)
context = RequestContext(request)
try:
template = get_template('page.html').template
with context.bind_template(template):
rendered = plugin.render_plugin(context, ph)
except AttributeError:
rendered = plugin.render_plugin(context, ph)
self.assertTrue(rendered.find(reverse('djangocms_blog:posts-author', kwargs={'username': self.user.get_username()})) > -1)
self.assertTrue(rendered.find('2 articles') > -1)
@ -81,8 +98,13 @@ class PluginTest(BaseTest):
ph = page1.placeholders.get(slot='content')
plugin = add_plugin(ph, 'BlogTagsPlugin', language='en')
request = self.get_page_request(page1, self.user, r'/en/blog/', lang_code='en', edit=True)
context = RequestContext(request, {'request': request})
rendered = plugin.render_plugin(context, ph).replace("\n", "")
context = RequestContext(request)
try:
template = get_template('page.html').template
with context.bind_template(template):
rendered = plugin.render_plugin(context, ph).replace('\n', '')
except AttributeError:
rendered = plugin.render_plugin(context, ph).replace('\n', '')
for tag in Tag.objects.all():
self.assertTrue(rendered.find(reverse('djangocms_blog:posts-tagged', kwargs={'tag': tag.slug})) > -1)
if tag.slug == 'test-tag':
@ -103,7 +125,13 @@ class PluginTest(BaseTest):
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, {'request': request}), plugin, ph)
context = RequestContext(request)
try:
template = get_template('page.html').template
with context.bind_template(template):
context = plugin_class.render(context, plugin, ph)
except AttributeError:
context = plugin_class.render(context, plugin, ph)
self.assertTrue(context['categories'])
self.assertEqual(list(context['categories']), [self.category_1])
@ -118,7 +146,13 @@ class PluginTest(BaseTest):
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, {'request': request}), plugin, ph)
context = RequestContext(request)
try:
template = get_template('page.html').template
with context.bind_template(template):
context = plugin_class.render(context, plugin, ph)
except AttributeError:
context = plugin_class.render(context, plugin, ph)
self.assertEqual(context['dates'][0]['date'].date(), now().replace(year=now().year, month=now().month, day=1).date())
self.assertEqual(context['dates'][0]['count'], 2)