def test_render_urlinvoker_error(self): '''This test case ensures a concrete fantastico exception is thrown during rendering if an exception occurs within url invoker.''' from fantastico.rendering.component import Component expected_url = "/simple/url" expected_template = "/template.html" expected_result = "works" expected_environment = {"HTTP_CUSTOM_HEADER": "Simple header", "HTTP_CONTENT_TYPE": "application/json"} url_invoker = Mock() url_invoker.invoke_url = Mock(side_effect=FantasticoUrlInvokerError("Unexpected invoker exception")) environment = self._mock_render_dependencies(expected_template, expected_url, expected_environment, expected_result)[0] url_invoker_cls = Mock(return_value=url_invoker) component = Component(environment, url_invoker_cls) with self.assertRaises(FantasticoUrlInvokerError) as ctx: component.render(expected_template, expected_url) self.assertTrue(str(ctx.exception).find("Unexpected invoker exception") > -1)
def _test_scenario_component_parse(self, mock_template, mock_url, mock_runtime, mock_body, expected_template, expected_url, expected_runtime, expected_body, expected_lineno): '''This method provides the skeleton required for component parse test cases.''' from fantastico.rendering.component import Component environment, parser = self._mock_parser_parse(mock_template, mock_url, mock_runtime, expected_lineno, expected_body) component = Component(environment) block = component.parse(parser) template_kwarg = block.call_block.kwargs[0] url_kwarg = block.call_block.kwargs[1] runtime_kwarg = block.call_block.kwargs[2] self.assertEqual("template", template_kwarg.key) self.assertEqual(expected_template, template_kwarg.value.value) self.assertEqual("url", url_kwarg.key) self.assertEqual(expected_url, url_kwarg.value.value) self.assertEqual("runtime", runtime_kwarg.key) self.assertEqual(expected_runtime, runtime_kwarg.value.value) self.assertEqual(expected_lineno, block.call_block.lineno)
def test_render_with_template(self): '''This test case ensures a template rendering is triggered with url response data when template argument is given.''' from fantastico.rendering.component import Component expected_url = "/simple/url" expected_template = "/test.html" expected_result = "works" expected_environment = {"HTTP_CUSTOM_HEADER": "Simple header", "HTTP_CONTENT_TYPE": "application/json"} environment, url_invoker_cls = self._mock_render_dependencies(expected_template, expected_url, expected_environment, expected_result) component = Component(environment, url_invoker_cls) result = component.render(expected_template, expected_url) self.assertEqual(expected_result, result) self.assertEqual("application/json", expected_environment["HTTP_CONTENT_TYPE"])
def test_render_without_template(self): '''This test case ensure component reusage without template work as expected by delegating rendering process to template /raw_dump.html.''' from fantastico.rendering.component import Component expected_url = "/simple/url" expected_template = "/raw_dump.html" expected_result = "works" expected_environment = {"HTTP_CUSTOM_HEADER": "Simple header", "HTTP_CONTENT_TYPE": "application/json"} environment, url_invoker_cls = self._mock_render_dependencies(expected_template, expected_url, expected_environment, expected_result, json_output=False) component = Component(environment, url_invoker_cls) result = component.render(url=expected_url) self.assertEqual(expected_result, result) self.assertEqual("application/json", expected_environment["HTTP_CONTENT_TYPE"])
def test_render_notfound_template(self): '''This test case covers the scenario where the requested template passed to component tag is not found.''' from fantastico.rendering.component import Component expected_url = "/simple/url" expected_template = "/template.html" expected_result = "works" expected_environment = {"HTTP_CUSTOM_HEADER": "Simple header", "HTTP_CONTENT_TYPE": "application/json"} environment, url_invoker_cls = self._mock_render_dependencies(expected_template, expected_url, expected_environment, expected_result) environment.get_template = Mock(side_effect=TemplateNotFound("template does not exist.")) component = Component(environment, url_invoker_cls) with self.assertRaises(FantasticoTemplateNotFoundError) as ctx: component.render(expected_template, expected_url) self.assertTrue(str(ctx.exception).find(expected_template) > -1)