Skip to content

TST: Simplify test_register_writer #45984

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 14, 2022
Merged
Changes from all 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
38 changes: 18 additions & 20 deletions pandas/tests/io/excel/test_writers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1309,11 +1309,6 @@ def test_ExcelWriter_dispatch_raises(self):
ExcelWriter("nothing")

def test_register_writer(self):
# some awkward mocking to test out dispatch and such actually works
called_save = []
called_write_cells = []
called_sheets = []

class DummyClass(ExcelWriter):
called_save = False
called_write_cells = False
Expand All @@ -1325,34 +1320,37 @@ def book(self):
pass

def _save(self):
called_save.append(True)
type(self).called_save = True

def _write_cells(self, *args, **kwargs):
called_write_cells.append(True)
type(self).called_write_cells = True

@property
def sheets(self):
called_sheets.append(True)
type(self).called_sheets = True

@classmethod
def assert_called_and_reset(cls):
assert cls.called_save
assert cls.called_write_cells
assert not cls.called_sheets
cls.called_save = False
cls.called_write_cells = False

def check_called(func):
func()
assert len(called_save) >= 1
assert len(called_write_cells) >= 1
assert len(called_sheets) == 0
del called_save[:]
del called_write_cells[:]
del called_sheets[:]
register_writer(DummyClass)

with option_context("io.excel.xlsx.writer", "dummy"):
path = "something.xlsx"
with tm.ensure_clean(path) as filepath:
register_writer(DummyClass)
with ExcelWriter(filepath) as writer:
assert isinstance(writer, DummyClass)
df = tm.makeCustomDataframe(1, 1)
check_called(lambda: df.to_excel(filepath))
with tm.ensure_clean("something.xls") as filepath:
check_called(lambda: df.to_excel(filepath, engine="dummy"))
df.to_excel(filepath)
DummyClass.assert_called_and_reset()

with tm.ensure_clean("something.xls") as filepath:
df.to_excel(filepath, engine="dummy")
DummyClass.assert_called_and_reset()

@pytest.mark.parametrize(
"ext",
Expand Down