def test_object_from_dict(): obj = Object.from_dict({ 'type': 'dummy_type', 'id': 'dummy_id', 'attributes': { 'attr1': 'foo', 'attr2': 1, } }) assert obj.id == 'dummy_id' assert obj.type == 'dummy_type' assert obj.attr1 == 'foo' assert obj.attr2 == 1 with pytest.raises(ValueError, match=r"Expecting dictionary, got: int"): Object.from_dict(1) with pytest.raises(ValueError, match=r"Object type not found"): Object.from_dict({}) with pytest.raises(ValueError, match=r"Object id not found"): Object.from_dict({'type': 'dummy_type'}) with pytest.raises(ValueError, match=r'Object attributes must be a dictionary'): Object.from_dict({ 'type': 'dummy_type', 'id': 'dummy_id', 'attributes': 1 })
def _get(self, resource): data, tag = self.cache.get(resource, tag=True) if data and tag in ['sha1', 'md5']: data, tag = self.cache.get(data, tag=True) if data and tag == 'object': data = Object.from_dict(data) return data, tag
def test_patch_object(httpserver): obj = Object('dummy_type', 'dummy_id', {'foo': 1, 'bar': 2}) obj.foo = 2 httpserver.expect_request( '/api/v3/dummy_types/dummy_id', method='PATCH', headers={'X-Apikey': 'dummy_api_key'}, data=json.dumps({'data': obj.to_dict(modified_attributes_only=True)}), ).respond_with_json({ 'data': { 'id': 'dummy_id', 'type': 'dummy_type', 'attributes': { 'foo': 2, } } }) with new_client(httpserver) as client: client.patch_object('/dummy_types/dummy_id', obj=obj)
def test_post_object(httpserver): obj = Object('dummy_type') obj.foo = 'foo' httpserver.expect_request( '/api/v3/dummy_types', method='POST', headers={'X-Apikey': 'dummy_api_key'}, data=json.dumps({'data': obj.to_dict()}), ).respond_with_json({ 'data': { 'id': 'dummy_id', 'type': 'dummy_type', 'attributes': { 'foo': 'foo', } } }) with new_client(httpserver) as client: obj = client.post_object('/dummy_types', obj=obj) assert obj.id == 'dummy_id'
def test_object_from_dict(): obj = Object.from_dict({ 'type': 'dummy_type', 'id': 'dummy_id', 'attributes': { 'attr1': 'foo', 'attr2': 1, }, 'relationships': { 'foos': { 'data': [{ 'type': 'foo', 'id': 'foo_id' }] } } }) assert obj.id == 'dummy_id' assert obj.type == 'dummy_type' assert obj.attr1 == 'foo' assert obj.attr2 == 1 assert obj.relationships['foos']['data'][0]['id'] == 'foo_id' with pytest.raises(ValueError, match=r'Expecting dictionary, got: int'): Object.from_dict(1) with pytest.raises(ValueError, match=r'Object type not found'): Object.from_dict({}) with pytest.raises(ValueError, match=r'Object id not found'): Object.from_dict({'type': 'dummy_type'}) with pytest.raises(ValueError, match=r'Object attributes must be a dictionary'): Object.from_dict({ 'type': 'dummy_type', 'id': 'dummy_id', 'attributes': 1 })
def test_object_modified_attrs(): obj = Object.from_dict({ 'type': 'dummy_type', 'id': 'dummy_id', 'attributes': { 'attr1': 'foo', 'attr2': 1, 'attr3': { 'subattr1': 'bar' }, 'attr4': { 'subattr1': 'baz' } } }) # No changes, attributes shouldn't appear in the dictionary. obj_dict = obj.to_dict(modified_attributes_only=True) assert 'attributes' not in obj_dict # attr1 set to its previous value, no changes yet. obj.attr1 = 'foo' obj_dict = obj.to_dict(modified_attributes_only=True) assert 'attributes' not in obj_dict # attr1 changed to 'bar', this should be the only attribute in the dictionary. obj.attr1 = 'bar' obj_dict = obj.to_dict(modified_attributes_only=True) assert len(obj_dict['attributes']) == 1 assert obj_dict['attributes']['attr1'] == 'bar' obj.attr3['subattr1'] = 'foo' obj_dict = obj.to_dict(modified_attributes_only=True) assert len(obj_dict['attributes']) == 2 assert obj_dict['attributes']['attr1'] == 'bar' assert obj_dict['attributes']['attr3'] == {'subattr1': 'foo'} del obj.attr4['subattr1'] obj_dict = obj.to_dict(modified_attributes_only=True) assert len(obj_dict['attributes']) == 3 assert obj_dict['attributes']['attr1'] == 'bar' assert obj_dict['attributes']['attr3'] == {'subattr1': 'foo'} assert obj_dict['attributes']['attr4'] == {}
def test_object_date_attrs(): obj = Object('dummy_type') obj.foo_date = 0 assert obj.foo_date == datetime.datetime(1970, 1, 1, 0, 0, 0)