def test_columnar_converter(self): col_schema = XMLSchema(self.col_xsd_filename, converter=ColumnarConverter) obj = col_schema.decode(self.col_xml_filename) self.assertIn("'authorid'", str(obj)) self.assertNotIn("'author_id'", str(obj)) self.assertNotIn("'author__id'", str(obj)) obj = col_schema.decode(self.col_xml_filename, attr_prefix='_') self.assertNotIn("'authorid'", str(obj)) self.assertIn("'author_id'", str(obj)) self.assertNotIn("'author__id'", str(obj)) obj = col_schema.decode(self.col_xml_filename, attr_prefix='__') self.assertNotIn("'authorid'", str(obj)) self.assertNotIn("'author_id'", str(obj)) self.assertIn("'author__id'", str(obj)) col_schema = XMLSchema(self.col_xsd_filename) obj = col_schema.decode(self.col_xml_filename, converter=ColumnarConverter, attr_prefix='__') self.assertNotIn("'authorid'", str(obj)) self.assertNotIn("'author_id'", str(obj)) self.assertIn("'author__id'", str(obj))
def test_parquet_converter(self): col_xsd_filename = self.casepath('examples/collection/collection.xsd') col_xml_filename = self.casepath('examples/collection/collection.xml') col_schema = XMLSchema(col_xsd_filename, converter=ColumnarConverter) obj = col_schema.decode(col_xml_filename) self.assertIn("'authorid'", str(obj)) self.assertNotIn("'author_id'", str(obj)) self.assertNotIn("'author__id'", str(obj)) obj = col_schema.decode(col_xml_filename, attr_prefix='_') self.assertNotIn("'authorid'", str(obj)) self.assertIn("'author_id'", str(obj)) self.assertNotIn("'author__id'", str(obj)) obj = col_schema.decode(col_xml_filename, attr_prefix='__') self.assertNotIn("'authorid'", str(obj)) self.assertNotIn("'author_id'", str(obj)) self.assertIn("'author__id'", str(obj)) col_schema = XMLSchema(col_xsd_filename) obj = col_schema.decode(col_xml_filename, converter=ColumnarConverter, attr_prefix='__') self.assertNotIn("'authorid'", str(obj)) self.assertNotIn("'author_id'", str(obj)) self.assertIn("'author__id'", str(obj))
def test_strip_namespace_argument(self): # Test for issue #161 converter = XMLSchemaConverter(strip_namespaces=True) col_xsd_filename = self.casepath('examples/collection/collection.xsd') col_xml_filename = self.casepath('examples/collection/collection.xml') col_schema = XMLSchema(col_xsd_filename, converter=converter) self.assertIn('@xmlns:', str(col_schema.decode(col_xml_filename, strip_namespaces=False))) self.assertNotIn('@xmlns:', str(col_schema.decode(col_xml_filename)))
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_data_element_converter(self): col_schema = XMLSchema(self.col_xsd_filename, converter=DataElementConverter) obj = col_schema.decode(self.col_xml_filename) self.assertIsInstance(obj, DataElement) self.assertEqual(obj.tag, self.col_xml_root.tag) self.assertEqual(obj.nsmap, self.col_nsmap)
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_cdata_mapping(self): schema = XMLSchema(""" <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="root"> <xs:complexType mixed="true"> <xs:sequence> <xs:element name="node" type="xs:string" maxOccurs="unbounded"/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema> """) self.assertEqual( schema.decode('<root>1<node/>2<node/>3</root>'), {'node': [None, None]} ) self.assertEqual( schema.decode('<root>1<node/>2<node/>3</root>', cdata_prefix='#'), {'#1': '1', 'node': [None, None], '#2': '2', '#3': '3'} )
def test_decode_encode_parker_converter(self): col_schema = XMLSchema(self.col_xsd_filename, converter=ParkerConverter) obj1 = col_schema.decode(self.col_xml_filename) with self.assertRaises(XMLSchemaValidationError) as ec: col_schema.encode(obj1, path='./col:collection', namespaces=self.col_nsmap) self.assertIn("missing required attribute 'id'", str(ec.exception))
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))