- 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.
54 lines
1.5 KiB
Python
54 lines
1.5 KiB
Python
from fastapi import APIRouter, Depends, HTTPException
|
|
from sqlalchemy.orm import Session
|
|
from config.database import SessionLocal
|
|
from models.scenario import Scenario
|
|
from pydantic import BaseModel, ConfigDict
|
|
from typing import Optional
|
|
from datetime import datetime
|
|
|
|
router = APIRouter(prefix="/api/scenarios", tags=["scenarios"])
|
|
|
|
# Pydantic schemas
|
|
|
|
|
|
class ScenarioCreate(BaseModel):
|
|
name: str
|
|
description: Optional[str] = None
|
|
|
|
|
|
class ScenarioRead(ScenarioCreate):
|
|
id: int
|
|
created_at: datetime
|
|
updated_at: Optional[datetime] = None
|
|
model_config = ConfigDict(from_attributes=True)
|
|
|
|
# Dependency
|
|
|
|
|
|
def get_db():
|
|
db = SessionLocal()
|
|
try:
|
|
yield db
|
|
finally:
|
|
db.close()
|
|
|
|
|
|
@router.post("/", response_model=ScenarioRead)
|
|
def create_scenario(scenario: ScenarioCreate, db: Session = Depends(get_db)):
|
|
print(f"Creating scenario with name: {scenario.name}")
|
|
db_s = db.query(Scenario).filter(Scenario.name == scenario.name).first()
|
|
if db_s:
|
|
print(f"Scenario with name {scenario.name} already exists.")
|
|
raise HTTPException(status_code=400, detail="Scenario already exists")
|
|
new_s = Scenario(name=scenario.name, description=scenario.description)
|
|
db.add(new_s)
|
|
db.commit()
|
|
db.refresh(new_s)
|
|
print(f"Scenario with name {scenario.name} created successfully.")
|
|
return new_s
|
|
|
|
|
|
@router.get("/", response_model=list[ScenarioRead])
|
|
def list_scenarios(db: Session = Depends(get_db)):
|
|
return db.query(Scenario).all()
|