Skip to content

Commit 7ebabd1

Browse files
Aokizy2aokizy
andauthored
TST: Replace ensure_clean with temp_file in test_file_store.py (#62896)
Co-authored-by: aokizy <14817191+aokizy2@user.noreply.gitee.com>
1 parent 9f4c0ba commit 7ebabd1

File tree

1 file changed

+42
-41
lines changed

1 file changed

+42
-41
lines changed

pandas/tests/io/pytables/test_store.py

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,23 @@
4141
tables = pytest.importorskip("tables")
4242

4343

44-
def test_context(setup_path):
45-
with tm.ensure_clean(setup_path) as path:
46-
try:
47-
with HDFStore(path) as tbl:
48-
raise ValueError("blah")
49-
except ValueError:
50-
pass
51-
with tm.ensure_clean(setup_path) as path:
52-
with HDFStore(path) as tbl:
53-
tbl["a"] = DataFrame(
54-
1.1 * np.arange(120).reshape((30, 4)),
55-
columns=Index(list("ABCD"), dtype=object),
56-
index=Index([f"i-{i}" for i in range(30)], dtype=object),
57-
)
58-
assert len(tbl) == 1
59-
assert type(tbl["a"]) == DataFrame
44+
def test_context(tmp_path):
45+
path1 = tmp_path / "test1.h5"
46+
try:
47+
with HDFStore(path1) as tbl:
48+
raise ValueError("blah")
49+
except ValueError:
50+
pass
51+
52+
path2 = tmp_path / "test2.h5"
53+
with HDFStore(path2) as tbl:
54+
tbl["a"] = DataFrame(
55+
1.1 * np.arange(120).reshape((30, 4)),
56+
columns=Index(list("ABCD"), dtype=object),
57+
index=Index([f"i-{i}" for i in range(30)], dtype=object),
58+
)
59+
assert len(tbl) == 1
60+
assert type(tbl["a"]) == DataFrame
6061

6162

6263
def test_no_track_times(tmp_path, setup_path):
@@ -971,37 +972,37 @@ def test_pickle_path_localpath():
971972

972973

973974
@pytest.mark.parametrize("propindexes", [True, False])
974-
def test_copy(propindexes):
975+
def test_copy(propindexes, temp_file):
975976
df = DataFrame(
976977
1.1 * np.arange(120).reshape((30, 4)),
977978
columns=Index(list("ABCD")),
978979
index=Index([f"i-{i}" for i in range(30)]),
979980
)
980981

981-
with tm.ensure_clean() as path:
982-
with HDFStore(path) as st:
983-
st.append("df", df, data_columns=["A"])
984-
with tempfile.NamedTemporaryFile() as new_f:
985-
with HDFStore(path) as store:
986-
with contextlib.closing(
987-
store.copy(new_f.name, keys=None, propindexes=propindexes)
988-
) as tstore:
989-
# check keys
990-
keys = store.keys()
991-
assert set(keys) == set(tstore.keys())
992-
# check indices & nrows
993-
for k in tstore.keys():
994-
if tstore.get_storer(k).is_table:
995-
new_t = tstore.get_storer(k)
996-
orig_t = store.get_storer(k)
997-
998-
assert orig_t.nrows == new_t.nrows
999-
1000-
# check propindixes
1001-
if propindexes:
1002-
for a in orig_t.axes:
1003-
if a.is_indexed:
1004-
assert new_t[a.name].is_indexed
982+
path = temp_file
983+
with HDFStore(path) as st:
984+
st.append("df", df, data_columns=["A"])
985+
with tempfile.NamedTemporaryFile() as new_f:
986+
with HDFStore(path) as store:
987+
with contextlib.closing(
988+
store.copy(new_f.name, keys=None, propindexes=propindexes)
989+
) as tstore:
990+
# check keys
991+
keys = store.keys()
992+
assert set(keys) == set(tstore.keys())
993+
# check indices & nrows
994+
for k in tstore.keys():
995+
if tstore.get_storer(k).is_table:
996+
new_t = tstore.get_storer(k)
997+
orig_t = store.get_storer(k)
998+
999+
assert orig_t.nrows == new_t.nrows
1000+
1001+
# check propindixes
1002+
if propindexes:
1003+
for a in orig_t.axes:
1004+
if a.is_indexed:
1005+
assert new_t[a.name].is_indexed
10051006

10061007

10071008
def test_duplicate_column_name(tmp_path, setup_path):

0 commit comments

Comments
 (0)