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'
def setUp(self): self.renderer = AdminRenderer()
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' def test_render_dict(self): factory = APIRequestFactory() class DummyView(APIView): renderer_classes = (AdminRenderer, ) def get(self, request): return Response({'foo': 'a string'}) view = DummyView.as_view() request = factory.get('/') response = view(request) response.render() self.assertContains(response, '<tr><th>Foo</th><td>a string</td></tr>', html=True) def test_render_dict_with_items_key(self): factory = APIRequestFactory() class DummyView(APIView): renderer_classes = (AdminRenderer, ) def get(self, request): return Response({'items': 'a string'}) view = DummyView.as_view() request = factory.get('/') response = view(request) response.render() self.assertContains(response, '<tr><th>Items</th><td>a string</td></tr>', html=True) def test_render_dict_with_iteritems_key(self): factory = APIRequestFactory() class DummyView(APIView): renderer_classes = (AdminRenderer, ) def get(self, request): return Response({'iteritems': 'a string'}) view = DummyView.as_view() request = factory.get('/') response = view(request) response.render() self.assertContains(response, '<tr><th>Iteritems</th><td>a string</td></tr>', html=True)
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' def test_render_dict(self): factory = APIRequestFactory() class DummyView(APIView): renderer_classes = (AdminRenderer, ) def get(self, request): return Response({'foo': 'a string'}) view = DummyView.as_view() request = factory.get('/') response = view(request) response.render() self.assertContains(response, '<tr><th>Foo</th><td>a string</td></tr>', html=True) def test_render_dict_with_items_key(self): factory = APIRequestFactory() class DummyView(APIView): renderer_classes = (AdminRenderer, ) def get(self, request): return Response({'items': 'a string'}) view = DummyView.as_view() request = factory.get('/') response = view(request) response.render() self.assertContains(response, '<tr><th>Items</th><td>a string</td></tr>', html=True) def test_render_dict_with_iteritems_key(self): factory = APIRequestFactory() class DummyView(APIView): renderer_classes = (AdminRenderer, ) def get(self, request): return Response({'iteritems': 'a string'}) view = DummyView.as_view() request = factory.get('/') response = view(request) response.render() self.assertContains(response, '<tr><th>Iteritems</th><td>a string</td></tr>', html=True) def test_get_result_url(self): factory = APIRequestFactory() class DummyGenericViewsetLike(APIView): lookup_field = 'test' def reverse_action(view, *args, **kwargs): self.assertEqual(kwargs['kwargs']['test'], 1) return '/example/' # get the view instance instead of the view function view = DummyGenericViewsetLike.as_view() request = factory.get('/') response = view(request) view = response.renderer_context['view'] self.assertEqual(self.renderer.get_result_url({'test': 1}, view), '/example/') self.assertIsNone(self.renderer.get_result_url({}, view)) def test_get_result_url_no_result(self): factory = APIRequestFactory() class DummyView(APIView): lookup_field = 'test' # get the view instance instead of the view function view = DummyView.as_view() request = factory.get('/') response = view(request) view = response.renderer_context['view'] self.assertIsNone(self.renderer.get_result_url({'test': 1}, view)) self.assertIsNone(self.renderer.get_result_url({}, view)) def test_get_context_result_urls(self): factory = APIRequestFactory() class DummyView(APIView): lookup_field = 'test' def reverse_action(view, url_name, args=None, kwargs=None): return '/%s/%d' % (url_name, kwargs['test']) # get the view instance instead of the view function view = DummyView.as_view() request = factory.get('/') response = view(request) data = [ { 'test': 1 }, { 'url': '/example', 'test': 2 }, { 'url': None, 'test': 3 }, {}, ] context = { 'view': DummyView(), 'request': Request(request), 'response': response } context = self.renderer.get_context(data, None, context) results = context['results'] self.assertEqual(len(results), 4) self.assertEqual(results[0]['url'], '/detail/1') self.assertEqual(results[1]['url'], '/example') self.assertEqual(results[2]['url'], None) self.assertNotIn('url', results[3])
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' def test_render_dict(self): factory = APIRequestFactory() class DummyView(APIView): renderer_classes = (AdminRenderer, ) def get(self, request): return Response({'foo': 'a string'}) view = DummyView.as_view() request = factory.get('/') response = view(request) response.render() self.assertContains(response, '<tr><th>Foo</th><td>a string</td></tr>', html=True) def test_render_dict_with_items_key(self): factory = APIRequestFactory() class DummyView(APIView): renderer_classes = (AdminRenderer, ) def get(self, request): return Response({'items': 'a string'}) view = DummyView.as_view() request = factory.get('/') response = view(request) response.render() self.assertContains(response, '<tr><th>Items</th><td>a string</td></tr>', html=True) def test_render_dict_with_iteritems_key(self): factory = APIRequestFactory() class DummyView(APIView): renderer_classes = (AdminRenderer, ) def get(self, request): return Response({'iteritems': 'a string'}) view = DummyView.as_view() request = factory.get('/') response = view(request) response.render() self.assertContains(response, '<tr><th>Iteritems</th><td>a string</td></tr>', html=True) def test_get_result_url(self): factory = APIRequestFactory() class DummyGenericViewsetLike(APIView): lookup_field = 'test' def reverse_action(view, *args, **kwargs): self.assertEqual(kwargs['kwargs']['test'], 1) return '/example/' # get the view instance instead of the view function view = DummyGenericViewsetLike.as_view() request = factory.get('/') response = view(request) view = response.renderer_context['view'] self.assertEqual(self.renderer.get_result_url({'test': 1}, view), '/example/') self.assertIsNone(self.renderer.get_result_url({}, view)) def test_get_result_url_no_result(self): factory = APIRequestFactory() class DummyView(APIView): lookup_field = 'test' # get the view instance instead of the view function view = DummyView.as_view() request = factory.get('/') response = view(request) view = response.renderer_context['view'] self.assertIsNone(self.renderer.get_result_url({'test': 1}, view)) self.assertIsNone(self.renderer.get_result_url({}, view)) def test_get_context_result_urls(self): factory = APIRequestFactory() class DummyView(APIView): lookup_field = 'test' def reverse_action(view, url_name, args=None, kwargs=None): return '/%s/%d' % (url_name, kwargs['test']) # get the view instance instead of the view function view = DummyView.as_view() request = factory.get('/') response = view(request) data = [ {'test': 1}, {'url': '/example', 'test': 2}, {'url': None, 'test': 3}, {}, ] context = { 'view': DummyView(), 'request': Request(request), 'response': response } context = self.renderer.get_context(data, None, context) results = context['results'] self.assertEqual(len(results), 4) self.assertEqual(results[0]['url'], '/detail/1') self.assertEqual(results[1]['url'], '/example') self.assertEqual(results[2]['url'], None) self.assertNotIn('url', results[3])