| 33 | |
| 34 | @pytest.mark.parametrize("mode", ["r", "r+", "a", "w"]) |
| 35 | def test_mode(temp_h5_path, mode, using_infer_string): |
| 36 | df = DataFrame( |
| 37 | np.random.default_rng(2).standard_normal((10, 4)), |
| 38 | columns=Index(list("ABCD"), dtype=object), |
| 39 | index=date_range("2000-01-01", periods=10, freq="B"), |
| 40 | ) |
| 41 | msg = r"[\S]* does not exist" |
| 42 | doesnt_exist = f"{uuid.uuid4()}.h5" |
| 43 | |
| 44 | # constructor |
| 45 | if mode in ["r", "r+"]: |
| 46 | with pytest.raises(OSError, match=msg): |
| 47 | HDFStore(doesnt_exist, mode=mode) |
| 48 | |
| 49 | else: |
| 50 | with HDFStore(temp_h5_path, mode=mode) as store: |
| 51 | assert store._handle.mode == mode |
| 52 | |
| 53 | # context |
| 54 | if mode in ["r", "r+"]: |
| 55 | with pytest.raises(OSError, match=msg): |
| 56 | with HDFStore(doesnt_exist, mode=mode) as store: |
| 57 | pass |
| 58 | else: |
| 59 | with HDFStore(temp_h5_path, mode=mode) as store: |
| 60 | assert store._handle.mode == mode |
| 61 | |
| 62 | # conv write |
| 63 | if mode in ["r", "r+"]: |
| 64 | with pytest.raises(OSError, match=msg): |
| 65 | df.to_hdf(doesnt_exist, key="df", mode=mode) |
| 66 | df.to_hdf(temp_h5_path, key="df", mode="w") |
| 67 | else: |
| 68 | df.to_hdf(temp_h5_path, key="df", mode=mode) |
| 69 | |
| 70 | # conv read |
| 71 | if mode in ["w"]: |
| 72 | msg = ( |
| 73 | "mode w is not allowed while performing a read. " |
| 74 | r"Allowed modes are r, r\+ and a." |
| 75 | ) |
| 76 | with pytest.raises(ValueError, match=msg): |
| 77 | read_hdf(temp_h5_path, "df", mode=mode) |
| 78 | else: |
| 79 | result = read_hdf(temp_h5_path, "df", mode=mode) |
| 80 | if using_infer_string: |
| 81 | df.columns = df.columns.astype("str") |
| 82 | tm.assert_frame_equal(result, df) |
| 83 | |
| 84 | |
| 85 | def test_default_mode(temp_h5_path, using_infer_string): |