Skip to content

Commit

Permalink
Fix auto_dir bug
Browse files Browse the repository at this point in the history
Signed-off-by: Bram Stoeller <[email protected]>
  • Loading branch information
bramstoeller committed Mar 7, 2023
1 parent 44c764a commit c9a25c8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
5 changes: 2 additions & 3 deletions src/power_grid_model_io/utils/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ def get_download_path(
"""

# If no specific download path was given, we need to generate a unique key (based on the given unique key)
if dir_path is None or file_name is None:
if file_name is None or unique_key is not None:
if unique_key is None:
raise ValueError("Supply a unique key in order to auto generate a download path.")

Expand All @@ -228,8 +228,7 @@ def get_download_path(
if file_name is None:
file_name = Path(f"{unique_key}.download")
# Otherwise, use the unique key as a sub directory
else:
assert dir_path is None # sanity check
elif dir_path is None:
dir_path = Path(tempfile.gettempdir()) / unique_key

# If no dir_path is given, use the system's designated folder for temporary files.
Expand Down
12 changes: 11 additions & 1 deletion tests/unit/utils/test_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
# The / and + will be replaced with a _ and - character and the trailing = character(s) will be removed.
FOO_KEY = "LCa0a2j_xo_5m0U8HTBBNBNCLXBkg7-g-YpeiGJm564"

TEMP_DIR = Path(tempfile.gettempdir()).resolve()


@pytest.fixture()
def temp_dir():
Expand Down Expand Up @@ -374,12 +376,20 @@ def test_get_download_path__ignore_unique_key(temp_dir: Path):
assert path == temp_dir / "file_name.zip"


def test_get_download_path__temp_dir():
# Act
path = get_download_path(file_name="file_name.zip")

# Assert
assert path == TEMP_DIR / "file_name.zip"


def test_get_download_path__auto_dir():
# Act
path = get_download_path(file_name="file_name.zip", unique_key="foo")

# Assert
assert path == Path(tempfile.gettempdir()).resolve() / FOO_KEY / "file_name.zip"
assert path == TEMP_DIR / FOO_KEY / "file_name.zip"


def test_get_download_path__auto_file_name(temp_dir: Path):
Expand Down

0 comments on commit c9a25c8

Please sign in to comment.