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

feat: enhance cart API to return sorted items and total price calcula… #22

Merged
merged 1 commit into from
Jan 9, 2025
Merged
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
40 changes: 25 additions & 15 deletions backend/app/routes/cart.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,31 @@
@token_required
def get_cart(current_user):
try:
cart_items = Cart.query.filter_by(user_id=current_user.id).all()
return jsonify(
[
{
"id": item.id,
"product": {
"id": item.product_id,
"name": Product.query.get(item.product_id).name,
"price": Product.query.get(item.product_id).price,
},
"quantity": item.quantity,
}
for item in cart_items
]
)
cart_items = Cart.query.filter_by(user_id=current_user.id).order_by(Cart.id).all()

items = []
total = 0

for item in cart_items:
product = Product.query.get(item.product_id)
item_total = product.price * item.quantity
total += item_total

items.append({
"id": item.id,
"product": {
"id": item.product_id,
"name": product.name,
"price": product.price,
},
"quantity": item.quantity,
})

return jsonify({
"items": items,
"total": total
})

except Exception as e:
return jsonify(
{"message": "Failed to fetch cart items", "details": str(e)}
Expand Down
65 changes: 62 additions & 3 deletions backend/tests/test_cart.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import pytest
from flask import json
from app.models import Product, Cart


def test_get_cart(client, auth_headers, test_cart):
"""Test getting user's cart items."""
response = client.get("/api/cart/", headers=auth_headers)
assert response.status_code == 200
data = json.loads(response.data)
assert len(data) == 1
assert data[0]["quantity"] == test_cart.quantity
assert data[0]["product"]["id"] == test_cart.product_id
assert "items" in data
assert "total" in data
assert len(data["items"]) == 1
assert data["items"][0]["quantity"] == test_cart.quantity
assert data["items"][0]["product"]["id"] == test_cart.product_id
assert data["total"] == test_cart.quantity * Product.query.get(test_cart.product_id).price


def test_add_to_cart(client, auth_headers, test_products):
Expand Down Expand Up @@ -83,3 +87,58 @@ def test_cart_unauthorized(client):
"""Test accessing cart without authentication."""
response = client.get("/api/cart/")
assert response.status_code == 401

def test_get_cart_order_and_total(client, auth_headers, test_user, test_products, db_session):
"""Test cart sorting by ID and total price calculation."""

cart_items = [
Cart(user_id=test_user.id, product_id=test_products[0].id, quantity=2),
Cart(user_id=test_user.id, product_id=test_products[0].id, quantity=1),
Cart(user_id=test_user.id, product_id=test_products[1].id, quantity=3),
]

for item in cart_items:
db_session.add(item)
db_session.commit()

expected_total = (
test_products[0].price * 2 +
test_products[0].price * 1 +
test_products[1].price * 3
)

response = client.get("/api/cart/", headers=auth_headers)
assert response.status_code == 200
data = json.loads(response.data)

assert "items" in data
assert "total" in data

items = data["items"]
assert len(items) == 3
assert items[0]["product"]["id"] == test_products[0].id
assert items[1]["product"]["id"] == test_products[0].id
assert items[2]["product"]["id"] == test_products[1].id

assert data["total"] == expected_total

response = client.put(
f"/api/cart/update/{items[1]['id']}",
headers=auth_headers,
json={"quantity": 5}
)
assert response.status_code == 200

response = client.get("/api/cart/", headers=auth_headers)
data = json.loads(response.data)

assert data["items"][0]["product"]["id"] == test_products[0].id
assert data["items"][1]["product"]["id"] == test_products[0].id
assert data["items"][2]["product"]["id"] == test_products[1].id

new_expected_total = (
test_products[0].price * 2 +
test_products[0].price * 5 +
test_products[1].price * 3
)
assert data["total"] == new_expected_total
Loading