def test_xml_resource_open(self): resource = XMLResource(self.vh_xml_file) xml_file = resource.open() data = xml_file.read().decode('utf-8') self.assertTrue(data.startswith('<?xml ')) xml_file.close() resource = XMLResource('<A/>') self.assertRaises(ValueError, resource.open)
def test_xml_resource_from_resource(self): xml_file = urlopen('file://{}'.format(add_leading_slash(self.vh_xml_file))) try: resource = XMLResource(xml_file) self.assertEqual(resource.source, xml_file) self.assertEqual(resource.root.tag, '{http://example.com/vehicles}vehicles') self.check_url(resource.url, self.vh_xml_file) self.assertIsNone(resource.document) self.assertIsNone(resource.text) resource.load() self.assertTrue(resource.text.startswith('<?xml')) finally: xml_file.close()
def test_xml_resource_get_namespaces(self): with open(self.vh_xml_file) as schema_file: resource = XMLResource(schema_file) self.assertEqual(resource.url, normalize_url(self.vh_xml_file)) self.assertEqual(set(resource.get_namespaces().keys()), {'vh', 'xsi'}) with open(self.vh_xsd_file) as schema_file: resource = XMLResource(schema_file) self.assertEqual(resource.url, normalize_url(self.vh_xsd_file)) self.assertEqual(set(resource.get_namespaces().keys()), {'xs', 'vh'}) resource = XMLResource(self.col_xml_file) self.assertEqual(resource.url, normalize_url(self.col_xml_file)) self.assertEqual(set(resource.get_namespaces().keys()), {'col', 'xsi'}) resource = XMLResource(self.col_xsd_file) self.assertEqual(resource.url, normalize_url(self.col_xsd_file)) self.assertEqual(set(resource.get_namespaces().keys()), {'', 'xs'})
def test_xml_resource_copy(self): resource = XMLResource(self.vh_xml_file) resource2 = resource.copy(defuse='never') self.assertEqual(resource2.defuse, 'never') resource2 = resource.copy(timeout=30) self.assertEqual(resource2.timeout, 30) resource2 = resource.copy(lazy=False) self.assertFalse(resource2.is_lazy()) self.assertIsNone(resource2.text) self.assertIsNone(resource.text) resource.load() self.assertIsNotNone(resource.text) resource2 = resource.copy() self.assertEqual(resource.text, resource2.text)
def test_xml_resource_from_url(self): resource = XMLResource(self.vh_xml_file) self.assertEqual(resource.source, self.vh_xml_file) self.assertEqual(resource.root.tag, '{http://example.com/vehicles}vehicles') self.check_url(resource.url, self.vh_xml_file) self.assertIsNone(resource.document) self.assertIsNone(resource.text) resource.load() self.assertTrue(resource.text.startswith('<?xml')) resource = XMLResource(self.vh_xml_file, lazy=False) self.assertEqual(resource.source, self.vh_xml_file) self.assertEqual(resource.root.tag, '{http://example.com/vehicles}vehicles') self.check_url(resource.url, self.vh_xml_file) self.assertIsInstance(resource.document, ElementTree.ElementTree) self.assertIsNone(resource.text) resource.load() self.assertTrue(resource.text.startswith('<?xml'))
def test_xml_resource_from_lxml(self): vh_etree = lxml_etree.parse(self.vh_xml_file) vh_root = vh_etree.getroot() resource = XMLResource(vh_etree) self.assertEqual(resource.source, vh_etree) self.assertEqual(resource.document, vh_etree) self.assertEqual(resource.root.tag, '{http://example.com/vehicles}vehicles') self.assertIsNone(resource.url) self.assertIsNone(resource.text) resource.load() self.assertIsNone(resource.text) resource = XMLResource(vh_root) self.assertEqual(resource.source, vh_root) self.assertEqual(resource.root.tag, '{http://example.com/vehicles}vehicles') self.assertIsNone(resource.url) self.assertIsNone(resource.text) resource.load() self.assertIsNone(resource.text)
def test_xml_resource_from_file(self): with open(self.vh_xsd_file) as schema_file: resource = XMLResource(schema_file) self.assertEqual(resource.source, schema_file) self.assertEqual(resource.root.tag, '{http://www.w3.org/2001/XMLSchema}schema') self.check_url(resource.url, self.vh_xsd_file) self.assertIsNone(resource.document) self.assertIsNone(resource.text) resource.load() self.assertTrue(resource.text.startswith('<xs:schema')) with open(self.vh_xsd_file) as schema_file: resource = XMLResource(schema_file, lazy=False) self.assertEqual(resource.source, schema_file) self.assertEqual(resource.root.tag, '{http://www.w3.org/2001/XMLSchema}schema') self.check_url(resource.url, self.vh_xsd_file) self.assertIsInstance(resource.document, ElementTree.ElementTree) self.assertIsNone(resource.text) resource.load() self.assertTrue(resource.text.startswith('<xs:schema'))
def test_xml_resource_tostring(self): resource = XMLResource(self.vh_xml_file) self.assertTrue(resource.tostring().startswith('<vh:vehicles'))
def test_xml_resource_from_file(self): with open(self.vh_xsd_file) as schema_file: resource = XMLResource(schema_file) self.assertEqual(resource.source, schema_file) self.assertEqual(resource.root.tag, '{http://www.w3.org/2001/XMLSchema}schema') self.assertIsNone(resource.url) self.assertIsNone(resource.document) self.assertIsNone(resource.text) resource.load() self.assertTrue(resource.text.startswith('<xs:schema')) self.assertFalse(schema_file.closed) for _ in resource.iter(): pass self.assertFalse(schema_file.closed) for _ in resource.iterfind(): pass self.assertFalse(schema_file.closed) with open(self.vh_xsd_file) as schema_file: resource = XMLResource(schema_file, lazy=False) self.assertEqual(resource.source, schema_file) self.assertEqual(resource.root.tag, '{http://www.w3.org/2001/XMLSchema}schema') self.assertIsNone(resource.url) self.assertIsInstance(resource.document, ElementTree.ElementTree) self.assertIsNone(resource.text) resource.load() self.assertTrue(resource.text.startswith('<xs:schema')) self.assertFalse(schema_file.closed) for _ in resource.iter(): pass self.assertFalse(schema_file.closed) for _ in resource.iterfind(): pass self.assertFalse(schema_file.closed)
def test_xml_resource_is_loaded(self): resource = XMLResource(self.vh_xml_file) self.assertFalse(resource.is_loaded()) resource.load() self.assertTrue(resource.is_loaded())
def test_xml_resource_is_lazy(self): resource = XMLResource(self.vh_xml_file) self.assertTrue(resource.is_lazy()) resource = XMLResource(self.vh_xml_file, lazy=False) self.assertFalse(resource.is_lazy())
def test_xml_resource_get_namespaces(self): with open(self.vh_xml_file) as schema_file: resource = XMLResource(schema_file) self.assertIsNone(resource.url) self.assertEqual(set(resource.get_namespaces().keys()), {'vh', 'xsi'}) self.assertFalse(schema_file.closed) with open(self.vh_xsd_file) as schema_file: resource = XMLResource(schema_file) self.assertIsNone(resource.url) self.assertEqual(set(resource.get_namespaces().keys()), {'xs', 'vh'}) self.assertFalse(schema_file.closed) resource = XMLResource(self.col_xml_file) self.assertEqual(resource.url, normalize_url(self.col_xml_file)) self.assertEqual(set(resource.get_namespaces().keys()), {'col', 'xsi'}) resource = XMLResource(self.col_xsd_file) self.assertEqual(resource.url, normalize_url(self.col_xsd_file)) self.assertEqual(set(resource.get_namespaces().keys()), {'', 'xs'}) resource = XMLResource("""<?xml version="1.0" ?> <root xmlns="tns1"> <tns:elem1 xmlns:tns="tns1" xmlns="unknown"/> </root>""", lazy=False) self.assertEqual(set(resource.get_namespaces().keys()), {'', 'tns', 'default'}) resource._lazy = True self.assertEqual(resource.get_namespaces().keys(), {''}) resource = XMLResource("""<?xml version="1.0" ?> <root xmlns:tns="tns1"> <tns:elem1 xmlns:tns="tns1" xmlns="unknown"/> </root>""", lazy=False) self.assertEqual(set(resource.get_namespaces().keys()), {'default', 'tns'}) self.assertEqual( resource.get_namespaces(root_only=True).keys(), {'tns'}) resource._lazy = True self.assertEqual(resource.get_namespaces().keys(), {'tns'}) resource = XMLResource("""<?xml version="1.0" ?> <root xmlns:tns="tns1"> <tns:elem1 xmlns:tns="tns3" xmlns="unknown"/> </root>""", lazy=False) self.assertEqual(set(resource.get_namespaces().keys()), {'default', 'tns', 'tns0'}) resource._lazy = True self.assertEqual(resource.get_namespaces().keys(), {'tns'})
def test_xml_resource_iterfind(self): namespaces = {'xs': XSD_NAMESPACE} resource = XMLResource(self.schema_class.meta_schema.source.url, lazy=False) self.assertFalse(resource.is_lazy()) lazy_resource = XMLResource(self.schema_class.meta_schema.source.url) self.assertTrue(lazy_resource.is_lazy()) # Note: Element change with lazy resource so compare only tags tags = [x.tag for x in resource.iterfind()] self.assertEqual(len(tags), 1) self.assertEqual(tags[0], '{%s}schema' % XSD_NAMESPACE) self.assertListEqual(tags, [x.tag for x in lazy_resource.iterfind()]) tags = [x.tag for x in resource.iterfind(path='.')] self.assertEqual(len(tags), 1) self.assertEqual(tags[0], '{%s}schema' % XSD_NAMESPACE) self.assertListEqual(tags, [x.tag for x in lazy_resource.iterfind(path='.')]) tags = [x.tag for x in resource.iterfind(path='*')] self.assertEqual(len(tags), 156) self.assertEqual(tags[0], '{%s}annotation' % XSD_NAMESPACE) self.assertListEqual(tags, [x.tag for x in lazy_resource.iterfind(path='*')]) tags = [x.tag for x in resource.iterfind('xs:complexType', namespaces)] self.assertEqual(len(tags), 35) self.assertTrue( all(t == '{%s}complexType' % XSD_NAMESPACE for t in tags)) self.assertListEqual(tags, [ x.tag for x in lazy_resource.iterfind('xs:complexType', namespaces) ]) tags = [ x.tag for x in resource.iterfind('. /. / xs:complexType', namespaces) ] self.assertEqual(len(tags), 35) self.assertTrue( all(t == '{%s}complexType' % XSD_NAMESPACE for t in tags)) self.assertListEqual(tags, [ x.tag for x in lazy_resource.iterfind('. /. / xs:complexType', namespaces) ])
def test_xml_resource_close(self): resource = XMLResource(self.vh_xml_file) resource.close() xml_file = resource.open() self.assertTrue(callable(xml_file.read)) with open(self.vh_xml_file) as xml_file: resource = XMLResource(source=xml_file) resource.close() with self.assertRaises(ValueError): resource.open()
def test_xml_resource_get_locations(self): resource = XMLResource(self.col_xml_file) self.check_url(resource.url, normalize_url(self.col_xml_file)) locations = resource.get_locations([('ns', 'other.xsd')]) self.assertEqual(len(locations), 2) self.check_url(locations[0][1], os.path.join(self.col_dir, 'other.xsd'))