Spaces:
Paused
Paused
| import pytest | |
| from httpx import AsyncClient | |
| from sqlalchemy.ext.asyncio import AsyncSession | |
| pytestmark = pytest.mark.asyncio | |
| async def user_token(client: AsyncClient) -> str: | |
| """Create a test user and return their token""" | |
| # Register user | |
| await client.post( | |
| "/api/v1/auth/register", | |
| data={ | |
| "username": "testuser@example.com", | |
| "password": "testpass123", | |
| "full_name": "Test User" | |
| } | |
| ) | |
| # Login to get token | |
| response = await client.post( | |
| "/api/v1/auth/login", | |
| data={ | |
| "username": "testuser@example.com", | |
| "password": "testpass123" | |
| } | |
| ) | |
| return response.json()["access_token"] | |
| async def test_product(client: AsyncClient, user_token: str) -> dict: | |
| """Create a test product and return its data""" | |
| response = await client.post( | |
| "/api/v1/products/", | |
| json={ | |
| "name": "Test Product", | |
| "description": "A test product", | |
| "price": 99.99, | |
| "category": "test", | |
| "inventory_count": 10 | |
| }, | |
| headers={"Authorization": f"Bearer {user_token}"} | |
| ) | |
| return response.json() | |
| async def test_create_order(client: AsyncClient, user_token: str, test_product: dict): | |
| # Test creating an order | |
| response = await client.post( | |
| "/api/v1/orders/", | |
| json={ | |
| "items": [ | |
| { | |
| "product_id": test_product["id"], | |
| "quantity": 2 | |
| } | |
| ] | |
| }, | |
| headers={"Authorization": f"Bearer {user_token}"} | |
| ) | |
| assert response.status_code == 200 | |
| data = response.json() | |
| assert len(data["items"]) == 1 | |
| assert data["items"][0]["product_id"] == test_product["id"] | |
| assert data["total_amount"] == test_product["price"] * 2 | |
| return data["id"] | |
| async def test_get_order(client: AsyncClient, user_token: str, test_product: dict): | |
| # First create an order | |
| order_id = await test_create_order(client, user_token, test_product) | |
| # Test getting the order | |
| response = await client.get( | |
| f"/api/v1/orders/{order_id}", | |
| headers={"Authorization": f"Bearer {user_token}"} | |
| ) | |
| assert response.status_code == 200 | |
| data = response.json() | |
| assert data["id"] == order_id | |
| assert len(data["items"]) == 1 | |
| async def test_list_orders(client: AsyncClient, user_token: str, test_product: dict): | |
| # Create multiple orders | |
| await test_create_order(client, user_token, test_product) | |
| await test_create_order(client, user_token, test_product) | |
| # Test listing orders | |
| response = await client.get( | |
| "/api/v1/orders/", | |
| headers={"Authorization": f"Bearer {user_token}"} | |
| ) | |
| assert response.status_code == 200 | |
| data = response.json() | |
| assert len(data) >= 2 | |
| async def test_update_order_status(client: AsyncClient, user_token: str, test_product: dict): | |
| # First create an order | |
| order_id = await test_create_order(client, user_token, test_product) | |
| # Test updating the order status | |
| response = await client.put( | |
| f"/api/v1/orders/{order_id}/status", | |
| json={"status": "processing"}, | |
| headers={"Authorization": f"Bearer {user_token}"} | |
| ) | |
| assert response.status_code == 200 | |
| data = response.json() | |
| assert data["status"] == "processing" | |
| # Test invalid status | |
| response = await client.put( | |
| f"/api/v1/orders/{order_id}/status", | |
| json={"status": "invalid_status"}, | |
| headers={"Authorization": f"Bearer {user_token}"} | |
| ) | |
| assert response.status_code == 400 | |
| async def test_cancel_order(client: AsyncClient, user_token: str, test_product: dict): | |
| # First create an order | |
| order_id = await test_create_order(client, user_token, test_product) | |
| # Get initial product inventory | |
| product_response = await client.get( | |
| f"/api/v1/products/{test_product['id']}", | |
| headers={"Authorization": f"Bearer {user_token}"} | |
| ) | |
| initial_inventory = product_response.json()["inventory_count"] | |
| # Cancel the order | |
| response = await client.put( | |
| f"/api/v1/orders/{order_id}/status", | |
| json={"status": "cancelled"}, | |
| headers={"Authorization": f"Bearer {user_token}"} | |
| ) | |
| assert response.status_code == 200 | |
| assert response.json()["status"] == "cancelled" | |
| # Verify inventory was restored | |
| product_response = await client.get( | |
| f"/api/v1/products/{test_product['id']}", | |
| headers={"Authorization": f"Bearer {user_token}"} | |
| ) | |
| final_inventory = product_response.json()["inventory_count"] | |
| assert final_inventory == initial_inventory + 2 # We ordered 2 items |