Skip to content

Commit

Permalink
add tests for {% chunk %} template tag
Browse files Browse the repository at this point in the history
  • Loading branch information
igorsobreira committed Jul 14, 2012
1 parent c9a9be9 commit fef8056
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
66 changes: 64 additions & 2 deletions chunks/tests.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,68 @@
from django.test import TestCase
from django.template import Context, Template, TemplateSyntaxError
from django.core.cache import cache

from chunks.models import Chunk

class ChuckTemplateTagTestCase(TestCase):

def test_should_pass(self):
assert 1
def setUp(self):
self.home_page_left = Chunk.objects.create(
key='home_page_left',
content='This is the content for left box')
cache.delete('cache_home_page_left')


def test_should_render_content_from_key(self):
result = self.render_template('{% load chunks %}'
'<div>{% chunk "home_page_left" %}</div>')

self.assertEquals('<div>This is the content for left box</div>', result)


def test_should_render_empty_string_if_key_not_found(self):
result = self.render_template('{% load chunks %}'
'<div>{% chunk "key_not_found" %}</div>')

self.assertEquals('<div></div>', result)


def test_should_cache_rendered_content(self):
cache_key = 'chunk_home_page_left'
self.assertFalse(cache.get(cache_key), "key %r should NOT be cached" % cache_key)

self.render_template("{% load chunks %}"
"<div>{% chunk 'home_page_left' 10 %}</div>")
cached_result = cache.get(cache_key)

self.assertTrue(cached_result, "key %r should be cached" % cache_key)
self.assertEquals('This is the content for left box', cached_result.content)


def test_should_fail_if_wrong_number_of_arguments(self):
with self.assertRaisesRegexp(TemplateSyntaxError, "'chunk' tag should have either 2 or 3 arguments"):
self.render_template('{% load chunks %}'
'{% chunk %}')

with self.assertRaisesRegexp(TemplateSyntaxError, "'chunk' tag should have either 2 or 3 arguments"):
self.render_template('{% load chunks %}'
'{% chunk "home_page_left" 10 "invalid" %}')

with self.assertRaisesRegexp(TemplateSyntaxError, "'chunk' tag should have either 2 or 3 arguments"):
self.render_template('{% load chunks %}'
'{% chunk "home_page_left" 10 too much invalid arguments %}')


def test_should_fail_if_key_not_quoted(self):
with self.assertRaisesRegexp(TemplateSyntaxError, "'chunk' tag's argument should be in quotes"):
self.render_template('{% load chunks %}'
'{% chunk home_page_left %}')

with self.assertRaisesRegexp(TemplateSyntaxError, "'chunk' tag's argument should be in quotes"):
self.render_template('{% load chunks %}'
'{% chunk "home_page_left\' %}')


def render_template(self, content_string, context={}):
template = Template(content_string)
return template.render(Context(context))
5 changes: 5 additions & 0 deletions runtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
'NAME': os.path.join(os.path.dirname(__file__), 'testdb.sqlite'),
}
},
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
}
},
INSTALLED_APPS = ('chunks',)
)

Expand Down

0 comments on commit fef8056

Please sign in to comment.