Skip to content
This repository was archived by the owner on Dec 24, 2024. It is now read-only.

Commit 5102519

Browse files
committed
feat: complete e2e tests
1 parent ac67637 commit 5102519

File tree

4 files changed

+131
-1
lines changed

4 files changed

+131
-1
lines changed

Pipfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ rich = "*"
1515
pylint = "*"
1616
mypy = "*"
1717
pytest = "*"
18+
httpx = "*"
1819

1920
[requires]
2021
python_version = "3.12"

Pipfile.lock

Lines changed: 58 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/routers/users.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ async def users_list(
4545
return storage.all()
4646

4747

48+
@router.post("/", tags=["users"])
4849
async def users_new(
4950
storage: typing.Annotated[services.Storage, fastapi.Depends()],
5051
user: User,

app/test_main.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
"""
2+
End-to-end testings for APIs.
3+
"""
4+
5+
from fastapi.testclient import TestClient
6+
7+
from .main import app
8+
9+
client = TestClient(app)
10+
11+
12+
def test_users_create_and_list():
13+
"""
14+
Create valid users and make sure it returns in the users list.
15+
"""
16+
17+
future_users = [
18+
{
19+
"first_name": "Parham",
20+
"last_name": "Alvani",
21+
"age": 30,
22+
},
23+
{
24+
"first_name": "Elahe",
25+
"last_name": "Dastan",
26+
"age": 20,
27+
},
28+
{
29+
"first_name": "Seyed Parham",
30+
"last_name": "Alvani",
31+
"age": 30,
32+
},
33+
]
34+
35+
for user in future_users:
36+
response = client.post(
37+
"/users",
38+
json=user,
39+
)
40+
assert response.status_code == 200
41+
assert response.json() == user
42+
43+
response = client.get("/users")
44+
assert response.status_code == 200
45+
users = response.json()
46+
assert len(users) == len(future_users)
47+
assert users == future_users
48+
49+
50+
def test_users_bad_create():
51+
"""
52+
Create a invalid user and make sure it returns error.
53+
"""
54+
55+
for user in [
56+
{
57+
"first_name": "Parham1",
58+
"last_name": "Alvani",
59+
"age": 30,
60+
},
61+
{
62+
"first_name": "Parham",
63+
"last_name": "Alvani",
64+
"age": -30,
65+
},
66+
]:
67+
response = client.post(
68+
"/users",
69+
json=user,
70+
)
71+
assert response.status_code == 422

0 commit comments

Comments
 (0)