From bca5672b7fb571456ea9f9442f1b3f6b246ef99a Mon Sep 17 00:00:00 2001
From: duyanhehe <duyanhex@gmail.com>
Date: Sun, 27 Apr 2025 17:21:16 +0700
Subject: [PATCH] add test cart

---
 app/tests/test_cart.py | 221 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 221 insertions(+)
 create mode 100644 app/tests/test_cart.py

diff --git a/app/tests/test_cart.py b/app/tests/test_cart.py
new file mode 100644
index 0000000..dcfb578
--- /dev/null
+++ b/app/tests/test_cart.py
@@ -0,0 +1,221 @@
+import pytest
+from fastapi.testclient import TestClient
+from sqlmodel import Session
+from app.backend.models.models import User, Shop, Product, Cart, CartItem, Payment
+from app.backend.utils.hashing import hash_password, create_access_token
+
+
+@pytest.fixture
+def test_user(db_session: Session):
+    """Create a test user"""
+    user = User(
+        username="testuser",
+        email="test@example.com",
+        password=hash_password("testpass123"),
+        phone_number="1234567890",
+        role="buyer",
+    )
+    db_session.add(user)
+    db_session.commit()
+    db_session.refresh(user)
+    return user
+
+
+@pytest.fixture
+def test_shop(db_session: Session):
+    """Create a test shop"""
+    shop = Shop(
+        owner_id=1,  # This will match with test_user's ID
+        name="Test Shop",
+        description="Test Shop Description",
+        address="123 Test St",
+        latitude=51.5074,
+        longitude=-0.1278,
+    )
+    db_session.add(shop)
+    db_session.commit()
+    db_session.refresh(shop)
+    return shop
+
+
+@pytest.fixture
+def test_product(db_session: Session, test_shop):
+    """Create a test product"""
+    product = Product(
+        shop_id=test_shop.id,
+        name="Test Product",
+        description="Test Product Description",
+        price=9.99,
+        stock=10,
+    )
+    db_session.add(product)
+    db_session.commit()
+    db_session.refresh(product)
+    return product
+
+
+@pytest.fixture
+def test_payment(db_session: Session, test_user):
+    """Create a test payment method"""
+    payment = Payment(
+        user_id=test_user.id,
+        payment_method="Test Card",
+        card_number="4111111111111111",
+        expiry_date="12/25",
+        cvv="123",
+    )
+    db_session.add(payment)
+    db_session.commit()
+    db_session.refresh(payment)
+    return payment
+
+
+@pytest.fixture
+def auth_headers(test_user):
+    """Create authentication headers"""
+    token = create_access_token(data={"sub": str(test_user.id)})
+    return {"Authorization": f"Bearer {token}"}
+
+
+def test_add_to_cart(client: TestClient, test_product, auth_headers):
+    """Test adding an item to cart"""
+    response = client.post(
+        "/cart/add",
+        headers=auth_headers,
+        json={
+            "product_id": test_product.id,
+            "shop_id": test_product.shop_id,
+            "quantity": 2,
+        },
+    )
+    assert response.status_code == 200
+    assert response.json()["message"] == "Item added to cart successfully"
+
+
+def test_get_cart_items(client: TestClient, auth_headers, test_product):
+    """Test getting cart items"""
+    # First add an item to cart
+    client.post(
+        "/cart/add",
+        headers=auth_headers,
+        json={
+            "product_id": test_product.id,
+            "shop_id": test_product.shop_id,
+            "quantity": 2,
+        },
+    )
+
+    # Then get cart items
+    response = client.get("/cart/items", headers=auth_headers)
+    assert response.status_code == 200
+    data = response.json()
+    assert "items" in data
+    assert len(data["items"]) == 1
+    assert data["items"][0]["product_id"] == test_product.id
+    assert data["items"][0]["quantity"] == 2
+
+
+def test_update_cart_item(client: TestClient, auth_headers, test_product):
+    """Test updating cart item quantity"""
+    # First add an item to cart
+    add_response = client.post(
+        "/cart/add",
+        headers=auth_headers,
+        json={
+            "product_id": test_product.id,
+            "shop_id": test_product.shop_id,
+            "quantity": 2,
+        },
+    )
+
+    # Get cart items to get the cart item ID
+    cart_response = client.get("/cart/items", headers=auth_headers)
+    cart_item_id = cart_response.json()["items"][0]["id"]
+
+    # Update quantity
+    response = client.put(
+        f"/cart/update/{cart_item_id}", headers=auth_headers, json={"quantity": 3}
+    )
+    assert response.status_code == 200
+    assert response.json()["message"] == "Cart item updated successfully"
+
+
+def test_remove_from_cart(client: TestClient, auth_headers, test_product):
+    """Test removing item from cart"""
+    # First add an item to cart
+    client.post(
+        "/cart/add",
+        headers=auth_headers,
+        json={
+            "product_id": test_product.id,
+            "shop_id": test_product.shop_id,
+            "quantity": 2,
+        },
+    )
+
+    # Get cart items to get the cart item ID
+    cart_response = client.get("/cart/items", headers=auth_headers)
+    cart_item_id = cart_response.json()["items"][0]["id"]
+
+    # Remove item
+    response = client.delete(f"/cart/remove/{cart_item_id}", headers=auth_headers)
+    assert response.status_code == 200
+    assert response.json()["message"] == "Item removed from cart successfully"
+
+
+def test_checkout_cart(client: TestClient, auth_headers, test_product, test_payment):
+    """Test cart checkout"""
+    # First add an item to cart
+    client.post(
+        "/cart/add",
+        headers=auth_headers,
+        json={
+            "product_id": test_product.id,
+            "shop_id": test_product.shop_id,
+            "quantity": 2,
+        },
+    )
+
+    # Get cart items to get the cart item ID
+    cart_response = client.get("/cart/items", headers=auth_headers)
+    cart_item_id = cart_response.json()["items"][0]["id"]
+
+    # Checkout
+    response = client.post(
+        "/cart/checkout",
+        headers=auth_headers,
+        json={
+            "delivery_address": "123 Test St, London, UK",
+            "payment_id": test_payment.id,
+            "selected_items": [cart_item_id],
+        },
+    )
+    assert response.status_code == 200
+    assert "order_ids" in response.json()
+    assert len(response.json()["order_ids"]) == 1
+
+
+def test_add_to_cart_invalid_product(client: TestClient, auth_headers):
+    """Test adding non-existent product to cart"""
+    response = client.post(
+        "/cart/add",
+        headers=auth_headers,
+        json={"product_id": 999, "shop_id": 1, "quantity": 2},
+    )
+    assert response.status_code == 404
+    assert response.json()["detail"] == "Product not found"
+
+
+def test_add_to_cart_insufficient_stock(client: TestClient, auth_headers, test_product):
+    """Test adding product with insufficient stock"""
+    response = client.post(
+        "/cart/add",
+        headers=auth_headers,
+        json={
+            "product_id": test_product.id,
+            "shop_id": test_product.shop_id,
+            "quantity": 100,  # More than available stock
+        },
+    )
+    assert response.status_code == 400
+    assert response.json()["detail"] == "Not enough stock available"
-- 
GitLab