Skip to content

Commit

Permalink
Replace tmp_dir2 fixture with tmp_path. (#270)
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanjmcdougall authored Jul 23, 2024
1 parent f3ea387 commit d215a8c
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 42 deletions.
9 changes: 0 additions & 9 deletions pins/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"):
Expand Down
22 changes: 11 additions & 11 deletions pins/tests/test_boards.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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()

Expand All @@ -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",
Expand All @@ -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 -------------------------------------------
Expand Down Expand Up @@ -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")
Expand Down
8 changes: 4 additions & 4 deletions pins/tests/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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):
Expand Down
6 changes: 3 additions & 3 deletions pins/tests/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
30 changes: 15 additions & 15 deletions pins/tests/test_drivers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)

Expand All @@ -83,50 +83,50 @@ 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")

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]})

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)
Expand All @@ -148,15 +148,15 @@ 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]})

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"

0 comments on commit d215a8c

Please sign in to comment.