1
0
mirror of https://github.com/enpaul/keyosk.git synced 2024-11-05 06:07:06 +00:00

Fix bug in epoch field tests

This commit is contained in:
Ethan Paul 2020-03-09 00:15:10 -04:00
parent e5b4de799c
commit 98089c42d4
2 changed files with 16 additions and 6 deletions

View File

@ -1,5 +1,5 @@
"""Custom fields for handing de/serializing custom data types"""
from datetime import datetime
import datetime
from enum import Enum
from pathlib import Path
from typing import Any
@ -87,10 +87,12 @@ class PathString(msh.fields.String):
class Epoch(msh.fields.Field):
"""Translate between datetime objects and an integer reperesenting epoch time"""
def _serialize(self, value: Union[datetime, int], attr, obj, **kwargs) -> int:
def _serialize(
self, value: Union[datetime.datetime, int], attr, obj, **kwargs
) -> int:
"""Serialize a datetime object to an integer"""
if isinstance(value, datetime):
if isinstance(value, datetime.datetime):
return int(value.replace(tzinfo=datetime.timezone.utc).timestamp())
return value
@ -104,7 +106,7 @@ class Epoch(msh.fields.Field):
raise msh.ValidationError(
f"Invalid epoch value '{value}' of type '{type(value)}'"
)
return datetime.utcfromtimestamp(int(value))
return datetime.datetime.utcfromtimestamp(int(value))
class RawMultiType(msh.fields.Raw):

View File

@ -183,7 +183,10 @@ def test_epoch():
for data in good_data:
loaded = serializer.load(data)
assert isinstance(loaded["iamepoch"], datetime.datetime)
assert int(loaded["iamepoch"].timestamp()) == data["iamepoch"]
assert (
int(loaded["iamepoch"].replace(tzinfo=datetime.timezone.utc).timestamp())
== data["iamepoch"]
)
assert data == serializer.dump(loaded)
for data in bad_data:
@ -210,7 +213,12 @@ def test_epoch_none():
assert loaded["iamepoch"] is None
else:
assert isinstance(loaded["iamepoch"], datetime.datetime)
assert int(loaded["iamepoch"].timestamp()) == item["iamepoch"]
assert (
int(
loaded["iamepoch"].replace(tzinfo=datetime.timezone.utc).timestamp()
)
== item["iamepoch"]
)
assert item == serializer.dump(loaded)