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_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)