From aa431667fdeb12dcb14f50e2a9eedf518e82966d Mon Sep 17 00:00:00 2001 From: Richard Shadrach Date: Mon, 14 Feb 2022 09:10:57 -0500 Subject: [PATCH] TST: Simplify test_register_writer --- pandas/tests/io/excel/test_writers.py | 38 +++++++++++++-------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/pandas/tests/io/excel/test_writers.py b/pandas/tests/io/excel/test_writers.py index 16a656d71e496..5b46c0baac165 100644 --- a/pandas/tests/io/excel/test_writers.py +++ b/pandas/tests/io/excel/test_writers.py @@ -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 @@ -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",