Skip to content

Commit

Permalink
Merge 0cf6cb1 into 417547c
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanmcc committed Jul 21, 2016
2 parents 417547c + 0cf6cb1 commit 9750ac8
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
8 changes: 5 additions & 3 deletions pinax/api/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,10 @@ def resolve_include(resource, path, included):
def resolve_value(value):
if callable(value):
value = resolve_value(value())
if isinstance(value, datetime.datetime):
return rfc3339.encode(value)
if hasattr(value, "as_json"):
elif isinstance(value, datetime.datetime):
value = rfc3339.encode(value)
elif isinstance(value, datetime.date):
value = datetime.date.isoformat(value)
elif hasattr(value, "as_json"):
value = value.as_json()
return value
34 changes: 29 additions & 5 deletions pinax/api/tests/test_resources.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import mock
from datetime import date, datetime
from mock import NonCallableMock, patch, sentinel

from django.contrib.auth.models import AnonymousUser
from django.core.urlresolvers import reverse
Expand All @@ -20,7 +21,7 @@ def test_get_empty_collection(self):
"""
collection_url = reverse("article-list")

with mock.patch("pinax.api.authentication.Anonymous.authenticate", autospec=True) as mock_authenticate:
with patch("pinax.api.authentication.Anonymous.authenticate", autospec=True) as mock_authenticate:
mock_authenticate.return_value = AnonymousUser()
response = self.client.get(collection_url)
payload = json.loads(response.content.decode("utf-8"))
Expand Down Expand Up @@ -57,7 +58,7 @@ def test_get_collection(self):
kwargs=dict(pk=article.pk)
)

with mock.patch("pinax.api.authentication.Anonymous.authenticate", autospec=True) as mock_authenticate:
with patch("pinax.api.authentication.Anonymous.authenticate", autospec=True) as mock_authenticate:
mock_authenticate.return_value = AnonymousUser()
response = self.client.get(collection_url)
payload = json.loads(response.content.decode("utf-8"))
Expand Down Expand Up @@ -126,7 +127,7 @@ def test_get_article(self):
kwargs=dict(pk=article.pk)
)

with mock.patch("pinax.api.authentication.Anonymous.authenticate", autospec=True) as mock_authenticate:
with patch("pinax.api.authentication.Anonymous.authenticate", autospec=True) as mock_authenticate:
mock_authenticate.return_value = AnonymousUser()
response = self.client.get(detail_url)
payload = json.loads(response.content.decode("utf-8"))
Expand Down Expand Up @@ -184,7 +185,7 @@ def test_create(self):
}
}
}
with mock.patch("pinax.api.authentication.Anonymous.authenticate", autospec=True) as mock_authenticate:
with patch("pinax.api.authentication.Anonymous.authenticate", autospec=True) as mock_authenticate:
mock_authenticate.return_value = AnonymousUser()
collection_url = reverse("article-list")
response = self.client.post(
Expand Down Expand Up @@ -235,3 +236,26 @@ def test_create(self):
}
}
self.assertEqual(expected, payload)


class ResolveValueTestCase(api.TestCase):

def test_should_call_callables(self):
result = api.resource.resolve_value(lambda: sentinel.callable_result)
self.assertEqual(result, sentinel.callable_result)

def test_should_coerce_datetime(self):
datetime_ = datetime.now()
result = api.resource.resolve_value(datetime_)
self.assertEqual(result, api.rfc3339.encode(datetime_))

def test_should_coerce_date(self):
date_ = date.today()
result = api.resource.resolve_value(date_)
self.assertEqual(result, date.isoformat(date_))

def test_should_return_as_json(self):
with_as_json = NonCallableMock()
with_as_json.as_json.return_value = sentinel.as_json
result = api.resource.resolve_value(with_as_json)
self.assertEqual(result, sentinel.as_json)

0 comments on commit 9750ac8

Please sign in to comment.