Files
nifodea/tests/test_forecast.py
T
oqyude cdda703b37 T9: Excel as the model authoring surface
- ExcelRepository: canonical columns with ru/en aliases, date/number normalization
  (Excel dates, DD.MM.YYYY, '1 234,56', currency symbols, parentheses), account
  name->UUID resolution, per-row errors with sheet!row, warnings for formulas
  without cached values, duplicate names and default exchange rates
- ExcelTemplate: skeleton generator (number/date formats, dropdowns for currency,
  frequency en/ru and account, 'Инструкция' sheet); ship data/model_template.xlsx
- CLI: cf template-xlsx [--force]; cf import-xlsx --dry-run with rich error table,
  import writes nothing when the file has errors
- RecurringCashflow.account (empty = all accounts) + name/UUID matching in forecast;
  cf config recurring-add --account, account column in info/recurring-list
- Forecast: runs are idempotent, 'Итог: Баланс' sums all accounts of the last month
- README: 'Сборка модели в Excel' section
- ruff check . clean (9 pre-existing import/line-length findings fixed)
- MetaAgent: T9 archived, req-T9 approved, manifest/checkpoints/state synced

118 tests pass
2026-11-11 15:40:37 +03:00

115 lines
4.6 KiB
Python

import pytest
from application.forecast import ForecastError, ForecastService
from domain import Account, FinancialModel, RecurringCashflow, Transaction
class TestForecastService:
def test_forecast_returns_results(self, sample_model):
service = ForecastService(sample_model)
results = service.forecast_cashflow(months=12)
assert len(results) > 0
assert "balance" in results[0]
assert "month" in results[0]
def test_forecast_12_months(self, sample_model):
service = ForecastService(sample_model)
results = service.forecast_cashflow(months=12)
months = set(r["month"] for r in results)
assert max(months) == 12
def test_invalid_months(self, sample_model):
service = ForecastService(sample_model)
with pytest.raises(ForecastError):
service.forecast_cashflow(months=0)
def test_summary(self, sample_model):
service = ForecastService(sample_model)
s = service.summary(months=6)
assert "total_balance" in s
assert "total_income" in s
assert "total_expenses" in s
assert s["months"] == 6
def test_empty_model(self, empty_model):
service = ForecastService(empty_model)
results = service.forecast_cashflow(months=3)
assert results == []
class TestAccountAttribution:
"""Модели, собранные в Excel или руками, ссылаются на счёт по имени."""
def _model(self, *accounts):
return FinancialModel(accounts=list(accounts))
def test_transaction_matched_by_account_name(self):
acc = Account(name="Основной", balance=0)
model = self._model(acc)
model.transactions = [
Transaction(date="2026-01-01", account="Основной", amount=1000),
Transaction(date="2026-01-01", account="основной ", amount=-300),
]
results = ForecastService(model).forecast_cashflow(months=1)
assert len(results) == 1
assert results[0]["income"] == 1000
assert results[0]["expenses"] == 300
def test_transaction_matched_by_account_id(self):
acc = Account(name="Основной", balance=0)
model = self._model(acc)
model.transactions = [
Transaction(date="2026-01-01", account=str(acc.id), amount=250),
]
results = ForecastService(model).forecast_cashflow(months=1)
assert results[0]["income"] == 250
def test_recurring_scoped_to_one_account(self):
first = Account(name="Основной", balance=0)
second = Account(name="Сбережения", balance=0)
model = self._model(first, second)
model.recurring = [
RecurringCashflow(start_date="2026-01-01", frequency="monthly",
amount=500, category="income", account="Основной"),
]
results = ForecastService(model).forecast_cashflow(months=1)
by_account = {r["account"]: r for r in results}
assert by_account["Основной"]["income"] == 500
assert by_account["Сбережения"]["income"] == 0
def test_recurring_without_account_applies_to_all(self):
model = self._model(Account(name="A"), Account(name="B"))
model.recurring = [
RecurringCashflow(start_date="2026-01-01", frequency="monthly", amount=100),
]
results = ForecastService(model).forecast_cashflow(months=1)
assert all(r["income"] == 100 for r in results)
class TestSummary:
def test_total_balance_sums_all_accounts_of_last_month(self):
model = FinancialModel(
accounts=[Account(name="A", balance=100), Account(name="B", balance=200)],
)
summary = ForecastService(model).summary(months=1)
assert summary["total_balance"] == 300
def test_total_balance_is_not_one_account_row(self):
model = FinancialModel(
accounts=[Account(name="A", balance=100), Account(name="B", balance=200)],
)
summary = ForecastService(model).summary(months=3)
assert summary["total_balance"] == 300
def test_summary_matches_last_month_rows(self, sample_model):
service = ForecastService(sample_model)
rows = service.forecast_cashflow(6)
summary = service.summary(6)
last_month = max(r["month"] for r in rows)
expected = sum(r["balance"] for r in rows if r["month"] == last_month)
assert summary["total_balance"] == expected
def test_forecast_is_idempotent(self, sample_model):
service = ForecastService(sample_model)
assert service.forecast_cashflow(6) == service.forecast_cashflow(6)