-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #250 from HopkinsIDD/enhancement/document-test-gem…
…pyor-file_paths Document And Test `gempyor.file_paths`
- Loading branch information
Showing
7 changed files
with
475 additions
and
165 deletions.
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,32 @@ | ||
""" | ||
Unit testing utilities for `gempyor` | ||
This module contains unit testing utilities, mostly pytest fixtures. To use this module | ||
the optional test dependencies must be installed. | ||
""" | ||
|
||
from collections.abc import Generator | ||
import os | ||
from tempfile import TemporaryDirectory | ||
|
||
import pytest | ||
|
||
|
||
@pytest.fixture | ||
def change_directory_to_temp_directory() -> Generator[None, None, None]: | ||
"""Change test working directory to a temporary directory | ||
Pytest fixture that will create a temporary directory and change the working | ||
directory to that temporary directory. This fixture also cleans up after itself by | ||
resetting the working directory and removing the temporary directory on test end. | ||
Useful for testing functions that create files relative to the working directory. | ||
Yields: | ||
None | ||
""" | ||
current_dir = os.getcwd() | ||
temp_dir = TemporaryDirectory() | ||
os.chdir(temp_dir.name) | ||
yield | ||
os.chdir(current_dir) | ||
temp_dir.cleanup() |
62 changes: 62 additions & 0 deletions
62
flepimop/gempyor_pkg/tests/file_paths/test_create_dir_name.py
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,62 @@ | ||
import os.path | ||
from pathlib import Path | ||
|
||
import pytest | ||
|
||
from gempyor.file_paths import create_dir_name | ||
|
||
|
||
class TestCreateDirName: | ||
""" | ||
Unit tests for the `gempyor.file_paths.create_dir_name` function. | ||
""" | ||
|
||
@pytest.mark.parametrize( | ||
( | ||
"run_id", | ||
"prefix", | ||
"ftype", | ||
"inference_filepath_suffix", | ||
"inference_filename_prefix", | ||
), | ||
[ | ||
("abc", "def", "jkl", "mno", "pqr"), | ||
("20240101_000000", "test0001", "seed", "", ""), | ||
("20240101_000000", "test0002", "seed", "", ""), | ||
("20240101_000000", "test0003", "seed", "", ""), | ||
("20240101_000000", "test0004", "seed", "", ""), | ||
("20240101_000000", "test0005", "hosp", "", ""), | ||
("20240101_000000", "test0006", "hosp", "", ""), | ||
("20240101_000000", "test0007", "hosp", "", ""), | ||
("20240101_000000", "test0008", "hosp", "", ""), | ||
], | ||
) | ||
def test_create_file_name( | ||
self, | ||
run_id: str, | ||
prefix: str, | ||
ftype: str, | ||
inference_filepath_suffix: str, | ||
inference_filename_prefix: str, | ||
) -> None: | ||
# Setup | ||
path = create_dir_name( | ||
run_id=run_id, | ||
prefix=prefix, | ||
ftype=ftype, | ||
inference_filepath_suffix=inference_filepath_suffix, | ||
inference_filename_prefix=inference_filename_prefix, | ||
) | ||
expected_path = str( | ||
Path( | ||
"model_output", | ||
prefix, | ||
ftype, | ||
inference_filepath_suffix, | ||
) | ||
) | ||
|
||
# Assertions | ||
assert isinstance(path, str) | ||
assert not os.path.exists(path) | ||
assert path == expected_path |
Oops, something went wrong.