Skip to content

Commit

Permalink
Merge pull request #4 from artorious/ft-update-food-order-160233942
Browse files Browse the repository at this point in the history
#160233942 Add feature to update food order status
  • Loading branch information
Kalela committed Sep 25, 2018
2 parents c9b123b + 18cb10a commit 3072c20
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 2 deletions.
15 changes: 15 additions & 0 deletions app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,21 @@ def fetch_order_by_id(self, orderid):
"Order fetching error message": "orderid should be integer"
}

def update_order_by_id(self, orderid, update_status):
""" (FoodOrders, int, bool) -> dict
Returns custom message to user to indicat update status
"""
if isinstance(orderid, int) and isinstance(update_status, bool):
if orderid in self.all_food_orders:
self.all_food_orders[orderid]['order_accept_status'] =\
update_status
return {"Order update message": "Update Successful"}
return {"Order update error message": "orderid out of range"}
return {
"Order update error message": "Invalid Input"
}


if __name__ == '__main__':
FoodOrders()
54 changes: 53 additions & 1 deletion app/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def test_place_new_order_only_accepts_valid_data(self):
)

def test_fetch_one_order_returns_dict(self):
""" Test that fetch_one_order() returns dict """
""" Test that fetch_order_by_id(orderid) returns dict """
self.sample_food_orders.place_new_order(
self.sample_order_request_info
)
Expand Down Expand Up @@ -105,6 +105,58 @@ def test_fetch_one_order_with_non_int_orderid(self):
msg="Method does not handle non-integers for orderid"
)

def test_update_order_returns_dict(self):
""" Test that update_order_by_id(orderid) returns dict """
self.sample_food_orders.place_new_order(
self.sample_order_request_info
)

self.assertIsInstance(
self.sample_food_orders.update_order_by_id(1, True),
dict,
msg='Method does not return a dict'
)

def test_update_order_handles_out_of_range_orderid(self):
""" Test that out of range orderid return custom error message
(Out of range)
"""
self.sample_food_orders.place_new_order(
self.sample_order_request_info
)

self.assertDictEqual(
self.sample_food_orders.update_order_by_id(2, True),
{"Order update error message": "orderid out of range"},
msg="Method does not handle out of range orders"
)

self.assertDictEqual(
self.sample_food_orders.update_order_by_id(0, True),
{"Order update error message": "orderid out of range"},
msg="Method does not handle out of range orders"
)

def test_update_order_handles_invalid_input(self):
""" Test that invalid (non-int) orderid and update_status (non-bool)
returns custom error message
"""
self.sample_food_orders.place_new_order(
self.sample_order_request_info
)

self.assertDictEqual(
self.sample_food_orders.update_order_by_id('2.0', True),
{"Order update error message": "Invalid Input"},
msg="Method does not handle non-integers for orderid"
)

self.assertDictEqual(
self.sample_food_orders.update_order_by_id(1, "True"),
{"Order update error message": "Invalid Input"},
msg="Method does not handle non-integers for orderid"
)


if __name__ == '__main__':
unittest.main()
47 changes: 47 additions & 0 deletions app/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,53 @@ def test_fetch_one_order_operation_with_malformed_route(self):
msg='Error: The requested URL was not found on the server'
)

def test_update_order_operation_success(self):
""" Test that a non-error path returns a single order in JSON and
HTTP response code of 200 (OK)
"""
self.app.post(
'/api/v1/orders',
data=json.dumps(self.sample_order_request_info),
headers={'content-type': 'application/json'}
)

test_resp = self.app.put(
'/api/v1/orders/1',
data=json.dumps(True),
headers={'content-type': 'application/json'}
)
self.assertEqual(
test_resp.status_code, 200, msg='Expected 200'
)
self.assertIn(
b"Order update message",
test_resp.data,
msg="Does not output success msg to user"
)

def test_update_order_operation_malformed_route(self):
""" Test that path with an error (malformed syntax) returns an
appropriate error message in JSON and HTTP response code of
404 (BAD REQUEST)
"""

self.app.post(
'/api/v1/orders',
data=json.dumps(self.sample_order_request_info),
headers={'content-type': 'application/json'}
)

test_resp = self.app.put(
'/api/v1/orders/one',
data=json.dumps(True),
headers={'content-type': 'application/json'}
)
self.assertEqual(
test_resp.status_code,
404,
msg='Error: The requested URL was not found on the server'
)


if __name__ == '__main__':
unittest.main()
11 changes: 10 additions & 1 deletion app/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,17 @@ def place_new_order():

@app.route('/api/v1/orders/<int:orderid>', methods=['GET'])
def fetch_order_by_id(orderid):
""" Fetches a single food order corresponding to the provided <orderid>"""
""" Fetches a single food order matching the provided <orderid>"""
if isinstance(orderid, int):
return jsonify(SAMPLE_FOOD_ORDERS.fetch_order_by_id(orderid))
else:
return jsonify({"Order fetching error message": "orderid should be integer"})

@app.route('/api/v1/orders/<int:orderid>', methods=['PUT'])
def update_order_by_id(orderid):
""" Updates a single food order matching the provided <orderid> """
req_data = request.get_json()

if isinstance(orderid, int) and isinstance(req_data, bool):
return jsonify(SAMPLE_FOOD_ORDERS.update_order_by_id(orderid, req_data))
return jsonify({"Order update message": "Update Failed..Invalid input"})

0 comments on commit 3072c20

Please sign in to comment.