- Updated test functions in various test files to enhance code clarity by formatting long lines and improving indentation. - Adjusted assertions to use multi-line formatting for better readability. - Added new test cases for theme settings API to ensure proper functionality. - Ensured consistent use of line breaks and spacing across test files for uniformity.
180 lines
6.0 KiB
Python
180 lines
6.0 KiB
Python
from typing import Any, Dict, cast
|
|
|
|
import pytest
|
|
from fastapi.testclient import TestClient
|
|
|
|
from models.scenario import Scenario
|
|
from services import settings as settings_service
|
|
|
|
|
|
def test_dashboard_route_provides_summary(
|
|
api_client: TestClient, seeded_ui_data: Dict[str, Any]
|
|
) -> None:
|
|
response = api_client.get("/ui/dashboard")
|
|
assert response.status_code == 200
|
|
|
|
template = getattr(response, "template", None)
|
|
assert template is not None
|
|
assert template.name == "Dashboard.html"
|
|
|
|
context = cast(Dict[str, Any], getattr(response, "context", {}))
|
|
assert context.get("report_available") is True
|
|
|
|
metric_labels = {item["label"] for item in context["summary_metrics"]}
|
|
assert {
|
|
"CAPEX Total",
|
|
"OPEX Total",
|
|
"Production",
|
|
"Simulation Iterations",
|
|
}.issubset(metric_labels)
|
|
|
|
scenario = cast(Scenario, seeded_ui_data["scenario"])
|
|
scenario_row = next(
|
|
row
|
|
for row in context["scenario_rows"]
|
|
if row["scenario_name"] == scenario.name
|
|
)
|
|
assert scenario_row["iterations"] == 3
|
|
assert scenario_row["simulation_mean_display"] == "971,666.67"
|
|
assert scenario_row["capex_display"] == "$1,000,000.00"
|
|
assert scenario_row["opex_display"] == "$250,000.00"
|
|
assert scenario_row["production_display"] == "800.00"
|
|
assert scenario_row["consumption_display"] == "1,200.00"
|
|
|
|
|
|
def test_scenarios_route_lists_seeded_scenario(
|
|
api_client: TestClient, seeded_ui_data: Dict[str, Any]
|
|
) -> None:
|
|
response = api_client.get("/ui/scenarios")
|
|
assert response.status_code == 200
|
|
|
|
template = getattr(response, "template", None)
|
|
assert template is not None
|
|
assert template.name == "ScenarioForm.html"
|
|
|
|
context = cast(Dict[str, Any], getattr(response, "context", {}))
|
|
names = [item["name"] for item in context["scenarios"]]
|
|
scenario = cast(Scenario, seeded_ui_data["scenario"])
|
|
assert scenario.name in names
|
|
|
|
|
|
def test_reporting_route_includes_summary(
|
|
api_client: TestClient, seeded_ui_data: Dict[str, Any]
|
|
) -> None:
|
|
response = api_client.get("/ui/reporting")
|
|
assert response.status_code == 200
|
|
|
|
template = getattr(response, "template", None)
|
|
assert template is not None
|
|
assert template.name == "reporting.html"
|
|
|
|
context = cast(Dict[str, Any], getattr(response, "context", {}))
|
|
summaries = context["report_summaries"]
|
|
scenario = cast(Scenario, seeded_ui_data["scenario"])
|
|
scenario_summary = next(
|
|
item for item in summaries if item["scenario_id"] == scenario.id
|
|
)
|
|
assert scenario_summary["iterations"] == 3
|
|
mean_value = float(scenario_summary["summary"]["mean"])
|
|
assert abs(mean_value - 971_666.6666666666) < 1e-6
|
|
|
|
|
|
def test_dashboard_data_endpoint_returns_aggregates(
|
|
api_client: TestClient, seeded_ui_data: Dict[str, Any]
|
|
) -> None:
|
|
response = api_client.get("/ui/dashboard/data")
|
|
assert response.status_code == 200
|
|
|
|
payload = response.json()
|
|
assert payload["report_available"] is True
|
|
|
|
metric_map = {
|
|
item["label"]: item["value"] for item in payload["summary_metrics"]
|
|
}
|
|
assert metric_map["CAPEX Total"].startswith("$")
|
|
assert metric_map["Maintenance Cost"].startswith("$")
|
|
|
|
scenario = cast(Scenario, seeded_ui_data["scenario"])
|
|
scenario_rows = payload["scenario_rows"]
|
|
scenario_entry = next(
|
|
row for row in scenario_rows if row["scenario_name"] == scenario.name
|
|
)
|
|
assert scenario_entry["capex_display"] == "$1,000,000.00"
|
|
assert scenario_entry["production_display"] == "800.00"
|
|
|
|
labels = payload["scenario_cost_chart"]["labels"]
|
|
idx = labels.index(scenario.name)
|
|
assert payload["scenario_cost_chart"]["capex"][idx] == 1_000_000.0
|
|
|
|
activity_labels = payload["scenario_activity_chart"]["labels"]
|
|
activity_idx = activity_labels.index(scenario.name)
|
|
assert (
|
|
payload["scenario_activity_chart"]["production"][activity_idx] == 800.0
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("path", "template_name"),
|
|
[
|
|
("/", "Dashboard.html"),
|
|
("/ui/parameters", "ParameterInput.html"),
|
|
("/ui/costs", "costs.html"),
|
|
("/ui/consumption", "consumption.html"),
|
|
("/ui/production", "production.html"),
|
|
("/ui/equipment", "equipment.html"),
|
|
("/ui/maintenance", "maintenance.html"),
|
|
("/ui/simulations", "simulations.html"),
|
|
],
|
|
)
|
|
def test_additional_ui_routes_render_templates(
|
|
api_client: TestClient,
|
|
seeded_ui_data: Dict[str, Any],
|
|
path: str,
|
|
template_name: str,
|
|
) -> None:
|
|
response = api_client.get(path)
|
|
assert response.status_code == 200
|
|
|
|
template = getattr(response, "template", None)
|
|
assert template is not None
|
|
assert template.name == template_name
|
|
|
|
context = cast(Dict[str, Any], getattr(response, "context", {}))
|
|
assert context
|
|
|
|
|
|
def test_settings_route_provides_css_context(
|
|
api_client: TestClient,
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
) -> None:
|
|
env_var = settings_service.css_key_to_env_var("--color-accent")
|
|
monkeypatch.setenv(env_var, "#abcdef")
|
|
|
|
response = api_client.get("/ui/settings")
|
|
assert response.status_code == 200
|
|
|
|
template = getattr(response, "template", None)
|
|
assert template is not None
|
|
assert template.name == "settings.html"
|
|
|
|
context = cast(Dict[str, Any], getattr(response, "context", {}))
|
|
assert "css_variables" in context
|
|
assert "css_defaults" in context
|
|
assert "css_env_overrides" in context
|
|
assert "css_env_override_rows" in context
|
|
assert "css_env_override_meta" in context
|
|
|
|
assert context["css_variables"]["--color-accent"] == "#abcdef"
|
|
assert (
|
|
context["css_defaults"]["--color-accent"]
|
|
== settings_service.CSS_COLOR_DEFAULTS["--color-accent"]
|
|
)
|
|
assert context["css_env_overrides"]["--color-accent"] == "#abcdef"
|
|
|
|
override_rows = context["css_env_override_rows"]
|
|
assert any(row["env_var"] == env_var for row in override_rows)
|
|
|
|
meta = context["css_env_override_meta"]["--color-accent"]
|
|
assert meta["value"] == "#abcdef"
|
|
assert meta["env_var"] == env_var
|