def test_decode_encode_unordered_converter_with_preserve_root(self): col_schema = XMLSchema(self.col_xsd_filename, converter=UnorderedConverter) # Decode from XML file obj1 = col_schema.decode(self.col_xml_filename, preserve_root=True) self.assertIn("'col:collection'", repr(obj1)) self.assertIn("'@xmlns:col'", repr(obj1)) root = col_schema.encode(obj1, path='./col:collection', namespaces=self.col_nsmap, preserve_root=True) self.assertIsNone( etree_elements_assert_equal(self.col_xml_root, root, strict=False)) root = col_schema.encode(obj1, preserve_root=True) self.assertIsNone( etree_elements_assert_equal(self.col_xml_root, root, strict=False)) # Decode from lxml.etree.Element tree obj2 = col_schema.decode(self.col_lxml_root, preserve_root=True) self.assertIn("'col:collection'", repr(obj2)) self.assertIn("'@xmlns:col'", repr(obj2)) self.assertEqual(obj1, obj2) # Decode from ElementTree.Element tree providing namespaces obj2 = col_schema.decode(self.col_xml_root, namespaces=self.col_nsmap, preserve_root=True) self.assertIn("'col:collection'", repr(obj2)) self.assertIn("'@xmlns:col'", repr(obj2)) self.assertEqual(obj1, obj2) # Decode from ElementTree.Element tree without namespaces obj2 = col_schema.decode(self.col_xml_root, preserve_root=True) self.assertNotIn("'col:collection'", repr(obj2)) self.assertNotIn("'@xmlns:col'", repr(obj2)) self.assertNotEqual(obj1, obj2) root = col_schema.encode(obj2, path='./col:collection', namespaces=self.col_nsmap, preserve_root=True) self.assertIsNone( etree_elements_assert_equal(self.col_xml_root, root, strict=False)) root = col_schema.encode( obj2, preserve_root=True) # No namespace unmap is required self.assertIsNone( etree_elements_assert_equal(self.col_xml_root, root, strict=False))
def test_decode_encode_data_element_converter(self): col_schema = XMLSchema(self.col_xsd_filename, converter=DataElementConverter) obj1 = col_schema.decode(self.col_xml_filename) # self.assertIn('col:collection', repr(obj1)) self.assertIn('col', obj1.nsmap) root = col_schema.encode(obj1, path='./col:collection', namespaces=self.col_nsmap) self.assertIsNone( etree_elements_assert_equal(self.col_xml_root, root, strict=False)) root = col_schema.encode(obj1, path='./{%s}collection' % self.col_namespace) self.assertIsNone( etree_elements_assert_equal(self.col_xml_root, root, strict=False)) root = col_schema.encode(obj1) self.assertIsNone( etree_elements_assert_equal(self.col_xml_root, root, strict=False)) # With ElementTree namespaces are not mapped obj2 = col_schema.decode(self.col_xml_root) # Equivalent if compared as Element trees (tag, text, attrib, tail) self.assertIsNone(etree_elements_assert_equal(obj1, obj2)) self.assertIsNone( etree_elements_assert_equal( obj1, col_schema.decode(self.col_xml_root, namespaces=self.col_nsmap))) # With lxml.etree namespaces are mapped self.assertIsNone( etree_elements_assert_equal(obj1, col_schema.decode(self.col_lxml_root))) root = col_schema.encode(obj2, path='./col:collection', namespaces=self.col_nsmap) self.assertIsNone( etree_elements_assert_equal(self.col_xml_root, root, strict=False)) root = col_schema.encode(obj2) # No namespace unmap is required self.assertIsNone( etree_elements_assert_equal(self.col_xml_root, root, strict=False))
def test_decode_encode_jsonml_converter(self): col_schema = XMLSchema(self.col_xsd_filename, converter=JsonMLConverter) obj1 = col_schema.decode(self.col_xml_filename) self.assertIn('col:collection', repr(obj1)) self.assertIn('xmlns:col', repr(obj1)) root = col_schema.encode(obj1, path='./col:collection', namespaces=self.col_nsmap) self.assertIsNone( etree_elements_assert_equal(self.col_xml_root, root, strict=False)) root = col_schema.encode(obj1, path='./{%s}collection' % self.col_namespace) self.assertIsNone( etree_elements_assert_equal(self.col_xml_root, root, strict=False)) root = col_schema.encode(obj1) self.assertIsNone( etree_elements_assert_equal(self.col_xml_root, root, strict=False)) # With ElementTree namespaces are not mapped obj2 = col_schema.decode(self.col_xml_root) self.assertNotIn('col:collection', repr(obj2)) self.assertNotEqual(obj1, obj2) self.assertEqual( obj1, col_schema.decode(self.col_xml_root, namespaces=self.col_nsmap)) # With lxml.etree namespaces are mapped self.assertEqual(obj1, col_schema.decode(self.col_lxml_root)) root = col_schema.encode(obj2, path='./col:collection', namespaces=self.col_nsmap) self.assertIsNone( etree_elements_assert_equal(self.col_xml_root, root, strict=False)) root = col_schema.encode(obj2) # No namespace unmap is required self.assertIsNone( etree_elements_assert_equal(self.col_xml_root, root, strict=False))
def test_preserve_root__issue_215(self): schema = XMLSchema(""" <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://xmlschema.test/ns" targetNamespace="http://xmlschema.test/ns"> <xs:element name="a"> <xs:complexType> <xs:sequence> <xs:element name="b1" type="xs:string" maxOccurs="unbounded"/> <xs:element name="b2" type="xs:string" maxOccurs="unbounded"/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema> """) xml_data = """<tns:a xmlns:tns="http://xmlschema.test/ns"><b1/><b2/></tns:a>""" obj = schema.decode(xml_data) self.assertListEqual(list(obj), ['@xmlns:tns', 'b1', 'b2']) self.assertEqual(schema.encode(obj).tag, '{http://xmlschema.test/ns}a') obj = schema.decode(xml_data, preserve_root=True) self.assertListEqual(list(obj), ['tns:a']) root = schema.encode(obj, preserve_root=True, path='tns:a', namespaces={'tns': 'http://xmlschema.test/ns'}) self.assertEqual(root.tag, '{http://xmlschema.test/ns}a') root = schema.encode(obj, preserve_root=True, path='{http://xmlschema.test/ns}a') self.assertEqual(root.tag, '{http://xmlschema.test/ns}a') root = schema.encode(obj, preserve_root=True) self.assertEqual(root.tag, '{http://xmlschema.test/ns}a')
def test_decode_encode_columnar_converter(self): col_schema = XMLSchema(self.col_xsd_filename, converter=ColumnarConverter) obj1 = col_schema.decode(self.col_xml_filename) root = col_schema.encode(obj1, path='./col:collection', namespaces=self.col_nsmap) self.assertIsNone( etree_elements_assert_equal(self.col_xml_root, root, strict=False)) # Namespace mapping is required with self.assertRaises(XMLSchemaValidationError) as ec: col_schema.encode(obj1, path='./{%s}collection' % self.col_namespace) self.assertIn("'xsi:schemaLocation' attribute not allowed", str(ec.exception)) # With ElementTree namespaces are not mapped obj2 = col_schema.decode(self.col_xml_root) self.assertNotEqual(obj1, obj2) self.assertEqual( obj1, col_schema.decode(self.col_xml_root, namespaces=self.col_nsmap)) # With lxml.etree namespaces are mapped self.assertEqual(obj1, col_schema.decode(self.col_lxml_root)) root = col_schema.encode(obj2, path='./col:collection', namespaces=self.col_nsmap) self.assertIsNone( etree_elements_assert_equal(self.col_xml_root, root, strict=False)) root = col_schema.encode(obj2) # No namespace unmap is required self.assertIsNone( etree_elements_assert_equal(self.col_xml_root, root, strict=False))