Template html renderer tests (#4817)

This commit is contained in:
Artem Muterko 2017-01-16 20:16:43 +02:00 committed by Tom Christie
parent 50c2d71a80
commit e965270f51

View File

@ -1,8 +1,9 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import django.template.loader import django.template.loader
import pytest
from django.conf.urls import url from django.conf.urls import url
from django.core.exceptions import PermissionDenied from django.core.exceptions import ImproperlyConfigured, PermissionDenied
from django.http import Http404 from django.http import Http404
from django.template import Template, TemplateDoesNotExist from django.template import Template, TemplateDoesNotExist
from django.test import TestCase, override_settings from django.test import TestCase, override_settings
@ -46,6 +47,12 @@ urlpatterns = [
@override_settings(ROOT_URLCONF='tests.test_htmlrenderer') @override_settings(ROOT_URLCONF='tests.test_htmlrenderer')
class TemplateHTMLRendererTests(TestCase): class TemplateHTMLRendererTests(TestCase):
def setUp(self): def setUp(self):
class MockResponse(object):
template_name = None
self.mock_response = MockResponse()
self._monkey_patch_get_template()
def _monkey_patch_get_template(self):
""" """
Monkeypatch get_template Monkeypatch get_template
""" """
@ -87,6 +94,40 @@ class TemplateHTMLRendererTests(TestCase):
self.assertEqual(response.content, six.b("403 Forbidden")) self.assertEqual(response.content, six.b("403 Forbidden"))
self.assertEqual(response['Content-Type'], 'text/html; charset=utf-8') self.assertEqual(response['Content-Type'], 'text/html; charset=utf-8')
# 2 tests below are based on order of if statements in corresponding method
# of TemplateHTMLRenderer
def test_get_template_names_returns_own_template_name(self):
renderer = TemplateHTMLRenderer()
renderer.template_name = 'test_template'
template_name = renderer.get_template_names(self.mock_response, view={})
assert template_name == ['test_template']
def test_get_template_names_returns_view_template_name(self):
renderer = TemplateHTMLRenderer()
class MockResponse(object):
template_name = None
class MockView(object):
def get_template_names(self):
return ['template from get_template_names method']
class MockView2(object):
template_name = 'template from template_name attribute'
template_name = renderer.get_template_names(self.mock_response,
MockView())
assert template_name == ['template from get_template_names method']
template_name = renderer.get_template_names(self.mock_response,
MockView2())
assert template_name == ['template from template_name attribute']
def test_get_template_names_raises_error_if_no_template_found(self):
renderer = TemplateHTMLRenderer()
with pytest.raises(ImproperlyConfigured):
renderer.get_template_names(self.mock_response, view=object())
@override_settings(ROOT_URLCONF='tests.test_htmlrenderer') @override_settings(ROOT_URLCONF='tests.test_htmlrenderer')
class TemplateHTMLRendererExceptionTests(TestCase): class TemplateHTMLRendererExceptionTests(TestCase):