Spaces:
Sleeping
Sleeping
Praneeth Yerrapragada
commited on
Commit
•
4192f93
1
Parent(s):
312b13f
test: pytests for users crud
Browse files- tests/test_postgresdb_connection.py +0 -15
- tests/test_users_crud.py +49 -0
tests/test_postgresdb_connection.py
DELETED
@@ -1,15 +0,0 @@
|
|
1 |
-
import pytest
|
2 |
-
from app.engine.postgresdb import get_db
|
3 |
-
|
4 |
-
|
5 |
-
@pytest.mark.skip
|
6 |
-
def test_postgres_db_connection():
|
7 |
-
"""
|
8 |
-
Tests the connection to the postgres db
|
9 |
-
"""
|
10 |
-
with get_db() as session:
|
11 |
-
# Execute a query to test the connection
|
12 |
-
result = session.execute(f"SELECT 1").scalar()
|
13 |
-
|
14 |
-
# Assert that the result is equal to 1
|
15 |
-
assert result == 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
tests/test_users_crud.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
from httpx import AsyncClient
|
3 |
+
import pytest
|
4 |
+
|
5 |
+
from app.core.config import settings
|
6 |
+
|
7 |
+
|
8 |
+
@pytest.mark.asyncio
|
9 |
+
async def test_users_crud(httpx_async_client: AsyncClient):
|
10 |
+
# Test CREATE
|
11 |
+
user_data = {
|
12 |
+
"name": "John Doe",
|
13 |
+
"email": "john.doe@example.com",
|
14 |
+
"password": "password123",
|
15 |
+
}
|
16 |
+
response = await httpx_async_client.post(
|
17 |
+
f"{settings.API_V1_STR}/users/",
|
18 |
+
data=json.dumps(user_data),
|
19 |
+
headers={"Content-Type": "application/json"},
|
20 |
+
)
|
21 |
+
assert response.status_code == 200
|
22 |
+
user_id = response.json()["user_id"]
|
23 |
+
|
24 |
+
# Test READ
|
25 |
+
response = await httpx_async_client.get(f"{settings.API_V1_STR}/users/{user_id}")
|
26 |
+
assert response.status_code == 200
|
27 |
+
user = response.json()
|
28 |
+
assert user["name"] == user_data["name"]
|
29 |
+
assert user["email"] == user_data["email"]
|
30 |
+
assert "password" not in user
|
31 |
+
|
32 |
+
# Test UPDATE
|
33 |
+
updated_user_data = {
|
34 |
+
"name": "Jane Doe",
|
35 |
+
}
|
36 |
+
response = await httpx_async_client.put(
|
37 |
+
f"{settings.API_V1_STR}/users/{user_id}",
|
38 |
+
data=json.dumps(updated_user_data),
|
39 |
+
headers={"Content-Type": "application/json"},
|
40 |
+
)
|
41 |
+
assert response.status_code == 200
|
42 |
+
updated_user = response.json()
|
43 |
+
assert updated_user["name"] == updated_user_data["name"]
|
44 |
+
assert updated_user["email"] == user_data["email"]
|
45 |
+
assert "password" not in updated_user
|
46 |
+
|
47 |
+
# Test DELETE
|
48 |
+
response = await httpx_async_client.delete(f"{settings.API_V1_STR}/users/{user_id}")
|
49 |
+
assert response.status_code == 200
|