feat: Enhance dashboard metrics and summary statistics
- Added new summary fields: variance, 5th percentile, 95th percentile, VaR (95%), and expected shortfall (95%) to the dashboard. - Updated the display logic for summary metrics to handle non-finite values gracefully. - Modified the chart rendering to include additional percentile points and tail risk metrics in tooltips. test: Introduce unit tests for consumption, costs, and other modules - Created a comprehensive test suite for consumption, costs, equipment, maintenance, production, reporting, and simulation modules. - Implemented fixtures for database setup and teardown using an in-memory SQLite database for isolated testing. - Added tests for creating, listing, and validating various entities, ensuring proper error handling and response validation. refactor: Consolidate parameter tests and remove deprecated files - Merged parameter-related tests into a new test file for better organization and clarity. - Removed the old parameter test file that was no longer in use. - Improved test coverage for parameter creation, listing, and validation scenarios. fix: Ensure proper validation and error handling in API endpoints - Added validation to reject negative amounts in consumption and production records. - Implemented checks to prevent duplicate scenario creation and ensure proper error messages are returned. - Enhanced reporting endpoint tests to validate input formats and expected outputs.
This commit is contained in:
123
tests/unit/test_parameters.py
Normal file
123
tests/unit/test_parameters.py
Normal file
@@ -0,0 +1,123 @@
|
||||
from typing import Any, Dict, List
|
||||
from uuid import uuid4
|
||||
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from config.database import Base, engine
|
||||
from main import app
|
||||
|
||||
|
||||
def setup_module(module: object) -> None:
|
||||
Base.metadata.create_all(bind=engine)
|
||||
|
||||
|
||||
def teardown_module(module: object) -> None:
|
||||
Base.metadata.drop_all(bind=engine)
|
||||
|
||||
|
||||
def _create_scenario(name: str | None = None) -> int:
|
||||
payload: Dict[str, Any] = {
|
||||
"name": name or f"ParamScenario-{uuid4()}",
|
||||
"description": "Parameter test scenario",
|
||||
}
|
||||
response = TestClient(app).post("/api/scenarios/", json=payload)
|
||||
assert response.status_code == 200
|
||||
return response.json()["id"]
|
||||
|
||||
|
||||
def _create_distribution() -> int:
|
||||
payload: Dict[str, Any] = {
|
||||
"name": f"NormalDist-{uuid4()}",
|
||||
"distribution_type": "normal",
|
||||
"parameters": {"mu": 10, "sigma": 2},
|
||||
}
|
||||
response = TestClient(app).post("/api/distributions/", json=payload)
|
||||
assert response.status_code == 200
|
||||
return response.json()["id"]
|
||||
|
||||
|
||||
client = TestClient(app)
|
||||
|
||||
|
||||
def test_create_and_list_parameter():
|
||||
scenario_id = _create_scenario()
|
||||
distribution_id = _create_distribution()
|
||||
parameter_payload: Dict[str, Any] = {
|
||||
"scenario_id": scenario_id,
|
||||
"name": f"param-{uuid4()}",
|
||||
"value": 3.14,
|
||||
"distribution_id": distribution_id,
|
||||
}
|
||||
|
||||
create_response = client.post("/api/parameters/", json=parameter_payload)
|
||||
assert create_response.status_code == 200
|
||||
created = create_response.json()
|
||||
assert created["scenario_id"] == scenario_id
|
||||
assert created["name"] == parameter_payload["name"]
|
||||
assert created["value"] == parameter_payload["value"]
|
||||
assert created["distribution_id"] == distribution_id
|
||||
assert created["distribution_type"] == "normal"
|
||||
assert created["distribution_parameters"] == {"mu": 10, "sigma": 2}
|
||||
|
||||
list_response = client.get("/api/parameters/")
|
||||
assert list_response.status_code == 200
|
||||
params = list_response.json()
|
||||
assert any(p["id"] == created["id"] for p in params)
|
||||
|
||||
|
||||
def test_create_parameter_for_missing_scenario():
|
||||
payload: Dict[str, Any] = {
|
||||
"scenario_id": 0, "name": "invalid", "value": 1.0}
|
||||
response = client.post("/api/parameters/", json=payload)
|
||||
assert response.status_code == 404
|
||||
assert response.json()["detail"] == "Scenario not found"
|
||||
|
||||
|
||||
def test_multiple_parameters_listed():
|
||||
scenario_id = _create_scenario()
|
||||
payloads: List[Dict[str, Any]] = [
|
||||
{"scenario_id": scenario_id, "name": f"alpha-{i}", "value": float(i)}
|
||||
for i in range(2)
|
||||
]
|
||||
|
||||
for payload in payloads:
|
||||
resp = client.post("/api/parameters/", json=payload)
|
||||
assert resp.status_code == 200
|
||||
|
||||
list_response = client.get("/api/parameters/")
|
||||
assert list_response.status_code == 200
|
||||
names = {item["name"] for item in list_response.json()}
|
||||
for payload in payloads:
|
||||
assert payload["name"] in names
|
||||
|
||||
|
||||
def test_parameter_inline_distribution_metadata():
|
||||
scenario_id = _create_scenario()
|
||||
payload: Dict[str, Any] = {
|
||||
"scenario_id": scenario_id,
|
||||
"name": "inline-param",
|
||||
"value": 7.5,
|
||||
"distribution_type": "uniform",
|
||||
"distribution_parameters": {"min": 5, "max": 10},
|
||||
}
|
||||
|
||||
response = client.post("/api/parameters/", json=payload)
|
||||
assert response.status_code == 200
|
||||
created = response.json()
|
||||
assert created["distribution_id"] is None
|
||||
assert created["distribution_type"] == "uniform"
|
||||
assert created["distribution_parameters"] == {"min": 5, "max": 10}
|
||||
|
||||
|
||||
def test_parameter_with_missing_distribution_reference():
|
||||
scenario_id = _create_scenario()
|
||||
payload: Dict[str, Any] = {
|
||||
"scenario_id": scenario_id,
|
||||
"name": "missing-dist",
|
||||
"value": 1.0,
|
||||
"distribution_id": 9999,
|
||||
}
|
||||
|
||||
response = client.post("/api/parameters/", json=payload)
|
||||
assert response.status_code == 404
|
||||
assert response.json()["detail"] == "Distribution not found"
|
||||
Reference in New Issue
Block a user