Beispiel #1
0
    def test_schema(self):
        if inspect:
            SchemaObserver.clear()
        # print("Run %s" % self.id())
        try:
            if expected_errors > 0:
                xs = schema_class(xsd_file,
                                  validation='lax',
                                  locations=locations)
            else:
                xs = schema_class(xsd_file, locations=locations)
        except (XMLSchemaParseError, XMLSchemaURLError, KeyError) as err:
            num_errors = 1
            errors = [str(err)]
        else:
            num_errors = len(xs.all_errors)
            errors = xs.all_errors

            if inspect:
                components_ids = set([id(c) for c in xs.iter_components()])
                missing = [
                    c for c in SchemaObserver.components
                    if id(c) not in components_ids
                ]
                if any([c for c in missing]):
                    raise ValueError("schema missing %d components: %r" %
                                     (len(missing), missing))

        # Check with lxml.etree.XMLSchema if it's installed
        if False and _lxml_etree is not None and not num_errors:
            xsd = _lxml_etree.parse(xsd_file)
            try:
                _lxml_etree.XMLSchema(xsd.getroot())
            except _lxml_etree.XMLSchemaParseError as err:
                self.assertTrue(
                    False,
                    "Schema without errors but lxml's validator report an error: {}"
                    .format(err))

        if num_errors != expected_errors:
            print("\n%s: %r errors, %r expected." %
                  (self.id()[13:], num_errors, expected_errors))
            if num_errors == 0:
                raise ValueError("found no errors when %d expected." %
                                 expected_errors)
            else:
                raise ValueError("n.%d errors expected, found %d: %s" %
                                 (expected_errors, num_errors, errors[0]))
        else:
            self.assertTrue(
                True, "Successfully created schema for {}".format(xsd_file))
Beispiel #2
0
        def test_xsd_schema(self):
            if inspect:
                SchemaObserver.clear()
            del self.errors[:]

            start_time = time.time()
            if expected_warnings > 0:
                with warnings.catch_warnings(record=True) as ctx:
                    warnings.simplefilter("always")
                    self.check_schema()
                    self.assertEqual(len(ctx), expected_warnings, "Wrong number of include/import warnings")
            else:
                self.check_schema()

                # Check with lxml.etree.XMLSchema class
            if check_with_lxml and lxml_etree is not None:
                self.check_lxml_schema(xmlschema_time=time.time()-start_time)
            self.check_errors(xsd_file, expected_errors)
Beispiel #3
0
        def test_xsd_schema(self):
            if inspect:
                SchemaObserver.clear()
            del self.errors[:]

            start_time = time.time()
            if expected_warnings > 0:
                with warnings.catch_warnings(record=True) as ctx:
                    warnings.simplefilter("always")
                    self.check_schema()
                    self.assertEqual(
                        len(ctx), expected_warnings,
                        "%r: Wrong number of include/import warnings" %
                        xsd_file)
            else:
                self.check_schema()

                # Check with lxml.etree.XMLSchema class
            if check_with_lxml and lxml_etree is not None:
                self.check_lxml_schema(xmlschema_time=time.time() - start_time)
            self.check_errors(xsd_file, expected_errors)
Beispiel #4
0
    def test_schema(self):
        if inspect:
            SchemaObserver.clear()
        # print("Run %s" % self.id())
        try:
            if expected_errors > 0:
                xs = schema_class(xsd_file,
                                  validation='lax',
                                  locations=locations)
            else:
                xs = schema_class(xsd_file, locations=locations)
        except (XMLSchemaParseError, XMLSchemaURLError, KeyError) as err:
            num_errors = 1
            errors = [str(err)]
        else:
            num_errors = len(xs.all_errors)
            errors = xs.all_errors

            if inspect:
                components_ids = set([id(c) for c in xs.iter_components()])
                missing = [
                    c for c in SchemaObserver.components
                    if id(c) not in components_ids
                ]
                if any([c for c in missing]):
                    raise ValueError("schema missing %d components: %r" %
                                     (len(missing), missing))

        if num_errors != expected_errors:
            print("\n%s: %r errors, %r expected." %
                  (self.id()[13:], num_errors, expected_errors))
            if num_errors == 0:
                raise ValueError("found no errors when %d expected." %
                                 expected_errors)
            else:
                raise ValueError("n.%d errors expected, found %d: %s" %
                                 (expected_errors, num_errors, errors[0]))
        else:
            self.assertTrue(
                True, "Successfully created schema for {}".format(xsd_file))
Beispiel #5
0
    def test_schema(self):
        if debug_mode:
            print("\n##\n## Testing schema %s in debug mode.\n##" % rel_path)
            import pdb
            pdb.set_trace()

        if inspect:
            SchemaObserver.clear()

        def check_schema():
            if expected_errors > 0:
                xs = schema_class(xsd_file,
                                  validation='lax',
                                  locations=locations,
                                  defuse=defuse)
            else:
                xs = schema_class(xsd_file, locations=locations, defuse=defuse)

            errors_ = xs.all_errors

            if inspect:
                components_ids = set([id(c) for c in xs.iter_components()])
                missing = [
                    c for c in SchemaObserver.components
                    if id(c) not in components_ids
                ]
                if any([c for c in missing]):
                    raise ValueError("schema missing %d components: %r" %
                                     (len(missing), missing))

            # Pickling test (only for Python 3, skip inspected schema classes test)
            if not inspect and PY3:
                deserialized_schema = pickle.loads(pickle.dumps(xs))
                self.assertTrue(isinstance(deserialized_schema, XMLSchemaBase))
                self.assertEqual(xs.built, deserialized_schema.built)

            # XPath API tests
            if not inspect and not errors_:
                context = ElementPathContext(xs)
                elements = [e for e in xs.iter()]
                context_elements = [
                    e for e in context.iter() if isinstance(e, XsdValidator)
                ]
                self.assertEqual(context_elements,
                                 [e for e in context.iter_descendants()])
                self.assertEqual(context_elements, elements)

            return errors_

        if expected_warnings > 0:
            with warnings.catch_warnings(record=True) as ctx:
                warnings.simplefilter("always")
                errors = check_schema()
                self.assertEqual(len(ctx), expected_warnings,
                                 "Wrong number of include/import warnings")
        else:
            errors = check_schema()

        # Checks errors completeness
        for e in errors:
            self.assertTrue(e.path, "Missing path for: %s" % str(e))
            self.assertTrue(e.namespaces,
                            "Missing namespaces for: %s" % str(e))

        num_errors = len(errors)
        if num_errors != expected_errors:
            print("\n%s: %r errors, %r expected." %
                  (self.id()[13:], num_errors, expected_errors))
            if num_errors == 0:
                raise ValueError("found no errors when %d expected." %
                                 expected_errors)
            else:
                raise ValueError("n.%d errors expected, found %d: %s" %
                                 (expected_errors, num_errors,
                                  '\n++++++\n'.join([str(e) for e in errors])))
        else:
            self.assertTrue(
                True, "Successfully created schema for {}".format(xsd_file))