Skip to content

Commit

Permalink
Merge pull request #51 from vertical-knowledge/additional-jsonapi-fea…
Browse files Browse the repository at this point in the history
…tures

Fixed issue where JSONAPI adapter broke when a no_pks resource was passed
  • Loading branch information
timmartin19 committed Nov 23, 2015
2 parents cc088e9 + 4cdfc79 commit 86e19d9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
4 changes: 4 additions & 0 deletions ripozo/adapters/jsonapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,16 @@ def _construct_id(resource):
May not work particularly well for composite ids since
apparently JSON API hates them. It will simply
join the pks with a `"/"` in the appropriate order.
Additionally, for resources with no_pks=True, it
will return an empty string.
:param ResourceBase resource: The resource whose
id needs to be constructed.
:return: The id in a format `<pk1>/<pk2>`
:rtype: unicode
"""
if resource.no_pks:
return ""
pks = resource.pks
id_parts = [resource.item_pks[pk] for pk in pks]
id_ = join_url_parts(*id_parts)
Expand Down
29 changes: 29 additions & 0 deletions ripozo_tests/unit/dispatch/adapters/jsonapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,3 +272,32 @@ class MyResource(ResourceBase):
self.assertEqual(body['type'], 'my_resource')
self.assertDictEqual(body['links'], dict(self='/my_resource/1'))
self.assertDictEqual(body['attributes'], dict(id=1))

def test_no_pks_resource_construct_id(self):
"""
Tests that a response is appropriately returned
if there are no pks
"""
class MyResource(ResourceBase):
pks = 'id',

res = MyResource(properties=dict(value=1), no_pks=True)
id_ = JSONAPIAdapter._construct_id(res)
self.assertEqual(id_, "")

def test_no_pks_resource(self):
"""
Tests that a resource with no pks is appropriately
constructed.
"""
class MyResource(ResourceBase):
pks = 'id',

res = MyResource(properties=dict(value=1), no_pks=True)
adapter = JSONAPIAdapter(res)
resp = adapter.formatted_body
data = json.loads(resp)['data']
self.assertDictEqual(data['attributes'], dict(value=1))
self.assertEqual(data['links'], dict(self='/my_resource'))
self.assertEqual(data['id'], '')
self.assertEqual(data['type'], 'my_resource')

0 comments on commit 86e19d9

Please sign in to comment.