T4: Repository pattern — ModelRepository, JsonFileRepository, ExcelRepository
- application/repositories/model_repository.py: ModelRepository Protocol with load(path) and save(model, path) - infrastructure/repositories/json_file_repository.py: JsonFileRepository (вынесен из FinancialModel.save/load) - infrastructure/repositories/excel_repository.py: ExcelRepository (бывший ExcelSync, переименован) - FinancialModel.save/load удалены — теперь только данные - infrastructure/sync/ удалён (ExcelSync перенесён в repositories/) - infrastructure/cli/main.py и config.py используют JsonFileRepository + ExcelRepository через локальные singletons - test_excel_sync.py переименован в test для ExcelRepository - Новый test_repositories.py: 5 тестов для JsonFileRepository + протокол - test_model.py: использован JsonFileRepository Tests: 68/68 pass (было 63 + 5 новых).
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
"""JsonFileRepository — хранит FinancialModel в .json (включая version)."""
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
from application.repositories.model_repository import ModelRepository
|
||||
from domain import FinancialModel
|
||||
|
||||
|
||||
class JsonFileRepository(ModelRepository):
|
||||
def load(self, path: str | Path) -> FinancialModel:
|
||||
p = Path(path)
|
||||
with open(p, "r", encoding="utf-8") as f:
|
||||
data = json.load(f)
|
||||
return FinancialModel.from_dict(data)
|
||||
|
||||
def save(self, model: FinancialModel, path: str | Path) -> None:
|
||||
p = Path(path)
|
||||
p.parent.mkdir(parents=True, exist_ok=True)
|
||||
with open(p, "w", encoding="utf-8") as f:
|
||||
json.dump(model.to_dict(), f, indent=2, ensure_ascii=False)
|
||||
Reference in New Issue
Block a user