Skip to content

Commit dde820f

Browse files
Backport PR #53754 on branch 2.0.x (TST: Make test_complibs deterministic) (#53797)
Backport PR #53754: TST: Make test_complibs deterministic Co-authored-by: Matthew Roeschke <[email protected]>
1 parent 7839406 commit dde820f

File tree

1 file changed

+35
-25
lines changed

1 file changed

+35
-25
lines changed

pandas/tests/io/pytables/test_file_handling.py

+35-25
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@
33
import numpy as np
44
import pytest
55

6-
from pandas.compat import is_platform_little_endian
6+
from pandas.compat import (
7+
PY311,
8+
is_ci_environment,
9+
is_platform_linux,
10+
is_platform_little_endian,
11+
)
712
from pandas.errors import (
813
ClosedFileError,
914
PossibleDataLossError,
@@ -222,39 +227,44 @@ def test_complibs_default_settings_override(tmp_path, setup_path):
222227
assert node.filters.complib == "blosc"
223228

224229

225-
def test_complibs(tmp_path, setup_path):
230+
@pytest.mark.parametrize("lvl", range(10))
231+
@pytest.mark.parametrize("lib", tables.filters.all_complibs)
232+
@pytest.mark.filterwarnings("ignore:object name is not a valid")
233+
@pytest.mark.xfail(
234+
not PY311 and is_ci_environment() and is_platform_linux(),
235+
reason="producing invalid start bytes",
236+
raises=UnicodeDecodeError,
237+
strict=False,
238+
)
239+
def test_complibs(tmp_path, lvl, lib):
226240
# GH14478
227-
df = tm.makeDataFrame()
241+
df = DataFrame(
242+
np.ones((30, 4)), columns=list("ABCD"), index=np.arange(30).astype(np.str_)
243+
)
228244

229-
# Building list of all complibs and complevels tuples
230-
all_complibs = tables.filters.all_complibs
231245
# Remove lzo if its not available on this platform
232246
if not tables.which_lib_version("lzo"):
233-
all_complibs.remove("lzo")
247+
pytest.skip("lzo not available")
234248
# Remove bzip2 if its not available on this platform
235249
if not tables.which_lib_version("bzip2"):
236-
all_complibs.remove("bzip2")
250+
pytest.skip("bzip2 not available")
237251

238-
all_levels = range(0, 10)
239-
all_tests = [(lib, lvl) for lib in all_complibs for lvl in all_levels]
252+
tmpfile = tmp_path / f"{lvl}_{lib}.h5"
253+
gname = f"{lvl}_{lib}"
240254

241-
for lib, lvl in all_tests:
242-
tmpfile = tmp_path / setup_path
243-
gname = "foo"
244-
245-
# Write and read file to see if data is consistent
246-
df.to_hdf(tmpfile, gname, complib=lib, complevel=lvl)
247-
result = read_hdf(tmpfile, gname)
248-
tm.assert_frame_equal(result, df)
255+
# Write and read file to see if data is consistent
256+
df.to_hdf(tmpfile, gname, complib=lib, complevel=lvl)
257+
result = read_hdf(tmpfile, gname)
258+
tm.assert_frame_equal(result, df)
249259

250-
# Open file and check metadata for correct amount of compression
251-
with tables.open_file(tmpfile, mode="r") as h5table:
252-
for node in h5table.walk_nodes(where="/" + gname, classname="Leaf"):
253-
assert node.filters.complevel == lvl
254-
if lvl == 0:
255-
assert node.filters.complib is None
256-
else:
257-
assert node.filters.complib == lib
260+
# Open file and check metadata for correct amount of compression
261+
with tables.open_file(tmpfile, mode="r") as h5table:
262+
for node in h5table.walk_nodes(where="/" + gname, classname="Leaf"):
263+
assert node.filters.complevel == lvl
264+
if lvl == 0:
265+
assert node.filters.complib is None
266+
else:
267+
assert node.filters.complib == lib
258268

259269

260270
@pytest.mark.skipif(

0 commit comments

Comments
 (0)