From 474e3068d62ba60e2656c54a01e42787dc618a03 Mon Sep 17 00:00:00 2001 From: Mike Crute Date: Mon, 30 Oct 2017 02:35:56 +0000 Subject: Extract date formatter logic to SyntheticField --- pandora/models/__init__.py | 18 ++++++++++++++---- pandora/models/pandora.py | 8 ++++---- tests/test_pandora/test_models.py | 24 +++++++++++++++++------- 3 files changed, 35 insertions(+), 15 deletions(-) diff --git a/pandora/models/__init__.py b/pandora/models/__init__.py index 7a60889..bafb6ce 100644 --- a/pandora/models/__init__.py +++ b/pandora/models/__init__.py @@ -57,6 +57,20 @@ class SyntheticField(namedtuple("SyntheticField", ["field"])): raise NotImplementedError +class DateField(SyntheticField): + """Date Field + + Handles a JSON map that contains a time field which is the timestamp with + nanosecond precision. + """ + + def formatter(self, api_client, data, value): + if not value: + return None + + return datetime.utcfromtimestamp(value["time"] / 1000) + + class ModelMetaClass(type): def __new__(cls, name, parents, dct): @@ -84,10 +98,6 @@ class PandoraModel(with_metaclass(ModelMetaClass, object)): consumers of these instances can ignore all of the details of this class. """ - @staticmethod - def json_to_date(api_client, data): - return datetime.utcfromtimestamp(data["time"] / 1000) - @classmethod def from_json_list(cls, api_client, data): """Convert a list of JSON values to a list of models diff --git a/pandora/models/pandora.py b/pandora/models/pandora.py index cd1e588..e6a37ff 100644 --- a/pandora/models/pandora.py +++ b/pandora/models/pandora.py @@ -1,7 +1,7 @@ from ..client import BaseAPIClient from ..errors import ParameterMissing -from . import SyntheticField -from . import Field, PandoraModel, PandoraListModel, PandoraDictListModel +from . import Field, DateField, SyntheticField +from . import PandoraModel, PandoraListModel, PandoraDictListModel class Station(PandoraModel): @@ -13,7 +13,7 @@ class Station(PandoraModel): is_quickmix = Field("isQuickMix") art_url = Field("artUrl") - date_created = Field("dateCreated", formatter=PandoraModel.json_to_date) + date_created = DateField("dateCreated") detail_url = Field("stationDetailUrl") id = Field("stationId") name = Field("stationName") @@ -231,7 +231,7 @@ class Bookmark(PandoraModel): artist_name = Field("artistName") art_url = Field("artUrl") bookmark_token = Field("bookmarkToken") - date_created = Field("dateCreated", formatter=PandoraModel.json_to_date) + date_created = DateField("dateCreated") # song only sample_url = Field("sampleUrl") diff --git a/tests/test_pandora/test_models.py b/tests/test_pandora/test_models.py index 1310d71..808efa4 100644 --- a/tests/test_pandora/test_models.py +++ b/tests/test_pandora/test_models.py @@ -35,6 +35,23 @@ class TestModelMetaClass(TestCase): self.assertFalse("__field__" in self.TestModel._fields) +class TestDateField(TestCase): + + class SampleModel(m.PandoraModel): + + date_field = m.DateField("foo") + + def test_json_to_date(self): + expected = datetime(2015, 7, 18, 3, 8, 17) + data = {"foo": {"time": 1437188897616}} + + model = self.SampleModel.from_json(None, data) + + # Python2.7 doesn't restore microseconds and we don't care about + # it anyhow so just remove it for this test + self.assertEqual(expected, model.date_field.replace(microsecond=0)) + + class TestPandoraModel(TestCase): JSON_DATA = { @@ -66,13 +83,6 @@ class TestPandoraModel(TestCase): def __repr__(self): return self._base_repr("Foo") - def test_json_to_date(self): - expected = datetime(2015, 7, 18, 3, 8, 17) - result = m.PandoraModel.json_to_date(None, {"time": 1437188897616}) - # Python2.7 doesn't restore microseconds and we don't care about - # it anyhow so just remove it for this test - self.assertEqual(expected, result.replace(microsecond=0)) - def test_init_sets_defaults(self): model = self.TestModel(None) self.assertEqual(model.field1, "a string") -- cgit v1.2.3