From 9595aec67522c2f132075db4228376f058c74a82 Mon Sep 17 00:00:00 2001 From: Donkie Date: Mon, 2 Oct 2023 21:36:44 +0200 Subject: [PATCH] Fixed registered field having wrong timezone This happened with at least Postgres, which took it's server's timezone. It's complicated. Let's just set it from our side instead where we have some control. --- spoolman/database/filament.py | 2 ++ spoolman/database/spool.py | 1 + spoolman/database/vendor.py | 2 ++ tests_integration/tests/filament/test_add.py | 5 +++++ tests_integration/tests/spool/test_add.py | 5 +++++ tests_integration/tests/vendor/test_add.py | 6 ++++++ 6 files changed, 21 insertions(+) diff --git a/spoolman/database/filament.py b/spoolman/database/filament.py index 59ef66b..6d5ebef 100644 --- a/spoolman/database/filament.py +++ b/spoolman/database/filament.py @@ -2,6 +2,7 @@ import logging from collections.abc import Sequence +from datetime import datetime from typing import Optional, Union from sqlalchemy import func, select @@ -44,6 +45,7 @@ async def create( db_item = models.Filament( name=name, + registered=datetime.utcnow().replace(microsecond=0), vendor=vendor_item, material=material, price=price, diff --git a/spoolman/database/spool.py b/spoolman/database/spool.py index da9148f..7b4e543 100644 --- a/spoolman/database/spool.py +++ b/spoolman/database/spool.py @@ -61,6 +61,7 @@ async def create( db_item = models.Spool( filament=filament_item, + registered=datetime.utcnow().replace(microsecond=0), used_weight=used_weight, first_used=first_used, last_used=last_used, diff --git a/spoolman/database/vendor.py b/spoolman/database/vendor.py index d0e6039..2619f5b 100644 --- a/spoolman/database/vendor.py +++ b/spoolman/database/vendor.py @@ -1,5 +1,6 @@ """Helper functions for interacting with vendor database objects.""" +from datetime import datetime from typing import Optional from sqlalchemy import func, select @@ -19,6 +20,7 @@ async def create( """Add a new vendor to the database.""" db_item = models.Vendor( name=name, + registered=datetime.utcnow().replace(microsecond=0), comment=comment, ) db.add(db_item) diff --git a/tests_integration/tests/filament/test_add.py b/tests_integration/tests/filament/test_add.py index accb310..13d3ff5 100644 --- a/tests_integration/tests/filament/test_add.py +++ b/tests_integration/tests/filament/test_add.py @@ -1,5 +1,6 @@ """Integration tests for the Filament API endpoint.""" +from datetime import datetime, timezone from typing import Any import httpx @@ -62,6 +63,10 @@ def test_add_filament(random_vendor: dict[str, Any]): "color_hex": color_hex, } + # Verify that registered happened almost now (within 1 minute) + diff = abs((datetime.now(tz=timezone.utc) - datetime.fromisoformat(filament["registered"])).total_seconds()) + assert diff < 60 + # Clean up httpx.delete(f"{URL}/api/v1/filament/{filament['id']}").raise_for_status() diff --git a/tests_integration/tests/spool/test_add.py b/tests_integration/tests/spool/test_add.py index a64b0e1..f5ae7fd 100644 --- a/tests_integration/tests/spool/test_add.py +++ b/tests_integration/tests/spool/test_add.py @@ -1,5 +1,6 @@ """Integration tests for the Spool API endpoint.""" +from datetime import datetime, timezone from typing import Any import httpx @@ -63,6 +64,10 @@ def test_add_spool_remaining_weight(random_filament: dict[str, Any]): "archived": archived, } + # 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() diff --git a/tests_integration/tests/vendor/test_add.py b/tests_integration/tests/vendor/test_add.py index d82ac97..547a60e 100644 --- a/tests_integration/tests/vendor/test_add.py +++ b/tests_integration/tests/vendor/test_add.py @@ -1,5 +1,7 @@ """Integration tests for the Vendor API endpoint.""" +from datetime import datetime, timezone + import httpx URL = "http://spoolman:8000" @@ -25,6 +27,10 @@ def test_add_vendor(): "comment": comment, } + # Verify that registered happened almost now (within 1 minute) + diff = abs((datetime.now(tz=timezone.utc) - datetime.fromisoformat(vendor["registered"])).total_seconds()) + assert diff < 60 + # Clean up httpx.delete(f"{URL}/api/v1/vendor/{vendor['id']}").raise_for_status()