def test_lxml(self): xs = xmlschema.XMLSchema( self.casepath('examples/vehicles/vehicles.xsd')) xt1 = lxml_etree.parse(self.casepath('examples/vehicles/vehicles.xml')) xt2 = lxml_etree.parse( self.casepath('examples/vehicles/vehicles-1_error.xml')) self.assertTrue(xs.is_valid(xt1)) self.assertFalse(xs.is_valid(xt2)) self.assertTrue(xs.validate(xt1) is None) self.assertRaises(xmlschema.XMLSchemaValidationError, xs.validate, xt2)
def check_xsd_file_with_lxml(self, xmlschema_time): start_time = time.time() lxs = lxml_etree.parse(xsd_file) try: lxml_etree.XMLSchema(lxs.getroot()) except lxml_etree.XMLSchemaParseError as err: if not self.errors: print( "\nSchema error with lxml.etree.XMLSchema for file {!r} ({}): {}" .format(xsd_file, self.__class__.__name__, unicode_type(err))) else: if self.errors: print( "\nUnrecognized errors with lxml.etree.XMLSchema for file {!r} ({}): {}" .format( xsd_file, self.__class__.__name__, '\n++++++\n'.join( [unicode_type(e) for e in self.errors]))) lxml_schema_time = time.time() - start_time if lxml_schema_time >= xmlschema_time: print( "\nSlower lxml.etree.XMLSchema ({:.3f}s VS {:.3f}s) with file {!r} ({})" .format(lxml_schema_time, xmlschema_time, xsd_file, self.__class__.__name__))
def check_decoding_and_encoding_with_lxml(self): xml_tree = lxml_etree.parse(xml_file) namespaces = fetch_namespaces(xml_file) errors = [] chunks = [] for obj in self.schema.iter_decode(xml_tree, namespaces=namespaces): if isinstance(obj, xmlschema.XMLSchemaValidationError): errors.append(obj) else: chunks.append(obj) self.assertEqual(chunks, self.chunks, msg_tmpl % "decoded data change with lxml") self.assertEqual(len(errors), len(self.errors), msg_tmpl % "errors number change with lxml") if not errors: root = xml_tree.getroot() if namespaces.get(''): # Add a not empty prefix for encoding to avoid the use of reserved prefix ns0 namespaces['tns0'] = namespaces[''] options = { 'etree_element_class': lxml_etree_element, 'namespaces': namespaces, 'dict_class': ordered_dict_class, } self.check_etree_encode(root, cdata_prefix='#', **options) # Default converter self.check_etree_encode(root, ParkerConverter, validation='lax', **options) self.check_etree_encode(root, ParkerConverter, validation='skip', **options) self.check_etree_encode(root, BadgerFishConverter, **options) self.check_etree_encode(root, AbderaConverter, **options) self.check_etree_encode(root, JsonMLConverter, **options) options.pop('dict_class') self.check_json_serialization(root, cdata_prefix='#', **options) self.check_json_serialization(root, ParkerConverter, validation='lax', **options) self.check_json_serialization(root, ParkerConverter, validation='skip', **options) self.check_json_serialization(root, BadgerFishConverter, **options) self.check_json_serialization(root, AbderaConverter, **options) self.check_json_serialization(root, JsonMLConverter, **options)
def check_lxml_validation(self): try: schema = lxml_etree.XMLSchema(self.lxml_schema.getroot()) except lxml_etree.XMLSchemaParseError: print("\nSkip lxml.etree.XMLSchema validation test for {!r} ({})". format(xml_file, TestValidator.__name__, )) else: xml_tree = lxml_etree.parse(xml_file) if self.errors: self.assertFalse(schema.validate(xml_tree)) else: self.assertTrue(schema.validate(xml_tree))
def setUpClass(cls): # Builds schema instance using 'lax' validation mode to accepts also schemas with not crashing errors. cls.schema_class = schema_class source, _locations = xmlschema.fetch_schema_locations(xml_file, locations) cls.schema = schema_class(source, validation='lax', locations=_locations, defuse=defuse) if check_with_lxml and lxml_etree is not None: cls.lxml_schema = lxml_etree.parse(source) cls.errors = [] cls.chunks = [] cls.longMessage = True if debug_mode: print("\n##\n## Testing %r validation in debug mode.\n##" % xml_file) pdb.set_trace()
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 check_lxml_schema(self, xmlschema_time): start_time = time.time() lxs = lxml_etree.parse(xsd_file) try: lxml_etree.XMLSchema(lxs.getroot()) except lxml_etree.XMLSchemaParseError as err: if not self.errors: print("\nSchema error with lxml.etree.XMLSchema for file {!r} ({}): {}".format( xsd_file, self.__class__.__name__, unicode_type(err) )) else: if self.errors: print("\nUnrecognized errors with lxml.etree.XMLSchema for file {!r} ({}): {}".format( xsd_file, self.__class__.__name__, '\n++++++\n'.join([unicode_type(e) for e in self.errors]) )) lxml_schema_time = time.time() - start_time if lxml_schema_time >= xmlschema_time: print( "\nSlower lxml.etree.XMLSchema ({:.3f}s VS {:.3f}s) with file {!r} ({})".format( lxml_schema_time, xmlschema_time, xsd_file, self.__class__.__name__ ))
def test_lxml(self): vh_xml_tree = lxml_etree.parse(self.vh_xml_file) self.assertEqual(self.vh_schema.to_dict(vh_xml_tree), VEHICLES_DICT) self.assertEqual(xmlschema.to_dict(vh_xml_tree, self.vh_schema.url), VEHICLES_DICT)