Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add update total price func and add related test cases #59

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions service/models/shop_cart.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,18 @@ def deserialize(self, data):
) from error
return self

def update_total_price(self):
"""
update the total price of a ShopCart
"""
price = 0
for item in self.items:
if item.price is not None and item.quantity is not None:
price += item.price * item.quantity

self.total_price = price
self.update()

##################################################
# CLASS METHODS
##################################################
Expand Down
19 changes: 19 additions & 0 deletions service/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@
# Append item to the shopcart
shopcart.items.append(item)
shopcart.update()
# update the total price
shopcart.update_total_price()

# Create a message to return
message = item.serialize()
Expand Down Expand Up @@ -243,6 +245,15 @@
if item:
item.delete()

# update the total price
shopcart = ShopCart.find(shopcart_id)
if not shopcart:
abort(

Check warning on line 251 in service/routes.py

View check run for this annotation

Codecov / codecov/patch

service/routes.py#L251

Added line #L251 was not covered by tests
status.HTTP_404_NOT_FOUND,
f"ShopCart with ID '{shopcart_id}' could not be found",
)
shopcart.update_total_price()

return "", status.HTTP_204_NO_CONTENT


Expand Down Expand Up @@ -274,6 +285,14 @@
item.id = item_id
item.update()

shopcart = ShopCart.find(shopcart_id)
if not shopcart:
abort(

Check warning on line 290 in service/routes.py

View check run for this annotation

Codecov / codecov/patch

service/routes.py#L290

Added line #L290 was not covered by tests
status.HTTP_404_NOT_FOUND,
f"ShopCart with ID '{shopcart_id}' could not be found",
)
shopcart.update_total_price()

return jsonify(item.serialize()), status.HTTP_200_OK


Expand Down
115 changes: 115 additions & 0 deletions tests/test_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,3 +473,118 @@ def test_bad_request(self):
"""It should not Create when sending the wrong data"""
resp = self.client.post(BASE_URL, json={"name": "not enough data"})
self.assertEqual(resp.status_code, status.HTTP_400_BAD_REQUEST)

def test_shopcart_total_price_with_new_item(self):
"""
It should update the shopcart total price
once a new item is added
"""
shop_cart = self._create_shopcarts(1)[0]
item_1 = ShopCartItemFactory()
resp = self.client.post(
f"{BASE_URL}/{shop_cart.id}/items",
json=item_1.serialize(),
content_type="application/json",
)
self.assertEqual(resp.status_code, status.HTTP_201_CREATED)

item_2 = ShopCartItemFactory()
resp = self.client.post(
f"{BASE_URL}/{shop_cart.id}/items",
json=item_2.serialize(),
content_type="application/json",
)
self.assertEqual(resp.status_code, status.HTTP_201_CREATED)

resp = self.client.get(
f"{BASE_URL}/{shop_cart.id}", content_type="application/json"
)
self.assertEqual(resp.status_code, status.HTTP_200_OK)
data = resp.get_json()
self.assertEqual(
data["total_price"],
str(item_1.price * item_1.quantity + item_2.price * item_2.quantity),
)

def test_shopcart_total_price_with_delete_item(self):
"""
It should update the shopcart total price
once an item is deleted
"""
shopcart = self._create_shopcarts(1)[0]
# add the first item
item = ShopCartItemFactory()
resp = self.client.post(
f"{BASE_URL}/{shopcart.id}/items",
json=item.serialize(),
content_type="application/json",
)
self.assertEqual(resp.status_code, status.HTTP_201_CREATED)
data = resp.get_json()
item_id = data["id"]

# add the second item
item_2 = ShopCartItemFactory()
resp = self.client.post(
f"{BASE_URL}/{shopcart.id}/items",
json=item_2.serialize(),
content_type="application/json",
)
self.assertEqual(resp.status_code, status.HTTP_201_CREATED)

# send delete request to delete the first item
resp = self.client.delete(
f"{BASE_URL}/{shopcart.id}/items/{item_id}",
content_type="application/json",
)
self.assertEqual(resp.status_code, status.HTTP_204_NO_CONTENT)

resp = self.client.get(
f"{BASE_URL}/{shopcart.id}/items/{item_id}",
content_type="application/json",
)
self.assertEqual(resp.status_code, status.HTTP_404_NOT_FOUND)

# the total price should equal to the second item's total price
resp = self.client.get(
f"{BASE_URL}/{shopcart.id}", content_type="application/json"
)
self.assertEqual(resp.status_code, status.HTTP_200_OK)
data = resp.get_json()
self.assertEqual(data["total_price"], str(item_2.price * item_2.quantity))

def test_shopcart_total_price_with_update_item(self):
"""
It should update the shopcart total price
once an item is updated
"""
# create a known item
shopcart = self._create_shopcarts(1)[0]
item = ShopCartItemFactory()
resp = self.client.post(
f"{BASE_URL}/{shopcart.id}/items",
json=item.serialize(),
content_type="application/json",
)
self.assertEqual(resp.status_code, status.HTTP_201_CREATED)

data = resp.get_json()
logging.debug(data)
item_id = data["id"]
data["quantity"] = 3

# send the update back
resp = self.client.put(
f"{BASE_URL}/{shopcart.id}/items/{item_id}",
json=data,
content_type="application/json",
)
self.assertEqual(resp.status_code, status.HTTP_200_OK)

# check total price
resp = self.client.get(
f"{BASE_URL}/{shopcart.id}", content_type="application/json"
)
self.assertEqual(resp.status_code, status.HTTP_200_OK)
data = resp.get_json()
self.assertEqual(data["total_price"], str(item.price * 3))
Loading