diff --git a/movement/sample_data.py b/movement/sample_data.py index 67a228cf..b66ad886 100644 --- a/movement/sample_data.py +++ b/movement/sample_data.py @@ -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( diff --git a/tests/test_unit/test_sample_data.py b/tests/test_unit/test_sample_data.py index ad408126..6cda0c1a 100644 --- a/tests/test_unit/test_sample_data.py +++ b/tests/test_unit/test_sample_data.py @@ -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): @@ -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)