From 98089c42d42aff6e01437125b24cfa2736c135d6 Mon Sep 17 00:00:00 2001 From: Ethan Paul Date: Mon, 9 Mar 2020 00:15:10 -0400 Subject: [PATCH] Fix bug in epoch field tests --- keyosk/_fields.py | 10 ++++++---- tests/test_fields.py | 12 ++++++++++-- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/keyosk/_fields.py b/keyosk/_fields.py index 50daeed..cc98eb6 100644 --- a/keyosk/_fields.py +++ b/keyosk/_fields.py @@ -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): diff --git a/tests/test_fields.py b/tests/test_fields.py index 622f8d9..c8f6d81 100644 --- a/tests/test_fields.py +++ b/tests/test_fields.py @@ -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)