Skip to content

Commit

Permalink
feat: add a few useful pytest fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
Rizhiy committed Mar 3, 2024
1 parent 0e368ab commit ac2c381
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 1 deletion.
6 changes: 5 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ dynamic = ["description", "version"]
dependencies = ["coloredlogs", "docstring-parser", "python-dateutil", "xxhash"]

[project.optional-dependencies]
test = ["flaky", "pytest", "pytest-asyncio", "pytest-coverage", "types-python-dateutil", "types-xxhash"]
testing = ["pytest", "pyyaml"]
test = ["flaky", "pytest", "pytest-asyncio", "pytest-coverage", "replete[testing]", "types-python-dateutil", "types-xxhash"]
dev = ["replete[test]", "ruff"]

[project.entry-points.pytest11]
replete = "replete.testing"

[tool.flit.sdist]
include = ["README.md"]
exclude = [".github", ".gitignore", "tests/*"]
Expand Down
44 changes: 44 additions & 0 deletions replete/testing.py
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 added tests/data/test_testing/test.pkl
Binary file not shown.
1 change: 1 addition & 0 deletions tests/data/test_testing/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foo text
2 changes: 2 additions & 0 deletions tests/data/test_testing/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
foo: yaml
15 changes: 15 additions & 0 deletions tests/test_testing.py
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

0 comments on commit ac2c381

Please sign in to comment.