diff --git a/service/routes.py b/service/routes.py index e85e5fc..8ccfbd5 100644 --- a/service/routes.py +++ b/service/routes.py @@ -190,7 +190,7 @@ def update_shopcarts(shopcart_id): # ADD AN ITEM TO A SHOPCART ###################################################################### @app.route("/shopcarts//items", methods=["POST"]) -def create_items(shopcart_id): +def create_item(shopcart_id): """ Create an Item on an Shopcart @@ -204,7 +204,7 @@ def create_items(shopcart_id): if not shopcart: abort( status.HTTP_404_NOT_FOUND, - f"Shopcart with id '{shopcart_id}' could not be found.", + f"Shopcart with id '{shopcart_id}' was not found.", ) # Create an item from the json data @@ -225,14 +225,14 @@ def create_items(shopcart_id): # RETRIEVE AN ITEM FROM A SHOPCART ###################################################################### @app.route("/shopcarts//items/", methods=["GET"]) -def get_items(shopcart_id, item_id): +def get_item(shopcart_id, item_id): """ Get an Item This endpoint returns just an item """ app.logger.info( - "Request to retrieve Item %s for Account id: %s", (item_id, shopcart_id) + "Request to retrieve Item %s for Shopcart id: %s", (item_id, shopcart_id) ) # See if the item exists and abort if it doesn't @@ -240,7 +240,7 @@ def get_items(shopcart_id, item_id): if not item: abort( status.HTTP_404_NOT_FOUND, - f"Account with id '{item_id}' could not be found.", + f"Item with id '{item_id}' was not found.", ) return jsonify(item.serialize()), status.HTTP_200_OK @@ -299,7 +299,7 @@ def list_items(shopcart_id): if not shopcart: abort( status.HTTP_404_NOT_FOUND, - f"Account with id '{shopcart_id}' could not be found.", + f"Shopcart with id '{shopcart_id}' was not found.", ) # See if any query filters were passed in diff --git a/tests/test_routes.py b/tests/test_routes.py index 414a32e..700d483 100644 --- a/tests/test_routes.py +++ b/tests/test_routes.py @@ -113,11 +113,14 @@ def test_update_shopcart(self): test_shopcart = ShopcartFactory() response = self.client.post(BASE_URL, json=test_shopcart.serialize()) self.assertEqual(response.status_code, status.HTTP_201_CREATED) - # update the shopcart new_shopcart = response.get_json() logging.debug(new_shopcart) new_shopcart["user_id"] = "123" + # test that we cannot update a shopcart with the shopcart id that DNE + response = self.client.put(f"{BASE_URL}/0", json=new_shopcart) + self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) + # test that we can actually update the desired shopcart when needed response = self.client.put( f"{BASE_URL}/{new_shopcart['id']}", json=new_shopcart ) @@ -147,6 +150,14 @@ def test_add_item(self): """It should Add an item to an shopcart""" shopcart = self._create_shopcarts(1)[0] item = ItemFactory() + # test that we cannot create an item with the shopcart id that DNE + resp = self.client.post( + f"{BASE_URL}/0/items", + json=item.serialize(), + content_type="application/json", + ) + self.assertEqual(resp.status_code, status.HTTP_404_NOT_FOUND) + resp = self.client.post( f"{BASE_URL}/{shopcart.id}/items", json=item.serialize(), @@ -197,10 +208,9 @@ def test_get_item(self): def test_update_shopcart_item(self): """It should Update an existing item in a Shopcart""" # create a shopcart with an item to update - shopcart = ShopcartFactory() - shopcart.create() - item = ItemFactory(shopcart=shopcart) - item.create() + # create a shopcart and an item + shopcart = self._create_shopcarts(1)[0] + item = ItemFactory() response = self.client.post( f"{BASE_URL}/{shopcart.id}/items", json=item.serialize(), @@ -214,6 +224,24 @@ def test_update_shopcart_item(self): logging.debug(data) product_name = data["product_name"] item_id = data["id"] + + # test for the shopcart ID being invalid + resp = self.client.put( + f"{BASE_URL}/0/items/{item_id}", + json=data, + content_type="application/json", + ) + self.assertEqual(resp.status_code, status.HTTP_404_NOT_FOUND) + + dummy_item_id = item_id + 1 + shopcart_id = shopcart.id + # test for the item ID being invalid + resp = self.client.put( + f"{BASE_URL}/{shopcart_id}/items/{dummy_item_id}", + json=data, + content_type="application/json", + ) + self.assertEqual(resp.status_code, status.HTTP_404_NOT_FOUND) data["product_name"] = "Updated Item Name" # send the update back @@ -321,7 +349,7 @@ def test_clear_shopcart(self): self.assertEqual(resp.data, b"") resp = self.client.delete( - f"{BASE_URL}/-1/clear", + f"{BASE_URL}/0/clear", content_type="application/json", ) self.assertEqual(resp.status_code, status.HTTP_404_NOT_FOUND) @@ -350,10 +378,9 @@ def test_query_shopcart_list_by_total_price(self): def test_increment_quantity_by_one(self): """It should increment quantity by one""" # add four items to the shopcart - shopcart = ShopcartFactory() - shopcart.create() - item = ItemFactory(shopcart=shopcart) - item.create() + # create a shopcart and an item + shopcart = self._create_shopcarts(1)[0] + item = ItemFactory() # let's commit the newly created item to the DB initial_quantity = item.quantity resp = self.client.post( @@ -361,34 +388,33 @@ def test_increment_quantity_by_one(self): ) self.assertEqual(resp.status_code, status.HTTP_201_CREATED) - # let's get the item - resp = self.client.get(f"{BASE_URL}/{shopcart.id}/items") - self.assertEqual(resp.status_code, status.HTTP_200_OK) + data = resp.json + item_id = data["id"] resp = self.client.put( - f"{BASE_URL}/{shopcart.id}/items/{item.id}/increment", json=item.serialize() + f"{BASE_URL}/{shopcart.id}/items/{item_id}/increment", json=item.serialize() ) - new_quantity = item.quantity - self.assertEqual(new_quantity, initial_quantity + 1) self.assertEqual(resp.status_code, status.HTTP_200_OK) + data = resp.json + new_quantity = data["quantity"] + self.assertEqual(new_quantity, initial_quantity + 1) resp = self.client.put( - f"{BASE_URL}/-1/items/{item.id}/increment", json=item.serialize() + f"{BASE_URL}/0/items/{item.id}/increment", json=item.serialize() ) self.assertEqual(resp.status_code, status.HTTP_404_NOT_FOUND) resp = self.client.put( - f"{BASE_URL}/{shopcart.id}/items/-1/increment", json=item.serialize() + f"{BASE_URL}/{shopcart.id}/items/0/increment", json=item.serialize() ) self.assertEqual(resp.status_code, status.HTTP_404_NOT_FOUND) def test_decrement_quantity_by_one(self): """It should decrement quantity by one""" # add four items to the shopcart - shopcart = ShopcartFactory() - shopcart.create() - item = ItemFactory(shopcart=shopcart) - item.create() + # create a shopcart and an item + shopcart = self._create_shopcarts(1)[0] + item = ItemFactory() # let's commit the newly created item to the DB initial_quantity = item.quantity resp = self.client.post( @@ -396,24 +422,24 @@ def test_decrement_quantity_by_one(self): ) self.assertEqual(resp.status_code, status.HTTP_201_CREATED) - # let's get the item - resp = self.client.get(f"{BASE_URL}/{shopcart.id}/items") - self.assertEqual(resp.status_code, status.HTTP_200_OK) + data = resp.json + item_id = data["id"] resp = self.client.put( - f"{BASE_URL}/{shopcart.id}/items/{item.id}/decrement", json=item.serialize() + f"{BASE_URL}/{shopcart.id}/items/{item_id}/decrement", json=item.serialize() ) - new_quantity = item.quantity - self.assertEqual(new_quantity, initial_quantity - 1) self.assertEqual(resp.status_code, status.HTTP_200_OK) + data = resp.json + new_quantity = data["quantity"] + self.assertEqual(new_quantity, initial_quantity - 1) resp = self.client.put( - f"{BASE_URL}/-1/items/{item.id}/decrement", json=item.serialize() + f"{BASE_URL}/0/items/{item.id}/decrement", json=item.serialize() ) self.assertEqual(resp.status_code, status.HTTP_404_NOT_FOUND) resp = self.client.put( - f"{BASE_URL}/{shopcart.id}/items/-1/decrement", json=item.serialize() + f"{BASE_URL}/{shopcart.id}/items/0/decrement", json=item.serialize() ) self.assertEqual(resp.status_code, status.HTTP_404_NOT_FOUND) @@ -435,7 +461,7 @@ def test_create_shopcart_no_data(self): def test_get_shopcart_not_found(self): """It should not Get a Shopcart that's not found""" - response = self.client.get(f"{BASE_URL}/-1") + response = self.client.get(f"{BASE_URL}/0") self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) data = response.get_json() logging.debug("Response data = %s", data) @@ -459,7 +485,7 @@ def test_get_shopcart_with_method_not_allowed(self): def test_get_items_when_shopcart_not_found(self): """It should not Get any items from a shopcart that's not found""" - response = self.client.get(f"{BASE_URL}/-1/items") + response = self.client.get(f"{BASE_URL}/0/items") self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) data = response.get_json() logging.debug("Response data = %s", data)