diff --git a/app/models.py b/app/models.py index 5edf860..a21b4f2 100644 --- a/app/models.py +++ b/app/models.py @@ -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() diff --git a/app/tests/test_models.py b/app/tests/test_models.py index 0f9c0d0..9802eb1 100644 --- a/app/tests/test_models.py +++ b/app/tests/test_models.py @@ -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 ) @@ -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() diff --git a/app/tests/test_views.py b/app/tests/test_views.py index e7af207..57fe846 100644 --- a/app/tests/test_views.py +++ b/app/tests/test_views.py @@ -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() diff --git a/app/views.py b/app/views.py index 135aae0..6183811 100644 --- a/app/views.py +++ b/app/views.py @@ -33,8 +33,17 @@ def place_new_order(): @app.route('/api/v1/orders/', methods=['GET']) def fetch_order_by_id(orderid): - """ Fetches a single food order corresponding to the provided """ + """ Fetches a single food order matching the provided """ 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/', methods=['PUT']) +def update_order_by_id(orderid): + """ Updates a single food order matching the provided """ + 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"})