D16b: месячная доля recurring по frequency и учёт start_date/end_date
amount — итог за период frequency: daily /30.4, weekly /4.33, monthly /1, yearly /12 (делители — именованные константы domain/recurring.py). Платёж учитывается в месяце сетки прогноза, если start_date <= конец месяца и end_date >= начало месяца; сравнение по ISO-строкам, пустые даты — без границ. Сетка прогноза привязана к start_month (по умолчанию текущий календарный месяц), округление — только в точке отображения.
This commit is contained in:
1 parent
abcd84c0fa
commit
f50106ca96
6 files changed
+259
-32
No files matched your search
+45
-10
@@ -7,6 +7,10 @@ _BALANCE = "total_balance"
|
||||
_INCOME = "total_income"
|
||||
_EXPENSES = "total_expenses"
|
||||
|
||||
# Месяц старта сетки прогноза для D11-характеристики (совпадает с start_date
|
||||
# recurring в sample_model) — числа не зависят от даты запуска тестов (D16b).
|
||||
_GRID_START = "2026-01-01"
|
||||
|
||||
_DESCRIPTIONS = {
|
||||
"baseline": "Базовый сценарий без изменений",
|
||||
"optimistic": "Оптимистичный: доход +15%, расход -5%, рост активов +20%",
|
||||
@@ -16,20 +20,20 @@ _DESCRIPTIONS = {
|
||||
|
||||
class TestScenarioService:
|
||||
def test_baseline(self, sample_model):
|
||||
service = ScenarioService(sample_model)
|
||||
service = ScenarioService(sample_model, start_month=_GRID_START)
|
||||
result = service.apply(DEFAULT_SCENARIOS["baseline"], months=6)
|
||||
assert result["scenario"] == "baseline"
|
||||
assert result["total_balance"] is not None
|
||||
|
||||
def test_compare_returns_three(self, sample_model):
|
||||
service = ScenarioService(sample_model)
|
||||
service = ScenarioService(sample_model, start_month=_GRID_START)
|
||||
results = service.compare(months=6)
|
||||
assert "baseline" in results
|
||||
assert "optimistic" in results
|
||||
assert "pessimistic" in results
|
||||
|
||||
def test_what_if(self, sample_model):
|
||||
service = ScenarioService(sample_model)
|
||||
service = ScenarioService(sample_model, start_month=_GRID_START)
|
||||
result = service.what_if(income_mult=1.2, expense_mult=0.9, months=6)
|
||||
assert result["scenario"] == "what-if"
|
||||
|
||||
@@ -90,7 +94,7 @@ class TestScenarioCharacterization:
|
||||
|
||||
for name, scenario in DEFAULT_SCENARIOS.items():
|
||||
# СВЕЖИЙ ScenarioService на каждый сценарий.
|
||||
result = ScenarioService(sample_model).apply(scenario, months=6)
|
||||
result = ScenarioService(sample_model, start_month=_GRID_START).apply(scenario, months=6)
|
||||
assert result["scenario"] == name
|
||||
assert result["scenario_description"] == _DESCRIPTIONS[name]
|
||||
assert result["months"] == 6
|
||||
@@ -109,7 +113,7 @@ class TestScenarioCharacterization:
|
||||
|
||||
for name, scenario in DEFAULT_SCENARIOS.items():
|
||||
# СВЕЖИЙ ScenarioService на каждый сценарий.
|
||||
result = ScenarioService(sample_model).apply(scenario, months=12)
|
||||
result = ScenarioService(sample_model, start_month=_GRID_START).apply(scenario, months=12)
|
||||
assert result["scenario"] == name
|
||||
assert result["scenario_description"] == _DESCRIPTIONS[name]
|
||||
assert result["months"] == 12
|
||||
@@ -122,7 +126,9 @@ class TestScenarioCharacterization:
|
||||
def test_what_if_numbers(self, sample_model):
|
||||
description = "What-if: income x1.2, expense x0.9, growth x1.0"
|
||||
|
||||
six = ScenarioService(sample_model).what_if(income_mult=1.2, expense_mult=0.9, months=6)
|
||||
six = ScenarioService(sample_model, start_month=_GRID_START).what_if(
|
||||
income_mult=1.2, expense_mult=0.9, months=6
|
||||
)
|
||||
assert six == {
|
||||
_BALANCE: Decimal("12231.75"),
|
||||
_INCOME: Decimal("30833.63"),
|
||||
@@ -132,7 +138,9 @@ class TestScenarioCharacterization:
|
||||
"scenario_description": description,
|
||||
}
|
||||
|
||||
twelve = ScenarioService(sample_model).what_if(income_mult=1.2, expense_mult=0.9, months=12)
|
||||
twelve = ScenarioService(sample_model, start_month=_GRID_START).what_if(
|
||||
income_mult=1.2, expense_mult=0.9, months=12
|
||||
)
|
||||
assert twelve == {
|
||||
_BALANCE: Decimal("14514.02"),
|
||||
_INCOME: Decimal("61749.97"),
|
||||
@@ -143,21 +151,48 @@ class TestScenarioCharacterization:
|
||||
}
|
||||
|
||||
def test_compare_matches_apply(self, sample_model):
|
||||
comparison = ScenarioService(sample_model).compare(months=12)
|
||||
comparison = ScenarioService(sample_model, start_month=_GRID_START).compare(months=12)
|
||||
assert set(comparison) == {"baseline", "optimistic", "pessimistic"}
|
||||
|
||||
for name, scenario in DEFAULT_SCENARIOS.items():
|
||||
# apply() на СВЕЖЕМ сервисе даёт те же числа, что и compare().
|
||||
single = ScenarioService(sample_model).apply(scenario, months=12)
|
||||
single = ScenarioService(sample_model, start_month=_GRID_START).apply(scenario, months=12)
|
||||
for key in (_BALANCE, _INCOME, _EXPENSES):
|
||||
assert comparison[name][key] == single[key]
|
||||
|
||||
def test_scenario_service_is_not_mutated_by_apply(self, sample_model):
|
||||
# apply() работает на копии модели, поэтому сервис не «тратит»
|
||||
# обязательство: повторный apply() того же сценария даёт те же числа.
|
||||
service = ScenarioService(sample_model)
|
||||
service = ScenarioService(sample_model, start_month=_GRID_START)
|
||||
first = service.apply(DEFAULT_SCENARIOS["baseline"], months=6)
|
||||
second = service.apply(DEFAULT_SCENARIOS["baseline"], months=6)
|
||||
|
||||
assert first == second
|
||||
assert first[_BALANCE] == Decimal("11527.64")
|
||||
|
||||
|
||||
class TestRecurringScheduleInScenarios:
|
||||
"""D16b: сценарии видят месячную долю и границы recurring."""
|
||||
|
||||
_GRID = "2026-01-01"
|
||||
|
||||
@staticmethod
|
||||
def _model(*recurring: RecurringCashflow) -> FinancialModel:
|
||||
return FinancialModel(
|
||||
accounts=[Account(name="Счёт", balance=Decimal("0"))],
|
||||
recurring=list(recurring),
|
||||
)
|
||||
|
||||
def test_yearly_amount_is_divided_by_12_after_multiplier(self):
|
||||
# 1200 за год × 1.15 (optimistic) / 12 = 115 в месяц.
|
||||
model = self._model(RecurringCashflow(amount=Decimal("1200"), frequency="yearly"))
|
||||
result = ScenarioService(model, start_month=self._GRID).apply(DEFAULT_SCENARIOS["optimistic"], months=1)
|
||||
assert result[_INCOME] == Decimal("115.00")
|
||||
assert result[_BALANCE] == Decimal("115.00")
|
||||
|
||||
def test_end_date_limits_payments_inside_scenario(self):
|
||||
model = self._model(RecurringCashflow(amount=Decimal("500"), end_date="2026-02-28"))
|
||||
result = ScenarioService(model, start_month=self._GRID).apply(DEFAULT_SCENARIOS["baseline"], months=6)
|
||||
# Платежи только в январе и феврале: 500 + 500.
|
||||
assert result[_INCOME] == Decimal("1000.00")
|
||||
assert result[_BALANCE] == Decimal("1000.00")
|
||||
Reference in new issue
Block a user