Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions pandas/tests/io/excel/test_odf.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,11 @@
import numpy as np
import pytest

from pandas.compat import is_platform_windows

import pandas as pd
import pandas._testing as tm

pytest.importorskip("odf")

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


@pytest.fixture(autouse=True)
def cd_and_set_engine(monkeypatch, datapath):
Expand Down
99 changes: 50 additions & 49 deletions pandas/tests/io/excel/test_odswriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,63 +3,63 @@
datetime,
)
import re
import uuid

import pytest

from pandas.compat import is_platform_windows

import pandas as pd
import pandas._testing as tm

from pandas.io.excel import ExcelWriter

odf = pytest.importorskip("odf")

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


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


def test_write_append_mode_raises(ext):
@pytest.fixture
def tmp_excel(ext, tmp_path):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this fixture just for excel?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think our Excel code is picky about checking the file extension and hence if/how ext is defined in our tests. I think in the general case this won't matter but there might be a better way to generalize how this is set up in our Excel tests?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good. Not a blocker by any means

tmp = tmp_path / f"{uuid.uuid4()}{ext}"
tmp.touch()
yield str(tmp)
tmp.unlink()


def test_write_append_mode_raises(tmp_excel):
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")
with pytest.raises(ValueError, match=msg):
ExcelWriter(tmp_excel, engine="odf", mode="a")


@pytest.mark.parametrize("engine_kwargs", [None, {"kwarg": 1}])
def test_engine_kwargs(ext, engine_kwargs):
def test_engine_kwargs(tmp_excel, 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):
if engine_kwargs is not None:
error = re.escape(
"OpenDocumentSpreadsheet() got an unexpected keyword argument 'kwarg'"
)
with pytest.raises(
TypeError,
match=error,
):
ExcelWriter(tmp_excel, engine="odf", engine_kwargs=engine_kwargs)
else:
with ExcelWriter(tmp_excel, engine="odf", engine_kwargs=engine_kwargs) as _:
pass


def test_book_and_sheets_consistent(tmp_excel):
# 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}
with ExcelWriter(tmp_excel) 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(
Expand All @@ -78,7 +78,9 @@ def test_book_and_sheets_consistent(ext):
(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):
def test_cell_value_type(
tmp_excel, 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
Expand All @@ -89,18 +91,17 @@ def test_cell_value_type(ext, value, cell_value_type, cell_value_attribute, cell

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
pd.DataFrame([[value]]).to_excel(tmp_excel, header=False, index=False)

with pd.ExcelFile(tmp_excel) 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
Loading