diff --git a/backend/tests/conftest.py b/backend/tests/conftest.py index 5b9273d..719638b 100644 --- a/backend/tests/conftest.py +++ b/backend/tests/conftest.py @@ -7,6 +7,7 @@ from app.database import Base, get_db # Import your app and models from app.main import app +from app import models from fastapi import Request from httpx import ASGITransport, AsyncClient from sqlalchemy import create_engine @@ -43,6 +44,15 @@ def db(prepare_db) -> Generator[Session, None, None]: connection.close() +@pytest.fixture(autouse=True) +def clean_db(db): + db.query(models.Match).delete() + db.query(models.Team).delete() + db.query(models.Court).delete() + db.commit() + yield + + @pytest.fixture(scope="function") async def client(db: Session) -> AsyncGenerator[AsyncClient, None]: def override_get_db(): @@ -85,13 +95,20 @@ def auth_headers(client): @pytest.fixture -def valid_tournament_payload(): +async def valid_tournament_payload(db): + # 1. Create global courts first + c1 = models.Court(name="Plan 1") + c2 = models.Court(name="Plan 2") + db.add_all([c1, c2]) + db.commit() + + # 2. Return payload with IDs return { "name": "Test Tournament", "code": "1234", "type": "Double", - "timestamp": "2024-01-01T10:00:00", + "timestamp": "2026-05-24T11:00:00", "duration": 15, "teams": ["Team A", "Team B", "Team C", "Team D"], - "courts": ["Court 1", "Court 2"], + "courts": [c1.id, c2.id], } diff --git a/backend/tests/test_structure.py b/backend/tests/test_structure.py index 2a9be1e..2d263d6 100644 --- a/backend/tests/test_structure.py +++ b/backend/tests/test_structure.py @@ -44,7 +44,9 @@ async def test_manage_teams( # Verify the teams are actually in the first match first_match = final_matches[0] - print(first_match) + assert first_match["p1_team_id"] is not None + assert first_match["p2_team_id"] is not None + p1_name = first_match["p1_team_id"] p2_name = first_match["p2_team_id"] @@ -60,21 +62,21 @@ async def test_manage_courts( ) t_id = res.json()["id"] - # Get initial courts + # 1. Get initial courts from the tournament detail courts_res = await client.get(f"/tournaments/{t_id}") initial_courts = courts_res.json()["courts"] assert len(initial_courts) == 2 - # Delete - court_id = initial_courts[0]["id"] - del_res = await client.delete( - f"/tournaments/{t_id}/courts/{court_id}", headers=auth_headers - ) + # 2. Extract the ID from the list so the variable is defined! + court_to_delete_id = initial_courts[0]["id"] + + # 3. Now use that variable in your delete call + del_res = await client.delete(f"/courts/{court_to_delete_id}", headers=auth_headers) assert del_res.status_code == 200 - # Create + # CREATE: Change this from a tournament-specific route to the global one create_res = await client.post( - f"/tournaments/{t_id}/courts", json={"name": "New Court"}, headers=auth_headers + "/courts", json={"name": "New Court"}, headers=auth_headers # GLOBAL ROUTE ) assert create_res.status_code == 200 assert create_res.json()["name"] == "New Court"