From 4bf184e2a2deab74b47f807a04456935885983a7 Mon Sep 17 00:00:00 2001 From: Evgeny Fadeev Date: Wed, 29 Sep 2010 21:17:22 -0400 Subject: test cases work for jinja templates as well --- .gitignore | 1 + askbot/tests/page_load_tests.py | 58 +++++++++++++++++++++++++++-------------- askbot/views/readers.py | 8 +++--- 3 files changed, 42 insertions(+), 25 deletions(-) diff --git a/.gitignore b/.gitignore index 37be4274..dc9a7c81 100755 --- a/.gitignore +++ b/.gitignore @@ -29,3 +29,4 @@ build askbot/upfiles/*.jpg askbot/upfiles/*.gif askbot/skins/common/media/mathjax/ +/jinja2 diff --git a/askbot/tests/page_load_tests.py b/askbot/tests/page_load_tests.py index 87537e58..ee5d304f 100644 --- a/askbot/tests/page_load_tests.py +++ b/askbot/tests/page_load_tests.py @@ -1,8 +1,24 @@ -from django.test import TestCase +from django.test import TestCase, signals +from jinja2.environment import Template as Jinja2Template from django.template import defaultfilters +from django.core.management import call_command from django.core.urlresolvers import reverse +import coffin.template from askbot import models from askbot.utils.slug import slugify +import sys + +#note - this code can be run only once +ORIG_JINJA2_RENDERER = Jinja2Template.render +def instrumented_render(template_object, *args, **kwargs): + context = dict(*args, **kwargs) + signals.template_rendered.send( + sender=template_object, + template=template_object, + context=context + ) + return ORIG_JINJA2_RENDERER(template_object, *args, **kwargs) +Jinja2Template.render = instrumented_render class PageLoadTestCase(TestCase): def try_url( @@ -24,9 +40,13 @@ class PageLoadTestCase(TestCase): self.assertEqual(r.status_code, status_code) if template: - #asuming that there is more than one template - print 'templates are %s' % ','.join([t.name for t in r.template]) - self.assertEqual(r.template[0].name, template) + if isinstance(r.template, coffin.template.Template): + self.assertEqual(r.template.name, template) + else: + #asuming that there is more than one template + template_names = ','.join([t.name for t in r.template]) + print 'templates are %s' % template_names + self.assertEqual(r.template[0].name, template) class PageLoadTests(PageLoadTestCase): fixtures = ['tmp/fixture1.json', ] @@ -37,9 +57,7 @@ class PageLoadTests(PageLoadTestCase): self.assertEqual(response.status_code, 200) self.failUnless(len(response.redirect_chain) == 1) self.failUnless(response.redirect_chain[0][0].endswith('/questions/')) - c = response.context[0] - t = response.template[0] - self.assertEqual(t.name, 'questions.html') + self.assertEquals(response.template.name, 'questions.jinja.html') def proto_test_non_user_urls(self): """test all reader views thoroughly @@ -65,67 +83,67 @@ class PageLoadTests(PageLoadTestCase): #todo: test different sort methods and scopes self.try_url( 'questions', - template='questions.html' + template='questions.jinja.html' ) self.try_url( 'questions', data={'start_over':'true'}, - template='questions.html' + template='questions.jinja.html' ) self.try_url( 'questions', data={'scope':'unanswered'}, - template='questions.html' + template='questions.jinja.html' ) self.try_url( 'questions', data={'scope':'all'}, - template='questions.html' + template='questions.jinja.html' ) self.try_url( 'questions', data={'scope':'favorite'}, - template='questions.html' + template='questions.jinja.html' ) self.try_url( 'questions', data={'scope':'unanswered', 'sort':'latest'}, - template='questions.html' + template='questions.jinja.html' ) self.try_url( 'questions', data={'scope':'unanswered', 'sort':'oldest'}, - template='questions.html' + template='questions.jinja.html' ) self.try_url( 'questions', data={'scope':'unanswered', 'sort':'active'}, - template='questions.html' + template='questions.jinja.html' ) self.try_url( 'questions', data={'scope':'unanswered', 'sort':'inactive'}, - template='questions.html' + template='questions.jinja.html' ) self.try_url( 'questions', data={'sort':'hottest'}, - template='questions.html' + template='questions.jinja.html' ) self.try_url( 'questions', data={'sort':'coldest'}, - template='questions.html' + template='questions.jinja.html' ) self.try_url( 'questions', data={'sort':'mostvoted'}, - template='questions.html' + template='questions.jinja.html' ) self.try_url( 'questions', data={'sort':'leastvoted'}, - template='questions.html' + template='questions.jinja.html' ) self.try_url( 'question', diff --git a/askbot/views/readers.py b/askbot/views/readers.py index 078accac..934be675 100644 --- a/askbot/views/readers.py +++ b/askbot/views/readers.py @@ -344,14 +344,12 @@ def questions(request): #print simplejson.dumps(output) return HttpResponse(simplejson.dumps(output), mimetype='application/json') else: - before = datetime.datetime.now() + #before = datetime.datetime.now() template = ENV.get_template('questions.jinja.html') response = HttpResponse(template.render(template_context)) - after = datetime.datetime.now() - print after - before + #after = datetime.datetime.now() + #print after - before return response - #after = datetime.datetime.now() - #print 'time to render %s' % (after - before) def search(request): #generates listing of questions matching a search query - including tags and just words """redirects to people and tag search pages -- cgit v1.2.3-1-g7c22