diff --git a/spoolman/database/spool.py b/spoolman/database/spool.py index 93c523f..efeacc4 100644 --- a/spoolman/database/spool.py +++ b/spoolman/database/spool.py @@ -1,6 +1,6 @@ """Helper functions for interacting with spool database objects.""" -from datetime import datetime +from datetime import UTC, datetime from typing import Optional import sqlalchemy @@ -12,6 +12,11 @@ from spoolman.exceptions import ItemCreateError, ItemNotFoundError from spoolman.math import weight_from_length +def utc_timezone_naive(dt: datetime) -> datetime: + """Convert a datetime object to UTC and remove timezone info.""" + return dt.astimezone(UTC).replace(tzinfo=None) + + async def create( *, db: AsyncSession, @@ -34,6 +39,12 @@ async def create( else: used_weight = 0 + # Convert datetime values to UTC and remove timezone info + if first_used is not None: + first_used = utc_timezone_naive(first_used) + if last_used is not None: + last_used = utc_timezone_naive(last_used) + db_item = models.Spool( filament=filament_item, used_weight=used_weight, @@ -112,6 +123,8 @@ async def update( if spool.filament.weight is None: raise ItemCreateError("remaining_weight can only be used if the filament type has a weight set.") spool.used_weight = max(spool.filament.weight - v, 0) + elif isinstance(v, datetime): + setattr(spool, k, utc_timezone_naive(v)) else: setattr(spool, k, v) await db.flush() diff --git a/tests_integration/docker-compose-cockroachdb.yml b/tests_integration/docker-compose-cockroachdb.yml index 941724d..3483d55 100644 --- a/tests_integration/docker-compose-cockroachdb.yml +++ b/tests_integration/docker-compose-cockroachdb.yml @@ -25,7 +25,7 @@ services: - SPOOLMAN_DB_PORT=26257 - SPOOLMAN_DB_NAME=spoolman - SPOOLMAN_DB_USERNAME=john - - SPOOLMAN_LOGGING_LEVEL=DEBUG + - SPOOLMAN_LOGGING_LEVEL=INFO depends_on: db: condition: service_healthy diff --git a/tests_integration/docker-compose-mariadb.yml b/tests_integration/docker-compose-mariadb.yml index 66afe87..bee7d8f 100644 --- a/tests_integration/docker-compose-mariadb.yml +++ b/tests_integration/docker-compose-mariadb.yml @@ -29,7 +29,7 @@ services: - SPOOLMAN_DB_NAME=spoolman - SPOOLMAN_DB_USERNAME=john - SPOOLMAN_DB_PASSWORD=abc - - SPOOLMAN_LOGGING_LEVEL=DEBUG + - SPOOLMAN_LOGGING_LEVEL=INFO depends_on: db: condition: service_healthy diff --git a/tests_integration/docker-compose-postgres.yml b/tests_integration/docker-compose-postgres.yml index 41ff68a..708dd52 100644 --- a/tests_integration/docker-compose-postgres.yml +++ b/tests_integration/docker-compose-postgres.yml @@ -13,7 +13,7 @@ services: - SPOOLMAN_DB_NAME=postgres - SPOOLMAN_DB_USERNAME=postgres - SPOOLMAN_DB_PASSWORD=abc - - SPOOLMAN_LOGGING_LEVEL=DEBUG + - SPOOLMAN_LOGGING_LEVEL=INFO depends_on: - db tester: diff --git a/tests_integration/docker-compose-sqlite.yml b/tests_integration/docker-compose-sqlite.yml index 072ba4e..a76eec5 100644 --- a/tests_integration/docker-compose-sqlite.yml +++ b/tests_integration/docker-compose-sqlite.yml @@ -3,7 +3,7 @@ services: spoolman: image: donkie/spoolman:test environment: - - SPOOLMAN_LOGGING_LEVEL=DEBUG + - SPOOLMAN_LOGGING_LEVEL=INFO tester: image: donkie/spoolman-tester:latest volumes: diff --git a/tests_integration/tests/test_spool.py b/tests_integration/tests/test_spool.py index 0725cfb..17cf882 100644 --- a/tests_integration/tests/test_spool.py +++ b/tests_integration/tests/test_spool.py @@ -12,8 +12,6 @@ URL = "http://spoolman:8000" def test_add_spool_remaining_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" remaining_weight = 750 location = "The Pantry" lot_nr = "123456789" @@ -21,8 +19,8 @@ def test_add_spool_remaining_weight(random_filament: dict[str, Any]): result = httpx.post( f"{URL}/api/v1/spool", json={ - "first_used": first_used, - "last_used": last_used, + "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, @@ -37,8 +35,8 @@ def test_add_spool_remaining_weight(random_filament: dict[str, Any]): assert spool == { "id": spool["id"], "registered": spool["registered"], - "first_used": first_used, - "last_used": last_used, + "first_used": "2023-01-02T11:00:00", + "last_used": "2023-01-02T11:00:00", "filament": random_filament, "remaining_weight": pytest.approx(remaining_weight), "used_weight": pytest.approx(random_filament["weight"] - remaining_weight), @@ -362,8 +360,8 @@ def test_update_spool(random_filament: dict[str, Any]): spool = result.json() # Execute - first_used = "2023-01-01T00:00:00" - last_used = "2023-01-02T00:00:00" + first_used = "2023-01-01T12:00:00+02:00" + last_used = "2023-01-02T12:00:00+02:00" remaining_weight = 750 location = "Living Room" lot_nr = "987654321" @@ -383,8 +381,8 @@ def test_update_spool(random_filament: dict[str, Any]): # Verify spool = result.json() - assert spool["first_used"] == first_used - assert spool["last_used"] == last_used + assert spool["first_used"] == "2023-01-01T10:00:00" + assert spool["last_used"] == "2023-01-02T10:00:00" assert spool["remaining_weight"] == pytest.approx(remaining_weight) assert spool["used_weight"] == pytest.approx(random_filament["weight"] - remaining_weight) assert spool["location"] == location