Backend now fully supports negative consumption

Updated integration tests to verify this.
This commit is contained in:
Donkie
2023-07-08 20:07:32 +02:00
parent 746b891703
commit 936b11de24
2 changed files with 23 additions and 11 deletions

View File

@@ -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
),
),
)