From 98c0e1964d63b957146058f76a265a82bf5ff72b Mon Sep 17 00:00:00 2001 From: Iacopo Spalletti Date: Tue, 1 Sep 2015 01:05:17 +0200 Subject: [PATCH] Better RequestContext handling in Django 1.8 during tests --- tests/test_plugins.py | 54 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 44 insertions(+), 10 deletions(-) diff --git a/tests/test_plugins.py b/tests/test_plugins.py index 664e364..7c9e789 100644 --- a/tests/test_plugins.py +++ b/tests/test_plugins.py @@ -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('

first line

') > -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('

second post first line

') > -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)