|
| 1 | +import fsspec |
| 2 | +import pytest |
| 3 | + |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +from pins.tests.helpers import rm_env |
| 7 | + |
| 8 | +from pins.meta import MetaRaw |
| 9 | +from pins.config import PINS_ENV_INSECURE_READ |
| 10 | +from pins.drivers import load_data, save_data |
| 11 | +from pins.errors import PinsInsecureReadError |
| 12 | + |
| 13 | + |
| 14 | +@pytest.fixture |
| 15 | +def some_joblib(tmp_dir2): |
| 16 | + import joblib |
| 17 | + |
| 18 | + p_obj = tmp_dir2 / "some.joblib" |
| 19 | + joblib.dump({"a": 1}, p_obj) |
| 20 | + |
| 21 | + return p_obj |
| 22 | + |
| 23 | + |
| 24 | +def test_driver_roundtrip_csv(tmp_dir2): |
| 25 | + # TODO: I think this test highlights the challenge of getting the flow |
| 26 | + # between metadata, drivers, and the metafactory right. |
| 27 | + # There is the name of the data (relative to the pin directory), and the full |
| 28 | + # name of data in its temporary directory. |
| 29 | + import pandas as pd |
| 30 | + |
| 31 | + df = pd.DataFrame({"x": [1, 2, 3]}) |
| 32 | + |
| 33 | + fname = "some_df" |
| 34 | + type_ = "csv" |
| 35 | + |
| 36 | + p_obj = tmp_dir2 / fname |
| 37 | + res_fname = save_data(df, p_obj, type_) |
| 38 | + |
| 39 | + assert Path(res_fname).name == f"{fname}.csv" |
| 40 | + |
| 41 | + meta = MetaRaw(f"{fname}.csv", type_, "my_pin") |
| 42 | + obj = load_data(meta, fsspec.filesystem("file"), tmp_dir2) |
| 43 | + |
| 44 | + assert df.equals(obj) |
| 45 | + |
| 46 | + |
| 47 | +@pytest.mark.skip("TODO: complete once driver story is fleshed out") |
| 48 | +def test_driver_roundtrip_joblib(tmp_dir2): |
| 49 | + pass |
| 50 | + |
| 51 | + |
| 52 | +def test_driver_pickle_read_fail_explicit(some_joblib): |
| 53 | + meta = MetaRaw(some_joblib.name, "joblib", "my_pin") |
| 54 | + with pytest.raises(PinsInsecureReadError): |
| 55 | + load_data( |
| 56 | + meta, fsspec.filesystem("file"), some_joblib.parent, allow_pickle_read=False |
| 57 | + ) |
| 58 | + |
| 59 | + |
| 60 | +def test_driver_pickle_read_fail_default(some_joblib): |
| 61 | + meta = MetaRaw(some_joblib.name, "joblib", "my_pin") |
| 62 | + with rm_env(PINS_ENV_INSECURE_READ), pytest.raises(PinsInsecureReadError): |
| 63 | + load_data( |
| 64 | + meta, fsspec.filesystem("file"), some_joblib.parent, allow_pickle_read=False |
| 65 | + ) |
| 66 | + |
| 67 | + |
| 68 | +def test_driver_apply_suffix_false(tmp_dir2): |
| 69 | + import pandas as pd |
| 70 | + |
| 71 | + df = pd.DataFrame({"x": [1, 2, 3]}) |
| 72 | + |
| 73 | + fname = "some_df" |
| 74 | + type_ = "csv" |
| 75 | + |
| 76 | + p_obj = tmp_dir2 / fname |
| 77 | + res_fname = save_data(df, p_obj, type_, apply_suffix=False) |
| 78 | + |
| 79 | + assert Path(res_fname).name == "some_df" |
0 commit comments