Now correctly handles datetimes with timezone set
This commit is contained in:
@@ -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()
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user