summaryrefslogtreecommitdiffstats
path: root/test/python/test_xls_xml.py
blob: b8ae918752532e2486509abfbd1382dbf43655e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
#!/usr/bin/env python3
########################################################################
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
########################################################################

import unittest
import os
import os.path

import orcus
from orcus import xls_xml

import file_load_common as common


class TestCase(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        # base directory for ods test files.
        basedir = os.path.join(os.path.dirname(__file__), "..", "xls-xml")
        cls.basedir = os.path.normpath(basedir)

    def test_import(self):

        test_dirs = (
            "basic",
            "bold-and-italic",
            "colored-text",
            "empty-rows",
            "merged-cells",
            "named-expression",
            "named-expression-sheet-local",
            "raw-values-1",
        )

        for test_dir in test_dirs:
            test_dir = os.path.join(self.basedir, test_dir)
            common.run_test_dir(self, test_dir, common.DocLoader(xls_xml))

    def test_skip_error_cells(self):
        filepath = os.path.join(self.basedir, "formula-cells-parse-error", "input.xml")
        with open(filepath, "rb") as f:
            bytes = f.read()

        with self.assertRaises(RuntimeError):
            doc = xls_xml.read(bytes)

        with self.assertRaises(RuntimeError):  # TODO : should we raise a more specific error?
            doc = xls_xml.read(bytes, error_policy="fail")

        # With the 'skip' policy, formula cells with erroneous formulas are
        # imported as formula cells with error.
        doc = xls_xml.read(bytes, error_policy="skip")

        # Make sure cells B2 and A5 are imported as formula cells.
        rows = [row for row in doc.sheets[0].get_rows()]
        c = rows[1][1]
        self.assertEqual(c.type, orcus.CellType.FORMULA_WITH_ERROR)
        self.assertFalse(c.formula)  # formula string should be empty
        # error formula tokens consist of: error token, string token (original formula), string token (error message).
        formula_tokens = [t for t in c.get_formula_tokens()]
        self.assertEqual(formula_tokens[0].type, orcus.FormulaTokenType.ERROR)
        self.assertEqual(formula_tokens[1].type, orcus.FormulaTokenType.VALUE)
        self.assertEqual(formula_tokens[2].type, orcus.FormulaTokenType.VALUE)
        c = rows[4][0]
        self.assertEqual(c.type, orcus.CellType.FORMULA_WITH_ERROR)
        self.assertFalse(c.formula)  # formula string should be empty
        formula_tokens = [t for t in c.get_formula_tokens()]
        self.assertEqual(formula_tokens[0].type, orcus.FormulaTokenType.ERROR)
        self.assertEqual(formula_tokens[1].type, orcus.FormulaTokenType.VALUE)
        self.assertEqual(formula_tokens[2].type, orcus.FormulaTokenType.VALUE)


if __name__ == '__main__':
    unittest.main()