def test_invalid_schematron(self): """ Checks that data that is invalid only due to the schematron check is recognized as invalid, and the the validity is saved after being computed. """ tree = lxml.etree.fromstring(valid_editable) sfs = tree.xpath("//btw:example/btw:semantic-fields | " "//btw:example-explained/btw:semantic-fields", namespaces=xml.default_namespace_mapping) for el in sfs: el.getparent().remove(el) data = lxml.etree.tostring( tree, xml_declaration=True, encoding='utf-8').decode('utf-8') self.assertTrue( util.validate_with_rng(xml.schema_for_version(schema_version), data), "the data should validate") self.assertFalse(util.schematron( xml.schematron_for_version(schema_version), data), "the data should not pass the schematron check") c = Chunk(data=data, schema_version=schema_version) c.save() self.assertIsNone(c._valid) self.assertFalse(c.valid) self.assertFalse(Chunk.objects.get(pk=c.pk)._valid, "_valid was saved.")
def test_invalid_schematron(self): """ Checks that data that is invalid only due to the schematron check is recognized as invalid, and the the validity is saved after being computed. """ tree = lxml.etree.fromstring(valid_editable) sfs = tree.xpath( "//btw:example/btw:semantic-fields | " "//btw:example-explained/btw:semantic-fields", namespaces=xml.default_namespace_mapping) for el in sfs: el.getparent().remove(el) data = lxml.etree.tostring(tree, xml_declaration=True, encoding='utf-8').decode('utf-8') self.assertTrue( util.validate_with_rng(xml.schema_for_version(schema_version), data), "the data should validate") self.assertFalse( util.schematron(xml.schematron_for_version(schema_version), data), "the data should not pass the schematron check") c = Chunk(data=data, schema_version=schema_version) c.save() self.assertIsNone(c._valid) self.assertFalse(c.valid) self.assertFalse( Chunk.objects.get(pk=c.pk)._valid, "_valid was saved.")
def setUpClass(cls): # This is really a precondition to the rest of the test # suite. If this assertion fails, there's no point in running # anything else. It is also a test of sorts because if this # fails, then the schematron is wrong or the file is wrong. # # Running this here instead of checking for every test cuts # down considerably on the running time of the test suite. # assert util.schematron(xml.schematron_for_version(schema_version), valid_editable.decode('utf-8'))
def test_cognate_without_semantic_fields(self): tree = lxml.etree.fromstring(valid_editable) # We remove all semantic-fields from one cognate sfs = tree.xpath("//btw:cognate[1]//btw:semantic-fields", namespaces=xml.default_namespace_mapping) for el in sfs: el.getparent().remove(el) data = lxml.etree.tostring( tree, xml_declaration=True, encoding='utf-8').decode('utf-8') self.assertFalse( util.schematron(xml.schematron_for_version(schema_version), data))
def test_cognate_without_semantic_fields(self): tree = lxml.etree.fromstring(valid_editable) # We remove all semantic-fields from one cognate sfs = tree.xpath("//btw:cognate[1]//btw:semantic-fields", namespaces=xml.default_namespace_mapping) for el in sfs: el.getparent().remove(el) data = lxml.etree.tostring(tree, xml_declaration=True, encoding='utf-8').decode('utf-8') self.assertFalse( util.schematron(xml.schematron_for_version(schema_version), data))
def test_incorrect_semantic_field(self): """ Test that the schematron test will report an error if a semantic field is incorrect. """ tree = lxml.etree.fromstring(valid_editable) # We remove all semantic-fields from one sense sfs = tree.xpath("//btw:sf", namespaces=xml.default_namespace_mapping) sfs[0].text += "x" data = lxml.etree.tostring(tree, xml_declaration=True, encoding='utf-8').decode('utf-8') self.assertFalse( util.schematron(xml.schematron_for_version(schema_version), data))
def test_empty_surname(self): """ Test that an empty surname raises an error """ tree = lxml.etree.fromstring(valid_editable) surnames = tree.xpath("//tei:surname", namespaces=xml.default_namespace_mapping) surnames[0].text = "" data = lxml.etree.tostring( tree, xml_declaration=True, encoding='utf-8').decode('utf-8') self.assertFalse( util.schematron(xml.schematron_for_version(schema_version), data))
def test_incorrect_semantic_field(self): """ Test that the schematron test will report an error if a semantic field is incorrect. """ tree = lxml.etree.fromstring(valid_editable) # We remove all semantic-fields from one sense sfs = tree.xpath("//btw:sf", namespaces=xml.default_namespace_mapping) sfs[0].text += "x" data = lxml.etree.tostring( tree, xml_declaration=True, encoding='utf-8').decode('utf-8') self.assertFalse( util.schematron(xml.schematron_for_version(schema_version), data))
def test_sense_without_semantic_fields(self): tree = lxml.etree.fromstring(valid_editable) # We remove all semantic-fields from one sense sfs = tree.xpath( "//btw:sense[1]//btw:semantic-fields[not " "(ancestor::btw:english-rendition or " "ancestor::btw:contrastive-section)]", namespaces=xml.default_namespace_mapping) for el in sfs: el.getparent().remove(el) data = lxml.etree.tostring(tree, xml_declaration=True, encoding='utf-8').decode('utf-8') self.assertFalse( util.schematron(xml.schematron_for_version(schema_version), data))
def test_sense_without_semantic_fields(self): tree = lxml.etree.fromstring(valid_editable) # We remove all semantic-fields from one sense sfs = tree.xpath( "//btw:sense[1]//btw:semantic-fields[not " "(ancestor::btw:english-rendition or " "ancestor::btw:contrastive-section)]", namespaces=xml.default_namespace_mapping) for el in sfs: el.getparent().remove(el) data = lxml.etree.tostring( tree, xml_declaration=True, encoding='utf-8').decode('utf-8') self.assertFalse( util.schematron(xml.schematron_for_version(schema_version), data))
def test_empty_surname(self): """ Test that an empty surname raises an error """ tree = lxml.etree.fromstring(valid_editable) surnames = tree.xpath("//tei:surname", namespaces=xml.default_namespace_mapping) surnames[0].text = "" data = lxml.etree.tostring(tree, xml_declaration=True, encoding='utf-8').decode('utf-8') self.assertFalse( util.schematron(xml.schematron_for_version(schema_version), data))
def test_no_author(self): """ Test that a an author must be recorded. """ tree = lxml.etree.fromstring(valid_editable) authors = tree.xpath("//btw:credit", namespaces=xml.default_namespace_mapping) for el in authors: el.getparent().remove(el) data = lxml.etree.tostring( tree, xml_declaration=True, encoding='utf-8').decode('utf-8') self.assertFalse( util.schematron(xml.schematron_for_version(schema_version), data))
def test_no_author(self): """ Test that a an author must be recorded. """ tree = lxml.etree.fromstring(valid_editable) authors = tree.xpath("//btw:credit", namespaces=xml.default_namespace_mapping) for el in authors: el.getparent().remove(el) data = lxml.etree.tostring(tree, xml_declaration=True, encoding='utf-8').decode('utf-8') self.assertFalse( util.schematron(xml.schematron_for_version(schema_version), data))
def fetch(): with open("utils/schemas/prasada.xml") as f: data = f.read() # Clean it for raw edit. data = util.run_xsltproc("utils/xsl/strip.xsl", data) tree = xml.XMLTree(data) version = tree.extract_version() if not util.validate_with_rng(xml.schema_for_version(version), data): raise ValueError("the file is not actually valid!") sch = xml.schematron_for_version(version) if sch and not util.schematron(sch, data): raise ValueError("the file does not pass schematron validation!") return data
def fetch(): with open("utils/schemas/prasada.xml") as f: data = f.read().decode("utf-8") # Clean it for raw edit. data = util.run_xsltproc("utils/xsl/strip.xsl", data) tree = xml.XMLTree(data) version = tree.extract_version() if not util.validate_with_rng(xml.schema_for_version(version), data): raise ValueError("the file is not actually valid!") sch = xml.schematron_for_version(version) if sch and not util.schematron(sch, data): raise ValueError("the file does not pass schematron validation!") return data
def valid(self): """ Whether or not the chunk is valid. Use this rather than the ``_valid`` field. """ if self._valid is not None: return self._valid if not self.is_normal: self._valid = False else: self._valid = util.validate_with_rng( xml.schema_for_version(self.schema_version), self.data) if self._valid: # We must perform the schematron checks to see whether it is # actually valid. sch = xml.schematron_for_version(self.schema_version) if sch: self._valid = util.schematron(sch, self.data) self.save() return self._valid