def test_phase_with_unknown_pattern(self):
     schema = Schema(
         get_file("schematron", "malformed/bad_active_pattern.sch"))
     xml_doc = etree.parse(get_file("xml", "basic1_error_1.xml"))
     self.assertEqual("bad_phase", schema.get_phase("bad_phase").id)
     self.assertRaises(SchematronError, schema.validate_document, xml_doc,
                       "bad_phase")
Beispiel #2
0
 def get_schematron_minimal_xml(self, filename):
     # These are all schematrons, and schematrons with includes can fail
     # the schematron validation (for instance if a pattern which is defined in
     # an included file is referenced in the main file)
     # Therefore, we don't validate it directly, but convert it to a minimal version
     # first
     schema_to_check = Schema(get_file("schematron", filename))
     return schema_to_check.to_minimal_xml_document()
 def test_variables1(self):
     schema = Schema(
         get_file("schematron", "variables/variables1_xslt2.sch"))
     xml_doc = etree.parse(
         get_file("xml", "variables/variables1_correct.xml"))
     report = schema.validate_document(xml_doc)
     errors = report.get_failed_asserts()
     self.assertEqual([], errors, [e.test for e in errors])
class TestFullSchematronSample(unittest.TestCase):
    def setUp(self):
        self.schema = Schema(get_file("schematron", "full.sch"))

    def test_ok(self):
        xml_doc = etree.parse(get_file("xml", "basic1_ok.xml"))
        report = self.schema.validate_document(xml_doc)
        failed_asserts = report.get_failed_asserts_by_flag()
        self.assertEqual({}, failed_asserts)

    def test_bad_1(self):
        xml_doc = etree.parse(get_file("xml", "basic1_error_1.xml"))
        report = self.schema.validate_document(xml_doc)
        self.assertEqual(2, len(report.get_failed_asserts_by_flag()))
        self.assertEqual(
            1, len(report.get_failed_asserts_flag("builtin_existence")))
        self.assertEqual(
            1, len(report.get_failed_asserts_flag("included_existence")))

    def test_phases(self):
        xml_doc = etree.parse(get_file("xml", "basic1_error_1.xml"))
        report = self.schema.validate_document(xml_doc,
                                               phase="builtin_and_included")
        self.assertEqual(2, len(report.get_failed_asserts_by_flag()))
        self.assertEqual(
            1, len(report.get_failed_asserts_flag("builtin_existence")))
        self.assertEqual(
            1, len(report.get_failed_asserts_flag("included_existence")))

        report = self.schema.validate_document(xml_doc, phase="builtin_only")
        self.assertEqual(1, len(report.get_failed_asserts_by_flag()))
        self.assertEqual(
            1, len(report.get_failed_asserts_flag("builtin_existence")))
        self.assertEqual(
            0, len(report.get_failed_asserts_flag("included_existence")))

        report = self.schema.validate_document(xml_doc, phase="included_only")
        self.assertEqual(1, len(report.get_failed_asserts_by_flag()))
        self.assertEqual(
            0, len(report.get_failed_asserts_flag("builtin_existence")))
        self.assertEqual(
            1, len(report.get_failed_asserts_flag("included_existence")))

        self.assertRaises(SchematronError, self.schema.validate_document,
                          xml_doc, "unknown_phase")

    def test_phase_with_unknown_pattern(self):
        schema = Schema(
            get_file("schematron", "malformed/bad_active_pattern.sch"))
        xml_doc = etree.parse(get_file("xml", "basic1_error_1.xml"))
        self.assertEqual("bad_phase", schema.get_phase("bad_phase").id)
        self.assertRaises(SchematronError, schema.validate_document, xml_doc,
                          "bad_phase")
    def test_simple_diagnostics(self):
        schema = Schema(get_file("schematron", "diagnostics.sch"))
        xml_doc = etree.parse(
            get_file("xml", "diagnostics/more_than_three_animals.xml"))
        report = schema.validate_document(xml_doc)

        self.assertEqual(3, len(report.get_failed_asserts()))
        self.assertEqual(1,
                         len(report.get_failed_asserts()[0][0].diagnostic_ids))
        self.assertEqual(
            """Noah, you must remove as many animals from the ark so that
      only two of one species live in this accommodation.""".strip(),
            report.get_failed_asserts()[0][0].get_diagnostic_text(
                report.get_failed_asserts()[0][0].diagnostic_ids[0]).strip())
 def test_variables_multiple_error(self):
     schema = Schema(
         get_file("schematron",
                  "variables/variables1_xslt2_multiple_error.sch"))
     xml_doc = etree.parse(
         get_file("xml", "variables/variables1_correct.xml"))
     self.assertRaises(SchematronError, schema.validate_document, xml_doc)
Beispiel #7
0
def main(schematron_file,
         xml_file,
         phase="#DEFAULT",
         output_type="text",
         output_stream=sys.stdout,
         verbosity=1):
    schema = Schema(verbosity=verbosity)
    schema.read_from_file(schematron_file)
    schema.process_abstract_patterns()

    doc = etree.parse(xml_file)
    if output_type == 'text':
        return validate_to_text(schema, doc, xml_file, phase, output_stream,
                                verbosity)
    elif output_type == 'svrl':
        svrl = schema.validate_document_to_svrl(doc, phase)
        output_stream.write(
            etree.tostring(svrl.to_xml(),
                           pretty_print=True,
                           xml_declaration=True,
                           encoding='utf-8').decode('utf-8'))
        output_stream.write("\n")
        return 0
    else:
        raise Exception("Unknown output type: %s" % output_type)
