|
| 1 | +import pytest |
| 2 | +from fastapi.testclient import TestClient |
| 3 | +from src.app import app |
| 4 | + |
| 5 | +client = TestClient(app) |
| 6 | + |
| 7 | +# Test GET /activities |
| 8 | +def test_get_activities(): |
| 9 | + response = client.get("/activities") |
| 10 | + assert response.status_code == 200 |
| 11 | + data = response.json() |
| 12 | + assert "Chess Club" in data |
| 13 | + assert "Programming Class" in data |
| 14 | + assert "Gym Class" in data |
| 15 | + |
| 16 | +# Test POST /activities/{activity_name}/signup |
| 17 | +@pytest.mark.parametrize("activity,email", [ |
| 18 | + ("Chess Club", "newstudent@mergington.edu"), |
| 19 | + ("Programming Class", "newcoder@mergington.edu"), |
| 20 | +]) |
| 21 | +def test_signup_for_activity(activity, email): |
| 22 | + response = client.post(f"/activities/{activity}/signup?email={email}") |
| 23 | + assert response.status_code == 200 |
| 24 | + assert f"Signed up {email} for {activity}" in response.json()["message"] |
| 25 | + |
| 26 | +# Test duplicate signup |
| 27 | +def test_duplicate_signup(): |
| 28 | + activity = "Chess Club" |
| 29 | + email = "michael@mergington.edu" |
| 30 | + response = client.post(f"/activities/{activity}/signup?email={email}") |
| 31 | + assert response.status_code == 400 |
| 32 | + assert "already signed up" in response.json()["detail"] |
| 33 | + |
| 34 | +# Test DELETE /activities/{activity_name}/unregister |
| 35 | +@pytest.mark.parametrize("activity,email", [ |
| 36 | + ("Chess Club", "daniel@mergington.edu"), |
| 37 | + ("Programming Class", "emma@mergington.edu"), |
| 38 | +]) |
| 39 | +def test_unregister_from_activity(activity, email): |
| 40 | + response = client.delete(f"/activities/{activity}/unregister?email={email}") |
| 41 | + assert response.status_code == 200 |
| 42 | + assert f"Unregistered {email} from {activity}" in response.json()["message"] |
| 43 | + |
| 44 | +# Test unregister non-existent participant |
| 45 | +def test_unregister_nonexistent(): |
| 46 | + activity = "Chess Club" |
| 47 | + email = "notfound@mergington.edu" |
| 48 | + response = client.delete(f"/activities/{activity}/unregister?email={email}") |
| 49 | + assert response.status_code == 400 |
| 50 | + assert "Participant not found" in response.json()["detail"] |
| 51 | + |
| 52 | +# Test activity not found |
| 53 | +@pytest.mark.parametrize("endpoint", [ |
| 54 | + "/activities/Unknown/signup?email=test@mergington.edu", |
| 55 | + "/activities/Unknown/unregister?email=test@mergington.edu", |
| 56 | +]) |
| 57 | +def test_activity_not_found(endpoint): |
| 58 | + method = "post" if "signup" in endpoint else "delete" |
| 59 | + response = getattr(client, method)(endpoint) |
| 60 | + assert response.status_code == 404 |
| 61 | + assert "Activity not found" in response.json()["detail"] |
0 commit comments