def test_attributes_as_item_model(): ItemResponse = json_api_response(ItemModel) obj_to_validate = { 'meta': None, 'links': None, 'data': { 'id': '123', 'type': 'item', 'attributes': { 'name': 'apple', 'quantity': 10, 'price': 1.20 } } } my_response_obj = ItemResponse(**obj_to_validate) assert my_response_obj.dict() == { 'meta': None, 'links': None, 'data': { 'id': '123', 'type': 'item', 'attributes': { 'name': 'apple', 'quantity': 10, 'price': 1.20, } } }
def test_attributes_as_item_model__empty_dict(): ItemResponse = json_api_response(ItemModel) obj_to_validate = { 'data': { 'id': '123', 'type': 'item', 'attributes': {} } } with raises(ValidationError) as e: ItemResponse(**obj_to_validate) assert e.value.errors() == [ { 'loc': ('data', 'attributes', 'name'), 'msg': 'field required', 'type': 'value_error.missing' }, { 'loc': ('data', 'attributes', 'quantity'), 'msg': 'field required', 'type': 'value_error.missing' }, { 'loc': ('data', 'attributes', 'price'), 'msg': 'field required', 'type': 'value_error.missing' }, ]
def test_list_item_model(): ItemResponse = json_api_response(ItemModel, use_list=True) obj_to_validate = { 'meta': None, 'links': None, 'data': [ { 'id': '123', 'type': 'item', 'attributes': { 'name': 'apple', 'quantity': 10, 'price': 1.20 }, }, { 'id': '321', 'type': 'item', 'attributes': { 'name': 'banana', 'quantity': 20, 'price': 2.34 }, }, ], } my_response_obj = ItemResponse(**obj_to_validate) assert my_response_obj.dict() == { 'meta': None, 'links': None, 'data': [ { 'id': '123', 'type': 'item', 'attributes': { 'name': 'apple', 'quantity': 10, 'price': 1.20, }, }, { 'id': '321', 'type': 'item', 'attributes': { 'name': 'banana', 'quantity': 20, 'price': 2.34, }, }, ], }
def test_attributes_as_dict(): MyResponse = json_api_response(dict) obj_to_validate = { 'data': {'id': '123', 'type': 'item', 'attributes': {}}, } my_response_object = MyResponse(**obj_to_validate) assert my_response_object.dict() == { 'meta': None, 'links': None, 'data': { 'id': '123', 'type': 'item', 'attributes': {}, } }
def test_attributes_required(): ItemResponse = json_api_response(ItemModel) obj_to_validate = { 'data': {'id': '123', 'type': 'item', 'attributes': None} } with raises(ValidationError) as e: ItemResponse(**obj_to_validate) assert e.value.errors() == [ { 'loc': ('data', 'attributes'), 'msg': 'none is not an allowed value', 'type': 'type_error.none.not_allowed', }, ]
def test_response_constructed_with_resource_object_list(): ItemResponse = json_api_response(ItemModel, use_list=True) items = ( (1, ItemModel(name='apple', price=1.5, quantity=3)), (2, ItemModel(name='pear', price=1.2, quantity=10)), (3, ItemModel(name='orange', price=2.2, quantity=5)) ) response = ItemResponse( data=[ ResponseDataModel.create(resource_id=str(item[0]), attributes=item[1]) for item in items ] ) assert response.dict() == { 'meta': None, 'links': None, 'data': [ { 'id': '1', 'type': 'ItemModel', 'attributes': { 'name': 'apple', 'price': 1.5, 'quantity': 3, }, }, { 'id': '2', 'type': 'ItemModel', 'attributes': { 'name': 'pear', 'price': 1.2, 'quantity': 10, }, }, { 'id': '3', 'type': 'ItemModel', 'attributes': { 'name': 'orange', 'price': 2.2, 'quantity': 5, }, }, ] }
def test_missing_attributes_empty_model(): class EmptyModel(BaseModel): pass MyResponse = json_api_response(EmptyModel) obj_to_validate = { 'data': {'id': '123', 'type': 'item'} } my_response_object = MyResponse(**obj_to_validate) assert my_response_object.dict() == { 'meta': None, 'links': None, 'data': { 'id': '123', 'type': 'item', 'attributes': {}, } } assert isinstance(my_response_object.data.attributes, EmptyModel)
def test_response_constructed_with_resource_object(): ItemResponse = json_api_response(ItemModel) item = ItemModel(name='pear', price=1.2, quantity=10) data = ResponseDataModel.create( resource_id='abc123', attributes=item ).dict() assert ItemResponse(data=data).dict() == { 'meta': None, 'links': None, "data": { 'id': 'abc123', "type": 'ItemModel', "attributes": { 'name': 'pear', 'price': 1.2, 'quantity': 10, }, } }