|
| 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