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)