D16a: единое правило классификации recurring — только по знаку
This commit is contained in:
1 parent
0307533cef
commit
e7d14ba25d
4 files changed
+87
-1
No files matched your search
@@ -83,6 +83,8 @@ class ForecastService:
|
||||
expenses += abs(t.amount)
|
||||
|
||||
for r in self.model.recurring:
|
||||
# D16a: классификация только по знаку; `category` — подпись,
|
||||
# а не правило (то же правило в ScenarioService.apply/what_if).
|
||||
if r.amount > 0:
|
||||
income += r.amount
|
||||
else:
|
||||
|
||||
@@ -48,7 +48,9 @@ class ScenarioService:
|
||||
t.amount = t.amount * scenario.expense_multiplier
|
||||
|
||||
for r in model.recurring:
|
||||
if r.category == "income":
|
||||
# Классификация — только по знаку (D16a), как в ForecastService:
|
||||
# `category` — свободная подпись и на правило не влияет.
|
||||
if r.amount > 0:
|
||||
r.amount = r.amount * scenario.income_multiplier
|
||||
else:
|
||||
r.amount = r.amount * scenario.expense_multiplier
|
||||
|
||||
@@ -3,6 +3,7 @@ from decimal import Decimal
|
||||
import pytest
|
||||
|
||||
from application.forecast import ForecastError, ForecastService
|
||||
from domain import Account, FinancialModel, RecurringCashflow
|
||||
|
||||
_MAIN_ACCOUNT = "Основной счёт"
|
||||
_SAVINGS_ACCOUNT = "Сбережения"
|
||||
@@ -41,6 +42,46 @@ class TestForecastService:
|
||||
assert results == []
|
||||
|
||||
|
||||
class TestRecurringClassificationBySign:
|
||||
"""D16a: recurring классифицируется только по знаку, `category` — подпись."""
|
||||
|
||||
@staticmethod
|
||||
def _model(*recurring: RecurringCashflow) -> FinancialModel:
|
||||
return FinancialModel(
|
||||
accounts=[Account(name="Счёт", balance=Decimal("0"))],
|
||||
recurring=list(recurring),
|
||||
)
|
||||
|
||||
def test_negative_amount_with_income_category_is_expense(self):
|
||||
model = self._model(RecurringCashflow(amount=Decimal("-500"), category="income"))
|
||||
summary = ForecastService(model).summary(months=1)
|
||||
|
||||
assert summary["total_income"] == Decimal("0")
|
||||
assert summary["total_expenses"] == Decimal("500")
|
||||
assert summary["total_balance"] == Decimal("-500")
|
||||
|
||||
def test_positive_amount_with_expense_category_is_income(self):
|
||||
model = self._model(RecurringCashflow(amount=Decimal("500"), category="expense"))
|
||||
summary = ForecastService(model).summary(months=1)
|
||||
|
||||
assert summary["total_income"] == Decimal("500")
|
||||
assert summary["total_expenses"] == Decimal("0")
|
||||
assert summary["total_balance"] == Decimal("500")
|
||||
|
||||
def test_category_does_not_affect_classification(self):
|
||||
# Оба платежа противоположны своим категориям: по знаку это
|
||||
# доход 500 и расход 500, а не наоборот.
|
||||
model = self._model(
|
||||
RecurringCashflow(amount=Decimal("-500"), category="income"),
|
||||
RecurringCashflow(amount=Decimal("500"), category="expense"),
|
||||
)
|
||||
summary = ForecastService(model).summary(months=1)
|
||||
|
||||
assert summary["total_income"] == Decimal("500")
|
||||
assert summary["total_expenses"] == Decimal("500")
|
||||
assert summary["total_balance"] == Decimal("0")
|
||||
|
||||
|
||||
class TestForecastCharacterization:
|
||||
"""Фиксация текущих чисел движка прогноза.
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
from decimal import Decimal
|
||||
|
||||
from application.scenarios import DEFAULT_SCENARIOS, ScenarioService
|
||||
from domain import Account, FinancialModel, RecurringCashflow
|
||||
|
||||
_BALANCE = "total_balance"
|
||||
_INCOME = "total_income"
|
||||
@@ -33,6 +34,46 @@ class TestScenarioService:
|
||||
assert result["scenario"] == "what-if"
|
||||
|
||||
|
||||
class TestRecurringClassificationBySign:
|
||||
"""D16a: сценарии применяют множители к recurring по знаку, не по category."""
|
||||
|
||||
@staticmethod
|
||||
def _model(*recurring: RecurringCashflow) -> FinancialModel:
|
||||
return FinancialModel(
|
||||
accounts=[Account(name="Счёт", balance=Decimal("0"))],
|
||||
recurring=list(recurring),
|
||||
)
|
||||
|
||||
def test_negative_amount_with_income_category_uses_expense_multiplier(self):
|
||||
model = self._model(RecurringCashflow(amount=Decimal("-500"), category="income"))
|
||||
result = ScenarioService(model).apply(DEFAULT_SCENARIOS["optimistic"], months=1)
|
||||
|
||||
# Расход -500 × 0.95 = -475 (income-множитель 1.15 не применяется).
|
||||
assert result[_INCOME] == Decimal("0")
|
||||
assert result[_EXPENSES] == Decimal("475")
|
||||
assert result[_BALANCE] == Decimal("-475")
|
||||
|
||||
def test_positive_amount_with_expense_category_uses_income_multiplier(self):
|
||||
model = self._model(RecurringCashflow(amount=Decimal("500"), category="expense"))
|
||||
result = ScenarioService(model).apply(DEFAULT_SCENARIOS["optimistic"], months=1)
|
||||
|
||||
# Доход 500 × 1.15 = 575 (expense-множитель 0.95 не применяется).
|
||||
assert result[_INCOME] == Decimal("575")
|
||||
assert result[_EXPENSES] == Decimal("0")
|
||||
assert result[_BALANCE] == Decimal("575")
|
||||
|
||||
def test_what_if_classifies_recurring_by_sign(self):
|
||||
model = self._model(
|
||||
RecurringCashflow(amount=Decimal("500"), category="expense"),
|
||||
RecurringCashflow(amount=Decimal("-500"), category="income"),
|
||||
)
|
||||
result = ScenarioService(model).what_if(income_mult=2, expense_mult=Decimal("0.5"), months=1)
|
||||
|
||||
assert result[_INCOME] == Decimal("1000")
|
||||
assert result[_EXPENSES] == Decimal("250")
|
||||
assert result[_BALANCE] == Decimal("750")
|
||||
|
||||
|
||||
class TestScenarioCharacterization:
|
||||
"""Фиксация текущих чисел сервиса сценариев.
|
||||
|
||||
|
||||
Reference in new issue
Block a user