Skip to content

Commit

Permalink
Merge pull request #86 from CSCI-GA-2820-SP24-001/fix-codecov
Browse files Browse the repository at this point in the history
Fixed codecov coverage by adding tests to the lines which were not covered before
  • Loading branch information
vidishaholsambre authored Apr 7, 2024
2 parents f3a5963 + a57ccdf commit 8af4f02
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 38 deletions.
12 changes: 6 additions & 6 deletions service/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ def update_shopcarts(shopcart_id):
# ADD AN ITEM TO A SHOPCART
######################################################################
@app.route("/shopcarts/<int:shopcart_id>/items", methods=["POST"])
def create_items(shopcart_id):
def create_item(shopcart_id):
"""
Create an Item on an Shopcart
Expand All @@ -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
Expand All @@ -225,22 +225,22 @@ def create_items(shopcart_id):
# RETRIEVE AN ITEM FROM A SHOPCART
######################################################################
@app.route("/shopcarts/<int:shopcart_id>/items/<int:item_id>", 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
item = Item.find(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
Expand Down Expand Up @@ -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
Expand Down
90 changes: 58 additions & 32 deletions tests/test_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -350,70 +378,68 @@ 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(
f"{BASE_URL}/{shopcart.id}/items", json=item.serialize()
)
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(
f"{BASE_URL}/{shopcart.id}/items", json=item.serialize()
)
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)

Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 8af4f02

Please sign in to comment.