From 8c14c918260603ee0f78656976dfe55e87e4a35f Mon Sep 17 00:00:00 2001 From: vpershin Date: Fri, 9 Feb 2018 17:20:44 +0700 Subject: [PATCH 1/3] Add get --- jsonapi_requests/orm/fields.py | 10 +++++++++- tests/test_orm.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/jsonapi_requests/orm/fields.py b/jsonapi_requests/orm/fields.py index a94b69b..6b17855 100644 --- a/jsonapi_requests/orm/fields.py +++ b/jsonapi_requests/orm/fields.py @@ -124,7 +124,14 @@ def get_object_key(self): except KeyError: raise ObjectKeyError else: - data = relationship.data + data = None + if (hasattr(relationship, 'data') and relationship.data.as_data() != {}) or relationship.as_data() == {}: + data = relationship.data + elif hasattr(relationship, 'links') and relationship.links != {}: + res = self.instance._options.api.endpoint(path=relationship.links.as_data()['self']) + relationship_data = res.get() + data = relationship_data.data + if data.type is None or data.id is None: raise ObjectKeyError else: @@ -187,3 +194,4 @@ def set_cache(self, related): def get_cached(self): return self.instance.relationship_cache[self.source] + diff --git a/tests/test_orm.py b/tests/test_orm.py index f7e40ab..59eda39 100644 --- a/tests/test_orm.py +++ b/tests/test_orm.py @@ -5,6 +5,7 @@ from jsonapi_requests import orm + class TestApiModel: def test_empty_declaration(self): class Test(orm.ApiModel): @@ -106,6 +107,33 @@ class Meta: test = Test.from_response_content(response_content) assert test.other is None + + def test_from_response_with_relationship_with_links_only(self): + response_content = data.JsonApiResponse( + data=data.JsonApiObject(id='1', type='test', relationships={ + 'other': data.Relationship(data=data.ResourceIdentifier(id=None, type=None), + links=data.Dictionary(self='http://example.org/api/rest/admin/gateway-groups/14/relationships/vendor', + related='http://example.org/api/rest/admin/gateway-groups/14/vendor' + ) +# links={'self': 'http://example.org/api/rest/admin/gateway-groups/14/relationships/vendor', +# 'related': 'http://example.org/api/rest/admin/gateway-groups/14/vendor'} + ) + }) + ) + orm_api = orm.OrmApi(mock.MagicMock()) + + class Test(orm.ApiModel): + class Meta: + api = orm_api + type = 'test' + other = orm.RelationField(source='other') + name = orm.AttributeField(source='name') + + test = Test.from_response_content(response_content) + #print(type(test.other)) + assert test.other is None + + def test_issue_19_attributes_are_readable_with_multiple_relations(self): response_content = data.JsonApiResponse( data=data.JsonApiObject( From fee54547646b428fd48fddcece3062f8c33cdacc Mon Sep 17 00:00:00 2001 From: vpershin Date: Thu, 15 Feb 2018 12:12:00 +0700 Subject: [PATCH 2/3] Add tests --- tests/test_orm.py | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/tests/test_orm.py b/tests/test_orm.py index 59eda39..22ce86f 100644 --- a/tests/test_orm.py +++ b/tests/test_orm.py @@ -3,7 +3,7 @@ from jsonapi_requests import data from jsonapi_requests import request_factory from jsonapi_requests import orm - +import inspect class TestApiModel: @@ -109,18 +109,17 @@ class Meta: def test_from_response_with_relationship_with_links_only(self): + mock_api = mock.MagicMock() + mock_api.endpoint().get().data = data.ResourceIdentifier(type='test', id='159') response_content = data.JsonApiResponse( data=data.JsonApiObject(id='1', type='test', relationships={ - 'other': data.Relationship(data=data.ResourceIdentifier(id=None, type=None), - links=data.Dictionary(self='http://example.org/api/rest/admin/gateway-groups/14/relationships/vendor', - related='http://example.org/api/rest/admin/gateway-groups/14/vendor' + 'other': data.Relationship(links=data.Dictionary(self='http://example.org/api/rest/test/1/relationships/vendor', + related='http://example.org/api/rest/test/1/vendor' ) -# links={'self': 'http://example.org/api/rest/admin/gateway-groups/14/relationships/vendor', -# 'related': 'http://example.org/api/rest/admin/gateway-groups/14/vendor'} ) }) ) - orm_api = orm.OrmApi(mock.MagicMock()) + orm_api = orm.OrmApi(mock_api) class Test(orm.ApiModel): class Meta: @@ -128,10 +127,9 @@ class Meta: type = 'test' other = orm.RelationField(source='other') name = orm.AttributeField(source='name') - test = Test.from_response_content(response_content) - #print(type(test.other)) - assert test.other is None + assert test.other.type is 'test' + assert test.other.id is '159' def test_issue_19_attributes_are_readable_with_multiple_relations(self): From d94724e12058960636c76b99fc40d6a5f741a8fe Mon Sep 17 00:00:00 2001 From: vpershin Date: Thu, 15 Feb 2018 12:55:31 +0700 Subject: [PATCH 3/3] Fix flake8 error --- jsonapi_requests/orm/fields.py | 1 - jsonapi_requests/request_factory.py | 4 ++-- tests/test_orm.py | 12 +++++------- 3 files changed, 7 insertions(+), 10 deletions(-) diff --git a/jsonapi_requests/orm/fields.py b/jsonapi_requests/orm/fields.py index 6b17855..ff777b4 100644 --- a/jsonapi_requests/orm/fields.py +++ b/jsonapi_requests/orm/fields.py @@ -194,4 +194,3 @@ def set_cache(self, related): def get_cached(self): return self.instance.relationship_cache[self.source] - diff --git a/jsonapi_requests/request_factory.py b/jsonapi_requests/request_factory.py index 0af3d89..23c3004 100644 --- a/jsonapi_requests/request_factory.py +++ b/jsonapi_requests/request_factory.py @@ -36,8 +36,8 @@ def request(self, api_path, method, *, object: data.JsonApiObject=None, **kwargs @property def retrying(self): retry_condition = ( - tenacity.retry_if_exception_type(ApiConnectionError) - | tenacity.retry_if_exception_type(ApiInternalServerError) + tenacity.retry_if_exception_type(ApiConnectionError) | + tenacity.retry_if_exception_type(ApiInternalServerError) ) return tenacity.Retrying( reraise=True, diff --git a/tests/test_orm.py b/tests/test_orm.py index 22ce86f..838847e 100644 --- a/tests/test_orm.py +++ b/tests/test_orm.py @@ -3,7 +3,6 @@ from jsonapi_requests import data from jsonapi_requests import request_factory from jsonapi_requests import orm -import inspect class TestApiModel: @@ -107,16 +106,16 @@ class Meta: test = Test.from_response_content(response_content) assert test.other is None - def test_from_response_with_relationship_with_links_only(self): mock_api = mock.MagicMock() - mock_api.endpoint().get().data = data.ResourceIdentifier(type='test', id='159') + mock_api.endpoint().get().data = data.ResourceIdentifier(type='test', id='159') response_content = data.JsonApiResponse( data=data.JsonApiObject(id='1', type='test', relationships={ - 'other': data.Relationship(links=data.Dictionary(self='http://example.org/api/rest/test/1/relationships/vendor', + 'other': data.Relationship(links=data.Dictionary(self='http://example.org/api/rest/test/1/relationships' + '/vendor', related='http://example.org/api/rest/test/1/vendor' - ) - ) + ) + ) }) ) orm_api = orm.OrmApi(mock_api) @@ -131,7 +130,6 @@ class Meta: assert test.other.type is 'test' assert test.other.id is '159' - def test_issue_19_attributes_are_readable_with_multiple_relations(self): response_content = data.JsonApiResponse( data=data.JsonApiObject(