fix: handle OSError in save methods, add missing test, consistent logging

- Wrap save_snapshot_cache and save_last_run_date in try/except OSError, logging a warning instead of propagating the exception
- Add indent=2 to save_last_run_date for consistency
- Add warning log to load_last_run_date on read failure (matching load_snapshot_cache pattern)
- Add test_load_last_run_date_missing_key_returns_none covering valid JSON with absent key
- Remove unused asyncio, AsyncMock, and patch imports from test file

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Tudor Sitaru
2026-05-16 20:15:24 +01:00
parent d77226413d
commit b13f38c821
2 changed files with 17 additions and 6 deletions
+11 -4
View File
@@ -553,8 +553,11 @@ class SnapshotDownloader:
def save_snapshot_cache(self, snapshots: List[Dict[str, Any]]) -> None: def save_snapshot_cache(self, snapshots: List[Dict[str, Any]]) -> None:
cache_file = self.output_dir / "snapshots_cache.json" cache_file = self.output_dir / "snapshots_cache.json"
with open(cache_file, "w", encoding="utf-8") as f: try:
json.dump(snapshots, f, indent=2, default=str) 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]: def load_last_run_date(self) -> Optional[str]:
state_file = self.output_dir / "last_run.json" state_file = self.output_dir / "last_run.json"
@@ -565,12 +568,16 @@ class SnapshotDownloader:
data = json.load(f) data = json.load(f)
return data.get("last_date_to") return data.get("last_date_to")
except (json.JSONDecodeError, OSError): except (json.JSONDecodeError, OSError):
self.logger.warning("Could not read last run date; will do full fetch")
return None return None
def save_last_run_date(self, date: str) -> None: def save_last_run_date(self, date: str) -> None:
state_file = self.output_dir / "last_run.json" state_file = self.output_dir / "last_run.json"
with open(state_file, "w", encoding="utf-8") as f: try:
json.dump({"last_date_to": date}, f) 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( async def generate_html_file(
self, snapshots: List[Dict[str, Any]], date_from: str, date_to: str self, snapshots: List[Dict[str, Any]], date_from: str, date_to: str
+6 -2
View File
@@ -1,7 +1,5 @@
import asyncio
import json import json
import pytest import pytest
from unittest.mock import AsyncMock, patch
from src.snapshot_downloader import SnapshotDownloader 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 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 --- # --- save_last_run_date ---
def test_save_last_run_date_writes_json(tmp_path): def test_save_last_run_date_writes_json(tmp_path):