Ejemplo n.º 1
0
    def test_document_validate_api(self):
        self.assertIsNone(xmlschema.validate(self.vh_xml_file))
        self.assertIsNone(
            xmlschema.validate(self.vh_xml_file, use_defaults=False))

        vh_2_file = self.casepath('examples/vehicles/vehicles-2_errors.xml')
        self.assertRaises(XMLSchemaValidationError, xmlschema.validate,
                          vh_2_file)

        try:
            xmlschema.validate(
                vh_2_file, namespaces={'vhx': "http://example.com/vehicles"})
        except XMLSchemaValidationError as err:
            path_line = str(err).splitlines()[-1]
        else:
            path_line = ''

        if sys.version_info >= (3, 6):
            self.assertEqual('Path: /vhx:vehicles/vhx:cars', path_line)
        else:
            self.assertTrue('Path: /vh:vehicles/vh:cars' == path_line
                            or 'Path: /vhx:vehicles/vhx:cars',
                            path_line)  # Due to unordered dicts

        # Issue #80
        vh_2_xt = ElementTree.parse(vh_2_file)
        self.assertRaises(XMLSchemaValidationError, xmlschema.validate,
                          vh_2_xt, self.vh_xsd_file)

        # Issue #145
        with open(self.vh_xml_file) as f:
            self.assertIsNone(xmlschema.validate(f, schema=self.vh_xsd_file))
