Added additional integration tests for measure and add
This commit is contained in:
@@ -368,12 +368,13 @@ async def measure(db: AsyncSession, spool_id: int, weight: float) -> models.Spoo
|
|||||||
if initial_weight is None:
|
if initial_weight is None:
|
||||||
initial_weight = filament_info[0] + empty_weight
|
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)
|
# Calculate the current gross weight (initial_weight - used_weight)
|
||||||
current_use = initial_weight - spool_info[1]
|
current_use = initial_weight - spool_info[1]
|
||||||
|
|
||||||
|
# if the measurement is greater than the initial weight, raise an error
|
||||||
|
if weight > current_use:
|
||||||
|
raise SpoolMeasureError("Measured weight cannot increase.")
|
||||||
|
|
||||||
# Calculate the weight used since last measure
|
# Calculate the weight used since last measure
|
||||||
weight_to_use = current_use - weight
|
weight_to_use = current_use - weight
|
||||||
|
|
||||||
|
|||||||
@@ -58,7 +58,10 @@ def random_vendor_impl():
|
|||||||
# Add vendor
|
# Add vendor
|
||||||
result = httpx.post(
|
result = httpx.post(
|
||||||
f"{URL}/api/v1/vendor",
|
f"{URL}/api/v1/vendor",
|
||||||
json={"name": "John"},
|
json={
|
||||||
|
"name": "John",
|
||||||
|
"empty_spool_weight": 246,
|
||||||
|
},
|
||||||
)
|
)
|
||||||
result.raise_for_status()
|
result.raise_for_status()
|
||||||
|
|
||||||
|
|||||||
@@ -195,3 +195,145 @@ def test_add_spool_both_used_and_remaining_weight(random_filament: dict[str, Any
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
assert result.status_code == 400 # Cannot set both used and remaining weight
|
assert result.status_code == 400 # Cannot set both used and remaining weight
|
||||||
|
|
||||||
|
|
||||||
|
def test_add_spool_initial_weight(random_filament: dict[str, Any]):
|
||||||
|
"""Test adding a spool to the database."""
|
||||||
|
# Execute
|
||||||
|
remaining_weight = 750
|
||||||
|
initial_weight = 1245
|
||||||
|
location = "The Pantry"
|
||||||
|
lot_nr = "123456789"
|
||||||
|
comment = "abcdefghåäö"
|
||||||
|
archived = True
|
||||||
|
price = 25
|
||||||
|
result = httpx.post(
|
||||||
|
f"{URL}/api/v1/spool",
|
||||||
|
json={
|
||||||
|
"first_used": "2023-01-02T12:00:00+01:00",
|
||||||
|
"last_used": "2023-01-02T11:00:00Z",
|
||||||
|
"filament_id": random_filament["id"],
|
||||||
|
"remaining_weight": remaining_weight,
|
||||||
|
"initial_weight": initial_weight,
|
||||||
|
"location": location,
|
||||||
|
"lot_nr": lot_nr,
|
||||||
|
"comment": comment,
|
||||||
|
"archived": archived,
|
||||||
|
"price": price,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
result.raise_for_status()
|
||||||
|
|
||||||
|
# Verify
|
||||||
|
used_weight = initial_weight - random_filament["spool_weight"] - remaining_weight
|
||||||
|
used_length = length_from_weight(
|
||||||
|
weight=used_weight,
|
||||||
|
density=random_filament["density"],
|
||||||
|
diameter=random_filament["diameter"],
|
||||||
|
)
|
||||||
|
remaining_length = length_from_weight(
|
||||||
|
weight=remaining_weight,
|
||||||
|
density=random_filament["density"],
|
||||||
|
diameter=random_filament["diameter"],
|
||||||
|
)
|
||||||
|
|
||||||
|
spool = result.json()
|
||||||
|
assert_dicts_compatible(
|
||||||
|
spool,
|
||||||
|
{
|
||||||
|
"id": spool["id"],
|
||||||
|
"registered": spool["registered"],
|
||||||
|
"first_used": "2023-01-02T11:00:00Z",
|
||||||
|
"last_used": "2023-01-02T11:00:00Z",
|
||||||
|
"filament": random_filament,
|
||||||
|
"initial_weight": pytest.approx(initial_weight),
|
||||||
|
"empty_weight": pytest.approx(random_filament["spool_weight"]),
|
||||||
|
"remaining_weight": pytest.approx(remaining_weight),
|
||||||
|
"used_weight": pytest.approx(used_weight),
|
||||||
|
"remaining_length": pytest.approx(remaining_length),
|
||||||
|
"used_length": pytest.approx(used_length),
|
||||||
|
"location": location,
|
||||||
|
"lot_nr": lot_nr,
|
||||||
|
"comment": comment,
|
||||||
|
"archived": archived,
|
||||||
|
"price": price,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
# Verify that registered happened almost now (within 1 minute)
|
||||||
|
diff = abs((datetime.now(tz=timezone.utc) - datetime.fromisoformat(spool["registered"])).total_seconds())
|
||||||
|
assert diff < 60
|
||||||
|
|
||||||
|
# Clean up
|
||||||
|
httpx.delete(f"{URL}/api/v1/spool/{spool['id']}").raise_for_status()
|
||||||
|
|
||||||
|
|
||||||
|
def test_add_spool_empty_weight(random_filament: dict[str, Any]):
|
||||||
|
"""Test adding a spool to the database."""
|
||||||
|
# Execute
|
||||||
|
remaining_weight = 750
|
||||||
|
empty_weight = 200
|
||||||
|
location = "The Pantry"
|
||||||
|
lot_nr = "123456789"
|
||||||
|
comment = "abcdefghåäö"
|
||||||
|
archived = True
|
||||||
|
price = 25
|
||||||
|
result = httpx.post(
|
||||||
|
f"{URL}/api/v1/spool",
|
||||||
|
json={
|
||||||
|
"first_used": "2023-01-02T12:00:00+01:00",
|
||||||
|
"last_used": "2023-01-02T11:00:00Z",
|
||||||
|
"filament_id": random_filament["id"],
|
||||||
|
"remaining_weight": remaining_weight,
|
||||||
|
"empty_weight": empty_weight,
|
||||||
|
"location": location,
|
||||||
|
"lot_nr": lot_nr,
|
||||||
|
"comment": comment,
|
||||||
|
"archived": archived,
|
||||||
|
"price": price,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
result.raise_for_status()
|
||||||
|
|
||||||
|
# Verify
|
||||||
|
used_weight = random_filament["weight"] - remaining_weight
|
||||||
|
used_length = length_from_weight(
|
||||||
|
weight=used_weight,
|
||||||
|
density=random_filament["density"],
|
||||||
|
diameter=random_filament["diameter"],
|
||||||
|
)
|
||||||
|
remaining_length = length_from_weight(
|
||||||
|
weight=remaining_weight,
|
||||||
|
density=random_filament["density"],
|
||||||
|
diameter=random_filament["diameter"],
|
||||||
|
)
|
||||||
|
|
||||||
|
spool = result.json()
|
||||||
|
assert_dicts_compatible(
|
||||||
|
spool,
|
||||||
|
{
|
||||||
|
"id": spool["id"],
|
||||||
|
"registered": spool["registered"],
|
||||||
|
"first_used": "2023-01-02T11:00:00Z",
|
||||||
|
"last_used": "2023-01-02T11:00:00Z",
|
||||||
|
"filament": random_filament,
|
||||||
|
"initial_weight": pytest.approx(random_filament["weight"] + empty_weight),
|
||||||
|
"empty_weight": pytest.approx(empty_weight),
|
||||||
|
"remaining_weight": pytest.approx(remaining_weight),
|
||||||
|
"used_weight": pytest.approx(used_weight),
|
||||||
|
"remaining_length": pytest.approx(remaining_length),
|
||||||
|
"used_length": pytest.approx(used_length),
|
||||||
|
"location": location,
|
||||||
|
"lot_nr": lot_nr,
|
||||||
|
"comment": comment,
|
||||||
|
"archived": archived,
|
||||||
|
"price": price,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
# Verify that registered happened almost now (within 1 minute)
|
||||||
|
diff = abs((datetime.now(tz=timezone.utc) - datetime.fromisoformat(spool["registered"])).total_seconds())
|
||||||
|
assert diff < 60
|
||||||
|
|
||||||
|
# Clean up
|
||||||
|
httpx.delete(f"{URL}/api/v1/spool/{spool['id']}").raise_for_status()
|
||||||
|
|||||||
@@ -89,3 +89,56 @@ def test_measure_spool_invalid(random_filament: dict[str, Any], measurement: flo
|
|||||||
|
|
||||||
# Clean up
|
# Clean up
|
||||||
httpx.delete(f"{URL}/api/v1/spool/{spool['id']}").raise_for_status()
|
httpx.delete(f"{URL}/api/v1/spool/{spool['id']}").raise_for_status()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("measurements", [[1244, 1233, 1200], [1000, 900, 800], [1000, 1000, 1000]])
|
||||||
|
def test_measure_spool_sequence(random_filament: dict[str, Any], measurements: list[float]):
|
||||||
|
"""Test using a spool in the database."""
|
||||||
|
# Setup
|
||||||
|
random_filament["weight"]
|
||||||
|
initial_weight = 1255
|
||||||
|
current_weight = initial_weight
|
||||||
|
empty_weight = 246
|
||||||
|
start_weight = 1000
|
||||||
|
result = httpx.post(
|
||||||
|
f"{URL}/api/v1/spool",
|
||||||
|
json={
|
||||||
|
"filament_id": random_filament["id"],
|
||||||
|
"remaining_weight": start_weight,
|
||||||
|
"initial_weight": initial_weight,
|
||||||
|
"empty_weight": empty_weight,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
result.raise_for_status()
|
||||||
|
spool = result.json()
|
||||||
|
|
||||||
|
for m in measurements:
|
||||||
|
# Execute
|
||||||
|
result = httpx.put(
|
||||||
|
f"{URL}/api/v1/spool/{spool['id']}/measure",
|
||||||
|
json={
|
||||||
|
"weight": m,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
result.raise_for_status()
|
||||||
|
|
||||||
|
# Verify
|
||||||
|
spool = result.json()
|
||||||
|
# remaining_weight should be clamped so it's never negative,
|
||||||
|
# but used_weight should not be clamped to the net weight
|
||||||
|
expected_use = min(initial_weight - m, initial_weight - empty_weight)
|
||||||
|
assert spool["used_weight"] == pytest.approx(expected_use)
|
||||||
|
expected_remaining = max(m - empty_weight, 0)
|
||||||
|
assert spool["remaining_weight"] == pytest.approx(expected_remaining)
|
||||||
|
# Verify that first_used has been updated
|
||||||
|
diff = abs((datetime.now(tz=timezone.utc) - datetime.fromisoformat(spool["first_used"])).total_seconds())
|
||||||
|
assert diff < 60
|
||||||
|
|
||||||
|
# Verify that last_used has been updated
|
||||||
|
diff = abs((datetime.now(tz=timezone.utc) - datetime.fromisoformat(spool["last_used"])).total_seconds())
|
||||||
|
assert diff < 60
|
||||||
|
|
||||||
|
current_weight = current_weight - expected_use
|
||||||
|
|
||||||
|
# Clean up
|
||||||
|
httpx.delete(f"{URL}/api/v1/spool/{spool['id']}").raise_for_status()
|
||||||
|
|||||||
Reference in New Issue
Block a user