Skip to content

test_corpusxmlfile

Class to test the class CorpusXMLFile.

TestCorpusXMLFile

Bases: unittest.TestCase

A test class for the CorpusXMLFile class.

Source code in /home/anders/projects/CorpusTools/corpustools/test/test_corpusxmlfile.py
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
class TestCorpusXMLFile(unittest.TestCase):
    """A test class for the CorpusXMLFile class."""

    def setUp(self):
        self.pfile = corpusxmlfile.CorpusXMLFile(
            os.path.join(
                HERE,
                "parallelize_data",
                "converted/sme/facta/skuvlahistorja2/",
                "aarseth2-s.html.xml",
            )
        )

    @staticmethod
    def assertXmlEqual(got, want):
        """Check if two stringified xml snippets are equal."""
        string_got = etree.tostring(got, encoding="unicode")
        string_want = etree.tostring(want, encoding="unicode")

        checker = doctestcompare.LXMLOutputChecker()
        if not checker.check_output(string_want, string_got, 0):
            message = checker.output_difference(
                doctest.Example("", string_want), string_got, 0
            )
            raise AssertionError(message)

    def test_get_translated_from(self):
        self.assertEqual(self.pfile.translated_from, "nob")

    def test_get_word_count(self):
        corpusfile = corpusxmlfile.CorpusXMLFile(
            os.path.join(
                HERE,
                "parallelize_data",
                "converted/sme/facta/skuvlahistorja2/",
                "aarseth2-s.html.xml",
            )
        )
        self.assertEqual(corpusfile.word_count, "3229")

    def test_remove_version(self):
        file_with_version = corpusxmlfile.CorpusXMLFile(
            os.path.join(
                HERE,
                "parallelize_data",
                "converted/sme/facta/skuvlahistorja2/",
                "aarseth2-s.html.xml",
            )
        )
        file_with_version.remove_version()

        self.assertIsNone(file_with_version.root.find(".//version"))

    def test_remove_skip(self):
        file_with_skip = corpusxmlfile.CorpusXMLFile(
            os.path.join(
                HERE,
                "parallelize_data",
                "converted/sme/facta/skuvlahistorja2/",
                "aarseth2-s-with-skip.html.xml",
            )
        )
        file_with_skip.remove_skip()
        self.assertListEqual(file_with_skip.root.xpath(".//skip"), [])

    def test_move_later(self):
        file_with_later = corpusxmlfile.CorpusXMLFile(
            os.path.join(
                HERE,
                "parallelize_data",
                "converted/sme/facta/skuvlahistorja2/",
                "aarseth2-s-with-later.html.xml",
            )
        )

        before = [
            later.getparent().index(later)
            for later in file_with_later.root.xpath(".//later")
        ]
        file_with_later.move_later()
        after = [
            later.getparent().index(later)
            for later in file_with_later.root.xpath(".//later")
        ]

        self.assertNotEqual(before, after)

assertXmlEqual(got, want) staticmethod

Check if two stringified xml snippets are equal.

Source code in /home/anders/projects/CorpusTools/corpustools/test/test_corpusxmlfile.py
46
47
48
49
50
51
52
53
54
55
56
57
@staticmethod
def assertXmlEqual(got, want):
    """Check if two stringified xml snippets are equal."""
    string_got = etree.tostring(got, encoding="unicode")
    string_want = etree.tostring(want, encoding="unicode")

    checker = doctestcompare.LXMLOutputChecker()
    if not checker.check_output(string_want, string_got, 0):
        message = checker.output_difference(
            doctest.Example("", string_want), string_got, 0
        )
        raise AssertionError(message)