Permalink
Browse files

Renderer tests (#4791)

  • Loading branch information...
1 parent 99d57df commit b6b2c4bb5b4eb3e3e48d8f43b6a62858e6683771 @micgeronimo micgeronimo committed with Jan 8, 2017
Showing with 82 additions and 2 deletions.
  1. +1 −1 rest_framework/renderers.py
  2. +81 −1 tests/test_renderers.py
@@ -228,7 +228,7 @@ class StaticHTMLRenderer(TemplateHTMLRenderer):
def render(self, data, accepted_media_type=None, renderer_context=None):
renderer_context = renderer_context or {}
- response = renderer_context['response']
+ response = renderer_context.get('response')
if response and response.exception:
request = renderer_context['request']
@@ -5,18 +5,22 @@
import re
from collections import MutableMapping, OrderedDict
+import pytest
from django.conf.urls import include, url
from django.core.cache import cache
from django.db import models
+from django.http.request import HttpRequest
from django.test import TestCase, override_settings
from django.utils import six
from django.utils.safestring import SafeText
from django.utils.translation import ugettext_lazy as _
from rest_framework import permissions, serializers, status
from rest_framework.renderers import (
- BaseRenderer, BrowsableAPIRenderer, HTMLFormRenderer, JSONRenderer
+ AdminRenderer, BaseRenderer, BrowsableAPIRenderer,
+ HTMLFormRenderer, JSONRenderer, StaticHTMLRenderer
)
+from rest_framework.request import Request
from rest_framework.response import Response
from rest_framework.settings import api_settings
from rest_framework.test import APIRequestFactory
@@ -269,6 +273,18 @@ def strip_trailing_whitespace(content):
return re.sub(' +\n', '\n', content)
+class BaseRendererTests(TestCase):
+ """
+ Tests BaseRenderer
+ """
+ def test_render_raise_error(self):
+ """
+ BaseRenderer.render should raise NotImplementedError
+ """
+ with pytest.raises(NotImplementedError):
+ BaseRenderer().render('test')
+
+
class JSONRendererTests(TestCase):
"""
Tests specific to the JSON Renderer
@@ -568,3 +584,67 @@ class TestSerializer(serializers.Serializer):
result)
self.assertInHTML('<option value="1">Option1</option>', result)
self.assertInHTML('<option value="2">Option2</option>', result)
+
+
+class StaticHTMLRendererTests(TestCase):
+ """
+ Tests specific for Static HTML Renderer
+ """
+ def setUp(self):
+ self.renderer = StaticHTMLRenderer()
+
+ def test_static_renderer(self):
+ data = '<html><body>text</body></html>'
+ result = self.renderer.render(data)
+ assert result == data
+
+ def test_static_renderer_with_exception(self):
+ context = {
+ 'response': Response(status=500, exception=True),
+ 'request': Request(HttpRequest())
+ }
+ result = self.renderer.render({}, renderer_context=context)
+ assert result == '500 Internal Server Error'
+
+
+class BrowsableAPIRendererTests(TestCase):
+
+ def setUp(self):
+ self.renderer = BrowsableAPIRenderer()
+
+ def test_get_description_returns_empty_string_for_401_and_403_statuses(self):
+ assert self.renderer.get_description({}, status_code=401) == ''
+ assert self.renderer.get_description({}, status_code=403) == ''
+
+ def test_get_filter_form_returns_none_if_data_is_not_list_instance(self):
+ class DummyView(object):
+ get_queryset = None
+ filter_backends = None
+
+ result = self.renderer.get_filter_form(data='not list',
+ view=DummyView(), request={})
+ assert result is None
+
+
+class AdminRendererTests(TestCase):
+
+ def setUp(self):
+ self.renderer = AdminRenderer()
+
+ def test_render_when_resource_created(self):
+ class DummyView(APIView):
+ renderer_classes = (AdminRenderer, )
+ request = Request(HttpRequest())
+ request.build_absolute_uri = lambda: 'http://example.com'
+ response = Response(status=201, headers={'Location': '/test'})
+ context = {
+ 'view': DummyView(),
+ 'request': request,
+ 'response': response
+ }
+
+ result = self.renderer.render(data={'test': 'test'},
+ renderer_context=context)
+ assert result == ''
+ assert response.status_code == status.HTTP_303_SEE_OTHER
+ assert response['Location'] == 'http://example.com'

0 comments on commit b6b2c4b

Please sign in to comment.