Coverage for backend/django/Economics/results/services/financial_metrics/warnings.py: 75%
12 statements
« prev ^ index » next coverage.py v7.10.7, created at 2026-07-22 05:22 +0000
« prev ^ index » next coverage.py v7.10.7, created at 2026-07-22 05:22 +0000
1"""Financial metric warning construction."""
3from __future__ import annotations
5from .contracts import FinancialCalculationInputs, FinancialWarning, TargetAssumptions, _decimal_string
6from Economics.studies.models import EconomicsStudy
9def add_incomplete_cashflow_warning(
10 *,
11 inputs: FinancialCalculationInputs,
12 warnings: list[FinancialWarning],
13) -> None:
14 warnings.append(
15 FinancialWarning(
16 code="financial_assumptions_incomplete",
17 severity="warning",
18 message="Project lifetime and discount rate are required for cash-flow, payback, NPV, and ROI.",
19 context={
20 "project_lifetime_years": inputs.project_lifetime_years,
21 "discount_rate_percent": _decimal_string(inputs.discount_rate_percent),
22 },
23 )
24 )
26def target_assumption_warnings(
27 *,
28 study: EconomicsStudy,
29 target_assumptions: TargetAssumptions,
30) -> list[FinancialWarning]:
31 if target_assumptions.assumptions_source == "missing": 31 ↛ 32line 31 didn't jump to line 32 because the condition on line 31 was never true
32 return [
33 FinancialWarning(
34 code="study_assumptions_missing",
35 severity="warning",
36 message="Study assumptions are required for project lifetime, discount rate, and basis metadata.",
37 context={"study_id": study.pk},
38 )
39 ]
40 missing_fields = [
41 field_name
42 for field_name in ("project_lifetime_years", "discount_rate_percent")
43 if getattr(target_assumptions, field_name) is None
44 ]
45 if not missing_fields: 45 ↛ 47line 45 didn't jump to line 47 because the condition on line 45 was always true
46 return []
47 return [
48 FinancialWarning(
49 code="target_financial_assumptions_incomplete",
50 severity="warning",
51 message="Target study financial assumptions are incomplete.",
52 context={"study_id": study.pk, "missing_fields": tuple(missing_fields)},
53 )
54 ]