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_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_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_saving_new(self): mock_api = mock.MagicMock() mock_api.endpoint.return_value.post.return_value.status_code = 201 mock_api.endpoint.return_value.post.return_value.content.data = data.JsonApiObject( attributes={'name': 'doctor_x'}, id='1', type='test') orm_api = orm.OrmApi(mock_api) class Design(orm.ApiModel): class Meta: type = 'designs' api = orm_api name = orm.AttributeField('name') design = Design() design.name = 'doctor_x' assert design.id is None design.save() assert design.id == '1' mock_api.endpoint.return_value.post.assert_called_with( object=data.JsonApiObject.from_data({ 'type': 'designs', 'attributes': { 'name': 'doctor_x' } }))
def test_exists_valid(self): mock_api = mock.MagicMock() mock_api.endpoint.return_value.get.return_value.content.data = data.JsonApiObject( type='test', id='123', attributes={'name': 'alice'}) orm_api = orm.OrmApi(mock_api) class Test(orm.ApiModel): class Meta: api = orm_api type = 'test' name = orm.AttributeField(source='name') assert Test.exists('123')
def test_refresh(self): mock_api = mock.MagicMock() mock_api.endpoint.return_value.get.return_value.content.data = data.JsonApiObject( type='test', id='123', attributes={'name': 'alice'}) orm_api = orm.OrmApi(mock_api) class Test(orm.ApiModel): class Meta: api = orm_api type = 'test' name = orm.AttributeField(source='name') test = Test.from_id('123') test.refresh() mock_api.endpoint.assert_called_with('test/123') assert test.name == 'alice'
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'
def __init__(self, raw_object=None): self.raw_object = raw_object or data.JsonApiObject( type=self._options.type) self.relationship_cache = {}
def test_custom_field_serialization(self): mock_api = mock.MagicMock() mock_api.endpoint.return_value.post.return_value.status_code = 201 mock_api.endpoint.return_value.post.return_value.content.data = data.JsonApiObject( type='test', id='123', attributes={'name': 'alice', 'extra': '0x1000'} ) orm_api = orm.OrmApi(mock_api) class HexField(orm.AttributeField): def deserialize(self, json_value): return int(json_value, 16) def serialize(self, value): return hex(value) hex_field = HexField(source='extra') # NOTE: assert_not_called() isn't used for py3.4 compatibility in this test decode_spy = mock.patch.object(HexField, 'deserialize', wraps=hex_field.deserialize).start() encode_spy = mock.patch.object(HexField, 'serialize', wraps=hex_field.serialize).start() class Test(orm.ApiModel): class Meta: api = orm_api type = 'test' name = orm.AttributeField(source='name') extra = hex_field test = Test() test.name = 'alice' test.extra = 1024 assert test.extra == 1024 # verify that we can successfully change the value and re-read it test.extra = 2048 assert test.extra == 2048 # nothing should have been decoded at this point -- the reads (for assert) should have been cached assert not decode_spy.called # should have done an encode for each set encode_spy.assert_has_calls([mock.call(1024), mock.call(2048)], any_order=False) encode_spy.reset_mock() test.save() mock_api.endpoint.return_value.post.assert_called_with( object=data.JsonApiObject.from_data( { 'type': 'test', 'attributes': {'name': 'alice', 'extra': '0x800'} } ) ) # no further encoding/decoding should have happened decode_spy.assert_has_calls([]) decode_spy.assert_has_calls([]) # verify that the new value that the API returned was set and decoded properly assert test.extra == 4096 decode_spy.assert_called_once_with('0x1000') decode_spy.reset_mock() # read the property again but check that we didn't re-decode it assert test.extra == 4096 assert not decode_spy.called