diff --git a/src/snapshot_downloader.py b/src/snapshot_downloader.py index 889f08f..1f971bf 100644 --- a/src/snapshot_downloader.py +++ b/src/snapshot_downloader.py @@ -553,8 +553,11 @@ class SnapshotDownloader: def save_snapshot_cache(self, snapshots: List[Dict[str, Any]]) -> None: cache_file = self.output_dir / "snapshots_cache.json" - with open(cache_file, "w", encoding="utf-8") as f: - json.dump(snapshots, f, indent=2, default=str) + try: + with open(cache_file, "w", encoding="utf-8") as f: + json.dump(snapshots, f, indent=2, default=str) + except OSError as e: + self.logger.warning(f"Could not write snapshot cache: {e}") def load_last_run_date(self) -> Optional[str]: state_file = self.output_dir / "last_run.json" @@ -565,12 +568,16 @@ class SnapshotDownloader: data = json.load(f) return data.get("last_date_to") except (json.JSONDecodeError, OSError): + self.logger.warning("Could not read last run date; will do full fetch") return None def save_last_run_date(self, date: str) -> None: state_file = self.output_dir / "last_run.json" - with open(state_file, "w", encoding="utf-8") as f: - json.dump({"last_date_to": date}, f) + try: + with open(state_file, "w", encoding="utf-8") as f: + json.dump({"last_date_to": date}, f, indent=2) + except OSError as e: + self.logger.warning(f"Could not write last run date: {e}") async def generate_html_file( self, snapshots: List[Dict[str, Any]], date_from: str, date_to: str diff --git a/tests/test_incremental_snapshot.py b/tests/test_incremental_snapshot.py index 52f0a87..487ee0c 100644 --- a/tests/test_incremental_snapshot.py +++ b/tests/test_incremental_snapshot.py @@ -1,7 +1,5 @@ -import asyncio import json import pytest -from unittest.mock import AsyncMock, patch from src.snapshot_downloader import SnapshotDownloader @@ -63,6 +61,12 @@ def test_load_last_run_date_malformed_returns_none(tmp_path): assert d.load_last_run_date() is None +def test_load_last_run_date_missing_key_returns_none(tmp_path): + d = _downloader(tmp_path) + (tmp_path / "last_run.json").write_text('{"date": "2025-01-01"}') + assert d.load_last_run_date() is None + + # --- save_last_run_date --- def test_save_last_run_date_writes_json(tmp_path):