|
| 1 | +import copy |
| 2 | +from fastapi.testclient import TestClient |
| 3 | +import pytest |
| 4 | + |
| 5 | +from src.app import app, activities |
| 6 | + |
| 7 | +client = TestClient(app) |
| 8 | + |
| 9 | +@pytest.fixture(autouse=True) |
| 10 | +def reset_activities(): |
| 11 | + # Make a deep copy of the activities before each test and restore after |
| 12 | + original = copy.deepcopy(activities) |
| 13 | + yield |
| 14 | + activities.clear() |
| 15 | + activities.update(copy.deepcopy(original)) |
| 16 | + |
| 17 | + |
| 18 | +def test_get_activities(): |
| 19 | + res = client.get("/activities") |
| 20 | + assert res.status_code == 200 |
| 21 | + data = res.json() |
| 22 | + assert isinstance(data, dict) |
| 23 | + assert "Football Club" in data |
| 24 | + assert isinstance(data["Football Club"]["participants"], list) |
| 25 | + |
| 26 | + |
| 27 | +def test_signup_and_unregister_flow(): |
| 28 | + email = "temp@example.com" |
| 29 | + activity = "Football Club" |
| 30 | + |
| 31 | + # Sign up |
| 32 | + res = client.post(f"/activities/{activity}/signup?email={email}") |
| 33 | + assert res.status_code == 200 |
| 34 | + assert "Signed up" in res.json().get("message", "") |
| 35 | + |
| 36 | + # Participant should be in the activity |
| 37 | + assert email in activities[activity]["participants"] |
| 38 | + |
| 39 | + # Unregister |
| 40 | + res = client.delete(f"/activities/{activity}/unregister?email={email}") |
| 41 | + assert res.status_code == 200 |
| 42 | + assert "Unregistered" in res.json().get("message", "") |
| 43 | + |
| 44 | + # Participant should be removed |
| 45 | + assert email not in activities[activity]["participants"] |
| 46 | + |
| 47 | + |
| 48 | +def test_signup_existing(): |
| 49 | + activity = "Football Club" |
| 50 | + email = "alex@mergington.edu" # already present |
| 51 | + res = client.post(f"/activities/{activity}/signup?email={email}") |
| 52 | + assert res.status_code == 400 |
| 53 | + assert "already signed up" in res.json().get("detail", "").lower() |
| 54 | + |
| 55 | + |
| 56 | +def test_unregister_not_found(): |
| 57 | + activity = "Football Club" |
| 58 | + email = "notfound@example.com" |
| 59 | + |
| 60 | + # Ensure the participant is not present |
| 61 | + if email in activities[activity]["participants"]: |
| 62 | + activities[activity]["participants"].remove(email) |
| 63 | + |
| 64 | + res = client.delete(f"/activities/{activity}/unregister?email={email}") |
| 65 | + assert res.status_code == 404 |
0 commit comments