Skip to content

Commit 7f8c78a

Browse files
committed
Add tests
1 parent 80e8cc2 commit 7f8c78a

4 files changed

Lines changed: 42 additions & 0 deletions

File tree

tests/api/__init__.py

Whitespace-only changes.

tests/api/test_users.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import httpx
2+
import pytest
3+
4+
from app.main import app
5+
6+
7+
@pytest.mark.asyncio
8+
async def test_create_user():
9+
transport = httpx.ASGITransport(app=app)
10+
async with httpx.AsyncClient(transport=transport, base_url="http://test") as ac:
11+
response = await ac.post("/api/v1/users/", json={"name": "Simone", "email": "simone@example.com"})
12+
assert response.status_code == 200
13+
data = response.json()
14+
assert data["name"] == "Simone"
15+
assert data["email"] == "simone@example.com"
16+
17+
@pytest.mark.asyncio
18+
async def test_get_users():
19+
transport = httpx.ASGITransport(app=app)
20+
async with httpx.AsyncClient(transport=transport, base_url="http://test") as ac:
21+
response = await ac.get("/api/v1/users/")
22+
assert response.status_code == 200
23+
data = response.json()
24+
assert isinstance(data, list)

tests/services/__init__.py

Whitespace-only changes.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import pytest
2+
from app.models.user import UserCreate
3+
from app.services.user_service import save_user, get_all_users
4+
5+
6+
@pytest.mark.asyncio
7+
async def test_save_user():
8+
user = UserCreate(name="Simone", email="simone@example.com")
9+
saved_user = await save_user(user)
10+
assert saved_user.name == "Simone"
11+
assert saved_user.email == "simone@example.com"
12+
13+
14+
@pytest.mark.asyncio
15+
async def test_get_all_users_empty():
16+
users = await get_all_users()
17+
assert isinstance(users, list)
18+
assert len(users) > 0

0 commit comments

Comments
 (0)