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..10e4d9f7a 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' @@ -119,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 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..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 @@ -7,6 +9,7 @@ from tests.integration_tests.sql import inserted_club, inserted_user_x_club +@pytest.mark.asyncio(loop_scope="session") async def test_clubs_endpoint( startup_and_shutdown_uvicorn_server: None, auth_context: AuthContext ) -> None: @@ -21,6 +24,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 +41,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..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 @@ -9,6 +11,7 @@ from tests.integration_tests.sql import assert_row_count_and_clear, inserted_court, inserted_team +@pytest.mark.asyncio(loop_scope="session") async def test_courts_endpoint( startup_and_shutdown_uvicorn_server: None, auth_context: AuthContext ) -> None: @@ -30,6 +33,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 +43,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 +62,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..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, @@ -17,6 +19,7 @@ ) +@pytest.mark.asyncio(loop_scope="session") async def test_available_inputs( startup_and_shutdown_uvicorn_server: None, auth_context: AuthContext ) -> None: @@ -40,6 +43,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 +78,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..7468e330a 100644 --- a/backend/tests/integration_tests/api/matches_test.py +++ b/backend/tests/integration_tests/api/matches_test.py @@ -1,5 +1,7 @@ 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 @@ -37,6 +39,7 @@ ) +@pytest.mark.asyncio(loop_scope="session") async def test_create_match( startup_and_shutdown_uvicorn_server: None, auth_context: AuthContext ) -> None: @@ -82,6 +85,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 +152,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 +232,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 +312,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..1a1c73106 100644 --- a/backend/tests/integration_tests/api/metrics_test.py +++ b/backend/tests/integration_tests/api/metrics_test.py @@ -1,7 +1,10 @@ +import pytest + from bracket.utils.http import HTTPMethod from tests.integration_tests.api.shared import send_request_raw +@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..26a9238f3 100644 --- a/backend/tests/integration_tests/api/ping_test.py +++ b/backend/tests/integration_tests/api/ping_test.py @@ -1,6 +1,9 @@ +import pytest + from bracket.utils.http import HTTPMethod from tests.integration_tests.api.shared import send_request +@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..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,6 +11,7 @@ from tests.integration_tests.sql import assert_row_count_and_clear, inserted_player, inserted_team +@pytest.mark.asyncio(loop_scope="session") async def test_players_endpoint( startup_and_shutdown_uvicorn_server: None, auth_context: AuthContext ) -> None: @@ -39,6 +42,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 +52,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 +64,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 +83,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..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 @@ -16,6 +18,7 @@ from tests.integration_tests.sql import inserted_ranking, inserted_team +@pytest.mark.asyncio(loop_scope="session") async def test_rankings_endpoint( startup_and_shutdown_uvicorn_server: None, auth_context: AuthContext ) -> None: @@ -38,6 +41,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 +54,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 +72,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..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 @@ -27,6 +29,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..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 @@ -16,6 +18,7 @@ ) +@pytest.mark.asyncio(loop_scope="session") async def test_create_round( startup_and_shutdown_uvicorn_server: None, auth_context: AuthContext ) -> None: @@ -46,6 +49,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 +76,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..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 ( @@ -27,6 +29,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..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 @@ -22,6 +24,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 +64,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 +88,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..2433fcb99 100644 --- a/backend/tests/integration_tests/api/stages_test.py +++ b/backend/tests/integration_tests/api/stages_test.py @@ -27,6 +27,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 +88,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 +108,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 +127,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 +157,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 +181,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..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 @@ -12,6 +13,7 @@ from tests.integration_tests.sql import assert_row_count_and_clear, inserted_team +@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..baa96f38b 100644 --- a/backend/tests/integration_tests/api/tournaments_test.py +++ b/backend/tests/integration_tests/api/tournaments_test.py @@ -1,6 +1,7 @@ import aiofiles import aiofiles.os import aiohttp +import pytest from bracket.database import database from bracket.logic.tournaments import sql_delete_tournament_completely @@ -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..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 @@ -10,6 +12,7 @@ from tests.integration_tests.sql import assert_row_count_and_clear +@pytest.mark.asyncio(loop_scope="session") async def test_users_endpoint( startup_and_shutdown_uvicorn_server: None, auth_context: AuthContext ) -> None: @@ -31,6 +34,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 +51,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 +63,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..a2e22f177 100644 --- a/backend/tests/integration_tests/conftest.py +++ b/backend/tests/integration_tests/conftest.py @@ -1,11 +1,10 @@ # pylint: disable=redefined-outer-name -import asyncio import os -from asyncio import AbstractEventLoop from collections.abc import AsyncIterator from time import sleep import pytest +import pytest_asyncio from databases import Database from bracket.database import database, engine @@ -14,19 +13,8 @@ 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.fixture(scope="session", autouse=True) -async def reinit_database(event_loop: AbstractEventLoop, worker_id: str) -> AsyncIterator[Database]: +@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. 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,