Skip to content

Commit 60da51d

Browse files
rhshadrachyehoshuadimarsky
authored andcommitted
TST: Simplify test_register_writer (pandas-dev#45984)
1 parent 48b6893 commit 60da51d

File tree

1 file changed

+18
-20
lines changed

1 file changed

+18
-20
lines changed

pandas/tests/io/excel/test_writers.py

+18-20
Original file line numberDiff line numberDiff line change
@@ -1309,11 +1309,6 @@ def test_ExcelWriter_dispatch_raises(self):
13091309
ExcelWriter("nothing")
13101310

13111311
def test_register_writer(self):
1312-
# some awkward mocking to test out dispatch and such actually works
1313-
called_save = []
1314-
called_write_cells = []
1315-
called_sheets = []
1316-
13171312
class DummyClass(ExcelWriter):
13181313
called_save = False
13191314
called_write_cells = False
@@ -1325,34 +1320,37 @@ def book(self):
13251320
pass
13261321

13271322
def _save(self):
1328-
called_save.append(True)
1323+
type(self).called_save = True
13291324

13301325
def _write_cells(self, *args, **kwargs):
1331-
called_write_cells.append(True)
1326+
type(self).called_write_cells = True
13321327

13331328
@property
13341329
def sheets(self):
1335-
called_sheets.append(True)
1330+
type(self).called_sheets = True
1331+
1332+
@classmethod
1333+
def assert_called_and_reset(cls):
1334+
assert cls.called_save
1335+
assert cls.called_write_cells
1336+
assert not cls.called_sheets
1337+
cls.called_save = False
1338+
cls.called_write_cells = False
13361339

1337-
def check_called(func):
1338-
func()
1339-
assert len(called_save) >= 1
1340-
assert len(called_write_cells) >= 1
1341-
assert len(called_sheets) == 0
1342-
del called_save[:]
1343-
del called_write_cells[:]
1344-
del called_sheets[:]
1340+
register_writer(DummyClass)
13451341

13461342
with option_context("io.excel.xlsx.writer", "dummy"):
13471343
path = "something.xlsx"
13481344
with tm.ensure_clean(path) as filepath:
1349-
register_writer(DummyClass)
13501345
with ExcelWriter(filepath) as writer:
13511346
assert isinstance(writer, DummyClass)
13521347
df = tm.makeCustomDataframe(1, 1)
1353-
check_called(lambda: df.to_excel(filepath))
1354-
with tm.ensure_clean("something.xls") as filepath:
1355-
check_called(lambda: df.to_excel(filepath, engine="dummy"))
1348+
df.to_excel(filepath)
1349+
DummyClass.assert_called_and_reset()
1350+
1351+
with tm.ensure_clean("something.xls") as filepath:
1352+
df.to_excel(filepath, engine="dummy")
1353+
DummyClass.assert_called_and_reset()
13561354

13571355
@pytest.mark.parametrize(
13581356
"ext",

0 commit comments

Comments
 (0)