Admin-Desk / tests /utils.py
Fred808's picture
Upload 7 files
4c5298a verified
from typing import Dict, Any
from httpx import AsyncClient
import json
async def create_user_and_login(
client: AsyncClient,
email: str = "testuser@example.com",
password: str = "testpass123",
full_name: str = "Test User",
is_superuser: bool = False
) -> Dict[str, Any]:
"""Create a test user and return their token and data"""
# Register user
register_response = await client.post(
"/api/v1/auth/register",
json={
"email": email,
"password": password,
"full_name": full_name,
"is_superuser": is_superuser
}
)
user_data = register_response.json()
# Login to get token
login_response = await client.post(
"/api/v1/auth/login",
data={
"username": email,
"password": password
}
)
token_data = login_response.json()
return {
"token": token_data["access_token"],
"user": user_data
}
async def create_branch(
client: AsyncClient,
token: str,
name: str = "Test Branch",
location: str = "Test Location"
) -> Dict[str, Any]:
"""Create a test branch and return its data"""
response = await client.post(
"/api/v1/branches/",
json={
"name": name,
"location": location
},
headers={"Authorization": f"Bearer {token}"}
)
return response.json()
async def create_product(
client: AsyncClient,
token: str,
branch_id: int,
name: str = "Test Product",
price: float = 99.99,
inventory: int = 10
) -> Dict[str, Any]:
"""Create a test product and return its data"""
response = await client.post(
"/api/v1/products/",
json={
"name": name,
"description": f"Description for {name}",
"price": price,
"category": "test",
"inventory_count": inventory,
"branch_id": branch_id
},
headers={"Authorization": f"Bearer {token}"}
)
return response.json()
async def create_order(
client: AsyncClient,
token: str,
items: list,
branch_id: int
) -> Dict[str, Any]:
"""Create a test order and return its data"""
response = await client.post(
"/api/v1/orders/",
json={
"items": items,
"branch_id": branch_id
},
headers={"Authorization": f"Bearer {token}"}
)
return response.json()
def assert_response_matches_schema(response_data: Dict[str, Any], schema: Dict[str, Any]):
"""Assert that an API response matches its expected schema"""
assert all(key in response_data for key in schema.keys()), \
f"Missing required fields. Expected {schema.keys()}, got {response_data.keys()}"
for key, value_type in schema.items():
assert isinstance(response_data[key], value_type), \
f"Field {key} should be of type {value_type}, got {type(response_data[key])}"