|
| 1 | +import pytest |
| 2 | +from fastapi.testclient import TestClient |
| 3 | +from src.app import app, activities |
| 4 | + |
| 5 | +client = TestClient(app) |
| 6 | + |
| 7 | +@pytest.fixture(autouse=True) |
| 8 | +def reset_activities(): |
| 9 | + # Arrange: Reset the in-memory activities before each test |
| 10 | + for activity in activities.values(): |
| 11 | + activity['participants'].clear() |
| 12 | + # Optionally, repopulate with initial data if needed |
| 13 | + |
| 14 | +# --- Root endpoint --- |
| 15 | +def test_root_redirect(): |
| 16 | + # Act |
| 17 | + response = client.get("/") |
| 18 | + # Assert |
| 19 | + assert response.status_code in (200, 307, 308) |
| 20 | + |
| 21 | +# --- List activities --- |
| 22 | +def test_get_activities(): |
| 23 | + # Act |
| 24 | + response = client.get("/activities") |
| 25 | + # Assert |
| 26 | + assert response.status_code == 200 |
| 27 | + data = response.json() |
| 28 | + assert isinstance(data, dict) |
| 29 | + assert "Chess Club" in data |
| 30 | + |
| 31 | +# --- Signup for activity --- |
| 32 | +def test_signup_success(): |
| 33 | + # Arrange |
| 34 | + email = "test1@mergington.edu" |
| 35 | + activity = "Chess Club" |
| 36 | + # Act |
| 37 | + response = client.post(f"/activities/{activity}/signup?email={email}") |
| 38 | + # Assert |
| 39 | + assert response.status_code == 200 |
| 40 | + assert email in activities[activity]["participants"] |
| 41 | + |
| 42 | +# --- Signup duplicate --- |
| 43 | +def test_signup_duplicate(): |
| 44 | + # Arrange |
| 45 | + email = "test2@mergington.edu" |
| 46 | + activity = "Chess Club" |
| 47 | + client.post(f"/activities/{activity}/signup?email={email}") |
| 48 | + # Act |
| 49 | + response = client.post(f"/activities/{activity}/signup?email={email}") |
| 50 | + # Assert |
| 51 | + assert response.status_code == 400 |
| 52 | + assert "already signed up" in response.json()["detail"] |
| 53 | + |
| 54 | +# --- Signup for non-existent activity --- |
| 55 | +def test_signup_activity_not_found(): |
| 56 | + # Act |
| 57 | + response = client.post("/activities/Nonexistent/signup?email=foo@bar.com") |
| 58 | + # Assert |
| 59 | + assert response.status_code == 404 |
| 60 | + assert "Activity not found" in response.json()["detail"] |
| 61 | + |
| 62 | +# --- Unregister participant --- |
| 63 | +def test_unregister_success(): |
| 64 | + # Arrange |
| 65 | + email = "test3@mergington.edu" |
| 66 | + activity = "Chess Club" |
| 67 | + client.post(f"/activities/{activity}/signup?email={email}") |
| 68 | + # Act |
| 69 | + response = client.delete(f"/activities/{activity}/signup?email={email}") |
| 70 | + # Assert |
| 71 | + assert response.status_code == 200 |
| 72 | + assert email not in activities[activity]["participants"] |
| 73 | + |
| 74 | +# --- Unregister not registered --- |
| 75 | +def test_unregister_not_registered(): |
| 76 | + # Arrange |
| 77 | + email = "notregistered@mergington.edu" |
| 78 | + activity = "Chess Club" |
| 79 | + # Act |
| 80 | + response = client.delete(f"/activities/{activity}/signup?email={email}") |
| 81 | + # Assert |
| 82 | + assert response.status_code == 404 |
| 83 | + assert "not registered" in response.json()["detail"] |
| 84 | + |
| 85 | +# --- Unregister from non-existent activity --- |
| 86 | +def test_unregister_activity_not_found(): |
| 87 | + # Act |
| 88 | + response = client.delete("/activities/Nonexistent/signup?email=foo@bar.com") |
| 89 | + # Assert |
| 90 | + assert response.status_code == 404 |
| 91 | + assert "Activity not found" in response.json()["detail"] |
0 commit comments