- 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.
121 lines
4.1 KiB
Python
121 lines
4.1 KiB
Python
from uuid import uuid4
|
|
|
|
import pytest
|
|
|
|
from fastapi.testclient import TestClient
|
|
|
|
|
|
@pytest.fixture
|
|
def client(api_client: TestClient) -> TestClient:
|
|
return api_client
|
|
|
|
|
|
def _create_scenario_and_equipment(client: TestClient):
|
|
scenario_payload = {
|
|
"name": f"Test Scenario {uuid4()}",
|
|
"description": "Scenario for maintenance tests",
|
|
}
|
|
scenario_response = client.post("/api/scenarios/", json=scenario_payload)
|
|
assert scenario_response.status_code == 200
|
|
scenario_id = scenario_response.json()["id"]
|
|
|
|
equipment_payload = {
|
|
"scenario_id": scenario_id,
|
|
"name": f"Test Equipment {uuid4()}",
|
|
"description": "Equipment linked to maintenance",
|
|
}
|
|
equipment_response = client.post("/api/equipment/", json=equipment_payload)
|
|
assert equipment_response.status_code == 200
|
|
equipment_id = equipment_response.json()["id"]
|
|
return scenario_id, equipment_id
|
|
|
|
|
|
def _create_maintenance_payload(equipment_id: int, scenario_id: int, description: str):
|
|
return {
|
|
"equipment_id": equipment_id,
|
|
"scenario_id": scenario_id,
|
|
"maintenance_date": "2025-10-20",
|
|
"description": description,
|
|
"cost": 100.0,
|
|
}
|
|
|
|
|
|
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")
|
|
|
|
response = client.post("/api/maintenance/", json=payload)
|
|
assert response.status_code == 201
|
|
created = response.json()
|
|
assert created["equipment_id"] == equipment_id
|
|
assert created["scenario_id"] == scenario_id
|
|
assert created["description"] == "Create maintenance"
|
|
|
|
list_response = client.get("/api/maintenance/")
|
|
assert list_response.status_code == 200
|
|
items = list_response.json()
|
|
assert any(item["id"] == created["id"] for item in items)
|
|
|
|
|
|
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"
|
|
)
|
|
create_response = client.post("/api/maintenance/", json=payload)
|
|
assert create_response.status_code == 201
|
|
maintenance_id = create_response.json()["id"]
|
|
|
|
response = client.get(f"/api/maintenance/{maintenance_id}")
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert data["id"] == maintenance_id
|
|
assert data["equipment_id"] == equipment_id
|
|
assert data["description"] == "Retrieve maintenance"
|
|
|
|
|
|
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(
|
|
equipment_id, scenario_id, "Maintenance before update"
|
|
),
|
|
)
|
|
assert create_response.status_code == 201
|
|
maintenance_id = create_response.json()["id"]
|
|
|
|
update_payload = {
|
|
"equipment_id": equipment_id,
|
|
"scenario_id": scenario_id,
|
|
"maintenance_date": "2025-11-01",
|
|
"description": "Maintenance after update",
|
|
"cost": 250.0,
|
|
}
|
|
|
|
response = client.put(
|
|
f"/api/maintenance/{maintenance_id}", json=update_payload)
|
|
assert response.status_code == 200
|
|
updated = response.json()
|
|
assert updated["maintenance_date"] == "2025-11-01"
|
|
assert updated["description"] == "Maintenance after update"
|
|
assert updated["cost"] == 250.0
|
|
|
|
|
|
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(
|
|
equipment_id, scenario_id, "Delete maintenance"),
|
|
)
|
|
assert create_response.status_code == 201
|
|
maintenance_id = create_response.json()["id"]
|
|
|
|
delete_response = client.delete(f"/api/maintenance/{maintenance_id}")
|
|
assert delete_response.status_code == 204
|
|
|
|
get_response = client.get(f"/api/maintenance/{maintenance_id}")
|
|
assert get_response.status_code == 404
|