File: test_odswriter.py

package info (click to toggle)
pandas 2.3.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 66,800 kB
  • sloc: python: 424,812; ansic: 9,190; sh: 264; xml: 102; makefile: 86
file content (107 lines) | stat: -rw-r--r-- 3,316 bytes parent folder | download | duplicates (2)
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
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
from datetime import (
    date,
    datetime,
)
import re

import pytest

import pandas.util._test_decorators as td
from pandas.compat import is_platform_windows

import pandas as pd
import pandas._testing as tm

from pandas.io.excel import ExcelWriter

odf = td.versioned_importorskip("odf")

if is_platform_windows():
    pytestmark = pytest.mark.single_cpu


@pytest.fixture
def ext():
    return ".ods"


def test_write_append_mode_raises(ext):
    msg = "Append mode is not supported with odf!"

    with tm.ensure_clean(ext) as f:
        with pytest.raises(ValueError, match=msg):
            ExcelWriter(f, engine="odf", mode="a")


@pytest.mark.parametrize("engine_kwargs", [None, {"kwarg": 1}])
def test_engine_kwargs(ext, engine_kwargs):
    # GH 42286
    # GH 43445
    # test for error: OpenDocumentSpreadsheet does not accept any arguments
    with tm.ensure_clean(ext) as f:
        if engine_kwargs is not None:
            error = re.escape(
                "OpenDocumentSpreadsheet() got an unexpected keyword argument 'kwarg'"
            )
            with pytest.raises(
                TypeError,
                match=error,
            ):
                ExcelWriter(f, engine="odf", engine_kwargs=engine_kwargs)
        else:
            with ExcelWriter(f, engine="odf", engine_kwargs=engine_kwargs) as _:
                pass


def test_book_and_sheets_consistent(ext):
    # GH#45687 - Ensure sheets is updated if user modifies book
    with tm.ensure_clean(ext) as f:
        with ExcelWriter(f) as writer:
            assert writer.sheets == {}
            table = odf.table.Table(name="test_name")
            writer.book.spreadsheet.addElement(table)
            assert writer.sheets == {"test_name": table}


@pytest.mark.parametrize(
    ["value", "cell_value_type", "cell_value_attribute", "cell_value"],
    argvalues=[
        (True, "boolean", "boolean-value", "true"),
        ("test string", "string", "string-value", "test string"),
        (1, "float", "value", "1"),
        (1.5, "float", "value", "1.5"),
        (
            datetime(2010, 10, 10, 10, 10, 10),
            "date",
            "date-value",
            "2010-10-10T10:10:10",
        ),
        (date(2010, 10, 10), "date", "date-value", "2010-10-10"),
    ],
)
def test_cell_value_type(ext, value, cell_value_type, cell_value_attribute, cell_value):
    # GH#54994 ODS: cell attributes should follow specification
    # http://docs.oasis-open.org/office/v1.2/os/OpenDocument-v1.2-os-part1.html#refTable13
    from odf.namespaces import OFFICENS
    from odf.table import (
        TableCell,
        TableRow,
    )

    table_cell_name = TableCell().qname

    with tm.ensure_clean(ext) as f:
        pd.DataFrame([[value]]).to_excel(f, header=False, index=False)

        with pd.ExcelFile(f) as wb:
            sheet = wb._reader.get_sheet_by_index(0)
            sheet_rows = sheet.getElementsByType(TableRow)
            sheet_cells = [
                x
                for x in sheet_rows[0].childNodes
                if hasattr(x, "qname") and x.qname == table_cell_name
            ]

            cell = sheet_cells[0]
            assert cell.attributes.get((OFFICENS, "value-type")) == cell_value_type
            assert cell.attributes.get((OFFICENS, cell_value_attribute)) == cell_value