Beispiel #8
0
class ValidateSchematronFiles(unittest.TestCase):
    """
    Tests the schematron files used in the test cases, validating them against the Schematron schematron from the specification
    """
    def setUp(self):
        self.schema = Schema(get_file("schematron", "schematron.sch"))

    def get_schematron_minimal_xml(self, filename):
        # These are all schematrons, and schematrons with includes can fail
        # the schematron validation (for instance if a pattern which is defined in
        # an included file is referenced in the main file)
        # Therefore, we don't validate it directly, but convert it to a minimal version
        # first
        schema_to_check = Schema(get_file("schematron", filename))
        return schema_to_check.to_minimal_xml_document()

    def test_correct_schematrons(self):
        for filename in [
                'basic.sch',
                'schematron.sch',
                # 'unknown_querybinding.sch',
                'svrl.sch',
                'full.sch'
        ]:
            xml_doc = self.get_schematron_minimal_xml(filename)
            report = self.schema.validate_document(xml_doc)
            self.assertEqual([], report.get_failed_asserts(),
                             [a.text for a in report.get_failed_asserts()])

    def test_bad_schematrons(self):
        for filename in ['malformed/bad_is_a_attribute.sch']:
            # These wouldn't even pass our own parsing, to read them directly
            xml_doc = etree.parse(get_file("schematron", filename))
            report = self.schema.validate_document(xml_doc)
            self.assertNotEqual(
                [], report.get_failed_asserts(),
                [a.to_string() for a, element in report.get_failed_asserts()])

        for filename in ['malformed/bad_active_pattern.sch']:
            xml_doc = self.get_schematron_minimal_xml(filename)
            report = self.schema.validate_document(xml_doc)
            self.assertNotEqual(
                [], report.get_failed_asserts(),
                [a.to_string() for a, element in report.get_failed_asserts()])
Beispiel #9
0
    def check_schema_validation(self, schema_file, xml_file, expected_errors,
                                expected_warnings):
        """
        expected_errors is a list of the id values of the assertions that should fail with either no flag or flag="error"
        expected_warnings is a list of the id values of the assertions that should fail with either flag="warning"
        """
        schema = Schema(schema_file)
        xml_doc = etree.parse(xml_file)

        report = schema.validate_document(xml_doc)
        errors = report.get_failed_asserts()
        error_id_list = [
            err.id for err, element in errors if err.flag != 'warning'
        ]
        warnings = report.get_failed_asserts()
        warning_id_list = [
            w.id for w, element in warnings if w.flag == 'warning'
        ]
        self.assertEqual(expected_errors, error_id_list)
        self.assertEqual(expected_warnings, warning_id_list)
    def test_basic_example(self):
        schema = Schema(get_file("schematron", "basic.sch"))

        doc = etree.parse(get_file("xml", "basic1_ok.xml"))

        variables = {}
        parser = XPath2Parser(schema.ns_prefixes, variables)
        for p in schema.patterns.values():
            # print("[XX] %s has %d rules" % (p.id, len(p.rules)))
            for r in p.rules:

                elements = select(doc, r.context)
                for element in elements:
                    context = XPathContext(root=doc, item=element)
                    for a in r.assertions:
                        root_token = parser.parse(a.test)
                        result = root_token.evaluate(context)
                        self.assertTrue(result, a.to_string())
    def check_rule_order(self, schematron_file):
        schema = Schema(get_file("schematron", schematron_file))

        # Element a should match rule 1 only
        self.assertEqual([], self.validate(schema, '<a>1</a>'))
        # self.assertEqual(['r1'], self.validate("<a>2</a>"))
        self.assertEqual(['r1'],
                         self.validate(schema, '<root id="1"><a>2</a></root>'))

        # Element c should match rule 3 only
        self.assertEqual([], self.validate(schema, "<c>1</c>"))
        self.assertEqual(['r3'], self.validate(schema, "<c>2</c>"))

        # Element c should match rule 4 only
        self.assertEqual([], self.validate(schema, "<d>1</d>"))
        self.assertEqual(['r4'], self.validate(schema, "<d>2</d>"))

        # An arbitraty element should match rule 5
        self.assertEqual([], self.validate(schema, '<arb id="a">1</arb>'))
        self.assertEqual(['r5'], self.validate(schema, "<arb>1</arb>"))

        # Make sure this goes for nested elements too
        self.assertEqual(
            [],
            self.validate(
                schema,
                '<arb id="a"><a>1</a><b>1</b><c>1</c><d>1</d><e id="1">1</e></arb>'
            ))
        self.assertEqual(
            ['r4'],
            self.validate(
                schema,
                '<arb id="a"><a>1</a><b>1</b><c>1</c><d>2</d><e id="1">1</e></arb>'
            ))
        self.assertEqual(
            ['r1', 'r2', 'r3', 'r4'],
            self.validate(
                schema,
                '<arb id="a"><a>2</a><b>2</b><c>2</c><d>2</d><e id="1">2</e></arb>'
            ))
Beispiel #12
0
 def setUp(self):
     self.schema = Schema(get_file("schematron", "schematron.sch"))
 def setUp(self):
     self.schema = Schema(get_file("schematron", "orderchecks/xslt.sch"))
 def test_error_let_statement(self):
     schema = Schema(
         get_file("schematron", "xpath2/error_let_statement.sch"))
     xml_doc = etree.parse(get_file("xml", "basic1_ok.xml"))
     self.assertRaises(SchematronQueryBindingError,
                       schema.validate_document, xml_doc)
Beispiel #15
0
 def setUp(self):
     self.schema = Schema(get_file("schematron", "all_elements.sch"))