Ejemplo n.º 2
0
        def check_encoding_with_element_tree(self):
            root = ElementTree.parse(xml_file).getroot()
            namespaces = fetch_namespaces(xml_file)
            options = {
                '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)
Ejemplo n.º 3
0
    def test_decode(self):
        xml_file = os.path.join(CASES_DIR, 'examples/vehicles/vehicles.xml')
        root = ElementTree.parse(xml_file).getroot()

        obj = self.schema.elements['vehicles'].decode(root)
        self.assertIsInstance(obj, dict)
        self.assertIn(self.schema.elements['cars'].name, obj)
        self.assertIn(self.schema.elements['bikes'].name, obj)

        xml_file = os.path.join(CASES_DIR, 'examples/vehicles/vehicles-2_errors.xml')
        root = ElementTree.parse(xml_file).getroot()

        obj, errors = self.schema.elements['vehicles'].decode(root, validation='lax')
        self.assertIsInstance(obj, dict)
        self.assertIn(self.schema.elements['cars'].name, obj)
        self.assertIn(self.schema.elements['bikes'].name, obj)
        self.assertEqual(len(errors), 2)
Ejemplo n.º 4
0
 def test_validation_strict(self):
     self.assertRaises(
         xmlschema.XMLSchemaValidationError,
         self.vh_schema.to_dict,
         ElementTree.parse(
             self.casepath('examples/vehicles/vehicles-2_errors.xml')),
         validation='strict',
         namespaces=self.vh_namespaces)
Ejemplo n.º 5
0
 def test_path(self):
     xt = ElementTree.parse(self.vh_xml_file)
     xd = self.vh_schema.to_dict(xt,
                                 '/vh:vehicles/vh:cars',
                                 namespaces=self.vh_namespaces)
     self.assertEqual(xd['vh:car'], VEHICLES_DICT['vh:cars']['vh:car'])
     xd = self.vh_schema.to_dict(xt,
                                 '/vh:vehicles/vh:bikes',
                                 namespaces=self.vh_namespaces)
     self.assertEqual(xd['vh:bike'], VEHICLES_DICT['vh:bikes']['vh:bike'])
Ejemplo n.º 6
0
    def test_json_dump_and_load(self):
        vh_xml_tree = ElementTree.parse(self.vh_xml_file)
        col_xml_tree = ElementTree.parse(self.col_xml_file)
        with open(self.vh_json_file, 'w') as f:
            xmlschema.to_json(self.vh_xml_file, f)

        with open(self.vh_json_file) as f:
            root = xmlschema.from_json(f, self.vh_schema)

        os.remove(self.vh_json_file)
        self.check_etree_elements(vh_xml_tree.getroot(), root)

        with open(self.col_json_file, 'w') as f:
            xmlschema.to_json(self.col_xml_file, f)

        with open(self.col_json_file) as f:
            root = xmlschema.from_json(f, self.col_schema)

        os.remove(self.col_json_file)
        self.check_etree_elements(col_xml_tree.getroot(), root)
Ejemplo n.º 7
0
    def test_to_dict_from_etree(self):
        vh_xml_tree = ElementTree.parse(self.vh_xml_file)
        col_xml_tree = ElementTree.parse(self.col_xml_file)

        xml_dict = self.vh_schema.to_dict(vh_xml_tree)
        self.assertNotEqual(xml_dict, VEHICLES_DICT)

        xml_dict = self.vh_schema.to_dict(vh_xml_tree, namespaces=self.vh_namespaces)
        self.assertEqual(xml_dict, VEHICLES_DICT)

        xml_dict = xmlschema.to_dict(vh_xml_tree, self.vh_schema.url, namespaces=self.vh_namespaces)
        self.assertEqual(xml_dict, VEHICLES_DICT)

        xml_dict = self.col_schema.to_dict(col_xml_tree)
        self.assertNotEqual(xml_dict, COLLECTION_DICT)

        xml_dict = self.col_schema.to_dict(col_xml_tree, namespaces=self.col_namespaces)
        self.assertEqual(xml_dict, COLLECTION_DICT)

        xml_dict = xmlschema.to_dict(col_xml_tree, self.col_schema.url, namespaces=self.col_namespaces)
        self.assertEqual(xml_dict, COLLECTION_DICT)
Ejemplo n.º 8
0
    def test_decode_encode(self):
        """Test encode after a decode, checking the re-encoded tree."""
        filename = self.casepath('examples/collection/collection.xml')
        xt = ElementTree.parse(filename)
        xd = self.col_schema.to_dict(filename, dict_class=ordered_dict_class)
        elem = self.col_schema.encode(xd, path='./col:collection', namespaces=self.col_namespaces)

        self.assertEqual(
            len([e for e in elem.iter()]), 20,
            msg="The encoded tree must have 20 elements as the origin."
        )
        self.assertTrue(all(
            local_name(e1.tag) == local_name(e2.tag)
            for e1, e2 in zip(elem.iter(), xt.getroot().iter())
        ))
Ejemplo n.º 9
0
def parse_xml(xml_file, dir_with_bands=None):
    try:
        xml_parsed = ElementTree.parse(xml_file)
    except ElementTree.ParseError:
        raise XMLParseError('error while parsing XML file')

    xml_file_version = get_xml_file_version(xml_parsed)

    if xml_file_version == QeXmlVersion.POST_6_2:
        parsed_data, logs = parse_xml_post_6_2(xml_parsed)
    elif xml_file_version == QeXmlVersion.PRE_6_2:
        xml_file.seek(0)
        parsed_data, logs = parse_pw_xml_pre_6_2(xml_file, dir_with_bands)

    return parsed_data, logs
Ejemplo n.º 10
0
    def test_decode_to_objects(self):
        xml_file = os.path.join(CASES_DIR, 'examples/vehicles/vehicles.xml')
        root = ElementTree.parse(xml_file).getroot()

        obj = self.schema.elements['vehicles'].to_objects(root)
        self.assertIsInstance(obj, DataElement)
        self.assertEqual(self.schema.elements['vehicles'].name, obj.tag)
        self.assertIs(obj.__class__, DataElement)

        obj = self.schema.elements['vehicles'].to_objects(root,
                                                          with_bindings=True)
        self.assertIsInstance(obj, DataElement)
        self.assertEqual(self.schema.elements['vehicles'].name, obj.tag)
        self.assertIsNot(obj.__class__, DataElement)
        self.assertTrue(issubclass(obj.__class__, DataElement))
        self.assertEqual(obj.__class__.__name__, 'VehiclesBinding')
Ejemplo n.º 11
0
    def test_xml_document_etree_interface(self):
        xml_document = XmlDocument(self.vh_xml_file)

        self.assertIs(xml_document.getroot(), xml_document._root)
        self.assertTrue(is_etree_element(xml_document.getroot()))

        self.assertTrue(is_etree_document(xml_document.get_etree_document()))

        xml_document = XmlDocument(self.vh_xml_file, lazy=1)
        with self.assertRaises(XMLResourceError) as ctx:
            xml_document.get_etree_document()
        self.assertIn('cannot create an ElementTree from a lazy resource',
                      str(ctx.exception))

        vh_tree = ElementTree.parse(self.vh_xml_file)
        xml_document = XmlDocument(vh_tree, base_url=self.vh_dir)
        self.assertIs(xml_document.source, vh_tree)
        self.assertIs(xml_document.get_etree_document(), vh_tree)
Ejemplo n.º 12
0
        def check_data_conversion_with_element_tree(self):
            root = ElementTree.parse(xml_file).getroot()
            namespaces = fetch_namespaces(xml_file)
            options = {'namespaces': namespaces}

            self.check_decode_encode(root, cdata_prefix='#',
                                     **options)  # Default converter
            self.check_decode_encode(root,
                                     UnorderedConverter,
                                     cdata_prefix='#',
                                     **options)
            self.check_decode_encode(root,
                                     ParkerConverter,
                                     validation='lax',
                                     **options)
            self.check_decode_encode(root,
                                     ParkerConverter,
                                     validation='skip',
                                     **options)
            self.check_decode_encode(root, BadgerFishConverter, **options)
            self.check_decode_encode(root, AbderaConverter, **options)
            self.check_decode_encode(root, JsonMLConverter, **options)
            self.check_decode_encode(root,
                                     ColumnarConverter,
                                     validation='lax',
                                     **options)

            self.check_json_serialization(root, cdata_prefix='#', **options)
            self.check_json_serialization(root, UnorderedConverter, **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)
            self.check_json_serialization(root,
                                          ColumnarConverter,
                                          validation='lax',
                                          **options)
Ejemplo n.º 13
0
    def test_xml_resource_from_element_tree(self):
        vh_etree = ElementTree.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.assertIsNone(resource.document)
        self.assertEqual(resource.root.tag, '{http://example.com/vehicles}vehicles')
        self.assertIsNone(resource.url)
        self.assertIsNone(resource.text)
        resource.load()
        self.assertIsNone(resource.text)
Ejemplo n.º 14
0
def parse_xml(xml_file, dir_with_bands=None, include_deprecated_v2_keys=False):
    try:
        xml_parsed = ElementTree.parse(xml_file)
    except ElementTree.ParseError:
        raise XMLParseError('error while parsing XML file')

    xml_file_version = get_xml_file_version(xml_parsed)

    try:
        if xml_file_version == QeXmlVersion.POST_6_2:
            parsed_data, logs = parse_pw_xml_post_6_2(
                xml_parsed, include_deprecated_v2_keys)
        elif xml_file_version == QeXmlVersion.PRE_6_2:
            xml_file.seek(0)
            parsed_data, logs = parse_pw_xml_pre_6_2(
                xml_file, dir_with_bands, include_deprecated_v2_keys)
    except Exception:
        import traceback
        logs = get_logging_container()
        logs.critical.append(traceback.format_exc())
        parsed_data = {}

    return parsed_data, logs
Ejemplo n.º 15
0
    def test_xml_resource_from_element_tree(self):
        vh_etree = ElementTree.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.assertIsNone(resource.document)
        self.assertEqual(resource.root.tag,
                         '{http://example.com/vehicles}vehicles')
        self.assertIsNone(resource.url)
        self.assertIsNone(resource.text)
        resource.load()
        self.assertIsNone(resource.text)
Ejemplo n.º 16
0
def w3c_tests_factory(argv=None):
    import sys

    if argv is None:
        argv = sys.argv[1:]

    def xsd_versions(value):
        if value not in ('1.0', '1.1', '1.0 1.1', '1.1 1.0'):
            raise argparse.ArgumentTypeError(
                "%r is not an XSD version string" % value)
        return value

    def number_or_interval(value):
        try:
            return int(value)
        except ValueError:
            if '-' not in value:
                raise
            try:
                start, stop = value.split('-')
                return [int(start), int(stop)]
            except ValueError:
                pass
            raise

    def iter_numbers(numbers):
        for x in numbers:
            if isinstance(x, int):
                yield x
            elif isinstance(x, list) and len(x) == 2:
                yield from range(x[0], x[1] + 1)

    parser = argparse.ArgumentParser(add_help=True)
    parser.add_argument('-v', '--verbose', default=False, action='store_true')
    parser.add_argument(
        '--version',
        dest='version',
        type=xsd_versions,
        default='1.0 1.1',
        help="Run only tests related to a specific XSD version.")
    parser.add_argument('--xml',
                        default=False,
                        action='store_true',
                        help="Include XML validation tests.")
    parser.add_argument(
        '--lxml',
        default=False,
        action='store_true',
        help="Use lxml's XMLSchema validator for XSD 1.0 tests.")
    parser.add_argument('--valid',
                        dest='expected',
                        default=ADMITTED_VALIDITY,
                        const='valid',
                        action='store_const',
                        help="Run only expected 'valid' tests.")
    parser.add_argument('--invalid',
                        dest='expected',
                        const='invalid',
                        action='store_const',
                        help="Run only expected 'invalid' tests.")
    parser.add_argument('--unknown',
                        dest='expected',
                        const='indeterminate',
                        action='store_const',
                        help="Run only expected 'indeterminate' tests.")
    parser.add_argument('numbers',
                        metavar='TEST_NUMBER',
                        type=number_or_interval,
                        nargs='*',
                        help='Runs only specific tests, selected by numbers.')
    args = parser.parse_args(args=argv)

    args.numbers = [x for x in iter_numbers(args.numbers)]

    if lxml_etree is None and args.lxml:
        print("Ignore --lxml option: library lxml is not available ...")
        args.lxml = False

    quiet = __name__ == '__main__'
    index_path = fetch_xsd_test_suite()
    index_dir = os.path.dirname(index_path)

    suite_xml = ElementTree.parse(index_path)
    test_classes = {}
    testgroup_num = 0

    if args.verbose and not quiet:
        print("\n>>>>> ADD TEST GROUPS FROM TESTSET FILES <<<<<\n")

    for testset_elem in suite_xml.iter("{%s}testSetRef" %
                                       TEST_SUITE_NAMESPACE):
        href_attr = testset_elem.attrib.get("{%s}href" % XLINK_NAMESPACE, '')
        testset_file = os.path.join(index_dir, href_attr)
        testset_groups = 0

        testset = ElementTree.parse(testset_file)
        testset_version = testset.getroot().get('version', '1.0 1.1')
        if testset_version not in XSD_VERSION_VALUES:
            if not quiet:
                msg = "Testset file %r has an invalid version=%r, skip ..."
                print(msg % (href_attr, testset_version))
            continue

        for testgroup_elem in testset.iter("{%s}testGroup" %
                                           TEST_SUITE_NAMESPACE):
            testgroup_num += 1

            testgroup_version = testgroup_elem.get('version', testset_version)
            if testgroup_version == 'full-xpath-in-CTA':
                # skip full XPath test for the moment ...
                if args.verbose and not quiet:
                    print("Skip full XPath test %r ..." %
                          testgroup_elem.get('name'))
                continue
            elif testgroup_version not in XSD_VERSION_VALUES:
                if not quiet:
                    _msg = "Test group %r has an invalid version=%r, skip ..."
                    print(_msg %
                          (testgroup_elem.get('name'), testgroup_version))
                continue
            elif testgroup_version not in testset_version:
                if args.verbose and not quiet:
                    _msg = "Warning: Test group %r version=%r is not included " \
                           "in test set version=%r"
                    print(_msg % (testgroup_elem.get('name'),
                                  testgroup_version, testset_version))

            cls = create_w3c_test_group_case(
                args=args,
                filename=testset_file,
                group_elem=testgroup_elem,
                group_num=testgroup_num,
                xsd_version=testgroup_version,
            )
            if cls is not None:
                test_classes[cls.__name__] = cls
                testset_groups += 1

        if args.verbose and testset_groups and not quiet:
            print("Added {} test groups from {}".format(
                testset_groups, href_attr))

    if test_classes and not quiet:
        print("\n+++ Number of classes under test: %d +++" % len(test_classes))
        if total_xml_files:
            print("+++ Number of XSD schemas under test: %d +++" %
                  total_xsd_files)
            print("+++ Number of XML files under test: %d +++" %
                  total_xml_files)
        print()

    if args.verbose and not quiet:
        print("\n>>>>> RUN TEST GROUPS <<<<<\n")

    return test_classes
Ejemplo n.º 17
0
 def test_datatypes(self):
     xt = ElementTree.parse(self.casepath('features/decoder/data.xml'))
     xd = self.st_schema.to_dict(xt, namespaces=self.default_namespaces)
     self.assertEqual(xd, DATA_DICT)
Ejemplo n.º 18
0
 def test_validation_skip(self):
     xt = ElementTree.parse(self.casepath('features/decoder/data3.xml'))
     xd = self.st_schema.decode(xt,
                                validation='skip',
                                namespaces={'ns': 'ns'})
     self.assertEqual(xd['decimal_value'], ['abc'])