"""Tests for cache loading and saving.""" import json import pytest from pathlib import Path import debian_todo class TestLoadCache: """Tests for load_cache function.""" def test_returns_none_when_file_missing(self, tmp_path, monkeypatch): cache_file = tmp_path / "cache.json" monkeypatch.setattr(debian_todo, "CACHE_PATH", cache_file) result = debian_todo.load_cache([]) assert result is None def test_returns_none_on_invalid_json(self, tmp_path, monkeypatch): cache_file = tmp_path / "cache.json" cache_file.write_text("not valid json") monkeypatch.setattr(debian_todo, "CACHE_PATH", cache_file) result = debian_todo.load_cache([]) assert result is None def test_returns_none_on_version_mismatch(self, tmp_path, monkeypatch): cache_file = tmp_path / "cache.json" cache_file.write_text(json.dumps({ "cache_version": debian_todo.CACHE_VERSION - 1, "sources_mtimes": {}, "vcs_by_source": {}, })) monkeypatch.setattr(debian_todo, "CACHE_PATH", cache_file) result = debian_todo.load_cache([]) assert result is None def test_returns_none_when_mtime_mismatch(self, tmp_path, monkeypatch): source_file = tmp_path / "Sources" source_file.write_text("dummy") cache_file = tmp_path / "cache.json" cache_file.write_text(json.dumps({ "cache_version": debian_todo.CACHE_VERSION, "sources_mtimes": {str(source_file): 0.0}, "vcs_by_source": {}, })) monkeypatch.setattr(debian_todo, "CACHE_PATH", cache_file) result = debian_todo.load_cache([str(source_file)]) assert result is None def test_returns_cache_when_valid(self, tmp_path, monkeypatch): import os source_file = tmp_path / "Sources" source_file.write_text("dummy") mtime = os.path.getmtime(str(source_file)) cache_file = tmp_path / "cache.json" cache_file.write_text(json.dumps({ "cache_version": debian_todo.CACHE_VERSION, "sources_mtimes": {str(source_file): mtime}, "vcs_by_source": { "foo": {"vcs_git": "python-team", "uploaders": "John "} }, })) monkeypatch.setattr(debian_todo, "CACHE_PATH", cache_file) result = debian_todo.load_cache([str(source_file)]) assert result == {"foo": {"vcs_git": "python-team", "uploaders": "John "}} def test_normalizes_legacy_string_format(self, tmp_path, monkeypatch): import os source_file = tmp_path / "Sources" source_file.write_text("dummy") mtime = os.path.getmtime(str(source_file)) cache_file = tmp_path / "cache.json" cache_file.write_text(json.dumps({ "cache_version": debian_todo.CACHE_VERSION, "sources_mtimes": {str(source_file): mtime}, "vcs_by_source": {"foo": "python-team"}, # legacy string format })) monkeypatch.setattr(debian_todo, "CACHE_PATH", cache_file) result = debian_todo.load_cache([str(source_file)]) assert result == {"foo": {"vcs_git": "python-team", "uploaders": ""}} class TestSaveCache: """Tests for save_cache function.""" def test_saves_cache_file(self, tmp_path, monkeypatch): import os source_file = tmp_path / "Sources" source_file.write_text("dummy") cache_file = tmp_path / "cache.json" monkeypatch.setattr(debian_todo, "CACHE_PATH", cache_file) vcs_by_source = {"foo": {"vcs_git": "python-team", "uploaders": ""}} debian_todo.save_cache([str(source_file)], vcs_by_source) assert cache_file.exists() data = json.loads(cache_file.read_text()) assert data["cache_version"] == debian_todo.CACHE_VERSION assert data["vcs_by_source"] == vcs_by_source assert str(source_file) in data["sources_mtimes"] def test_handles_missing_source_file(self, tmp_path, monkeypatch): cache_file = tmp_path / "cache.json" monkeypatch.setattr(debian_todo, "CACHE_PATH", cache_file) debian_todo.save_cache(["/nonexistent/file"], {}) # Should not create cache file if source file is missing assert not cache_file.exists()