| 
41 | 41 | tables = pytest.importorskip("tables")  | 
42 | 42 | 
 
  | 
43 | 43 | 
 
  | 
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  | 
60 | 61 | 
 
  | 
61 | 62 | 
 
  | 
62 | 63 | def test_no_track_times(tmp_path, setup_path):  | 
@@ -971,37 +972,37 @@ def test_pickle_path_localpath():  | 
971 | 972 | 
 
  | 
972 | 973 | 
 
  | 
973 | 974 | @pytest.mark.parametrize("propindexes", [True, False])  | 
974 |  | -def test_copy(propindexes):  | 
 | 975 | +def test_copy(propindexes, temp_file):  | 
975 | 976 |     df = DataFrame(  | 
976 | 977 |         1.1 * np.arange(120).reshape((30, 4)),  | 
977 | 978 |         columns=Index(list("ABCD")),  | 
978 | 979 |         index=Index([f"i-{i}" for i in range(30)]),  | 
979 | 980 |     )  | 
980 | 981 | 
 
  | 
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  | 
1005 | 1006 | 
 
  | 
1006 | 1007 | 
 
  | 
1007 | 1008 | def test_duplicate_column_name(tmp_path, setup_path):  | 
 | 
0 commit comments