Skip to content

Commit fe25d32

Browse files
committed
Try using helper function in test_openpyxl
1 parent 426233f commit fe25d32

File tree

1 file changed

+25
-70
lines changed

1 file changed

+25
-70
lines changed

pandas/tests/io/excel/test_openpyxl.py

+25-70
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,21 @@
2323
pytestmark = pytest.mark.parametrize("ext", [".xlsx"])
2424

2525

26+
def has_min_lxml_and_openpyxl():
27+
openpyxl = import_optional_dependency("openpyxl", errors="ignore")
28+
lxml = import_optional_dependency("lxml.etree", errors="ignore")
29+
return (
30+
openpyxl is not None
31+
and openpyxl.__version__ == VERSIONS["openpyxl"]
32+
and lxml is not None
33+
and lxml.__version__ == VERSIONS["lxml.etree"]
34+
)
35+
36+
37+
def openpyxl_installed_and_xlsx_xlsm(path):
38+
return ("xlsm" in path or "xlsx" in path) and has_min_lxml_and_openpyxl()
39+
40+
2641
def test_to_excel_styleconverter(ext):
2742
from openpyxl import styles
2843

@@ -57,17 +72,11 @@ def test_to_excel_styleconverter(ext):
5772

5873

5974
def test_write_cells_merge_styled(request, ext):
60-
import openpyxl
61-
6275
from pandas.io.formats.excel import ExcelCell
6376

