File size: 1,538 Bytes
4192f93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
from httpx import AsyncClient
import pytest

from app.core.config import settings


@pytest.mark.asyncio
async def test_users_crud(httpx_async_client: AsyncClient):
    # Test CREATE
    user_data = {
        "name": "John Doe",
        "email": "john.doe@example.com",
        "password": "password123",
    }
    response = await httpx_async_client.post(
        f"{settings.API_V1_STR}/users/",
        data=json.dumps(user_data),
        headers={"Content-Type": "application/json"},
    )
    assert response.status_code == 200
    user_id = response.json()["user_id"]

    # Test READ
    response = await httpx_async_client.get(f"{settings.API_V1_STR}/users/{user_id}")
    assert response.status_code == 200
    user = response.json()
    assert user["name"] == user_data["name"]
    assert user["email"] == user_data["email"]
    assert "password" not in user

    # Test UPDATE
    updated_user_data = {
        "name": "Jane Doe",
    }
    response = await httpx_async_client.put(
        f"{settings.API_V1_STR}/users/{user_id}",
        data=json.dumps(updated_user_data),
        headers={"Content-Type": "application/json"},
    )
    assert response.status_code == 200
    updated_user = response.json()
    assert updated_user["name"] == updated_user_data["name"]
    assert updated_user["email"] == user_data["email"]
    assert "password" not in updated_user

    # Test DELETE
    response = await httpx_async_client.delete(f"{settings.API_V1_STR}/users/{user_id}")
    assert response.status_code == 200