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:
@@ -1,75 +1,16 @@
|
||||
from uuid import uuid4
|
||||
|
||||
import pytest
|
||||
|
||||
from fastapi.testclient import TestClient
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
|
||||
from config.database import Base
|
||||
from main import app
|
||||
from routes import (
|
||||
consumption,
|
||||
costs,
|
||||
distributions,
|
||||
equipment,
|
||||
maintenance,
|
||||
parameters,
|
||||
production,
|
||||
reporting,
|
||||
scenarios,
|
||||
simulations,
|
||||
ui,
|
||||
)
|
||||
|
||||
SQLALCHEMY_DATABASE_URL = "sqlite:///./test.db"
|
||||
engine = create_engine(SQLALCHEMY_DATABASE_URL, connect_args={
|
||||
"check_same_thread": False})
|
||||
TestingSessionLocal = sessionmaker(
|
||||
autocommit=False, autoflush=False, bind=engine)
|
||||
|
||||
|
||||
def setup_module(module):
|
||||
Base.metadata.create_all(bind=engine)
|
||||
@pytest.fixture
|
||||
def client(api_client: TestClient) -> TestClient:
|
||||
return api_client
|
||||
|
||||
|
||||
def teardown_module(module):
|
||||
Base.metadata.drop_all(bind=engine)
|
||||
|
||||
|
||||
def override_get_db():
|
||||
db = TestingSessionLocal()
|
||||
try:
|
||||
yield db
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
app.dependency_overrides[maintenance.get_db] = override_get_db
|
||||
app.dependency_overrides[equipment.get_db] = override_get_db
|
||||
app.dependency_overrides[scenarios.get_db] = override_get_db
|
||||
app.dependency_overrides[distributions.get_db] = override_get_db
|
||||
app.dependency_overrides[parameters.get_db] = override_get_db
|
||||
app.dependency_overrides[costs.get_db] = override_get_db
|
||||
app.dependency_overrides[consumption.get_db] = override_get_db
|
||||
app.dependency_overrides[production.get_db] = override_get_db
|
||||
app.dependency_overrides[reporting.get_db] = override_get_db
|
||||
app.dependency_overrides[simulations.get_db] = override_get_db
|
||||
|
||||
app.include_router(maintenance.router)
|
||||
app.include_router(equipment.router)
|
||||
app.include_router(scenarios.router)
|
||||
app.include_router(distributions.router)
|
||||
app.include_router(ui.router)
|
||||
app.include_router(parameters.router)
|
||||
app.include_router(costs.router)
|
||||
app.include_router(consumption.router)
|
||||
app.include_router(production.router)
|
||||
app.include_router(reporting.router)
|
||||
app.include_router(simulations.router)
|
||||
|
||||
client = TestClient(app)
|
||||
|
||||
|
||||
def _create_scenario_and_equipment():
|
||||
def _create_scenario_and_equipment(client: TestClient):
|
||||
scenario_payload = {
|
||||
"name": f"Test Scenario {uuid4()}",
|
||||
"description": "Scenario for maintenance tests",
|
||||
@@ -99,8 +40,8 @@ def _create_maintenance_payload(equipment_id: int, scenario_id: int, description
|
||||
}
|
||||
|
||||
|
||||
def test_create_and_list_maintenance():
|
||||
scenario_id, equipment_id = _create_scenario_and_equipment()
|
||||
def test_create_and_list_maintenance(client: TestClient):
|
||||
scenario_id, equipment_id = _create_scenario_and_equipment(client)
|
||||
payload = _create_maintenance_payload(
|
||||
equipment_id, scenario_id, "Create maintenance")
|
||||
|
||||
@@ -117,8 +58,8 @@ def test_create_and_list_maintenance():
|
||||
assert any(item["id"] == created["id"] for item in items)
|
||||
|
||||
|
||||
def test_get_maintenance():
|
||||
scenario_id, equipment_id = _create_scenario_and_equipment()
|
||||
def test_get_maintenance(client: TestClient):
|
||||
scenario_id, equipment_id = _create_scenario_and_equipment(client)
|
||||
payload = _create_maintenance_payload(
|
||||
equipment_id, scenario_id, "Retrieve maintenance"
|
||||
)
|
||||
@@ -134,8 +75,8 @@ def test_get_maintenance():
|
||||
assert data["description"] == "Retrieve maintenance"
|
||||
|
||||
|
||||
def test_update_maintenance():
|
||||
scenario_id, equipment_id = _create_scenario_and_equipment()
|
||||
def test_update_maintenance(client: TestClient):
|
||||
scenario_id, equipment_id = _create_scenario_and_equipment(client)
|
||||
create_response = client.post(
|
||||
"/api/maintenance/",
|
||||
json=_create_maintenance_payload(
|
||||
@@ -162,8 +103,8 @@ def test_update_maintenance():
|
||||
assert updated["cost"] == 250.0
|
||||
|
||||
|
||||
def test_delete_maintenance():
|
||||
scenario_id, equipment_id = _create_scenario_and_equipment()
|
||||
def test_delete_maintenance(client: TestClient):
|
||||
scenario_id, equipment_id = _create_scenario_and_equipment(client)
|
||||
create_response = client.post(
|
||||
"/api/maintenance/",
|
||||
json=_create_maintenance_payload(
|
||||
|
||||
Reference in New Issue
Block a user