Backend now fully supports negative consumption
Updated integration tests to verify this.
This commit is contained in:
@@ -4,6 +4,7 @@ from datetime import datetime, timezone
|
||||
from typing import Optional
|
||||
|
||||
import sqlalchemy
|
||||
from sqlalchemy import case
|
||||
from sqlalchemy.exc import NoResultFound
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy.orm import contains_eager, joinedload
|
||||
@@ -149,7 +150,12 @@ async def use_weight_safe(db: AsyncSession, spool_id: int, weight: float) -> Non
|
||||
await db.execute(
|
||||
sqlalchemy.update(models.Spool)
|
||||
.where(models.Spool.id == spool_id)
|
||||
.values(used_weight=models.Spool.used_weight + weight),
|
||||
.values(
|
||||
used_weight=case(
|
||||
(models.Spool.used_weight + weight >= 0.0, models.Spool.used_weight + weight), # noqa: PLR2004
|
||||
else_=0.0, # Set used_weight to 0 if the result would be negative
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user