-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add a few useful pytest fixtures
- Loading branch information
Showing
6 changed files
with
67 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import pickle # noqa: S403 | ||
import warnings | ||
from pathlib import Path | ||
|
||
try: | ||
import pytest | ||
import yaml | ||
except ModuleNotFoundError as exc: | ||
raise ModuleNotFoundError( | ||
"this file requires optional dependencies, please install using: replete[testing]", | ||
) from exc | ||
|
||
|
||
DATA_DIRECTORY_NAME = "data" | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def build_data_file_full_path(request): | ||
def path_builder(file_path: Path) -> Path: | ||
test_module_name = Path(request.fspath).stem | ||
return Path(request.fspath).parent / DATA_DIRECTORY_NAME / test_module_name / file_path | ||
|
||
return path_builder | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def load_file(build_data_file_full_path): | ||
def loader(file_path: Path): | ||
full_file_path = build_data_file_full_path(file_path) | ||
ext = full_file_path.suffix | ||
if ext == ".pkl": | ||
with full_file_path.open("rb") as f: | ||
warnings.simplefilter("ignore") | ||
return pickle.load(f) # noqa: S301 | ||
elif ext == ".yaml": | ||
with full_file_path.open("r") as f: | ||
return yaml.safe_load(f) | ||
elif ext == ".txt": | ||
with full_file_path.open() as f: | ||
return f.read() | ||
else: | ||
raise ValueError(f"Unknown file type: {ext=}") | ||
|
||
return loader |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
foo text |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
--- | ||
foo: yaml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from pathlib import Path | ||
|
||
import pytest | ||
|
||
|
||
def test_build_data_file_full_path(build_data_file_full_path): | ||
assert build_data_file_full_path("test.pkl") == Path(__file__).parent / "data" / "test_testing" / "test.pkl" | ||
|
||
|
||
@pytest.mark.parametrize( | ||
("filename", "expected"), | ||
[("test.pkl", {"foo": "pickle"}), ("test.yaml", {"foo": "yaml"}), ("test.txt", "foo text\n")], | ||
) | ||
def test_load_file(load_file, filename: str, expected: dict | str): | ||
assert load_file(filename) == expected |