Skip to content

Commit

Permalink
Merge pull request #3 from artorious/ft-fetch-single-order-160233117
Browse files Browse the repository at this point in the history
[Finishes #160233117] fetch a single order by orderID
  • Loading branch information
artorious authored Sep 24, 2018
2 parents 6ce91aa + 98d1ee3 commit c9b123b
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 10 deletions.
27 changes: 24 additions & 3 deletions app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

import datetime


class FoodOrders():
""" Holds methods for food orders
Attributes:
all_food_orders (dict) - Variable holds all food orders
{order_count(int):
{'orderID': int,
{'orderid': int,
'username': str,
'order_qty': int,
'order_description': str,
Expand Down Expand Up @@ -42,6 +43,26 @@ def place_new_order(self, order_request_info):
self.all_food_orders[self.food_order_count] = order_request_info
self.food_order_count += 1

return {'Order placement message': 'Order succesfully placed'}
return {"Order placement message": "Order succesfully placed"}
else:
raise TypeError('Argument should be a dictionary')
raise TypeError("Argument should be a dictionary")

def fetch_order_by_id(self, orderid):
""" (FoodOrders, int) -> dict
Returns food order with corresponding orderid
"""
try:
int(orderid)
if orderid in self.all_food_orders:
return self.all_food_orders[orderid]
else:
return {"Order fetching error message": "orderid out of range"}
except ValueError:
return {
"Order fetching error message": "orderid should be integer"
}


if __name__ == '__main__':
FoodOrders()
66 changes: 59 additions & 7 deletions app/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ def setUp(self):
""" Instantiate """
self.sample_food_orders = FoodOrders()
self.sample_order_request_info = {
'username': 'mrnoname',
'order_qty': 2,
'order_description': '500ml soda @Ksh. 100/='
"username": "mrnoname",
"order_qty": 2,
"order_description": "500ml soda @Ksh. 100/="
}

def test_class_inits_with_dict(self):
Expand All @@ -27,14 +27,14 @@ def test_class_inits_with_an_order_count_var(self):
""" Test class inits with an order count variable to"""
self.assertIsInstance(
self.sample_food_orders.food_order_count, int,
msg='Class does not initialize with a valid order coount'
msg="Class does not initialize with a valid order count"
)

def test_fetch_all_food_orders_return_value(self):
""" Test that fetch_all_food_orders() returns a dictionary """
self.assertIsInstance(
self.sample_food_orders.fetch_all_food_orders(), dict,
msg='Method should return a dict'
msg="Method should return a dict"
)

def test_place_new_order_only_accepts_valid_data(self):
Expand All @@ -43,16 +43,68 @@ def test_place_new_order_only_accepts_valid_data(self):
"""
self.assertRaises(
TypeError, self.sample_food_orders.place_new_order, 1,
'Argument should be a dictionary'
"Argument should be a dictionary"
)

self.assertIn(
'Order placement message',
"Order placement message",
self.sample_food_orders.place_new_order(
self.sample_order_request_info
)
)

def test_fetch_one_order_returns_dict(self):
""" Test that fetch_one_order() returns dict """
self.sample_food_orders.place_new_order(
self.sample_order_request_info
)

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

def test_fetch_one_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.fetch_order_by_id(2),
{"Order fetching error message": "orderid out of range"},
msg="Method does not handle out of range orders"
)

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

def test_fetch_one_order_with_non_int_orderid(self):
""" Test that invalid (non-int) orderid returns custom error message
(only positive int)
"""
self.sample_food_orders.place_new_order(
self.sample_order_request_info
)

self.assertDictEqual(
self.sample_food_orders.fetch_order_by_id('2.0'),
{"Order fetching error message": "orderid should be integer"},
msg="Method does not handle non-integers for orderid"
)

self.assertDictEqual(
self.sample_food_orders.fetch_order_by_id('one'),
{"Order fetching error message": "orderid should be integer"},
msg="Method does not handle non-integers for orderid"
)


if __name__ == '__main__':
unittest.main()
39 changes: 39 additions & 0 deletions app/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,45 @@ def test_payload_to_place_a_new_order(self):
)
self.assertIn(b'Sorry.... Order placement Failed', test_resp.data)

def test_fetch_one_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.get(
'/api/v1/orders/1',
headers={'content-type': 'application/json'}
)

self.assertEqual(
test_resp.status_code, 200, msg='Expected 200'
)

def test_fetch_one_order_operation_with_malformed_route(self):
""" Test that a path with an error(non-existent) returns an appropriate
error message in JSON and HTTP response code of 404(NOT FOUND)
"""
self.app.post(
'/api/v1/orders',
data=json.dumps(self.sample_order_request_info),
headers={'content-type': 'application/json'}
)

test_resp = self.app.get(
'/api/v1/orders/one',
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()
8 changes: 8 additions & 0 deletions app/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,11 @@ def place_new_order():
return jsonify(SAMPLE_FOOD_ORDERS.place_new_order(req_data))
else:
return jsonify('Sorry.... Order placement Failed')

@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>"""
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"})

0 comments on commit c9b123b

Please sign in to comment.