Example #1
0
    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.")
Example #2
0
    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.")
Example #3
0
 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'))
Example #4
0
 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'))
Example #5
0
 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))
Example #6
0
 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))
Example #7
0
 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))
Example #8
0
    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))
Example #9
0
 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))
Example #10
0
 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))
Example #11
0
 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))
Example #12
0
    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))
Example #13
0
    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))
Example #14
0
    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))
Example #15
0
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
Example #16
0
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
Example #17
0
    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
Example #18
0
    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