Skip to content

Commit 5556478

Browse files
committed
Add tests for activity signup and management, including duplicate prevention and participant unregistration
1 parent 727edb7 commit 5556478

3 files changed

Lines changed: 69 additions & 1 deletion

File tree

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
fastapi
22
uvicorn
33
httpx
4-
watchfiles
4+
watchfiles
5+
pytest

tests/__init__.py

Whitespace-only changes.

tests/test_app.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import pytest
2+
from fastapi.testclient import TestClient
3+
from src.app import app, activities
4+
5+
client = TestClient(app)
6+
7+
# Helper to reset activities for test isolation
8+
def reset_participants():
9+
for activity in activities.values():
10+
if isinstance(activity.get("participants"), list):
11+
activity["participants"] = []
12+
13+
def test_get_activities():
14+
# Arrange
15+
reset_participants()
16+
# Act
17+
response = client.get("/activities")
18+
# Assert
19+
assert response.status_code == 200
20+
data = response.json()
21+
assert isinstance(data, dict)
22+
assert "Chess Club" in data
23+
24+
def test_signup_for_activity():
25+
# Arrange
26+
reset_participants()
27+
email = "testuser@mergington.edu"
28+
# Act
29+
response = client.post(f"/activities/Chess Club/signup?email={email}")
30+
# Assert
31+
assert response.status_code == 200
32+
assert email in activities["Chess Club"]["participants"]
33+
34+
35+
def test_signup_duplicate():
36+
# Arrange
37+
reset_participants()
38+
email = "testuser@mergington.edu"
39+
client.post(f"/activities/Chess Club/signup?email={email}")
40+
# Act
41+
response = client.post(f"/activities/Chess Club/signup?email={email}")
42+
# Assert
43+
assert response.status_code == 400
44+
assert response.json()["detail"] == "Student is already signed up for this activity"
45+
46+
47+
def test_unregister_participant():
48+
# Arrange
49+
reset_participants()
50+
email = "testuser@mergington.edu"
51+
client.post(f"/activities/Chess Club/signup?email={email}")
52+
# Act
53+
response = client.delete(f"/activities/Chess Club/unregister?email={email}")
54+
# Assert
55+
assert response.status_code == 200
56+
assert email not in activities["Chess Club"]["participants"]
57+
58+
59+
def test_unregister_nonexistent():
60+
# Arrange
61+
reset_participants()
62+
email = "notfound@mergington.edu"
63+
# Act
64+
response = client.delete(f"/activities/Chess Club/unregister?email={email}")
65+
# Assert
66+
assert response.status_code == 404
67+
assert response.json()["detail"] == "Participant not found in this activity"

0 commit comments

Comments
 (0)