From d215a8cd6cf958be0f4fc41c6e90e21c907b4655 Mon Sep 17 00:00:00 2001 From: Nathan McDougall Date: Wed, 24 Jul 2024 11:21:50 +1200 Subject: [PATCH] Replace tmp_dir2 fixture with tmp_path. (#270) --- pins/tests/conftest.py | 9 --------- pins/tests/test_boards.py | 22 +++++++++++----------- pins/tests/test_cache.py | 8 ++++---- pins/tests/test_constructors.py | 6 +++--- pins/tests/test_drivers.py | 30 +++++++++++++++--------------- 5 files changed, 33 insertions(+), 42 deletions(-) diff --git a/pins/tests/conftest.py b/pins/tests/conftest.py index 9963bdc..b4f8df9 100644 --- a/pins/tests/conftest.py +++ b/pins/tests/conftest.py @@ -64,15 +64,6 @@ def df(): return pd.DataFrame({"x": [1, 2, 3], "y": ["a", "b", "c"]}) -@pytest.fixture -def tmp_dir2(): - # fixture for offering a temporary directory - # note that pytest has a built-in fixture tmp_dir, but it uses the lib py.path - # which recommends using pathlib, etc.. - with tempfile.TemporaryDirectory() as tmp_dir: - yield Path(tmp_dir) - - @pytest.fixture def tmp_cache(): with rm_env("PINS_CACHE_DIR"): diff --git a/pins/tests/test_boards.py b/pins/tests/test_boards.py index 7567ff1..3c2d06e 100644 --- a/pins/tests/test_boards.py +++ b/pins/tests/test_boards.py @@ -76,14 +76,14 @@ def test_board_pin_write_default_title(board): assert meta.title == "df_csv: a pinned 3 x 2 DataFrame" -def test_board_pin_write_prepare_pin(board, tmp_dir2): +def test_board_pin_write_prepare_pin(board, tmp_path: Path): df = pd.DataFrame({"x": [1, 2, 3], "y": [4, 5, 6]}) - meta = board.prepare_pin_version(str(tmp_dir2), df, "df_csv", title=None, type="csv") + meta = board.prepare_pin_version(str(tmp_path), df, "df_csv", title=None, type="csv") assert meta.file == "df_csv.csv" - assert (tmp_dir2 / "data.txt").exists() - assert (tmp_dir2 / "df_csv.csv").exists() - assert not (tmp_dir2 / "df_csv.csv").is_dir() + assert (tmp_path / "data.txt").exists() + assert (tmp_path / "df_csv.csv").exists() + assert not (tmp_path / "df_csv.csv").is_dir() def test_board_pin_write_roundtrip(board): @@ -231,7 +231,7 @@ def test_board_pin_upload_path_list(board_with_cache, tmp_path): (pin_path,) = board_with_cache.pin_download("cool_pin") -def test_board_pin_write_rsc_index_html(board, tmp_dir2, snapshot): +def test_board_pin_write_rsc_index_html(board, tmp_path: Path, snapshot): if board.fs.protocol != "rsc": pytest.skip() @@ -240,7 +240,7 @@ def test_board_pin_write_rsc_index_html(board, tmp_dir2, snapshot): pin_name = "test_rsc_pin" board.prepare_pin_version( - str(tmp_dir2), + str(tmp_path), df, pin_name, type="csv", @@ -249,7 +249,7 @@ def test_board_pin_write_rsc_index_html(board, tmp_dir2, snapshot): created=DEFAULT_CREATION_DATE, ) - snapshot.assert_equal_dir(tmp_dir2) + snapshot.assert_equal_dir(tmp_path) # pin_write against different types ------------------------------------------- @@ -474,14 +474,14 @@ def test_board_pin_search_name(board, df, search, matches): from pins.cache import PinsCache # noqa -def test_board_base_pin_meta_cache_touch(tmp_dir2, df): +def test_board_base_pin_meta_cache_touch(tmp_path: Path, df): cache = fsspec.filesystem( "pinscache", target_protocol="file", same_names=True, - hash_prefix=str(tmp_dir2), + hash_prefix=str(tmp_path), ) - board = BaseBoard(str(tmp_dir2), fs=cache) + board = BaseBoard(str(tmp_path), fs=cache) board.pin_write(df, "some_df", type="csv") meta = board.pin_meta("some_df") diff --git a/pins/tests/test_cache.py b/pins/tests/test_cache.py index c93e2dc..8bbadb9 100644 --- a/pins/tests/test_cache.py +++ b/pins/tests/test_cache.py @@ -28,8 +28,8 @@ def _sleep(): @pytest.fixture -def some_file(tmp_dir2): - p = tmp_dir2 / "some_file.txt" +def some_file(tmp_path): + p = tmp_path / "some_file.txt" p.touch() return p @@ -87,8 +87,8 @@ def test_pins_cache_open(): @pytest.fixture -def a_cache(tmp_dir2): - return tmp_dir2 / "board_cache" +def a_cache(tmp_path): + return tmp_path / "board_cache" def create_metadata(p, access_time): diff --git a/pins/tests/test_constructors.py b/pins/tests/test_constructors.py index 4d0a20d..6f5654a 100644 --- a/pins/tests/test_constructors.py +++ b/pins/tests/test_constructors.py @@ -274,11 +274,11 @@ def test_board_constructor_temp_writable(): assert len(list(p_board.glob("*"))) == 0 -def test_board_constructor_folder(tmp_dir2, df): - board = c.board_folder(str(tmp_dir2)) +def test_board_constructor_folder(tmp_path: Path, df): + board = c.board_folder(str(tmp_path)) board.pin_write(df, "some_df", type="csv") - assert (tmp_dir2 / "some_df").exists() + assert (tmp_path / "some_df").exists() df2 = board.pin_read("some_df") assert df.equals(df2) diff --git a/pins/tests/test_drivers.py b/pins/tests/test_drivers.py index 5853045..84e80d2 100644 --- a/pins/tests/test_drivers.py +++ b/pins/tests/test_drivers.py @@ -13,10 +13,10 @@ @pytest.fixture -def some_joblib(tmp_dir2): +def some_joblib(tmp_path: Path): import joblib - p_obj = tmp_dir2 / "some.joblib" + p_obj = tmp_path / "some.joblib" joblib.dump({"a": 1}, p_obj) return p_obj @@ -54,7 +54,7 @@ def test_default_title(obj, dst_title): "joblib", ], ) -def test_driver_roundtrip(tmp_dir2, type_): +def test_driver_roundtrip(tmp_path: Path, type_): # TODO: I think this test highlights the challenge of getting the flow # between metadata, drivers, and the metafactory right. # There is the name of the data (relative to the pin directory), and the full @@ -66,13 +66,13 @@ def test_driver_roundtrip(tmp_dir2, type_): fname = "some_df" full_file = f"{fname}.{type_}" - p_obj = tmp_dir2 / fname + p_obj = tmp_path / fname res_fname = save_data(df, p_obj, type_) assert Path(res_fname).name == full_file meta = MetaRaw(full_file, type_, "my_pin") - obj = load_data(meta, fsspec.filesystem("file"), tmp_dir2, allow_pickle_read=True) + obj = load_data(meta, fsspec.filesystem("file"), tmp_path, allow_pickle_read=True) assert df.equals(obj) @@ -83,31 +83,31 @@ def test_driver_roundtrip(tmp_dir2, type_): "json", ], ) -def test_driver_roundtrip_json(tmp_dir2, type_): +def test_driver_roundtrip_json(tmp_path: Path, type_): df = {"x": [1, 2, 3]} fname = "some_df" full_file = f"{fname}.{type_}" - p_obj = tmp_dir2 / fname + p_obj = tmp_path / fname res_fname = save_data(df, p_obj, type_) assert Path(res_fname).name == full_file meta = MetaRaw(full_file, type_, "my_pin") - obj = load_data(meta, fsspec.filesystem("file"), tmp_dir2, allow_pickle_read=True) + obj = load_data(meta, fsspec.filesystem("file"), tmp_path, allow_pickle_read=True) assert df == obj -def test_driver_feather_write_error(tmp_dir2): +def test_driver_feather_write_error(tmp_path: Path): import pandas as pd df = pd.DataFrame({"x": [1, 2, 3]}) fname = "some_df" - p_obj = tmp_dir2 / fname + p_obj = tmp_path / fname with pytest.raises(NotImplementedError) as exc_info: save_data(df, p_obj, "feather") @@ -115,7 +115,7 @@ def test_driver_feather_write_error(tmp_dir2): assert '"feather" no longer supported.' in exc_info.value.args[0] -def test_driver_feather_read_backwards_compat(tmp_dir2): +def test_driver_feather_read_backwards_compat(tmp_path: Path): import pandas as pd df = pd.DataFrame({"x": [1, 2, 3]}) @@ -123,10 +123,10 @@ def test_driver_feather_read_backwards_compat(tmp_dir2): fname = "some_df" full_file = f"{fname}.feather" - df.to_feather(tmp_dir2 / full_file) + df.to_feather(tmp_path / full_file) obj = load_data( - MetaRaw(full_file, "feather", "my_pin"), fsspec.filesystem("file"), tmp_dir2 + MetaRaw(full_file, "feather", "my_pin"), fsspec.filesystem("file"), tmp_path ) assert df.equals(obj) @@ -148,7 +148,7 @@ def test_driver_pickle_read_fail_default(some_joblib): ) -def test_driver_apply_suffix_false(tmp_dir2): +def test_driver_apply_suffix_false(tmp_path: Path): import pandas as pd df = pd.DataFrame({"x": [1, 2, 3]}) @@ -156,7 +156,7 @@ def test_driver_apply_suffix_false(tmp_dir2): fname = "some_df" type_ = "csv" - p_obj = tmp_dir2 / fname + p_obj = tmp_path / fname res_fname = save_data(df, p_obj, type_, apply_suffix=False) assert Path(res_fname).name == "some_df"