Add integration tests for new measure endpoint

This commit is contained in:
Matt Gerega
2024-03-27 15:44:54 -04:00
parent 3ea4937f24
commit 19c414da4b
4 changed files with 125 additions and 9 deletions

View File

@@ -17,7 +17,7 @@ from spoolman.api.v1.models import Message, Spool, SpoolEvent
from spoolman.database import spool
from spoolman.database.database import get_db_session
from spoolman.database.utils import SortOrder
from spoolman.exceptions import ItemCreateError
from spoolman.exceptions import ItemCreateError, SpoolMeasureError
from spoolman.extra_fields import EntityType, get_extra_fields, validate_extra_field_dict
from spoolman.ws import websocket_manager
@@ -514,5 +514,12 @@ async def measure( # noqa: ANN201
spool_id: int,
body: SpoolMeasureParameters,
):
db_item = await spool.measure(db, spool_id, body.weight)
return Spool.from_db(db_item)
try:
db_item = await spool.measure(db, spool_id, body.weight)
return Spool.from_db(db_item)
except SpoolMeasureError as e:
logger.exception("Failed to update spool measurement.")
return JSONResponse(
status_code=400,
content={"message": e.args[0]},
)

View File

@@ -20,7 +20,7 @@ from spoolman.database.utils import (
add_where_clause_str_opt,
parse_nested_field,
)
from spoolman.exceptions import ItemCreateError, ItemNotFoundError
from spoolman.exceptions import ItemCreateError, ItemNotFoundError, SpoolMeasureError
from spoolman.math import weight_from_length
from spoolman.ws import websocket_manager
@@ -339,17 +339,19 @@ async def measure(db: AsyncSession, spool_id: int, weight: float) -> models.Spoo
"""
spool_result = await db.execute(
sqlalchemy.select(models.Spool.initial_weight, models.Spool.used_weight).where(models.Spool.id == spool_id),
sqlalchemy.select(models.Spool.initial_weight, models.Spool.used_weight, models.Spool.empty_weight).where(
models.Spool.id == spool_id,
),
)
try:
spool_info = spool_result.one()
except NoResultFound as exc:
raise ItemNotFoundError("Spool not found.") from exc
raise SpoolMeasureError("Spool not found.") from exc
initial_weight = spool_info[0]
if initial_weight is None:
empty_weight = spool_info[2]
if initial_weight is None or empty_weight is None:
# Get filament weight and spool_weight
result = await db.execute(
sqlalchemy.select(models.Filament.weight, models.Filament.spool_weight)
@@ -360,13 +362,25 @@ async def measure(db: AsyncSession, spool_id: int, weight: float) -> models.Spoo
filament_info = result.one()
except NoResultFound as exc:
raise ItemNotFoundError("Filament not found for spool.") from exc
initial_weight = filament_info[0] + filament_info[1]
if empty_weight is None:
empty_weight = filament_info[1]
if initial_weight is None:
initial_weight = filament_info[0] + empty_weight
# if the measurement is greater than the initial weight, raise an error
if weight > initial_weight:
raise SpoolMeasureError("Measured weight is greater than the initial weight of the spool.")
# Calculate the current gross weight (initial_weight - used_weight)
current_use = initial_weight - spool_info[1]
# Calculate the weight used since last measure
weight_to_use = current_use - weight
# If the measured weight is less than the empty weight, use the rest of the spool
if (initial_weight - weight_to_use) < empty_weight:
weight_to_use = current_use - empty_weight
return await use_weight(db, spool_id, weight_to_use)

View File

@@ -11,3 +11,7 @@ class ItemDeleteError(Exception):
class ItemCreateError(Exception):
pass
class SpoolMeasureError(Exception):
pass