def test_passed_syntax_attr_true_not_others_if_schema_error(self):
        filename = "FooBar.xml"
        exception = self.schema_exc(filename)
        result = ValidationResult(filename, exception)

        self.assertTrue(result.passed_syntax)
        self.assertFalse(result.passed_schema)
        self.assertFalse(result.passed_rules)
        self.assertFalse(result)
    def test_passed_XXX_attrs_true_with_exception_as_None(self):
        filename = "FooBar.xml"
        exception = None
        result = ValidationResult(filename, exception)

        self.assertTrue(result.passed_syntax)
        self.assertTrue(result.passed_schema)
        self.assertTrue(result.passed_rules)
        self.assertTrue(result)
    def test_passed_XXX_attrs_false_if_any_exception(self):
        filename = "FooBar.xml"
        exception = self.base_exc(filename)
        result = ValidationResult(filename, exception)

        self.assertFalse(result.passed_syntax)
        self.assertFalse(result.passed_schema)
        self.assertFalse(result.passed_rules)
        self.assertFalse(result)
    def test_args_become_attrs(self):
        filenames = "FooBar.xml", "BazCat.xml"
        exceptions = self.base_exc(filenames[0]), None
        types = self.base_exc, type(None)
        args = filenames, exceptions, types

        for fname, exc, type_ in zip(*args):
            result = ValidationResult(fname, exc)
            self.assertIsInstance(result.exception, type_)
            self.assertEqual(result.filename, fname)
    def test_attributes_are_properties(self):
        """This test forbids setting attributes externally."""
        filename = "FooBar.xml"
        exception = self.base_exc(filename)
        result = ValidationResult(filename, exception)
        var = "FooBar"

        attrs = [a for a in dir(result) if not a.startswith("_")]

        for attr in attrs:
            msg = f"result.{attr} can be set"
            with self.assertRaises(AttributeError, msg=msg):
                setattr(result, attr, var)
示例#6
0
def _validate_schema(filename):
    try:
        try:
            tree = parse_xml(filename)
            SCHEMA.assertValid(tree)
        except etree.DocumentInvalid as cause:
            raise exceptions.SchemaValidationError() from cause
    except exceptions.SchemaValidationError as exc:
        exception = exc
    else:
        exception = None
    result = ValidationResult(filename, exception)
    return result
示例#7
0
def _validate_syntax(filename):
    try:
        causalgrp = (etree.XMLSyntaxError, exceptions.EncodingOperationError)
        try:
            etree.parse(str(filename), parser=MY_PARSER)
            # Files with mismatched encodings may silently pass without raising
            # an exception.
            raise_if_mismatched_encodings(filename)
        except causalgrp as cause:
            raise exceptions.SyntaxValidationError() from cause
    except exceptions.SyntaxValidationError as exc:
        exception = exc
    else:
        exception = None
    result = ValidationResult(filename, exception)
    return result
    def test_is_true_with_exception_as_None(self):
        filename = "FooBar.xml"
        exception = None
        result = ValidationResult(filename, exception)

        self.assertTrue(result)
    def test_is_false_with_exception_attr(self):
        filename = "FooBar.xml"
        exception = self.base_exc(filename)
        result = ValidationResult(filename, exception)

        self.assertFalse(result)
    def test_instantialization_fails_without_exception(self):
        """This test forbids defining __init__ with a kwarg exception=None"""
        filename = "FooBar.xml"

        with self.assertRaises(TypeError):
            ValidationResult(filename)
    def test_instantialization_fails_with_other_exception(self):
        filename = "FooBar.xml"
        exception = ValueError(filename)

        with self.assertRaises(TypeError):
            ValidationResult(filename, exception)
    def test_instantialization_fails_with_package_general_exception(self):
        filename = "FooBar.xml"
        exception = self.pkg_base_exc(filename)

        with self.assertRaises(TypeError):
            ValidationResult(filename, exception)
    def test_instantialization_succeeds_with_None(self):
        filename = "FooBar.xml"
        exception = None

        ValidationResult(filename, exception)
    def test_instantialization_succeeds_with_package_validation_exception(
            self):
        filename = "FooBar.xml"
        exception = self.base_exc(filename)

        ValidationResult(filename, exception)
示例#15
0
def validate_rules(filename):
    exception = exceptions.RuleValidationError("Not yet written")
    return ValidationResult(filename, exception)