From 8ce7615f2c92e8ce40f5905ee4e61dc3f2a9a0ea Mon Sep 17 00:00:00 2001 From: Erik Vroon Date: Sat, 8 Feb 2025 12:04:31 +0100 Subject: [PATCH 1/5] Update pytest asyncio to 0.25.3 --- backend/Pipfile | 2 +- backend/pyproject.toml | 4 ++++ .../api/activate_next_stage_test.py | 3 +++ .../tests/integration_tests/api/auth_test.py | 6 +++++ .../api/auto_scheduling_matches_test.py | 2 ++ .../tests/integration_tests/api/clubs_test.py | 4 ++++ .../tests/integration_tests/api/conftest.py | 4 ++-- .../integration_tests/api/courts_test.py | 5 ++++ .../integration_tests/api/inputs_test.py | 4 ++++ .../integration_tests/api/matches_test.py | 6 +++++ .../integration_tests/api/metrics_test.py | 2 ++ .../tests/integration_tests/api/ping_test.py | 2 ++ .../integration_tests/api/players_test.py | 6 +++++ .../integration_tests/api/rankings_test.py | 5 ++++ .../api/rescheduling_matches_test.py | 2 ++ .../integration_tests/api/rounds_test.py | 4 ++++ .../api/scheduling_matches_test.py | 2 ++ .../integration_tests/api/stage_items_test.py | 4 ++++ .../integration_tests/api/stages_test.py | 7 ++++++ .../tests/integration_tests/api/teams_test.py | 8 +++++++ .../integration_tests/api/tournaments_test.py | 8 +++++++ .../tests/integration_tests/api/users_test.py | 5 ++++ backend/tests/integration_tests/conftest.py | 24 ++++++++++--------- .../cronjobs/demo_user_deletion_test.py | 3 +++ .../tests/integration_tests/db_init_test.py | 3 +++ .../integration_tests/index_lookup_test.py | 4 ++++ 26 files changed, 115 insertions(+), 14 deletions(-) diff --git a/backend/Pipfile b/backend/Pipfile index 315790fae..700082b8c 100644 --- a/backend/Pipfile +++ b/backend/Pipfile @@ -38,7 +38,7 @@ mypy = "1.14.1" pluggy = "1.5.0" pylint = "3.3.1" pytest = "8.3.4" -pytest-asyncio = "0.21.2" +pytest-asyncio = "0.25.3" pytest-cov = "6.0.0" pytest-xdist = "3.6.1" ruff = "0.9.1" diff --git a/backend/pyproject.toml b/backend/pyproject.toml index b4f7c6e83..9d1aefb9d 100644 --- a/backend/pyproject.toml +++ b/backend/pyproject.toml @@ -14,6 +14,10 @@ filterwarnings = [ 'ignore:.*The event_loop fixture provided by pytest-asyncio has been redefined.*:DeprecationWarning', 'ignore:.*Please use `import python_multipart` instead.*:PendingDeprecationWarning', ] +# Enable these once `asyncio_default_test_loop_scope` has been released +# Then remove the `@pytest.mark.asyncio(loop_scope="session")` calls. +#asyncio_default_fixture_loop_scope = "session" +#asyncio_default_test_loop_scope = "session" [tool.mypy] junit_xml = '.junit_report.xml' diff --git a/backend/tests/integration_tests/api/activate_next_stage_test.py b/backend/tests/integration_tests/api/activate_next_stage_test.py index a2e50d2b8..757fad3af 100644 --- a/backend/tests/integration_tests/api/activate_next_stage_test.py +++ b/backend/tests/integration_tests/api/activate_next_stage_test.py @@ -1,3 +1,5 @@ +import pytest + from bracket.logic.scheduling.builder import build_matches_for_stage_item from bracket.models.db.match import MatchBody, MatchWithDetailsDefinitive from bracket.models.db.stage_item import StageItemWithInputsCreate @@ -28,6 +30,7 @@ ) +@pytest.mark.asyncio(loop_scope="session") async def test_activate_next_stage( startup_and_shutdown_uvicorn_server: None, auth_context: AuthContext ) -> None: diff --git a/backend/tests/integration_tests/api/auth_test.py b/backend/tests/integration_tests/api/auth_test.py index 92da87e27..acc8e513d 100644 --- a/backend/tests/integration_tests/api/auth_test.py +++ b/backend/tests/integration_tests/api/auth_test.py @@ -3,6 +3,7 @@ from unittest.mock import Mock, patch import jwt +import pytest from bracket.config import config from bracket.models.db.account import UserAccountType @@ -21,6 +22,7 @@ def mock_auth_time() -> Generator[None, None, None]: yield +@pytest.mark.asyncio(loop_scope="session") async def test_get_token_success(startup_and_shutdown_uvicorn_server: None) -> None: mock_user = get_mock_user() body = { @@ -38,6 +40,7 @@ async def test_get_token_success(startup_and_shutdown_uvicorn_server: None) -> N assert decoded == {"user": mock_user.email, "exp": 7258723200} +@pytest.mark.asyncio(loop_scope="session") async def test_get_token_invalid_credentials(startup_and_shutdown_uvicorn_server: None) -> None: mock_user = get_mock_user() body = { @@ -51,6 +54,7 @@ async def test_get_token_invalid_credentials(startup_and_shutdown_uvicorn_server assert response == {"detail": "Incorrect email or password"} +@pytest.mark.asyncio(loop_scope="session") async def test_auth_on_protected_endpoint(startup_and_shutdown_uvicorn_server: None) -> None: mock_user = get_mock_user() headers = {"Authorization": f"Bearer {get_mock_token(mock_user)}"} @@ -71,6 +75,7 @@ async def test_auth_on_protected_endpoint(startup_and_shutdown_uvicorn_server: N } +@pytest.mark.asyncio(loop_scope="session") async def test_invalid_token(startup_and_shutdown_uvicorn_server: None) -> None: headers = {"Authorization": "Bearer some.invalid.token"} @@ -78,6 +83,7 @@ async def test_invalid_token(startup_and_shutdown_uvicorn_server: None) -> None: assert response == {"detail": "Could not validate credentials"} +@pytest.mark.asyncio(loop_scope="session") async def test_not_authenticated_for_tournament( startup_and_shutdown_uvicorn_server: None, auth_context: AuthContext ) -> None: diff --git a/backend/tests/integration_tests/api/auto_scheduling_matches_test.py b/backend/tests/integration_tests/api/auto_scheduling_matches_test.py index 28bbfdf3c..aef6dc2c8 100644 --- a/backend/tests/integration_tests/api/auto_scheduling_matches_test.py +++ b/backend/tests/integration_tests/api/auto_scheduling_matches_test.py @@ -1,3 +1,4 @@ +import pytest from heliclockter import datetime_utc from bracket.models.db.round import RoundInsertable @@ -28,6 +29,7 @@ ) +@pytest.mark.asyncio(loop_scope="session") async def test_start_next_round( startup_and_shutdown_uvicorn_server: None, auth_context: AuthContext ) -> None: diff --git a/backend/tests/integration_tests/api/clubs_test.py b/backend/tests/integration_tests/api/clubs_test.py index 39da0e2bb..275b0f5e0 100644 --- a/backend/tests/integration_tests/api/clubs_test.py +++ b/backend/tests/integration_tests/api/clubs_test.py @@ -6,7 +6,9 @@ from tests.integration_tests.models import AuthContext from tests.integration_tests.sql import inserted_club, inserted_user_x_club +import pytest +@pytest.mark.asyncio(loop_scope="session") async def test_clubs_endpoint( startup_and_shutdown_uvicorn_server: None, auth_context: AuthContext ) -> None: @@ -21,6 +23,7 @@ async def test_clubs_endpoint( } +@pytest.mark.asyncio(loop_scope="session") async def test_create_club( startup_and_shutdown_uvicorn_server: None, auth_context: AuthContext ) -> None: @@ -37,6 +40,7 @@ async def test_create_club( assert response["data"]["name"] == payload["name"] +@pytest.mark.asyncio(loop_scope="session") async def test_delete_club( startup_and_shutdown_uvicorn_server: None, auth_context: AuthContext ) -> None: diff --git a/backend/tests/integration_tests/api/conftest.py b/backend/tests/integration_tests/api/conftest.py index 3621a775e..ac26ccb9e 100644 --- a/backend/tests/integration_tests/api/conftest.py +++ b/backend/tests/integration_tests/api/conftest.py @@ -1,12 +1,12 @@ # pylint: disable=redefined-outer-name from collections.abc import AsyncIterator -import pytest +import pytest_asyncio from tests.integration_tests.api.shared import UvicornTestServer -@pytest.fixture(scope="module") +@pytest_asyncio.fixture(loop_scope="session", scope="module", autouse=True) async def startup_and_shutdown_uvicorn_server() -> AsyncIterator[None]: """ Start server as test fixture and tear down after test diff --git a/backend/tests/integration_tests/api/courts_test.py b/backend/tests/integration_tests/api/courts_test.py index 2e8975ce8..c44be68d7 100644 --- a/backend/tests/integration_tests/api/courts_test.py +++ b/backend/tests/integration_tests/api/courts_test.py @@ -8,7 +8,9 @@ from tests.integration_tests.models import AuthContext from tests.integration_tests.sql import assert_row_count_and_clear, inserted_court, inserted_team +import pytest +@pytest.mark.asyncio(loop_scope="session") async def test_courts_endpoint( startup_and_shutdown_uvicorn_server: None, auth_context: AuthContext ) -> None: @@ -30,6 +32,7 @@ async def test_courts_endpoint( } +@pytest.mark.asyncio(loop_scope="session") async def test_create_court( startup_and_shutdown_uvicorn_server: None, auth_context: AuthContext ) -> None: @@ -39,6 +42,7 @@ async def test_create_court( await assert_row_count_and_clear(courts, 1) +@pytest.mark.asyncio(loop_scope="session") async def test_delete_court( startup_and_shutdown_uvicorn_server: None, auth_context: AuthContext ) -> None: @@ -57,6 +61,7 @@ async def test_delete_court( await assert_row_count_and_clear(courts, 0) +@pytest.mark.asyncio(loop_scope="session") async def test_update_court( startup_and_shutdown_uvicorn_server: None, auth_context: AuthContext ) -> None: diff --git a/backend/tests/integration_tests/api/inputs_test.py b/backend/tests/integration_tests/api/inputs_test.py index 451ca9c4c..5a509de49 100644 --- a/backend/tests/integration_tests/api/inputs_test.py +++ b/backend/tests/integration_tests/api/inputs_test.py @@ -15,8 +15,10 @@ inserted_stage_item_input, inserted_team, ) +import pytest +@pytest.mark.asyncio(loop_scope="session") async def test_available_inputs( startup_and_shutdown_uvicorn_server: None, auth_context: AuthContext ) -> None: @@ -40,6 +42,7 @@ async def test_available_inputs( } +@pytest.mark.asyncio(loop_scope="session") async def test_update_stage_item_input( startup_and_shutdown_uvicorn_server: None, auth_context: AuthContext ) -> None: @@ -74,6 +77,7 @@ async def test_update_stage_item_input( assert response == {"success": True} +@pytest.mark.asyncio(loop_scope="session") async def test_update_stage_item_input_invalid_team( startup_and_shutdown_uvicorn_server: None, auth_context: AuthContext ) -> None: diff --git a/backend/tests/integration_tests/api/matches_test.py b/backend/tests/integration_tests/api/matches_test.py index 0dba4d626..074a02c94 100644 --- a/backend/tests/integration_tests/api/matches_test.py +++ b/backend/tests/integration_tests/api/matches_test.py @@ -6,6 +6,7 @@ from bracket.models.db.stage_item_inputs import ( StageItemInputInsertable, ) +import pytest from bracket.schema import matches from bracket.utils.db import fetch_one_parsed_certain from bracket.utils.dummy_records import ( @@ -37,6 +38,7 @@ ) +@pytest.mark.asyncio(loop_scope="session") async def test_create_match( startup_and_shutdown_uvicorn_server: None, auth_context: AuthContext ) -> None: @@ -82,6 +84,7 @@ async def test_create_match( await assert_row_count_and_clear(matches, 1) +@pytest.mark.asyncio(loop_scope="session") async def test_delete_match( startup_and_shutdown_uvicorn_server: None, auth_context: AuthContext ) -> None: @@ -148,6 +151,7 @@ async def test_delete_match( await assert_row_count_and_clear(matches, 0) +@pytest.mark.asyncio(loop_scope="session") async def test_update_match( startup_and_shutdown_uvicorn_server: None, auth_context: AuthContext ) -> None: @@ -227,6 +231,7 @@ async def test_update_match( await assert_row_count_and_clear(matches, 1) +@pytest.mark.asyncio(loop_scope="session") async def test_update_endpoint_custom_duration_margin( startup_and_shutdown_uvicorn_server: None, auth_context: AuthContext ) -> None: @@ -306,6 +311,7 @@ async def test_update_endpoint_custom_duration_margin( await assert_row_count_and_clear(matches, 1) +@pytest.mark.asyncio(loop_scope="session") async def test_upcoming_matches_endpoint( startup_and_shutdown_uvicorn_server: None, auth_context: AuthContext ) -> None: diff --git a/backend/tests/integration_tests/api/metrics_test.py b/backend/tests/integration_tests/api/metrics_test.py index 213d72b1c..973ba6009 100644 --- a/backend/tests/integration_tests/api/metrics_test.py +++ b/backend/tests/integration_tests/api/metrics_test.py @@ -1,7 +1,9 @@ from bracket.utils.http import HTTPMethod from tests.integration_tests.api.shared import send_request_raw +import pytest +@pytest.mark.asyncio(loop_scope="session") async def test_metrics_endpoint(startup_and_shutdown_uvicorn_server: None) -> None: text_response = await send_request_raw(HTTPMethod.GET, "metrics") assert "HELP bracket_response_time" in text_response diff --git a/backend/tests/integration_tests/api/ping_test.py b/backend/tests/integration_tests/api/ping_test.py index 413e03f7c..b7957abea 100644 --- a/backend/tests/integration_tests/api/ping_test.py +++ b/backend/tests/integration_tests/api/ping_test.py @@ -1,6 +1,8 @@ from bracket.utils.http import HTTPMethod from tests.integration_tests.api.shared import send_request +import pytest +@pytest.mark.asyncio(loop_scope="session") async def test_ping_endpoint(startup_and_shutdown_uvicorn_server: None) -> None: assert await send_request(HTTPMethod.GET, "ping") == "ping" diff --git a/backend/tests/integration_tests/api/players_test.py b/backend/tests/integration_tests/api/players_test.py index 98190aed9..a6ad7e00a 100644 --- a/backend/tests/integration_tests/api/players_test.py +++ b/backend/tests/integration_tests/api/players_test.py @@ -9,6 +9,8 @@ from tests.integration_tests.sql import assert_row_count_and_clear, inserted_player, inserted_team +import pytest +@pytest.mark.asyncio(loop_scope="session") async def test_players_endpoint( startup_and_shutdown_uvicorn_server: None, auth_context: AuthContext ) -> None: @@ -39,6 +41,7 @@ async def test_players_endpoint( } +@pytest.mark.asyncio(loop_scope="session") async def test_create_player( startup_and_shutdown_uvicorn_server: None, auth_context: AuthContext ) -> None: @@ -48,6 +51,7 @@ async def test_create_player( await assert_row_count_and_clear(players, 1) +@pytest.mark.asyncio(loop_scope="session") async def test_create_players( startup_and_shutdown_uvicorn_server: None, auth_context: AuthContext ) -> None: @@ -59,6 +63,7 @@ async def test_create_players( await assert_row_count_and_clear(players, 2) +@pytest.mark.asyncio(loop_scope="session") async def test_delete_player( startup_and_shutdown_uvicorn_server: None, auth_context: AuthContext ) -> None: @@ -77,6 +82,7 @@ async def test_delete_player( await assert_row_count_and_clear(players, 0) +@pytest.mark.asyncio(loop_scope="session") async def test_update_player( startup_and_shutdown_uvicorn_server: None, auth_context: AuthContext ) -> None: diff --git a/backend/tests/integration_tests/api/rankings_test.py b/backend/tests/integration_tests/api/rankings_test.py index 9165e403a..3a4f6a4be 100644 --- a/backend/tests/integration_tests/api/rankings_test.py +++ b/backend/tests/integration_tests/api/rankings_test.py @@ -15,7 +15,9 @@ from tests.integration_tests.models import AuthContext from tests.integration_tests.sql import inserted_ranking, inserted_team +import pytest +@pytest.mark.asyncio(loop_scope="session") async def test_rankings_endpoint( startup_and_shutdown_uvicorn_server: None, auth_context: AuthContext ) -> None: @@ -38,6 +40,7 @@ async def test_rankings_endpoint( } +@pytest.mark.asyncio(loop_scope="session") async def test_create_ranking( startup_and_shutdown_uvicorn_server: None, auth_context: AuthContext ) -> None: @@ -50,6 +53,7 @@ async def test_create_ranking( await sql_delete_ranking(tournament_id, ranking.id) +@pytest.mark.asyncio(loop_scope="session") async def test_delete_ranking( startup_and_shutdown_uvicorn_server: None, auth_context: AuthContext ) -> None: @@ -67,6 +71,7 @@ async def test_delete_ranking( ) +@pytest.mark.asyncio(loop_scope="session") async def test_update_ranking( startup_and_shutdown_uvicorn_server: None, auth_context: AuthContext ) -> None: diff --git a/backend/tests/integration_tests/api/rescheduling_matches_test.py b/backend/tests/integration_tests/api/rescheduling_matches_test.py index 3d9e43588..24924eaec 100644 --- a/backend/tests/integration_tests/api/rescheduling_matches_test.py +++ b/backend/tests/integration_tests/api/rescheduling_matches_test.py @@ -12,6 +12,7 @@ DUMMY_TEAM1, DUMMY_TEAM2, ) +import pytest from bracket.utils.http import HTTPMethod from tests.integration_tests.api.shared import SUCCESS_RESPONSE, send_tournament_request from tests.integration_tests.models import AuthContext @@ -27,6 +28,7 @@ ) +@pytest.mark.asyncio(loop_scope="session") async def test_reschedule_match( startup_and_shutdown_uvicorn_server: None, auth_context: AuthContext ) -> None: diff --git a/backend/tests/integration_tests/api/rounds_test.py b/backend/tests/integration_tests/api/rounds_test.py index 71887e1c6..de13c946c 100644 --- a/backend/tests/integration_tests/api/rounds_test.py +++ b/backend/tests/integration_tests/api/rounds_test.py @@ -14,8 +14,10 @@ inserted_stage_item, inserted_team, ) +import pytest +@pytest.mark.asyncio(loop_scope="session") async def test_create_round( startup_and_shutdown_uvicorn_server: None, auth_context: AuthContext ) -> None: @@ -46,6 +48,7 @@ async def test_create_round( await assert_row_count_and_clear(rounds, 1) +@pytest.mark.asyncio(loop_scope="session") async def test_delete_round( startup_and_shutdown_uvicorn_server: None, auth_context: AuthContext ) -> None: @@ -72,6 +75,7 @@ async def test_delete_round( await assert_row_count_and_clear(rounds, 0) +@pytest.mark.asyncio(loop_scope="session") async def test_update_round( startup_and_shutdown_uvicorn_server: None, auth_context: AuthContext ) -> None: diff --git a/backend/tests/integration_tests/api/scheduling_matches_test.py b/backend/tests/integration_tests/api/scheduling_matches_test.py index d8ac118ae..19dcaf555 100644 --- a/backend/tests/integration_tests/api/scheduling_matches_test.py +++ b/backend/tests/integration_tests/api/scheduling_matches_test.py @@ -14,6 +14,7 @@ DUMMY_STAGE_ITEM3, DUMMY_TEAM1, ) +import pytest from bracket.utils.http import HTTPMethod from tests.integration_tests.api.shared import ( SUCCESS_RESPONSE, @@ -27,6 +28,7 @@ ) +@pytest.mark.asyncio(loop_scope="session") async def test_schedule_all_matches( startup_and_shutdown_uvicorn_server: None, auth_context: AuthContext ) -> None: diff --git a/backend/tests/integration_tests/api/stage_items_test.py b/backend/tests/integration_tests/api/stage_items_test.py index 844c1021a..b7b41e7bb 100644 --- a/backend/tests/integration_tests/api/stage_items_test.py +++ b/backend/tests/integration_tests/api/stage_items_test.py @@ -13,6 +13,7 @@ SUCCESS_RESPONSE, send_tournament_request, ) +import pytest from tests.integration_tests.models import AuthContext from tests.integration_tests.sql import ( assert_row_count_and_clear, @@ -22,6 +23,7 @@ ) +@pytest.mark.asyncio(loop_scope="session") async def test_create_stage_item( startup_and_shutdown_uvicorn_server: None, auth_context: AuthContext ) -> None: @@ -61,6 +63,7 @@ async def test_create_stage_item( await assert_row_count_and_clear(stages, 1) +@pytest.mark.asyncio(loop_scope="session") async def test_delete_stage_item( startup_and_shutdown_uvicorn_server: None, auth_context: AuthContext ) -> None: @@ -84,6 +87,7 @@ async def test_delete_stage_item( await assert_row_count_and_clear(stages, 0) +@pytest.mark.asyncio(loop_scope="session") async def test_update_stage_item( startup_and_shutdown_uvicorn_server: None, auth_context: AuthContext ) -> None: diff --git a/backend/tests/integration_tests/api/stages_test.py b/backend/tests/integration_tests/api/stages_test.py index dfc25cd3a..05f715240 100644 --- a/backend/tests/integration_tests/api/stages_test.py +++ b/backend/tests/integration_tests/api/stages_test.py @@ -10,6 +10,7 @@ DUMMY_STAGE_ITEM1, DUMMY_TEAM1, ) +import pytest from bracket.utils.http import HTTPMethod from tests.integration_tests.api.shared import ( SUCCESS_RESPONSE, @@ -27,6 +28,7 @@ @pytest.mark.parametrize(("with_auth",), [(True,), (False,)]) +@pytest.mark.asyncio(loop_scope="session") async def test_stages_endpoint( startup_and_shutdown_uvicorn_server: None, auth_context: AuthContext, with_auth: bool ) -> None: @@ -87,6 +89,7 @@ async def test_stages_endpoint( } +@pytest.mark.asyncio(loop_scope="session") async def test_create_stage( startup_and_shutdown_uvicorn_server: None, auth_context: AuthContext ) -> None: @@ -106,6 +109,7 @@ async def test_create_stage( await assert_row_count_and_clear(stages, 1) +@pytest.mark.asyncio(loop_scope="session") async def test_delete_stage( startup_and_shutdown_uvicorn_server: None, auth_context: AuthContext ) -> None: @@ -124,6 +128,7 @@ async def test_delete_stage( await assert_row_count_and_clear(stages, 0) +@pytest.mark.asyncio(loop_scope="session") async def test_update_stage( startup_and_shutdown_uvicorn_server: None, auth_context: AuthContext ) -> None: @@ -153,6 +158,7 @@ async def test_update_stage( await assert_row_count_and_clear(stages, 1) +@pytest.mark.asyncio(loop_scope="session") async def test_activate_stage( startup_and_shutdown_uvicorn_server: None, auth_context: AuthContext ) -> None: @@ -176,6 +182,7 @@ async def test_activate_stage( await assert_row_count_and_clear(stages, 1) +@pytest.mark.asyncio(loop_scope="session") async def test_get_next_stage_rankings( startup_and_shutdown_uvicorn_server: None, auth_context: AuthContext ) -> None: diff --git a/backend/tests/integration_tests/api/teams_test.py b/backend/tests/integration_tests/api/teams_test.py index e1f3a5aa0..86c334066 100644 --- a/backend/tests/integration_tests/api/teams_test.py +++ b/backend/tests/integration_tests/api/teams_test.py @@ -11,7 +11,9 @@ from tests.integration_tests.models import AuthContext from tests.integration_tests.sql import assert_row_count_and_clear, inserted_team +import pytest +@pytest.mark.asyncio(loop_scope="session") async def test_teams_endpoint( startup_and_shutdown_uvicorn_server: None, auth_context: AuthContext ) -> None: @@ -41,6 +43,7 @@ async def test_teams_endpoint( } +@pytest.mark.asyncio(loop_scope="session") async def test_create_team( startup_and_shutdown_uvicorn_server: None, auth_context: AuthContext ) -> None: @@ -50,6 +53,7 @@ async def test_create_team( await assert_row_count_and_clear(teams, 1) +@pytest.mark.asyncio(loop_scope="session") async def test_create_teams( startup_and_shutdown_uvicorn_server: None, auth_context: AuthContext ) -> None: @@ -61,6 +65,7 @@ async def test_create_teams( await assert_row_count_and_clear(teams, 2) +@pytest.mark.asyncio(loop_scope="session") async def test_delete_team( startup_and_shutdown_uvicorn_server: None, auth_context: AuthContext ) -> None: @@ -76,6 +81,7 @@ async def test_delete_team( await assert_row_count_and_clear(teams, 0) +@pytest.mark.asyncio(loop_scope="session") async def test_update_team( startup_and_shutdown_uvicorn_server: None, auth_context: AuthContext ) -> None: @@ -95,6 +101,7 @@ async def test_update_team( await assert_row_count_and_clear(teams, 1) +@pytest.mark.asyncio(loop_scope="session") async def test_update_team_invalid_players( startup_and_shutdown_uvicorn_server: None, auth_context: AuthContext ) -> None: @@ -108,6 +115,7 @@ async def test_update_team_invalid_players( assert response == {"detail": "Could not find Player(s) with ID {-1}"} +@pytest.mark.asyncio(loop_scope="session") async def test_team_upload_and_remove_logo( startup_and_shutdown_uvicorn_server: None, auth_context: AuthContext ) -> None: diff --git a/backend/tests/integration_tests/api/tournaments_test.py b/backend/tests/integration_tests/api/tournaments_test.py index 4d8ed6879..be5fb5e80 100644 --- a/backend/tests/integration_tests/api/tournaments_test.py +++ b/backend/tests/integration_tests/api/tournaments_test.py @@ -2,6 +2,7 @@ import aiofiles.os import aiohttp +import pytest from bracket.database import database from bracket.logic.tournaments import sql_delete_tournament_completely from bracket.models.db.tournament import Tournament @@ -20,6 +21,7 @@ from tests.integration_tests.sql import inserted_tournament +@pytest.mark.asyncio(loop_scope="session") async def test_tournaments_endpoint( startup_and_shutdown_uvicorn_server: None, auth_context: AuthContext ) -> None: @@ -43,6 +45,7 @@ async def test_tournaments_endpoint( } +@pytest.mark.asyncio(loop_scope="session") async def test_tournament_endpoint( startup_and_shutdown_uvicorn_server: None, auth_context: AuthContext ) -> None: @@ -66,6 +69,7 @@ async def test_tournament_endpoint( } +@pytest.mark.asyncio(loop_scope="session") async def test_create_tournament( startup_and_shutdown_uvicorn_server: None, auth_context: AuthContext ) -> None: @@ -91,6 +95,7 @@ async def test_create_tournament( await sql_delete_tournament_completely(tournament.id) +@pytest.mark.asyncio(loop_scope="session") async def test_create_tournament_duplicate_dashboard_endpoint( startup_and_shutdown_uvicorn_server: None, auth_context: AuthContext ) -> None: @@ -110,6 +115,7 @@ async def test_create_tournament_duplicate_dashboard_endpoint( } +@pytest.mark.asyncio(loop_scope="session") async def test_update_tournament( startup_and_shutdown_uvicorn_server: None, auth_context: AuthContext ) -> None: @@ -135,6 +141,7 @@ async def test_update_tournament( assert updated_tournament.dashboard_public == body["dashboard_public"] +@pytest.mark.asyncio(loop_scope="session") async def test_delete_tournament( startup_and_shutdown_uvicorn_server: None, auth_context: AuthContext ) -> None: @@ -155,6 +162,7 @@ async def test_delete_tournament( await sql_delete_tournament(tournament_inserted.id) +@pytest.mark.asyncio(loop_scope="session") async def test_tournament_upload_and_remove_logo( startup_and_shutdown_uvicorn_server: None, auth_context: AuthContext ) -> None: diff --git a/backend/tests/integration_tests/api/users_test.py b/backend/tests/integration_tests/api/users_test.py index 4ae09c95d..6c549b94c 100644 --- a/backend/tests/integration_tests/api/users_test.py +++ b/backend/tests/integration_tests/api/users_test.py @@ -9,7 +9,9 @@ from tests.integration_tests.models import AuthContext from tests.integration_tests.sql import assert_row_count_and_clear +import pytest +@pytest.mark.asyncio(loop_scope="session") async def test_users_endpoint( startup_and_shutdown_uvicorn_server: None, auth_context: AuthContext ) -> None: @@ -31,6 +33,7 @@ async def test_users_endpoint( assert await send_auth_request(HTTPMethod.GET, "users/me", auth_context, {}) == expected_data +@pytest.mark.asyncio(loop_scope="session") async def test_create_user( startup_and_shutdown_uvicorn_server: None, auth_context: AuthContext ) -> None: @@ -47,6 +50,7 @@ async def test_create_user( await delete_user(response["data"]["user_id"]) +@pytest.mark.asyncio(loop_scope="session") async def test_create_demo_user( startup_and_shutdown_uvicorn_server: None, auth_context: AuthContext ) -> None: @@ -58,6 +62,7 @@ async def test_create_demo_user( await delete_user(response["data"]["user_id"]) +@pytest.mark.asyncio(loop_scope="session") async def test_update_user( startup_and_shutdown_uvicorn_server: None, auth_context: AuthContext ) -> None: diff --git a/backend/tests/integration_tests/conftest.py b/backend/tests/integration_tests/conftest.py index 0b8871d3f..23a89f860 100644 --- a/backend/tests/integration_tests/conftest.py +++ b/backend/tests/integration_tests/conftest.py @@ -6,6 +6,7 @@ from time import sleep import pytest +import pytest_asyncio from databases import Database from bracket.database import database, engine @@ -14,19 +15,20 @@ from tests.integration_tests.sql import inserted_auth_context -@pytest.fixture(scope="session") -def event_loop() -> AsyncIterator[AbstractEventLoop]: # type: ignore[misc] - try: - loop = asyncio.get_running_loop() - except RuntimeError: - loop = asyncio.new_event_loop() - - yield loop - loop.close() +# @pytest_asyncio.fixture(loop_scope="session") +# def event_loop() -> AsyncIterator[AbstractEventLoop]: # type: ignore[misc] +# try: +# loop = asyncio.get_running_loop() +# except RuntimeError: +# loop = asyncio.new_event_loop() +# +# yield loop +# loop.close() -@pytest.fixture(scope="session", autouse=True) -async def reinit_database(event_loop: AbstractEventLoop, worker_id: str) -> AsyncIterator[Database]: +# @pytest.fixture(scope="session", autouse=True) +@pytest_asyncio.fixture(loop_scope="session", scope="session", autouse=True) +async def reinit_database(worker_id: str) -> AsyncIterator[Database]: """ Creates the test database on the first test run in the session. diff --git a/backend/tests/integration_tests/cronjobs/demo_user_deletion_test.py b/backend/tests/integration_tests/cronjobs/demo_user_deletion_test.py index 4d7a98375..b3ff3b00b 100644 --- a/backend/tests/integration_tests/cronjobs/demo_user_deletion_test.py +++ b/backend/tests/integration_tests/cronjobs/demo_user_deletion_test.py @@ -1,9 +1,12 @@ +import pytest + from bracket.cronjobs.scheduling import delete_demo_accounts from bracket.models.db.account import UserAccountType from bracket.sql.users import get_user_by_id, update_user_account_type from tests.integration_tests.sql import inserted_auth_context +@pytest.mark.asyncio(loop_scope="session") async def test_delete_demo_accounts() -> None: async with inserted_auth_context() as auth_context: user_id = auth_context.user.id diff --git a/backend/tests/integration_tests/db_init_test.py b/backend/tests/integration_tests/db_init_test.py index 8e0e0d956..5ad34b205 100644 --- a/backend/tests/integration_tests/db_init_test.py +++ b/backend/tests/integration_tests/db_init_test.py @@ -1,7 +1,10 @@ +import pytest + from bracket.sql.users import delete_user_and_owned_clubs from bracket.utils.db_init import sql_create_dev_db +@pytest.mark.asyncio(loop_scope="session") async def test_db_init() -> None: user_id = await sql_create_dev_db() await delete_user_and_owned_clubs(user_id) diff --git a/backend/tests/integration_tests/index_lookup_test.py b/backend/tests/integration_tests/index_lookup_test.py index d87f3540f..5ce9f1b12 100644 --- a/backend/tests/integration_tests/index_lookup_test.py +++ b/backend/tests/integration_tests/index_lookup_test.py @@ -1,3 +1,5 @@ +import pytest + from bracket.database import database from bracket.utils.errors import ( foreign_key_violation_error_lookup, @@ -5,6 +7,7 @@ ) +@pytest.mark.asyncio(loop_scope="session") async def test_all_unique_indices_in_lookup() -> None: query = """ SELECT @@ -26,6 +29,7 @@ async def test_all_unique_indices_in_lookup() -> None: assert indices == expected_indices +@pytest.mark.asyncio(loop_scope="session") async def test_known_foreign_keys_in_lookup() -> None: query = """ SELECT conrelid::regclass AS table_name, From 87a3686090ca36601279171ca3065bac6c413c23 Mon Sep 17 00:00:00 2001 From: Erik Vroon Date: Sat, 8 Feb 2025 12:06:00 +0100 Subject: [PATCH 2/5] fixup! Update pytest asyncio to 0.25.3 --- backend/tests/integration_tests/conftest.py | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) diff --git a/backend/tests/integration_tests/conftest.py b/backend/tests/integration_tests/conftest.py index 23a89f860..a2e22f177 100644 --- a/backend/tests/integration_tests/conftest.py +++ b/backend/tests/integration_tests/conftest.py @@ -1,7 +1,5 @@ # pylint: disable=redefined-outer-name -import asyncio import os -from asyncio import AbstractEventLoop from collections.abc import AsyncIterator from time import sleep @@ -15,19 +13,7 @@ from tests.integration_tests.sql import inserted_auth_context -# @pytest_asyncio.fixture(loop_scope="session") -# def event_loop() -> AsyncIterator[AbstractEventLoop]: # type: ignore[misc] -# try: -# loop = asyncio.get_running_loop() -# except RuntimeError: -# loop = asyncio.new_event_loop() -# -# yield loop -# loop.close() - - -# @pytest.fixture(scope="session", autouse=True) -@pytest_asyncio.fixture(loop_scope="session", scope="session", autouse=True) +@pytest_asyncio.fixture(scope="session", autouse=True) async def reinit_database(worker_id: str) -> AsyncIterator[Database]: """ Creates the test database on the first test run in the session. From 9de982ff94e0e1c361d33933ecc5b945752f20d7 Mon Sep 17 00:00:00 2001 From: Erik Vroon Date: Sat, 8 Feb 2025 12:07:55 +0100 Subject: [PATCH 3/5] fixup! fixup! Update pytest asyncio to 0.25.3 --- backend/tests/integration_tests/api/clubs_test.py | 3 ++- backend/tests/integration_tests/api/courts_test.py | 3 ++- backend/tests/integration_tests/api/inputs_test.py | 3 ++- backend/tests/integration_tests/api/matches_test.py | 3 ++- backend/tests/integration_tests/api/metrics_test.py | 3 ++- backend/tests/integration_tests/api/ping_test.py | 3 ++- backend/tests/integration_tests/api/players_test.py | 3 ++- backend/tests/integration_tests/api/rankings_test.py | 3 ++- .../tests/integration_tests/api/rescheduling_matches_test.py | 3 ++- backend/tests/integration_tests/api/rounds_test.py | 3 ++- backend/tests/integration_tests/api/scheduling_matches_test.py | 3 ++- backend/tests/integration_tests/api/stage_items_test.py | 3 ++- backend/tests/integration_tests/api/stages_test.py | 1 - backend/tests/integration_tests/api/teams_test.py | 2 +- backend/tests/integration_tests/api/tournaments_test.py | 2 +- backend/tests/integration_tests/api/users_test.py | 3 ++- 16 files changed, 28 insertions(+), 16 deletions(-) diff --git a/backend/tests/integration_tests/api/clubs_test.py b/backend/tests/integration_tests/api/clubs_test.py index 275b0f5e0..063e38571 100644 --- a/backend/tests/integration_tests/api/clubs_test.py +++ b/backend/tests/integration_tests/api/clubs_test.py @@ -1,3 +1,5 @@ +import pytest + from bracket.models.db.user_x_club import UserXClubInsertable, UserXClubRelation from bracket.sql.clubs import get_clubs_for_user_id, sql_delete_club from bracket.utils.dummy_records import DUMMY_CLUB, DUMMY_MOCK_TIME @@ -6,7 +8,6 @@ from tests.integration_tests.models import AuthContext from tests.integration_tests.sql import inserted_club, inserted_user_x_club -import pytest @pytest.mark.asyncio(loop_scope="session") async def test_clubs_endpoint( diff --git a/backend/tests/integration_tests/api/courts_test.py b/backend/tests/integration_tests/api/courts_test.py index c44be68d7..41b62b365 100644 --- a/backend/tests/integration_tests/api/courts_test.py +++ b/backend/tests/integration_tests/api/courts_test.py @@ -1,3 +1,5 @@ +import pytest + from bracket.database import database from bracket.models.db.court import Court from bracket.schema import courts @@ -8,7 +10,6 @@ from tests.integration_tests.models import AuthContext from tests.integration_tests.sql import assert_row_count_and_clear, inserted_court, inserted_team -import pytest @pytest.mark.asyncio(loop_scope="session") async def test_courts_endpoint( diff --git a/backend/tests/integration_tests/api/inputs_test.py b/backend/tests/integration_tests/api/inputs_test.py index 5a509de49..0fd6b7ff9 100644 --- a/backend/tests/integration_tests/api/inputs_test.py +++ b/backend/tests/integration_tests/api/inputs_test.py @@ -1,3 +1,5 @@ +import pytest + from bracket.models.db.stage_item_inputs import StageItemInputInsertable from bracket.utils.dummy_records import ( DUMMY_STAGE1, @@ -15,7 +17,6 @@ inserted_stage_item_input, inserted_team, ) -import pytest @pytest.mark.asyncio(loop_scope="session") diff --git a/backend/tests/integration_tests/api/matches_test.py b/backend/tests/integration_tests/api/matches_test.py index 074a02c94..7468e330a 100644 --- a/backend/tests/integration_tests/api/matches_test.py +++ b/backend/tests/integration_tests/api/matches_test.py @@ -1,12 +1,13 @@ from decimal import Decimal +import pytest + from bracket.database import database from bracket.models.db.match import Match from bracket.models.db.stage_item import StageType from bracket.models.db.stage_item_inputs import ( StageItemInputInsertable, ) -import pytest from bracket.schema import matches from bracket.utils.db import fetch_one_parsed_certain from bracket.utils.dummy_records import ( diff --git a/backend/tests/integration_tests/api/metrics_test.py b/backend/tests/integration_tests/api/metrics_test.py index 973ba6009..1a1c73106 100644 --- a/backend/tests/integration_tests/api/metrics_test.py +++ b/backend/tests/integration_tests/api/metrics_test.py @@ -1,7 +1,8 @@ +import pytest + from bracket.utils.http import HTTPMethod from tests.integration_tests.api.shared import send_request_raw -import pytest @pytest.mark.asyncio(loop_scope="session") async def test_metrics_endpoint(startup_and_shutdown_uvicorn_server: None) -> None: diff --git a/backend/tests/integration_tests/api/ping_test.py b/backend/tests/integration_tests/api/ping_test.py index b7957abea..26a9238f3 100644 --- a/backend/tests/integration_tests/api/ping_test.py +++ b/backend/tests/integration_tests/api/ping_test.py @@ -1,7 +1,8 @@ +import pytest + from bracket.utils.http import HTTPMethod from tests.integration_tests.api.shared import send_request -import pytest @pytest.mark.asyncio(loop_scope="session") async def test_ping_endpoint(startup_and_shutdown_uvicorn_server: None) -> None: diff --git a/backend/tests/integration_tests/api/players_test.py b/backend/tests/integration_tests/api/players_test.py index a6ad7e00a..fc669bebe 100644 --- a/backend/tests/integration_tests/api/players_test.py +++ b/backend/tests/integration_tests/api/players_test.py @@ -1,3 +1,5 @@ +import pytest + from bracket.database import database from bracket.models.db.player import Player from bracket.schema import players @@ -9,7 +11,6 @@ from tests.integration_tests.sql import assert_row_count_and_clear, inserted_player, inserted_team -import pytest @pytest.mark.asyncio(loop_scope="session") async def test_players_endpoint( startup_and_shutdown_uvicorn_server: None, auth_context: AuthContext diff --git a/backend/tests/integration_tests/api/rankings_test.py b/backend/tests/integration_tests/api/rankings_test.py index 3a4f6a4be..18ab8cdcb 100644 --- a/backend/tests/integration_tests/api/rankings_test.py +++ b/backend/tests/integration_tests/api/rankings_test.py @@ -1,6 +1,8 @@ from decimal import Decimal from unittest.mock import ANY +import pytest + from bracket.database import database from bracket.models.db.ranking import Ranking from bracket.schema import rankings @@ -15,7 +17,6 @@ from tests.integration_tests.models import AuthContext from tests.integration_tests.sql import inserted_ranking, inserted_team -import pytest @pytest.mark.asyncio(loop_scope="session") async def test_rankings_endpoint( diff --git a/backend/tests/integration_tests/api/rescheduling_matches_test.py b/backend/tests/integration_tests/api/rescheduling_matches_test.py index 24924eaec..6899d72ca 100644 --- a/backend/tests/integration_tests/api/rescheduling_matches_test.py +++ b/backend/tests/integration_tests/api/rescheduling_matches_test.py @@ -1,3 +1,5 @@ +import pytest + from bracket.models.db.match import MatchRescheduleBody from bracket.models.db.stage_item_inputs import StageItemInputInsertable from bracket.schema import matches @@ -12,7 +14,6 @@ DUMMY_TEAM1, DUMMY_TEAM2, ) -import pytest from bracket.utils.http import HTTPMethod from tests.integration_tests.api.shared import SUCCESS_RESPONSE, send_tournament_request from tests.integration_tests.models import AuthContext diff --git a/backend/tests/integration_tests/api/rounds_test.py b/backend/tests/integration_tests/api/rounds_test.py index de13c946c..fe7b8e305 100644 --- a/backend/tests/integration_tests/api/rounds_test.py +++ b/backend/tests/integration_tests/api/rounds_test.py @@ -1,3 +1,5 @@ +import pytest + from bracket.database import database from bracket.models.db.round import Round from bracket.models.db.stage_item import StageType @@ -14,7 +16,6 @@ inserted_stage_item, inserted_team, ) -import pytest @pytest.mark.asyncio(loop_scope="session") diff --git a/backend/tests/integration_tests/api/scheduling_matches_test.py b/backend/tests/integration_tests/api/scheduling_matches_test.py index 19dcaf555..45a680c3a 100644 --- a/backend/tests/integration_tests/api/scheduling_matches_test.py +++ b/backend/tests/integration_tests/api/scheduling_matches_test.py @@ -1,3 +1,5 @@ +import pytest + from bracket.logic.scheduling.builder import build_matches_for_stage_item from bracket.models.db.stage_item import StageItemWithInputsCreate from bracket.models.db.stage_item_inputs import ( @@ -14,7 +16,6 @@ DUMMY_STAGE_ITEM3, DUMMY_TEAM1, ) -import pytest from bracket.utils.http import HTTPMethod from tests.integration_tests.api.shared import ( SUCCESS_RESPONSE, diff --git a/backend/tests/integration_tests/api/stage_items_test.py b/backend/tests/integration_tests/api/stage_items_test.py index b7b41e7bb..79873e2fe 100644 --- a/backend/tests/integration_tests/api/stage_items_test.py +++ b/backend/tests/integration_tests/api/stage_items_test.py @@ -1,3 +1,5 @@ +import pytest + from bracket.models.db.stage_item import StageType from bracket.models.db.stage_item_inputs import StageItemInputCreateBodyFinal from bracket.schema import matches, rounds, stage_items, stages @@ -13,7 +15,6 @@ SUCCESS_RESPONSE, send_tournament_request, ) -import pytest from tests.integration_tests.models import AuthContext from tests.integration_tests.sql import ( assert_row_count_and_clear, diff --git a/backend/tests/integration_tests/api/stages_test.py b/backend/tests/integration_tests/api/stages_test.py index 05f715240..2433fcb99 100644 --- a/backend/tests/integration_tests/api/stages_test.py +++ b/backend/tests/integration_tests/api/stages_test.py @@ -10,7 +10,6 @@ DUMMY_STAGE_ITEM1, DUMMY_TEAM1, ) -import pytest from bracket.utils.http import HTTPMethod from tests.integration_tests.api.shared import ( SUCCESS_RESPONSE, diff --git a/backend/tests/integration_tests/api/teams_test.py b/backend/tests/integration_tests/api/teams_test.py index 86c334066..0b28b391b 100644 --- a/backend/tests/integration_tests/api/teams_test.py +++ b/backend/tests/integration_tests/api/teams_test.py @@ -1,5 +1,6 @@ import aiofiles.os import aiohttp +import pytest from bracket.database import database from bracket.models.db.team import Team @@ -11,7 +12,6 @@ from tests.integration_tests.models import AuthContext from tests.integration_tests.sql import assert_row_count_and_clear, inserted_team -import pytest @pytest.mark.asyncio(loop_scope="session") async def test_teams_endpoint( diff --git a/backend/tests/integration_tests/api/tournaments_test.py b/backend/tests/integration_tests/api/tournaments_test.py index be5fb5e80..baa96f38b 100644 --- a/backend/tests/integration_tests/api/tournaments_test.py +++ b/backend/tests/integration_tests/api/tournaments_test.py @@ -1,8 +1,8 @@ import aiofiles import aiofiles.os import aiohttp - import pytest + from bracket.database import database from bracket.logic.tournaments import sql_delete_tournament_completely from bracket.models.db.tournament import Tournament diff --git a/backend/tests/integration_tests/api/users_test.py b/backend/tests/integration_tests/api/users_test.py index 6c549b94c..9821f46cf 100644 --- a/backend/tests/integration_tests/api/users_test.py +++ b/backend/tests/integration_tests/api/users_test.py @@ -1,3 +1,5 @@ +import pytest + from bracket.database import database from bracket.models.db.account import UserAccountType from bracket.models.db.user import User @@ -9,7 +11,6 @@ from tests.integration_tests.models import AuthContext from tests.integration_tests.sql import assert_row_count_and_clear -import pytest @pytest.mark.asyncio(loop_scope="session") async def test_users_endpoint( From 896e6e3c807b26bfd6070ff2ab99d56e4e25943c Mon Sep 17 00:00:00 2001 From: Erik Vroon Date: Sat, 8 Feb 2025 12:10:28 +0100 Subject: [PATCH 4/5] fixup! fixup! fixup! Update pytest asyncio to 0.25.3 --- backend/pyproject.toml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/backend/pyproject.toml b/backend/pyproject.toml index 9d1aefb9d..386d71237 100644 --- a/backend/pyproject.toml +++ b/backend/pyproject.toml @@ -123,10 +123,11 @@ min_confidence = 0 paths = ["."] ignore_decorators = ["@pytest.*", "@*router*", "@app.*", "@*validator*"] ignore_names = [ - "todo*", "RestartableUvicornWorker", "_generate_next_value_", "downgrade", - "upgrade" + "startup_and_shutdown_uvicorn_server" + "todo*", + "upgrade", ] sort_by_size = true From 768f1c08392bbf50515fd34b21d4c5f499c64795 Mon Sep 17 00:00:00 2001 From: Erik Vroon Date: Sat, 8 Feb 2025 12:10:35 +0100 Subject: [PATCH 5/5] fixup! fixup! fixup! fixup! Update pytest asyncio to 0.25.3 --- backend/pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/pyproject.toml b/backend/pyproject.toml index 386d71237..10e4d9f7a 100644 --- a/backend/pyproject.toml +++ b/backend/pyproject.toml @@ -126,7 +126,7 @@ ignore_names = [ "RestartableUvicornWorker", "_generate_next_value_", "downgrade", - "startup_and_shutdown_uvicorn_server" + "startup_and_shutdown_uvicorn_server", "todo*", "upgrade", ]