def test_from_response_with_relationships(self): response_content = data.JsonApiResponse( data=data.JsonApiObject( type='test', relationships={ 'other': data.Relationship( data=data.ResourceIdentifier(id='1', type='test')) }), included=[ mock.MagicMock(id='1', type='test', attributes={'name': 'alice'}) ]) 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) assert test.other.name == 'alice'
def test_refresh_with_relationships(self): mock_api = mock.Mock() mock_api.endpoint.return_value.get.return_value.content = data.JsonApiResponse( data=data.JsonApiObject( type='test', id='123', relationships={ 'other': data.Relationship( data=data.ResourceIdentifier(id='1', type='test')) }), included=[ mock.MagicMock(id='1', type='test', attributes={'name': 'alice'}) ]) orm_api = orm.OrmApi(mock_api) class Test(orm.ApiModel): class Meta: api = orm_api type = 'test' other = orm.RelationField(source='other') name = orm.AttributeField(source='name') test = Test.from_id('123') test.refresh() assert test.other.name == 'alice'
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(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) 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) assert test.other.type is 'test' assert test.other.id is '159'
def test_from_response_with_relationship_with_none_type(self): response_content = data.JsonApiResponse( data=data.JsonApiObject(id='1', type='test', relationships={ 'other': data.Relationship(data=data.ResourceIdentifier(id=None, type=None)) }) ) 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) assert test.other is None
def test_issue_19_attributes_are_readable_with_multiple_relations(self): response_content = data.JsonApiResponse( data=data.JsonApiObject( type='designs', relationships={'sub_designs': data.Relationship(data=[data.ResourceIdentifier(id=3, type='designs')])}, attributes={'name': 'doctor_x'} ), ) orm_api = orm.OrmApi(mock.MagicMock()) class Design(orm.ApiModel): class Meta: type = 'designs' api = orm_api name = orm.AttributeField('name') sub_designs = orm.RelationField('sub_designs') design = Design.from_response_content(response_content) assert design.name == 'doctor_x'