64-
lxml = import_optional_dependency("lxml.etree", errors="ignore")
6577
request.node.add_marker(
6678
pytest.mark.xfail(
67-
openpyxl.__version__ == VERSIONS["openpyxl"]
68-
and ext == ".xlsx"
69-
and lxml is not None
70-
and lxml.__version__ == VERSIONS["lxml.etree"],
79+
openpyxl_installed_and_xlsx_xlsm(ext),
7180
reason="Fails on the min version build",
7281
raises=TypeError,
7382
)
@@ -106,15 +115,9 @@ def test_write_cells_merge_styled(request, ext):
106115
@pytest.mark.parametrize("iso_dates", [True, False])
107116
def test_kwargs(request, ext, iso_dates):
108117
# GH 42286 GH 43445
109-
import openpyxl
110-
111-
lxml = import_optional_dependency("lxml.etree", errors="ignore")
112118
request.node.add_marker(
113119
pytest.mark.xfail(
114-
openpyxl.__version__ == VERSIONS["openpyxl"]
115-
and ext == ".xlsx"
116-
and lxml is not None
117-
and lxml.__version__ == VERSIONS["lxml.etree"],
120+
openpyxl_installed_and_xlsx_xlsm(ext),
118121
reason="Fails on the min version build",
119122
raises=TypeError,
120123
)
@@ -132,15 +135,9 @@ def test_kwargs(request, ext, iso_dates):
132135
@pytest.mark.parametrize("iso_dates", [True, False])
133136
def test_engine_kwargs_write(request, ext, iso_dates):
134137
# GH 42286 GH 43445
135-
import openpyxl
136-
137-
lxml = import_optional_dependency("lxml.etree", errors="ignore")
138138
request.node.add_marker(
139139
pytest.mark.xfail(
140-
openpyxl.__version__ == VERSIONS["openpyxl"]
141-
and ext == ".xlsx"
142-
and lxml is not None
143-
and lxml.__version__ == VERSIONS["lxml.etree"],
140+
openpyxl_installed_and_xlsx_xlsm(ext),
144141
reason="Fails on the min version build",
145142
raises=TypeError,
146143
)
@@ -176,15 +173,9 @@ def test_engine_kwargs_append_data_only(request, ext, data_only, expected):
176173
# GH 43445
177174
# tests whether the data_only engine_kwarg actually works well for
178175
# openpyxl's load_workbook
179-
import openpyxl
180-
181-
lxml = import_optional_dependency("lxml.etree", errors="ignore")
182176
request.node.add_marker(
183177
pytest.mark.xfail(
184-
openpyxl.__version__ == VERSIONS["openpyxl"]
185-
and ext == ".xlsx"
186-
and lxml is not None
187-
and lxml.__version__ == VERSIONS["lxml.etree"],
178+
openpyxl_installed_and_xlsx_xlsm(ext),
188179
reason="Fails on the min version build",
189180
raises=TypeError,
190181
)
@@ -203,15 +194,9 @@ def test_engine_kwargs_append_data_only(request, ext, data_only, expected):
203194
"mode,expected", [("w", ["baz"]), ("a", ["foo", "bar", "baz"])]
204195
)
205196
def test_write_append_mode(request, ext, mode, expected):
206-
import openpyxl
207-
208-
lxml = import_optional_dependency("lxml.etree", errors="ignore")
209197
request.node.add_marker(
210198
pytest.mark.xfail(
211-
openpyxl.__version__ == VERSIONS["openpyxl"]
212-
and ext == ".xlsx"
213-
and lxml is not None
214-
and lxml.__version__ == VERSIONS["lxml.etree"],
199+
openpyxl_installed_and_xlsx_xlsm(ext),
215200
reason="Fails on the min version build",
216201
raises=TypeError,
217202
)
@@ -249,15 +234,9 @@ def test_if_sheet_exists_append_modes(
249234
request, ext, if_sheet_exists, num_sheets, expected
250235
):
251236
# GH 40230
252-
import openpyxl
253-
254-
lxml = import_optional_dependency("lxml.etree", errors="ignore")
255237
request.node.add_marker(
256238
pytest.mark.xfail(
257-
openpyxl.__version__ == VERSIONS["openpyxl"]
258-
and ext == ".xlsx"
259-
and lxml is not None
260-
and lxml.__version__ == VERSIONS["lxml.etree"],
239+
openpyxl_installed_and_xlsx_xlsm(ext),
261240
reason="Fails on the min version build",
262241
raises=TypeError,
263242
)
@@ -295,15 +274,9 @@ def test_if_sheet_exists_append_modes(
295274
def test_append_overlay_startrow_startcol(
296275
request, ext, startrow, startcol, greeting, goodbye
297276
):
298-
import openpyxl
299-
300-
lxml = import_optional_dependency("lxml.etree", errors="ignore")
301277
request.node.add_marker(
302278
pytest.mark.xfail(
303-
openpyxl.__version__ == VERSIONS["openpyxl"]
304-
and ext == ".xlsx"
305-
and lxml is not None
306-
and lxml.__version__ == VERSIONS["lxml.etree"],
279+
openpyxl_installed_and_xlsx_xlsm(ext),
307280
reason="Fails on the min version build",
308281
raises=TypeError,
309282
)
@@ -351,15 +324,9 @@ def test_append_overlay_startrow_startcol(
351324
)
352325
def test_if_sheet_exists_raises(request, ext, if_sheet_exists, msg):
353326
# GH 40230
354-
import openpyxl
355-
356-
lxml = import_optional_dependency("lxml.etree", errors="ignore")
357327
request.node.add_marker(
358328
pytest.mark.xfail(
359-
openpyxl.__version__ == VERSIONS["openpyxl"]
360-
and ext == ".xlsx"
361-
and lxml is not None
362-
and lxml.__version__ == VERSIONS["lxml.etree"],
329+
openpyxl_installed_and_xlsx_xlsm(ext),
363330
reason="Fails on the min version build",
364331
raises=TypeError,
365332
)
@@ -376,15 +343,9 @@ def test_if_sheet_exists_raises(request, ext, if_sheet_exists, msg):
376343

377344
def test_to_excel_with_openpyxl_engine(request, ext):
378345
# GH 29854
379-
import openpyxl
380-
381-
lxml = import_optional_dependency("lxml.etree", errors="ignore")
382346
request.node.add_marker(
383347
pytest.mark.xfail(
384-
openpyxl.__version__ == VERSIONS["openpyxl"]
385-
and ext == ".xlsx"
386-
and lxml is not None
387-
and lxml.__version__ == VERSIONS["lxml.etree"],
348+
openpyxl_installed_and_xlsx_xlsm(ext),
388349
reason="Fails on the min version build",
389350
raises=TypeError,
390351
)
@@ -448,15 +409,9 @@ def test_read_with_bad_dimension(
448409

449410
def test_append_mode_file(request, ext):
450411
# GH 39576
451-
import openpyxl
452-
453-
lxml = import_optional_dependency("lxml.etree", errors="ignore")
454412
request.node.add_marker(
455413
pytest.mark.xfail(
456-
openpyxl.__version__ == VERSIONS["openpyxl"]
457-
and ext == ".xlsx"
458-
and lxml is not None
459-
and lxml.__version__ == VERSIONS["lxml.etree"],
414+
openpyxl_installed_and_xlsx_xlsm(ext),
460415
reason="Fails on the min version build",
461416
raises=TypeError,
462417
)

0 commit comments

Comments
 (0)