Skip to content

Commit 40f8eb3

Browse files
committed
Add FastAPI tests and improve UI feedback
1 parent 1091831 commit 40f8eb3

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
fastapi
22
uvicorn
3+
pytest
4+
httpx

src/static/app.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ document.addEventListener("DOMContentLoaded", () => {
2525
<p>${details.description}</p>
2626
<p><strong>Schedule:</strong> ${details.schedule}</p>
2727
<p><strong>Availability:</strong> ${spotsLeft} spots left</p>
28+
<p><strong>Participants:</strong></p>
29+
<ul class="participants">
30+
${details.participants.map(participant => `<li>${participant}</li>`).join('')}
31+
</ul>
2832
`;
2933

3034
activitiesList.appendChild(activityCard);
@@ -62,6 +66,7 @@ document.addEventListener("DOMContentLoaded", () => {
6266
messageDiv.textContent = result.message;
6367
messageDiv.className = "success";
6468
signupForm.reset();
69+
await fetchActivities(); // Fetch updated activities
6570
} else {
6671
messageDiv.textContent = result.detail || "An error occurred";
6772
messageDiv.className = "error";

src/static/styles.css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,14 @@ section h3 {
7474
margin-bottom: 8px;
7575
}
7676

77+
.activity-card .participants {
78+
margin-top: 10px;
79+
padding: 10px;
80+
border-top: 1px solid #ddd;
81+
list-style-type: disc;
82+
padding-left: 20px;
83+
}
84+
7785
.form-group {
7886
margin-bottom: 15px;
7987
}

tests/test_app.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import pytest
2+
from fastapi.testclient import TestClient
3+
from src import app as app_module
4+
5+
client = TestClient(app_module.app)
6+
7+
8+
def test_get_activities():
9+
"""GET /activities should return the activities dictionary."""
10+
response = client.get("/activities")
11+
assert response.status_code == 200
12+
data = response.json()
13+
# Basic sanity checks
14+
assert isinstance(data, dict)
15+
assert "Chess Club" in data
16+
assert isinstance(data["Chess Club"].get("participants"), list)
17+
18+
19+
def test_signup_for_activity():
20+
"""POST signup should add the email to the activity participants."""
21+
activity = "Chess Club"
22+
test_email = "[email protected]"
23+
24+
# Ensure clean state before test
25+
participants = app_module.activities[activity]["participants"]
26+
if test_email in participants:
27+
participants.remove(test_email)
28+
29+
try:
30+
response = client.post(f"/activities/{activity}/signup", params={"email": test_email})
31+
assert response.status_code == 200
32+
assert response.json() == {"message": f"Signed up {test_email} for {activity}"}
33+
assert test_email in app_module.activities[activity]["participants"]
34+
finally:
35+
# Cleanup added test email
36+
if test_email in app_module.activities[activity]["participants"]:
37+
app_module.activities[activity]["participants"].remove(test_email)

0 commit comments

Comments
 (0)