From e965270f518e01a08ebbf46a1ce7780389c35919 Mon Sep 17 00:00:00 2001 From: Artem Muterko Date: Mon, 16 Jan 2017 20:16:43 +0200 Subject: [PATCH] Template html renderer tests (#4817) --- tests/test_htmlrenderer.py | 43 +++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/tests/test_htmlrenderer.py b/tests/test_htmlrenderer.py index 3dfe6ef74..c49fc96d4 100644 --- a/tests/test_htmlrenderer.py +++ b/tests/test_htmlrenderer.py @@ -1,8 +1,9 @@ from __future__ import unicode_literals import django.template.loader +import pytest 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.template import Template, TemplateDoesNotExist from django.test import TestCase, override_settings @@ -46,6 +47,12 @@ urlpatterns = [ @override_settings(ROOT_URLCONF='tests.test_htmlrenderer') class TemplateHTMLRendererTests(TestCase): 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 """ @@ -87,6 +94,40 @@ class TemplateHTMLRendererTests(TestCase): self.assertEqual(response.content, six.b("403 Forbidden")) 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') class TemplateHTMLRendererExceptionTests(TestCase):