- 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.
72 lines
1.6 KiB
Python
72 lines
1.6 KiB
Python
from fastapi import APIRouter, Depends
|
|
from sqlalchemy.orm import Session
|
|
from typing import List, Optional
|
|
from pydantic import BaseModel, ConfigDict
|
|
from config.database import SessionLocal
|
|
from models.capex import Capex
|
|
from models.opex import Opex
|
|
|
|
router = APIRouter(prefix="/api/costs", tags=["Costs"])
|
|
|
|
|
|
def get_db():
|
|
db = SessionLocal()
|
|
try:
|
|
yield db
|
|
finally:
|
|
db.close()
|
|
|
|
|
|
# Pydantic schemas for Capex
|
|
class CapexCreate(BaseModel):
|
|
scenario_id: int
|
|
amount: float
|
|
description: Optional[str] = None
|
|
|
|
|
|
class CapexRead(CapexCreate):
|
|
id: int
|
|
model_config = ConfigDict(from_attributes=True)
|
|
|
|
|
|
# Pydantic schemas for Opex
|
|
class OpexCreate(BaseModel):
|
|
scenario_id: int
|
|
amount: float
|
|
description: Optional[str] = None
|
|
|
|
|
|
class OpexRead(OpexCreate):
|
|
id: int
|
|
model_config = ConfigDict(from_attributes=True)
|
|
|
|
|
|
# Capex endpoints
|
|
@router.post("/capex", response_model=CapexRead)
|
|
def create_capex(item: CapexCreate, db: Session = Depends(get_db)):
|
|
db_item = Capex(**item.model_dump())
|
|
db.add(db_item)
|
|
db.commit()
|
|
db.refresh(db_item)
|
|
return db_item
|
|
|
|
|
|
@router.get("/capex", response_model=List[CapexRead])
|
|
def list_capex(db: Session = Depends(get_db)):
|
|
return db.query(Capex).all()
|
|
|
|
|
|
# Opex endpoints
|
|
@router.post("/opex", response_model=OpexRead)
|
|
def create_opex(item: OpexCreate, db: Session = Depends(get_db)):
|
|
db_item = Opex(**item.model_dump())
|
|
db.add(db_item)
|
|
db.commit()
|
|
db.refresh(db_item)
|
|
return db_item
|
|
|
|
|
|
@router.get("/opex", response_model=List[OpexRead])
|
|
def list_opex(db: Session = Depends(get_db)):
|
|
return db.query(Opex).all()
|