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:
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
import logging
|
import logging
|
||||||
from collections.abc import Sequence
|
from collections.abc import Sequence
|
||||||
|
from datetime import datetime
|
||||||
from typing import Optional, Union
|
from typing import Optional, Union
|
||||||
|
|
||||||
from sqlalchemy import func, select
|
from sqlalchemy import func, select
|
||||||
@@ -44,6 +45,7 @@ async def create(
|
|||||||
|
|
||||||
db_item = models.Filament(
|
db_item = models.Filament(
|
||||||
name=name,
|
name=name,
|
||||||
|
registered=datetime.utcnow().replace(microsecond=0),
|
||||||
vendor=vendor_item,
|
vendor=vendor_item,
|
||||||
material=material,
|
material=material,
|
||||||
price=price,
|
price=price,
|
||||||
|
|||||||
@@ -61,6 +61,7 @@ async def create(
|
|||||||
|
|
||||||
db_item = models.Spool(
|
db_item = models.Spool(
|
||||||
filament=filament_item,
|
filament=filament_item,
|
||||||
|
registered=datetime.utcnow().replace(microsecond=0),
|
||||||
used_weight=used_weight,
|
used_weight=used_weight,
|
||||||
first_used=first_used,
|
first_used=first_used,
|
||||||
last_used=last_used,
|
last_used=last_used,
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
"""Helper functions for interacting with vendor database objects."""
|
"""Helper functions for interacting with vendor database objects."""
|
||||||
|
|
||||||
|
from datetime import datetime
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
from sqlalchemy import func, select
|
from sqlalchemy import func, select
|
||||||
@@ -19,6 +20,7 @@ async def create(
|
|||||||
"""Add a new vendor to the database."""
|
"""Add a new vendor to the database."""
|
||||||
db_item = models.Vendor(
|
db_item = models.Vendor(
|
||||||
name=name,
|
name=name,
|
||||||
|
registered=datetime.utcnow().replace(microsecond=0),
|
||||||
comment=comment,
|
comment=comment,
|
||||||
)
|
)
|
||||||
db.add(db_item)
|
db.add(db_item)
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
"""Integration tests for the Filament API endpoint."""
|
"""Integration tests for the Filament API endpoint."""
|
||||||
|
|
||||||
|
from datetime import datetime, timezone
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
import httpx
|
import httpx
|
||||||
@@ -62,6 +63,10 @@ def test_add_filament(random_vendor: dict[str, Any]):
|
|||||||
"color_hex": color_hex,
|
"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
|
# Clean up
|
||||||
httpx.delete(f"{URL}/api/v1/filament/{filament['id']}").raise_for_status()
|
httpx.delete(f"{URL}/api/v1/filament/{filament['id']}").raise_for_status()
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
"""Integration tests for the Spool API endpoint."""
|
"""Integration tests for the Spool API endpoint."""
|
||||||
|
|
||||||
|
from datetime import datetime, timezone
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
import httpx
|
import httpx
|
||||||
@@ -63,6 +64,10 @@ def test_add_spool_remaining_weight(random_filament: dict[str, Any]):
|
|||||||
"archived": archived,
|
"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
|
# Clean up
|
||||||
httpx.delete(f"{URL}/api/v1/spool/{spool['id']}").raise_for_status()
|
httpx.delete(f"{URL}/api/v1/spool/{spool['id']}").raise_for_status()
|
||||||
|
|
||||||
|
|||||||
6
tests_integration/tests/vendor/test_add.py
vendored
6
tests_integration/tests/vendor/test_add.py
vendored
@@ -1,5 +1,7 @@
|
|||||||
"""Integration tests for the Vendor API endpoint."""
|
"""Integration tests for the Vendor API endpoint."""
|
||||||
|
|
||||||
|
from datetime import datetime, timezone
|
||||||
|
|
||||||
import httpx
|
import httpx
|
||||||
|
|
||||||
URL = "http://spoolman:8000"
|
URL = "http://spoolman:8000"
|
||||||
@@ -25,6 +27,10 @@ def test_add_vendor():
|
|||||||
"comment": comment,
|
"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
|
# Clean up
|
||||||
httpx.delete(f"{URL}/api/v1/vendor/{vendor['id']}").raise_for_status()
|
httpx.delete(f"{URL}/api/v1/vendor/{vendor['id']}").raise_for_status()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user