Files
Donkie e509378392 chore: require Python 3.14
The declared floor was ">=3.10", but nothing enforced it: the Docker image
has pinned python:3.14-slim-bookworm for amd64/arm64/armv7 for some time,
and scripts/install.sh runs "uv sync" without UV_PYTHON_DOWNLOADS=0, so uv
fetches a managed interpreter when the system one is too old. Every
dependency declares a floor of <=3.10 with no upper cap.

Raising it unlocks enum.StrEnum, datetime.UTC and typing.Self for the
authentication work that follows.

Consequences handled here:

- uv.lock regenerated. Four pure-backport packages drop out
  (async-timeout, backports-asyncio-runner, exceptiongroup, tomli).

- ruff target-version follows to py314, which switches on PEP 649 deferred
  annotations and with them a batch of previously-dormant rules. The safe
  automatic fixes are applied: datetime.timezone.utc -> datetime.UTC
  (UP017) and redundant quotes dropped from annotations (UP037).

- TC001/TC002/TC003 are ignored rather than applied. They want type-only
  imports moved into "if TYPE_CHECKING:" blocks, which is wrong here:
  FastAPI resolves Annotated[AsyncSession, Depends(...)] and websocket
  parameters at runtime, Pydantic resolves field types at runtime, and
  SQLAlchemy resolves Mapped[...] at mapper-configuration time. A
  TYPE_CHECKING-only import is absent from the runtime namespace, so
  applying them breaks dependency injection. Ruff classifies all 50 as
  unsafe fixes for that reason.

Verified: ruff check/format clean, uv lock --check clean, alembic head
resolves, SQLAlchemy mappers configure with all relationships intact and
lazy="selectin" preserved on the extra-field collections.
2026-07-27 20:09:43 +02:00

340 lines
11 KiB
Python

"""Integration tests for the Spool API endpoint."""
from datetime import UTC, datetime
from typing import Any
import httpx
import pytest
from ..conftest import URL, assert_dicts_compatible, length_from_weight
def test_add_spool_remaining_weight(random_filament: dict[str, Any]):
"""Test adding a spool to the database."""
# Execute
remaining_weight = 750
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,
"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,
"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=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_used_weight(random_filament: dict[str, Any]):
"""Test adding a spool to the database."""
# Execute
first_used = "2023-01-01T00:00:00Z"
last_used = "2023-01-02T00:00:00Z"
used_weight = 250
location = "The Pantry"
lot_nr = "123456789"
comment = "abcdefghåäö"
archived = True
result = httpx.post(
f"{URL}/api/v1/spool",
json={
"first_used": first_used,
"last_used": last_used,
"filament_id": random_filament["id"],
"used_weight": used_weight,
"location": location,
"lot_nr": lot_nr,
"comment": comment,
"archived": archived,
},
)
result.raise_for_status()
# Verify
remaining_weight = random_filament["weight"] - used_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": first_used,
"last_used": last_used,
"filament": random_filament,
"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,
},
)
# Clean up
httpx.delete(f"{URL}/api/v1/spool/{spool['id']}").raise_for_status()
def test_add_spool_required(random_filament: dict[str, Any]):
"""Test adding a spool with only the required fields to the database."""
# Execute
used_weight = 250
result = httpx.post(
f"{URL}/api/v1/spool",
json={
"filament_id": random_filament["id"],
"used_weight": used_weight,
},
)
result.raise_for_status()
# Verify
remaining_weight = random_filament["weight"] - used_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"],
"filament": random_filament,
"used_weight": pytest.approx(used_weight),
"remaining_weight": pytest.approx(remaining_weight),
"used_length": pytest.approx(used_length),
"remaining_length": pytest.approx(remaining_length),
"archived": False,
},
)
# Clean up
httpx.delete(f"{URL}/api/v1/spool/{spool['id']}").raise_for_status()
def test_add_spool_both_used_and_remaining_weight(random_filament: dict[str, Any]):
"""Test adding a spool to the database."""
# Execute
result = httpx.post(
f"{URL}/api/v1/spool",
json={
"filament_id": random_filament["id"],
"remaining_weight": 750,
"used_weight": 250,
},
)
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 = 1010
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 - 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),
"spool_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=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_spool_weight(random_filament: dict[str, Any]):
"""Test adding a spool to the database."""
# Execute
remaining_weight = 750
spool_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,
"spool_weight": spool_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"]),
"spool_weight": pytest.approx(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=UTC) - datetime.fromisoformat(spool["registered"])).total_seconds())
assert diff < 60
# Clean up
httpx.delete(f"{URL}/api/v1/spool/{spool['id']}").raise_for_status()