def test_get_by_template_hash_raises_exception_if_not_templates(self, mock_get_all_by_hash): # Arrange mock_absent_hash = "dummy_hash" mock_get_all_by_hash.return_value = [] # Act + Assert with self.assertRaises(exceptions.DoesNotExist): template_xsl_rendering_api.get_by_template_hash(mock_absent_hash)
def test_get_by_template_hash_raises_exception_if_internal_error( self, mock_get_by_template_hash): # Arrange mock_absent_hash = "dummy_hash" mock_get_by_template_hash.side_effect = exceptions.ModelError("Error.") # Act + Assert with self.assertRaises(exceptions.ModelError): template_xsl_rendering_api.get_by_template_hash(mock_absent_hash)
def test_get_by_template_hash_raises_exception_if_object_does_not_exist( self, mock_get_by_template_hash): # Arrange mock_absent_hash = "dummy_hash" mock_get_by_template_hash.side_effect = exceptions.DoesNotExist( "Error.") # Act + Assert with self.assertRaises(exceptions.DoesNotExist): template_xsl_rendering_api.get_by_template_hash(mock_absent_hash)
def test_get_by_template_hash_returns_nothing_if_not_templates( self, mock_get_by_template_hash): # Arrange mock_absent_hash = "dummy_hash" mock_get_by_template_hash.return_value = [] # Act result = template_xsl_rendering_api.get_by_template_hash( mock_absent_hash) # Assert self.assertEqual(len(result), 0)
def test_get_by_template_hash_returns_object(self, mock_get_by_template_id, mock_get_all_by_hash): # Arrange mock_template_xsl_rendering = _create_mock_template_xsl_rendering() mock_get_by_template_id.return_value = mock_template_xsl_rendering mock_get_all_by_hash.return_value =[Template()] template_hash = "fhf7595ddha0d" # Act result = template_xsl_rendering_api.get_by_template_hash(template_hash) # Assert self.assertIsInstance(result, TemplateXslRendering)
def _render_xml_as_html( xml_string, template_id=None, template_hash=None, xslt_type=XSLType.type_list, xsl_transform_id=None, ): """Render an XML to HTML according to an xslt type (list or detail). Args: xml_string: template_id: template_hash: xslt_type: Returns: HTML """ try: try: if xslt_type not in (XSLType.type_list, XSLType.type_detail): raise Exception("XSLT Type unknown. Default xslt will be used.") if xsl_transform_id: xsl_transformation = xsl_transformation_api.get_by_id(xsl_transform_id) elif template_id or template_hash: if template_id: template_xsl_rendering = ( template_xsl_rendering_api.get_by_template_id(template_id) ) else: template_xsl_rendering = ( template_xsl_rendering_api.get_by_template_hash(template_hash) ) if xslt_type == XSLType.type_list: xsl_transformation = template_xsl_rendering.list_xslt else: xsl_transformation = template_xsl_rendering.default_detail_xslt else: raise Exception( "No template information provided. Default xslt will be used." ) xslt_string = xsl_transformation.content except (Exception, exceptions.DoesNotExist): default_xslt_path = finders.find(DEFAULT_DATA_RENDERING_XSLT) xslt_string = read_file_content(default_xslt_path) return xsl_transform(xml_string, xslt_string) except Exception: return xml_string