b13f38c821
- 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>
77 lines
2.3 KiB
Python
77 lines
2.3 KiB
Python
import json
|
|
import pytest
|
|
|
|
from src.snapshot_downloader import SnapshotDownloader
|
|
|
|
|
|
def _downloader(tmp_path):
|
|
return SnapshotDownloader(output_dir=str(tmp_path), api_key="test-key")
|
|
|
|
|
|
# --- load_snapshot_cache ---
|
|
|
|
def test_load_snapshot_cache_missing(tmp_path):
|
|
assert _downloader(tmp_path).load_snapshot_cache() == []
|
|
|
|
|
|
def test_load_snapshot_cache_returns_data(tmp_path):
|
|
d = _downloader(tmp_path)
|
|
snapshots = [{"id": "1", "notes": "hello"}]
|
|
(tmp_path / "snapshots_cache.json").write_text(json.dumps(snapshots))
|
|
assert d.load_snapshot_cache() == snapshots
|
|
|
|
|
|
def test_load_snapshot_cache_malformed_returns_empty(tmp_path):
|
|
d = _downloader(tmp_path)
|
|
(tmp_path / "snapshots_cache.json").write_text("not json{{{")
|
|
assert d.load_snapshot_cache() == []
|
|
|
|
|
|
def test_load_snapshot_cache_non_list_returns_empty(tmp_path):
|
|
d = _downloader(tmp_path)
|
|
(tmp_path / "snapshots_cache.json").write_text('{"key": "val"}')
|
|
assert d.load_snapshot_cache() == []
|
|
|
|
|
|
# --- save_snapshot_cache ---
|
|
|
|
def test_save_snapshot_cache_writes_json(tmp_path):
|
|
d = _downloader(tmp_path)
|
|
snapshots = [{"id": "1"}, {"id": "2"}]
|
|
d.save_snapshot_cache(snapshots)
|
|
data = json.loads((tmp_path / "snapshots_cache.json").read_text())
|
|
assert data == snapshots
|
|
|
|
|
|
# --- load_last_run_date ---
|
|
|
|
def test_load_last_run_date_missing(tmp_path):
|
|
assert _downloader(tmp_path).load_last_run_date() is None
|
|
|
|
|
|
def test_load_last_run_date_returns_date(tmp_path):
|
|
d = _downloader(tmp_path)
|
|
(tmp_path / "last_run.json").write_text('{"last_date_to": "2025-01-01"}')
|
|
assert d.load_last_run_date() == "2025-01-01"
|
|
|
|
|
|
def test_load_last_run_date_malformed_returns_none(tmp_path):
|
|
d = _downloader(tmp_path)
|
|
(tmp_path / "last_run.json").write_text("not json")
|
|
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):
|
|
d = _downloader(tmp_path)
|
|
d.save_last_run_date("2025-06-01")
|
|
data = json.loads((tmp_path / "last_run.json").read_text())
|
|
assert data == {"last_date_to": "2025-06-01"}
|