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.
This commit is contained in:
Donkie
2023-10-02 21:36:44 +02:00
parent 205574fa2c
commit 9595aec675
6 changed files with 21 additions and 0 deletions

View File

@@ -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,

View File

@@ -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,

View File

@@ -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)

View File

@@ -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()

View File

@@ -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()

View File

@@ -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()