Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear local temporary metadata.yaml file before each download #342

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 a 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
Loading