Better bracket logic

This commit is contained in:
2026-02-14 01:04:54 +01:00 Verified
parent 87d3ea5ef0
commit 7b878b3abe
32 changed files with 1326 additions and 1348 deletions
+23 -18
View File
@@ -15,16 +15,19 @@ async def test_scoring_flow(
t_id = res.json()["id"]
t_code = valid_tournament_payload["code"]
# 2. Get Bracket Nodes (Updated Endpoint)
bracket_res = await client.get(f"/tournaments/{t_id}/bracket")
nodes = bracket_res.json()
# 2. Get Bracket (Now returns list of Matches)
# Note: You can now get this from GET /tournaments/{id} or /bracket depending on your routes
bracket_res = await client.get(f"/tournaments/{t_id}")
matches = bracket_res.json()["matches"]
# Find active node
active_node = next(n for n in nodes if n.get("match") is not None)
# Find the first playable match (Round 1)
# We look for a match that has teams assigned but is not finished
active_match = next(
m for m in matches if m["status"] == "Pending" and m["p1_team"] and m["p2_team"]
)
match_data = active_node["match"]
match_id = match_data["id"]
next_node_id = active_node["winner_next_node_id"]
match_id = active_match["id"]
next_match_id = active_match["winner_next_match_id"]
# 3. Report Score
score_payload = {
@@ -38,15 +41,17 @@ async def test_scoring_flow(
)
assert report_res.status_code == 200
# 4. Verify Winner Advanced (Fetch bracket again)
updated_res = await client.get(f"/tournaments/{t_id}/bracket")
updated_nodes = updated_res.json()
# 4. Verify Winner Advanced
updated_res = await client.get(f"/tournaments/{t_id}")
updated_matches = updated_res.json()["matches"]
target_node = next(n for n in updated_nodes if n["id"] == next_node_id)
target_match = next(m for m in updated_matches if m["id"] == next_match_id)
winner_id = match_data["p1_team"]["id"]
p1_in_target = target_node["p1_team"]["id"] if target_node["p1_team"] else None
p2_in_target = target_node["p2_team"]["id"] if target_node["p2_team"] else None
winner_id = active_match["p1_team"]["id"]
# Check if winner is now in the next match
p1_in_target = target_match["p1_team"]["id"] if target_match["p1_team"] else None
p2_in_target = target_match["p2_team"]["id"] if target_match["p2_team"] else None
assert winner_id in [p1_in_target, p2_in_target]
@@ -67,9 +72,9 @@ async def test_clear_score(client: AsyncClient, auth_headers, valid_tournament_p
t_id = res.json()["id"]
# Get active match
bracket = (await client.get(f"/tournaments/{t_id}/bracket")).json()
active_node = next(n for n in bracket if n.get("match"))
match_id = active_node["match"]["id"]
detail = (await client.get(f"/tournaments/{t_id}")).json()
active_match = next(m for m in detail["matches"] if m["p1_team"] and m["p2_team"])
match_id = active_match["id"]
score_payload = {"id": match_id, "code": "1234", "sets": [{"p1": 25, "p2": 0}]}
await client.post(
+28 -10
View File
@@ -20,14 +20,13 @@ async def test_manage_teams(
f"/tournaments/{t_id}/teams", json=new_team, headers=auth_headers
)
assert post_res.status_code == 200
assert post_res.json()["name"] == "Team E"
# 2. Verify Bracket Regenerated (Call new endpoint)
bracket_res = await client.get(f"/tournaments/{t_id}/bracket")
nodes = bracket_res.json()
# 2. Verify Bracket Regenerated
detail_res = await client.get(f"/tournaments/{t_id}")
matches = detail_res.json()["matches"]
# With 5 teams -> size 8 bracket
assert len(nodes) > 4
# With 5 teams, bracket size increases
assert len(matches) > 3
# 3. Bulk Update
new_team_list = ["Team X", "Team Y"]
@@ -35,8 +34,27 @@ async def test_manage_teams(
f"/tournaments/{t_id}/teams", json=new_team_list, headers=auth_headers
)
assert patch_res.status_code == 200
data = patch_res.json()
assert len(data) == 2
updated_detail = await client.get(f"/tournaments/{t_id}")
final_matches = updated_detail.json()["matches"]
# Fix: For Double Elim with 2 teams, we might get 2 matches (WB Final + Grand Final).
# Just ensure we have at least 1 match.
assert len(final_matches) >= 1
# Verify the teams are actually in the first match
first_match = next(
m
for m in final_matches
if m["name"] == "Winners Final"
or m["name"] == "Grand Final"
or m["name"].startswith("WB")
)
p1_name = first_match["p1_team"]["name"] if first_match["p1_team"] else None
p2_name = first_match["p2_team"]["name"] if first_match["p2_team"] else None
assert "Team X" in [p1_name, p2_name]
assert "Team Y" in [p1_name, p2_name]
async def test_manage_courts(
@@ -48,8 +66,8 @@ async def test_manage_courts(
t_id = res.json()["id"]
# Get initial courts
courts_res = await client.get(f"/tournaments/{t_id}/courts")
initial_courts = courts_res.json()
courts_res = await client.get(f"/tournaments/{t_id}")
initial_courts = courts_res.json()["courts"]
assert len(initial_courts) == 2
# Delete
+31 -28
View File
@@ -33,7 +33,7 @@ async def test_list_tournaments(
assert data[0]["name"] == "Test Tournament"
async def test_get_tournament_detail_and_bracket(
async def test_delete_tournament(
client: AsyncClient, auth_headers, valid_tournament_payload
):
create_res = await client.post(
@@ -41,25 +41,41 @@ async def test_get_tournament_detail_and_bracket(
)
t_id = create_res.json()["id"]
# 1. Test Light Detail Endpoint
del_res = await client.delete(f"/tournaments/{t_id}", headers=auth_headers)
assert del_res.status_code == 200
get_res = await client.get(f"/tournaments/{t_id}")
assert get_res.status_code == 404
async def test_get_tournament_detail(
client: AsyncClient, auth_headers, valid_tournament_payload
):
create_res = await client.post(
"/tournaments", json=valid_tournament_payload, headers=auth_headers
)
t_id = create_res.json()["id"]
# 1. Test Detail Endpoint
response = await client.get(f"/tournaments/{t_id}")
assert response.status_code == 200
data = response.json()
# Should HAVE metadata
assert len(data["teams"]) == 4
# Should NOT have heavy bracket data
# Should HAVE matches (since we merged nodes into matches and put them in Detail)
assert "matches" in data
assert len(data["matches"]) > 0
# Check structure of a match
first_match = data["matches"][0]
assert "id" in first_match
assert "winner_next_match_id" in first_match
# Ensure no old "nodes" key
assert "nodes" not in data
# 2. Test New Bracket Endpoint
bracket_res = await client.get(f"/tournaments/{t_id}/bracket")
assert bracket_res.status_code == 200
nodes = bracket_res.json()
assert len(nodes) > 0
first_node = nodes[0]
assert "display_number" in first_node
async def test_update_settings(
client: AsyncClient, auth_headers, valid_tournament_payload
@@ -77,19 +93,6 @@ async def test_update_settings(
assert response.status_code == 200
data = response.json()
assert data["name"] == "Updated Name"
assert data["code"] == "9999"
async def test_delete_tournament(
client: AsyncClient, auth_headers, valid_tournament_payload
):
create_res = await client.post(
"/tournaments", json=valid_tournament_payload, headers=auth_headers
)
t_id = create_res.json()["id"]
del_res = await client.delete(f"/tournaments/{t_id}", headers=auth_headers)
assert del_res.status_code == 200
get_res = await client.get(f"/tournaments/{t_id}")
assert get_res.status_code == 404
# The response model is TournamentUpdateResponse which inherits TournamentDetail
# So it should also have matches
assert "matches" in data