Files
nifodea/tests/test_repositories.py
T
oqyude 8af1d9a010 D2: починить lint-гейт — ruff check . проходит
Было: `ruff check .` → 10 ошибок, CI-гейт на ruff был бы красным сразу.
- 9× I001 (порядок импортов) — application/forecast.py:1,
  application/scenarios.py:1, domain/currency.py:1,
  infrastructure/ai/assistant.py:1, infrastructure/cli/app.py:11,
  infrastructure/cli/commands/init.py:2, infrastructure/cli/services.py:2,
  tests/test_model.py:1, tests/test_repositories.py:1
- 1× E501 — infrastructure/cli/services.py:30, сигнатура build_services
  перенесена на три строки

Поведение не меняется: переупорядочены только импорты, убраны пустые
строки, перенесена длинная сигнатура. Порядок импорта в app.py не влияет
на момент вызова setup_i18n() — он по-прежнему после импорта команд.

Проверка: 75/75 тестов зелёные, ruff check . — All checks passed.
2026-10-23 18:29:48 +03:00

57 lines
2.0 KiB
Python

from decimal import Decimal
from pathlib import Path
from application.repositories import ModelRepository
from domain import Account, Asset, FinancialModel, Transaction
from infrastructure.repositories import JsonFileRepository
class TestJsonFileRepository:
def test_save_load_roundtrip(self, tmp_path: Path):
repo = JsonFileRepository()
model = FinancialModel(
accounts=[Account(name="Test", balance=Decimal("100.50"))],
transactions=[Transaction(amount=Decimal("33.33"), category="income")],
)
p = tmp_path / "model.json"
repo.save(model, p)
assert p.exists()
loaded = repo.load(p)
assert len(loaded.accounts) == 1
assert loaded.accounts[0].name == "Test"
assert loaded.accounts[0].balance == Decimal("100.50")
assert loaded.transactions[0].amount == Decimal("33.33")
def test_version_field_persisted(self, tmp_path: Path):
repo = JsonFileRepository()
model = FinancialModel()
p = tmp_path / "v.json"
repo.save(model, p)
import json
with open(p) as f:
data = json.load(f)
assert data["version"] == 1
def test_creates_parent_dirs(self, tmp_path: Path):
repo = JsonFileRepository()
model = FinancialModel()
p = tmp_path / "nested" / "dir" / "model.json"
repo.save(model, p)
assert p.exists()
def test_implements_protocol(self):
repo = JsonFileRepository()
assert isinstance(repo, ModelRepository)
def test_decimal_preserved(self, tmp_path: Path):
repo = JsonFileRepository()
model = FinancialModel(
assets=[Asset(name="Stocks", value=Decimal("12345.67"), growth_rate=Decimal("8.5"))]
)
p = tmp_path / "decimal.json"
repo.save(model, p)
loaded = repo.load(p)
assert loaded.assets[0].value == Decimal("12345.67")
assert loaded.assets[0].growth_rate == Decimal("8.5")