feat: Persist initial capex calculations and enhance navigation links in UI

This commit is contained in:
2025-11-12 23:52:06 +01:00
parent d9fd82b2e3
commit 1240b08740
7 changed files with 99 additions and 11 deletions

View File

@@ -1,8 +1,12 @@
from __future__ import annotations
import pytest
from collections.abc import Callable
from fastapi.testclient import TestClient
from services.unit_of_work import UnitOfWork
def _create_project(client: TestClient, name: str) -> int:
response = client.post(
@@ -33,7 +37,10 @@ def _create_scenario(client: TestClient, project_id: int, name: str) -> int:
return response.json()["id"]
def test_capex_calculation_html_flow(client: TestClient) -> None:
def test_capex_calculation_html_flow(
client: TestClient,
unit_of_work_factory: Callable[[], UnitOfWork],
) -> None:
project_id = _create_project(client, "Capex HTML Project")
scenario_id = _create_scenario(client, project_id, "Capex HTML Scenario")
@@ -69,8 +76,46 @@ def test_capex_calculation_html_flow(client: TestClient) -> None:
assert "$1,200,000.00" in response.text or "1,200,000" in response.text
assert "USD" in response.text
with unit_of_work_factory() as uow:
assert uow.project_capex is not None
assert uow.scenario_capex is not None
def test_capex_calculation_json_flow(client: TestClient) -> None:
project_snapshots = uow.project_capex.list_for_project(project_id)
scenario_snapshots = uow.scenario_capex.list_for_scenario(scenario_id)
assert len(project_snapshots) == 1
assert len(scenario_snapshots) == 1
project_snapshot = project_snapshots[0]
scenario_snapshot = scenario_snapshots[0]
assert project_snapshot.total_capex is not None
assert project_snapshot.contingency_pct is not None
assert project_snapshot.total_with_contingency is not None
assert float(project_snapshot.total_capex) == pytest.approx(2_000_000)
assert float(project_snapshot.contingency_pct) == pytest.approx(5.0)
assert float(project_snapshot.total_with_contingency) == pytest.approx(
2_100_000)
assert project_snapshot.component_count == 2
assert project_snapshot.currency_code == "USD"
assert scenario_snapshot.total_capex is not None
assert scenario_snapshot.contingency_amount is not None
assert scenario_snapshot.total_with_contingency is not None
assert float(scenario_snapshot.total_capex) == pytest.approx(2_000_000)
assert float(
scenario_snapshot.contingency_amount) == pytest.approx(100_000)
assert float(scenario_snapshot.total_with_contingency) == pytest.approx(
2_100_000)
assert scenario_snapshot.component_count == 2
assert scenario_snapshot.currency_code == "USD"
assert scenario_snapshot.payload is not None
def test_capex_calculation_json_flow(
client: TestClient,
unit_of_work_factory: Callable[[], UnitOfWork],
) -> None:
project_id = _create_project(client, "Capex JSON Project")
scenario_id = _create_scenario(client, project_id, "Capex JSON Scenario")
@@ -120,4 +165,29 @@ def test_capex_calculation_json_flow(client: TestClient) -> None:
assert len(data["timeline"]) == 2
assert data["timeline"][0]["year"] == 0
assert data["timeline"][0]["spend"] == pytest.approx(600_000)
assert data["timeline"][1]["cumulative"] == pytest.approx(1_000_000)
assert data["timeline"][1]["cumulative"] == pytest.approx(1_000_000)
with unit_of_work_factory() as uow:
assert uow.project_capex is not None
assert uow.scenario_capex is not None
scenario_snapshot = uow.scenario_capex.latest_for_scenario(scenario_id)
project_snapshot = uow.project_capex.latest_for_project(project_id)
assert scenario_snapshot is not None
assert project_snapshot is not None
assert scenario_snapshot.total_capex is not None
assert project_snapshot.total_with_contingency is not None
assert float(scenario_snapshot.total_capex) == pytest.approx(1_000_000)
assert float(project_snapshot.total_with_contingency) == pytest.approx(
1_125_000)
assert scenario_snapshot.payload is not None
payload = scenario_snapshot.payload or {}
result_payload = payload.get("result", {})
assert result_payload.get("currency") == "USD"
assert result_payload.get("totals", {}).get(
"with_contingency") == pytest.approx(1_125_000)
assert result_payload.get("totals", {}).get(
"overall") == pytest.approx(1_000_000)