Files
django-test/apps/base/tests/test_templates.py

77 lines
2.5 KiB
Python

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.conf import settings
from django.template.context import Context
from django.test import SimpleTestCase
class BaseTemplateTestCase(SimpleTestCase):
def assertInHTML_multi(self, response, needles, format_kwargs=None):
content = response.content.decode('utf-8')
for needle in needles:
if format_kwargs is not None:
needle = needle.format(**format_kwargs)
self.assertInHTML(needle, content)
def setUp(self):
super(BaseTemplateTestCase, self).setUp()
self.response = self.client.get('/')
self.static_url = settings.STATIC_URL
self.base_prefix = 'base/'
def test_template_usage(self):
response = self.response
self.assertTemplateUsed(response, 'base/base.html')
def test_local_css_link(self):
response = self.response
format_kwargs = {
'static_url': self.static_url,
'base_prefix': self.base_prefix,
}
needles = (
'<link type="text/css" href="{static_url}{base_prefix}css/local.css" rel="stylesheet" />',
)
self.assertInHTML_multi(response, needles, format_kwargs)
def test_bootstrap_css_links(self):
response = self.response
format_kwargs = {
'static_url': self.static_url,
'base_prefix': self.base_prefix,
}
needles = (
# bootstrap css
'<link type="text/css" href="{static_url}{base_prefix}bootstrap/css/bootstrap.min.css"'
' rel="stylesheet" />',
# jquery.js file
'<script type="text/javascript" src="{static_url}{base_prefix}js/jquery.min.js"></script>',
# bootstrap js file
'<script type="text/javascript" src="{static_url}{base_prefix}bootstrap/js/bootstrap.min.js"></script>',
)
self.assertInHTML_multi(response, needles, format_kwargs)
def test_page_footer(self):
response = self.response
self.assertTemplateUsed(response, 'signum.html')
for template in response.templates:
if template.name == 'signum.html':
signum = template.render(Context({}))
break
else: # pragma: no cover
self.fail('Cannot find signum template.')
needle = """
<div id="page-footer">
<div class="signum">{signum}</div>
</div>
""".format(signum=signum)
self.assertInHTML(needle, response.content.decode('utf-8'))