Skip to content

Commit 40be253

Browse files
committed
Tests for the API
Signed-off-by: Abdulazeez Abdulazeez Adeshina <youngestdev@gmail.com>
1 parent e07be94 commit 40be253

File tree

5 files changed

+256
-0
lines changed

5 files changed

+256
-0
lines changed

ch08/planner/tests/conftest.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import asyncio
2+
import httpx
3+
import pytest
4+
5+
from main import app
6+
from database.database import Settings
7+
from models.events import Event
8+
from models.users import User
9+
10+
11+
@pytest.fixture(scope="session")
12+
def event_loop():
13+
loop = asyncio.get_event_loop()
14+
yield loop
15+
loop.close()
16+
17+
18+
async def init_db():
19+
test_settings = Settings()
20+
test_settings.DATABASE_URL = "mongodb://localhost:27017/testdb"
21+
22+
await test_settings.initialize_database()
23+
24+
25+
@pytest.fixture(scope="session")
26+
async def default_client():
27+
await init_db()
28+
async with httpx.AsyncClient(app=app, base_url="http://app") as client:
29+
yield client
30+
31+
# Clean up resources
32+
await Event.find_all().delete()
33+
await User.find_all().delete()
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
def add(a: int, b: int) -> int:
2+
return a + b
3+
4+
5+
def subtract(a: int, b: int) -> int:
6+
return b - a
7+
8+
9+
def multiply(a: int, b: int) -> int:
10+
return a * b
11+
12+
13+
def divide(a: int, b: int) -> int:
14+
return b // a
15+
16+
17+
def test_add() -> None:
18+
assert add(1, 1) == 2
19+
20+
21+
def test_subtract() -> None:
22+
assert subtract(2, 5) == 3
23+
24+
25+
def test_multiply() -> None:
26+
assert multiply(10, 10) == 100
27+
28+
29+
def test_divide() -> None:
30+
assert divide(25, 100) == 4

ch08/planner/tests/test_fixture.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import pytest
2+
3+
4+
# Fixture is defined.
5+
from models.events import EventUpdate
6+
7+
8+
@pytest.fixture
9+
def event() -> EventUpdate:
10+
return EventUpdate(
11+
title="FastAPI Book Launch",
12+
image="https://packt.com/fastapi.png",
13+
description="We will be discussing the contents of the FastAPI book in this event.Ensure to come with your own copy to win gifts!",
14+
tags=["python", "fastapi", "book", "launch"],
15+
location="Google Meet"
16+
)
17+
18+
19+
def test_event_name(event: EventUpdate) -> None:
20+
assert event.title == "FastAPI Book Launch"

ch08/planner/tests/test_login.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import httpx
2+
import pytest
3+
4+
5+
@pytest.mark.asyncio
6+
async def test_sign_new_user(default_client: httpx.AsyncClient) -> None:
7+
payload = {
8+
"email": "testuser@packt.com",
9+
"password": "testpassword",
10+
}
11+
12+
headers = {
13+
"accept": "application/json",
14+
"Content-Type": "application/json"
15+
}
16+
17+
test_response = {
18+
"message": "User created successfully"
19+
}
20+
21+
response = await default_client.post("/user/signup", json=payload, headers=headers)
22+
23+
assert response.status_code == 200
24+
assert response.json() == test_response
25+
26+
27+
@pytest.mark.asyncio
28+
async def test_sign_user_in(default_client: httpx.AsyncClient) -> None:
29+
payload = {
30+
"username": "testuser@packt.com",
31+
"password": "testpassword"
32+
}
33+
34+
headers = {
35+
"accept": "application/json",
36+
"Content-Type": "application/x-www-form-urlencoded"
37+
}
38+
39+
response = await default_client.post("/user/signin", data=payload, headers=headers)
40+
41+
assert response.status_code == 200
42+
assert response.json()["token_type"] == "Bearer"

