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:
@@ -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
|
||||
|
||||
@@ -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):
|
||||
|
||||
Reference in New Issue
Block a user