Skip to content

Commit

Permalink
clear local temporary metadata.yaml file before downloading
Browse files Browse the repository at this point in the history
  • Loading branch information
niksirbi committed Nov 14, 2024
1 parent a3956c4 commit 97b323e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
9 changes: 8 additions & 1 deletion movement/sample_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,18 @@ def _download_metadata_file(file_name: str, data_dir: Path = DATA_DIR) -> Path:
Path to the downloaded file.
"""
# Delete any existing local temporary metadata file
# This is to force a fresh download of the metadata file each time
temp_file_name = f"temp_{file_name}"
local_temp_file_path = Path(data_dir / temp_file_name)
if local_temp_file_path.is_file():
local_temp_file_path.unlink()

local_file_path = pooch.retrieve(
url=f"{DATA_URL}/{file_name}",
known_hash=None,
path=data_dir,
fname=f"temp_{file_name}",
fname=temp_file_name,
progressbar=False,
)
logger.debug(
Expand Down
18 changes: 15 additions & 3 deletions tests/test_unit/test_sample_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,21 +86,30 @@ def validate_metadata(metadata: dict[str, dict]) -> None:

@pytest.mark.parametrize("download_fails", [True, False])
@pytest.mark.parametrize("local_exists", [True, False])
def test_fetch_metadata(tmp_path, caplog, download_fails, local_exists):
@pytest.mark.parametrize("local_temp_exists", [True, False])
def test_fetch_metadata(
tmp_path, caplog, download_fails, local_exists, local_temp_exists
):
"""Test the fetch_metadata function with different combinations of
failed download and pre-existing local file. The expected behavior is
failed download and pre-existing local files. The expected behavior is
that the function will try to download the metadata file, and if that
fails, it will try to load an existing local file. If neither succeeds,
an error is raised.
an error is raised. If an local temporary file exists, it should be
deleted in the process.
"""
metadata_file_name = "metadata.yaml"
local_file_path = tmp_path / metadata_file_name
local_temp_file_path = tmp_path / f"temp_{metadata_file_name}"

with patch("movement.sample_data.DATA_DIR", tmp_path):
# simulate the existence of a local metadata file
if local_exists:
local_file_path.touch()

# simulate the existence of a local temporary metadata file
if local_temp_exists:
local_temp_file_path.touch()

if download_fails:
# simulate a failed download
with patch("movement.sample_data.pooch.retrieve", mock_retrieve):
Expand All @@ -118,6 +127,9 @@ def test_fetch_metadata(tmp_path, caplog, download_fails, local_exists):
_fetch_metadata(metadata_file_name, data_dir=tmp_path)
else:
metadata = _fetch_metadata(metadata_file_name, data_dir=tmp_path)
# Check that the local temporary file was deleted
assert not local_temp_file_path.is_file()
# Check that the local metadata file exists and is valid
assert local_file_path.is_file()
validate_metadata(metadata)

Expand Down

0 comments on commit 97b323e

Please sign in to comment.