Skip to content

Commit

Permalink
CI/TST: Use tmp_path for excel test (pandas-dev#57516)
Browse files Browse the repository at this point in the history
* Revert "CI: Run excel tests on single cpu for windows (pandas-dev#57486)"

This reverts commit 9c02050.

* Use tmp_path fixture

* uuid4?

* Don't remove, pytest cleans itself up eventually
  • Loading branch information
mroeschke authored and pmhatre1 committed May 7, 2024
1 parent d37b760 commit e4575b8
Show file tree
Hide file tree
Showing 8 changed files with 836 additions and 864 deletions.
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
98 changes: 49 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,62 @@
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):
tmp = tmp_path / f"{uuid.uuid4()}{ext}"
tmp.touch()
return str(tmp)


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 +77,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 +90,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

0 comments on commit e4575b8

Please sign in to comment.