ch08/planner/tests/test_routes.py

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
import httpx
2+
import pytest
3+
4+
from auth.jwt_handler import create_access_token
5+
from models.events import Event
6+
7+
8+
@pytest.fixture(scope="module")
9+
async def access_token() -> str:
10+
return create_access_token("testuser@packt.com")
11+
12+
13+
@pytest.fixture(scope="module")
14+
async def mock_event() -> Event:
15+
new_event = Event(
16+
creator="testuser@packt.com",
17+
title="FastAPI Book Launch",
18+
image="https://linktomyimage.com/image.png",
19+
description="We will be discussing the contents of the FastAPI book in this event.Ensure to come with your own copy to win gifts!",
20+
tags=["python", "fastapi", "book", "launch"],
21+
location="Google Meet"
22+
)
23+
24+
await Event.insert_one(new_event)
25+
26+
yield new_event
27+
28+
29+
@pytest.mark.asyncio
30+
async def test_get_events(default_client: httpx.AsyncClient, mock_event: Event) -> None:
31+
response = await default_client.get("/event/")
32+
33+
assert response.status_code == 200
34+
assert response.json()[0]["_id"] == str(mock_event.id)
35+
36+
37+
@pytest.mark.asyncio
38+
async def test_get_event(default_client: httpx.AsyncClient, mock_event: Event) -> None:
39+
url = f"/event/{str(mock_event.id)}"
40+
response = await default_client.get(url)
41+
42+
assert response.status_code == 200
43+
assert response.json()["creator"] == mock_event.creator
44+
assert response.json()["_id"] == str(mock_event.id)
45+
46+
47+
@pytest.mark.asyncio
48+
async def test_post_event(default_client: httpx.AsyncClient, access_token: str) -> None:
49+
payload = {
50+
"title": "FastAPI Book Launch",
51+
"image": "https://linktomyimage.com/image.png",
52+
"description": "We will be discussing the contents of the FastAPI book in this event.Ensure to come with your own copy to win gifts!",
53+
"tags": [
54+
"python",
55+
"fastapi",
56+
"book",
57+
"launch"
58+
],
59+
"location": "Google Meet",
60+
}
61+
62+
headers = {
63+
"Content-Type": "application/json",
64+
"Authorization": f"Bearer {access_token}"
65+
}
66+
67+
test_response = {
68+
"message": "Event created successfully"
69+
}
70+
71+
response = await default_client.post("/event/new", json=payload, headers=headers)
72+
73+
assert response.status_code == 200
74+
assert response.json() == test_response
75+
76+
77+
@pytest.mark.asyncio
78+
async def test_get_events_count(default_client: httpx.AsyncClient) -> None:
79+
response = await default_client.get("/event/")
80+
81+
events = response.json()
82+
83+
assert response.status_code == 200
84+
assert len(events) == 2
85+
86+
87+
@pytest.mark.asyncio
88+
async def test_update_event(default_client: httpx.AsyncClient, mock_event: Event, access_token: str) -> None:
89+
test_payload = {
90+
"title": "Updated FastAPI event"
91+
}
92+
93+
headers = {
94+
"Content-Type": "application/json",
95+
"Authorization": f"Bearer {access_token}"
96+
}
97+
98+
url = f"/event/{str(mock_event.id)}"
99+
100+
response = await default_client.put(url, json=test_payload, headers=headers)
101+
102+
assert response.status_code == 200
103+
assert response.json()["title"] == test_payload["title"]
104+
105+
106+
@pytest.mark.asyncio
107+
async def test_delete_event(default_client: httpx.AsyncClient, mock_event: Event, access_token: str) -> None:
108+
test_response = {
109+
"message": "Event deleted successfully."
110+
}
111+
112+
headers = {
113+
"Content-Type": "application/json",
114+
"Authorization": f"Bearer {access_token}"
115+
}
116+
117+
url = f"/event/{mock_event.id}"
118+
119+
response = await default_client.delete(url, headers=headers)
120+
121+
assert response.status_code == 200
122+
assert response.json() == test_response
123+
124+
125+
@pytest.mark.asyncio
126+
async def test_get_event_again(default_client: httpx.AsyncClient, mock_event: Event) -> None:
127+
url = f"/event/{str(mock_event.id)}"
128+
response = await default_client.get(url)
129+
130+
assert response.status_code == 404
131+

0 commit comments

Comments
 (0)