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)
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_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])
def test_sample(self): xml_doc = etree.parse(get_file("svrl", "svrl_sample.xml")) svrl = SchematronOutput(xml_element=xml_doc.getroot()) new_xml = svrl.to_xml() xml_doc = etree.parse(get_file("svrl", "simple.xml")) svrl = SchematronOutput(xml_element=xml_doc.getroot()) new_xml = svrl.to_xml() xml_doc = etree.parse(get_file("svrl", "simple_2.xml")) svrl = SchematronOutput(xml_element=xml_doc.getroot()) new_xml = svrl.to_xml()
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_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_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 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_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 test_invalid_documents(self): self.check_schema_validation(get_file("schematron", "basic.sch"), get_file("xml", "basic1_error_1.xml"), ["1"], []) self.check_schema_validation(get_file("schematron", "basic.sch"), get_file("xml", "basic1_error_2.xml"), ["2"], []) self.check_schema_validation(get_file("schematron", "basic.sch"), get_file("xml", "basic1_warning_3.xml"), [], ["3"]) self.check_schema_validation(get_file("schematron", "basic.sch"), get_file("xml", "basic1_warning_4.xml"), [], ["4"])
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()])
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>' ))
def setUp(self): self.schema = Schema(get_file("schematron", "all_elements.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)
def setUp(self): self.schema = Schema(get_file("schematron", "schematron.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_valid_documents(self): self.check_schema_validation(get_file("schematron", "basic.sch"), get_file("xml", "basic1_ok.xml"), [], [])
def setUp(self): self.schema = Schema(get_file("schematron", "orderchecks/xslt.sch"))
def test_unknown_querybinding(self): self.assertRaises(SchematronNotImplementedError, Schema, get_file("schematron", "unknown_querybinding.sch"))