File size: 3,086 Bytes
4c5298a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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])}"