diff --git a/pyproject.toml b/pyproject.toml index 99efdeb..f5a1391 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -43,6 +43,9 @@ call = "spoolman.bump:bump" [tool.pdm.scripts.app] cmd = "uvicorn spoolman.main:app" +[tool.pdm.scripts.itest] +cmd = "python tests_integration/run.py" + [tool.ruff] select = ["ALL"] ignore = [ diff --git a/spoolman/api/v1/models.py b/spoolman/api/v1/models.py index ba4b051..7075692 100644 --- a/spoolman/api/v1/models.py +++ b/spoolman/api/v1/models.py @@ -1,14 +1,31 @@ """Pydantic data models for typing the FastAPI request/responses.""" -from datetime import datetime +from datetime import datetime, timezone from typing import Optional -from pydantic import BaseModel, Field +from pydantic import BaseModel as PydanticBaseModel +from pydantic import Field from spoolman.database import models from spoolman.math import length_from_weight +def datetime_to_str(dt: datetime) -> str: + """Convert a datetime object to a string.""" + if dt.tzinfo is None: + dt = dt.astimezone(tz=timezone.utc) + return dt.isoformat().replace("+00:00", "Z") + + +class BaseModel(PydanticBaseModel): + class Config: + """Pydantic configuration.""" + + json_encoders = { # noqa: RUF012 + datetime: datetime_to_str, + } + + class Message(BaseModel): message: str = Field() diff --git a/tests_integration/tests/spool/test_add.py b/tests_integration/tests/spool/test_add.py index dfb7603..a64b0e1 100644 --- a/tests_integration/tests/spool/test_add.py +++ b/tests_integration/tests/spool/test_add.py @@ -50,8 +50,8 @@ def test_add_spool_remaining_weight(random_filament: dict[str, Any]): assert spool == { "id": spool["id"], "registered": spool["registered"], - "first_used": "2023-01-02T11:00:00", - "last_used": "2023-01-02T11:00:00", + "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), @@ -70,8 +70,8 @@ def test_add_spool_remaining_weight(random_filament: dict[str, Any]): 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:00" - last_used = "2023-01-02T00:00:00" + first_used = "2023-01-01T00:00:00Z" + last_used = "2023-01-02T00:00:00Z" used_weight = 250 location = "The Pantry" lot_nr = "123456789" diff --git a/tests_integration/tests/spool/test_update.py b/tests_integration/tests/spool/test_update.py index 56e3f45..c1d3be3 100644 --- a/tests_integration/tests/spool/test_update.py +++ b/tests_integration/tests/spool/test_update.py @@ -61,8 +61,8 @@ def test_update_spool(random_filament: dict[str, Any]): ) spool = result.json() - assert spool["first_used"] == "2023-01-01T10:00:00" - assert spool["last_used"] == "2023-01-02T10:00:00" + assert spool["first_used"] == "2023-01-01T10:00:00Z" + assert spool["last_used"] == "2023-01-02T10:00:00Z" assert spool["remaining_weight"] == pytest.approx(remaining_weight) assert spool["used_weight"] == pytest.approx(used_weight) assert spool["remaining_length"] == pytest.approx(remaining_length)