Skip to content

Commit

Permalink
fixup: address PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
iansolano committed Mar 18, 2020
1 parent bf814b3 commit 821b170
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 31 deletions.
2 changes: 2 additions & 0 deletions robot-server/robot_server/service/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
tags=["settings"])
app.include_router(router=deck_calibration.router,
tags=["deckCalibration"])
# TODO(isk: 3/18/20): this is an example route, remove item route and model
# once response work is implemented in new route handlers
app.include_router(router=item.router,
tags=["item"])

Expand Down
10 changes: 6 additions & 4 deletions robot-server/robot_server/service/models/json_api/factory.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Type, Any, Tuple

from .request import JsonApiRequest, RequestModel
from .response import JsonApiResponse, ResponseModel
from .request import json_api_request, RequestModel
from .response import json_api_response, ResponseModel


def generate_json_api_models(
Expand All @@ -11,6 +11,8 @@ def generate_json_api_models(
list_response: bool = False
) -> Tuple[Type[RequestModel], Type[ResponseModel]]:
return (
JsonApiRequest(type_string, attributes_model),
JsonApiResponse(type_string, attributes_model, use_list=list_response),
json_api_request(type_string, attributes_model),
json_api_response(
type_string, attributes_model, use_list=list_response
),
)
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def attributes(self):


# Note(isk: 3/13/20): formats and returns request model
def JsonApiRequest(
def json_api_request(
type_string: str,
attributes_model: Any
) -> Type[RequestModel]:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def resource_object(

# Note(isk: 3/13/20): returns response based on whether
# the data object is a list or not
def JsonApiResponse(
def json_api_response(
type_string: str,
attributes_model: Any,
*,
Expand Down
2 changes: 1 addition & 1 deletion robot-server/robot_server/service/routers/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@


@router.get("/items/{item_id}",
description="Get an individual item by it's ID",
description="Get an individual item by its ID",
summary="Get an individual item",
response_model=ItemResponse,
response_model_exclude_unset=True,
Expand Down
4 changes: 2 additions & 2 deletions robot-server/tests/service/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from dataclasses import dataclass
from uuid import uuid4

from robot_server.service.models.json_api.request import JsonApiRequest
from robot_server.service.models.json_api.request import json_api_request


@dataclass
Expand All @@ -20,4 +20,4 @@ class ItemModel(BaseModel):


item_type_name = 'item'
ItemRequest = JsonApiRequest(item_type_name, ItemModel)
ItemRequest = json_api_request(item_type_name, ItemModel)
16 changes: 8 additions & 8 deletions robot-server/tests/service/models/json_api/test_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

from pydantic import ValidationError

from robot_server.service.models.json_api.request import JsonApiRequest
from robot_server.service.models.json_api.request import json_api_request
from tests.service.helpers import ItemModel


def test_attributes_as_dict():
DictRequest = JsonApiRequest('item', dict)
DictRequest = json_api_request('item', dict)
obj_to_validate = {
'data': {'type': 'item', 'attributes': {}}
}
Expand All @@ -22,7 +22,7 @@ def test_attributes_as_dict():


def test_attributes_as_item_model():
ItemRequest = JsonApiRequest('item', ItemModel)
ItemRequest = json_api_request('item', ItemModel)
obj_to_validate = {
'data': {
'type': 'item',
Expand All @@ -39,7 +39,7 @@ def test_attributes_as_item_model():


def test_attributes_as_item_model__empty_dict():
ItemRequest = JsonApiRequest('item', ItemModel)
ItemRequest = json_api_request('item', ItemModel)
obj_to_validate = {
'data': {
'type': 'item',
Expand Down Expand Up @@ -67,7 +67,7 @@ def test_attributes_as_item_model__empty_dict():


def test_type_invalid_string():
MyRequest = JsonApiRequest('item', dict)
MyRequest = json_api_request('item', dict)
obj_to_validate = {
'data': {'type': 'not_an_item', 'attributes': {}}
}
Expand All @@ -85,7 +85,7 @@ def test_type_invalid_string():


def test_attributes_required():
MyRequest = JsonApiRequest('item', dict)
MyRequest = json_api_request('item', dict)
obj_to_validate = {
'data': {'type': 'item', 'attributes': None}
}
Expand All @@ -102,7 +102,7 @@ def test_attributes_required():


def test_data_required():
MyRequest = JsonApiRequest('item', dict)
MyRequest = json_api_request('item', dict)
obj_to_validate = {
'data': None
}
Expand All @@ -119,7 +119,7 @@ def test_data_required():


def test_request_with_id():
MyRequest = JsonApiRequest('item', dict)
MyRequest = json_api_request('item', dict)
obj_to_validate = {
'data': {
'type': 'item',
Expand Down
28 changes: 14 additions & 14 deletions robot-server/tests/service/models/json_api/test_response.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from pytest import raises
from pydantic import BaseModel, ValidationError

from robot_server.service.models.json_api.response import JsonApiResponse
from robot_server.service.models.json_api.response import json_api_response
from tests.service.helpers import ItemModel, ItemData


def test_attributes_as_dict():
MyResponse = JsonApiResponse('item', dict)
MyResponse = json_api_response('item', dict)
obj_to_validate = {
'data': {'id': '123', 'type': 'item', 'attributes': {}},
}
Expand All @@ -23,7 +23,7 @@ def test_attributes_as_dict():


def test_missing_attributes_dict():
MyResponse = JsonApiResponse('item', dict)
MyResponse = json_api_response('item', dict)
obj_to_validate = {
'data': {'id': '123', 'type': 'item'}
}
Expand All @@ -43,7 +43,7 @@ def test_missing_attributes_empty_model():
class EmptyModel(BaseModel):
pass

MyResponse = JsonApiResponse('item', EmptyModel)
MyResponse = json_api_response('item', EmptyModel)
obj_to_validate = {
'data': {'id': '123', 'type': 'item'}
}
Expand All @@ -61,7 +61,7 @@ class EmptyModel(BaseModel):


def test_attributes_as_item_model():
ItemResponse = JsonApiResponse('item', ItemModel)
ItemResponse = json_api_response('item', ItemModel)
obj_to_validate = {
'meta': None,
'links': None,
Expand Down Expand Up @@ -92,7 +92,7 @@ def test_attributes_as_item_model():


def test_list_item_model():
ItemResponse = JsonApiResponse('item', ItemModel, use_list=True)
ItemResponse = json_api_response('item', ItemModel, use_list=True)
obj_to_validate = {
'meta': None,
'links': None,
Expand Down Expand Up @@ -145,7 +145,7 @@ def test_list_item_model():


def test_type_invalid_string():
MyResponse = JsonApiResponse('item', dict)
MyResponse = json_api_response('item', dict)
obj_to_validate = {
'data': {'id': '123', 'type': 'not_an_item', 'attributes': {}}
}
Expand All @@ -163,7 +163,7 @@ def test_type_invalid_string():


def test_attributes_required():
ItemResponse = JsonApiResponse('item', ItemModel)
ItemResponse = json_api_response('item', ItemModel)
obj_to_validate = {
'data': {'id': '123', 'type': 'item', 'attributes': None}
}
Expand All @@ -180,7 +180,7 @@ def test_attributes_required():


def test_attributes_as_item_model__empty_dict():
ItemResponse = JsonApiResponse('item', ItemModel)
ItemResponse = json_api_response('item', ItemModel)
obj_to_validate = {
'data': {
'id': '123',
Expand Down Expand Up @@ -209,7 +209,7 @@ def test_attributes_as_item_model__empty_dict():


def test_resource_object_constructor():
ItemResponse = JsonApiResponse('item', ItemModel)
ItemResponse = json_api_response('item', ItemModel)
item = ItemModel(name='pear', price=1.2, quantity=10)
document = ItemResponse.resource_object(
id='abc123',
Expand All @@ -228,7 +228,7 @@ def test_resource_object_constructor():


def test_resource_object_constructor__no_attributes():
IdentifierResponse = JsonApiResponse('item', dict)
IdentifierResponse = json_api_response('item', dict)
document = IdentifierResponse.resource_object(id='abc123').dict()

assert document == {
Expand All @@ -239,7 +239,7 @@ def test_resource_object_constructor__no_attributes():


def test_resource_object_constructor__with_list_response():
ItemResponse = JsonApiResponse('item', ItemModel, use_list=True)
ItemResponse = json_api_response('item', ItemModel, use_list=True)
item = ItemModel(name='pear', price=1.2, quantity=10)
document = ItemResponse.resource_object(
id='abc123',
Expand All @@ -258,7 +258,7 @@ def test_resource_object_constructor__with_list_response():


def test_response_constructed_with_resource_object():
ItemResponse = JsonApiResponse('item', ItemModel)
ItemResponse = json_api_response('item', ItemModel)
item = ItemModel(name='pear', price=1.2, quantity=10)
data = ItemResponse.resource_object(
id='abc123',
Expand All @@ -281,7 +281,7 @@ def test_response_constructed_with_resource_object():


def test_response_constructed_with_resource_object__list():
ItemResponse = JsonApiResponse('item', ItemModel, use_list=True)
ItemResponse = json_api_response('item', ItemModel, use_list=True)
items = [
ItemData(id=1, name='apple', price=1.5, quantity=3),
ItemData(id=2, name='pear', price=1.2, quantity=10),
Expand Down

0 comments on commit 821b170

Please sign in to comment.