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

Commit

Permalink
feat: complete e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
1995parham committed Mar 10, 2024
1 parent ac67637 commit 5102519
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 1 deletion.
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ rich = "*"
pylint = "*"
mypy = "*"
pytest = "*"
httpx = "*"

[requires]
python_version = "3.12"
59 changes: 58 additions & 1 deletion Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions app/routers/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ async def users_list(
return storage.all()


@router.post("/", tags=["users"])
async def users_new(
storage: typing.Annotated[services.Storage, fastapi.Depends()],
user: User,
Expand Down
71 changes: 71 additions & 0 deletions app/test_main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
"""
End-to-end testings for APIs.
"""

from fastapi.testclient import TestClient

from .main import app

client = TestClient(app)


def test_users_create_and_list():
"""
Create valid users and make sure it returns in the users list.
"""

future_users = [
{
"first_name": "Parham",
"last_name": "Alvani",
"age": 30,
},
{
"first_name": "Elahe",
"last_name": "Dastan",
"age": 20,
},
{
"first_name": "Seyed Parham",
"last_name": "Alvani",
"age": 30,
},
]

for user in future_users:
response = client.post(
"/users",
json=user,
)
assert response.status_code == 200
assert response.json() == user

response = client.get("/users")
assert response.status_code == 200
users = response.json()
assert len(users) == len(future_users)
assert users == future_users


def test_users_bad_create():
"""
Create a invalid user and make sure it returns error.
"""

for user in [
{
"first_name": "Parham1",
"last_name": "Alvani",
"age": 30,
},
{
"first_name": "Parham",
"last_name": "Alvani",
"age": -30,
},
]:
response = client.post(
"/users",
json=user,
)
assert response.status_code == 422

0 comments on commit 5102519

Please sign in to comment.