diff --git a/tests/base.py b/tests/base.py index 10aa3c80..f15c81ab 100644 --- a/tests/base.py +++ b/tests/base.py @@ -1,6 +1,7 @@ """Contains base test case with needed setup and helpers.""" import os +import warnings from collections import namedtuple from contextlib import contextmanager from unittest import mock @@ -22,6 +23,15 @@ TestResult, TestResultFile, TestStatus, TestType) from mod_upload.models import Platform, Upload +# Filter RuntimeWarning about coroutines from AsyncMock in Python 3.13+ +# This occurs when MagicMock auto-detects async-like method names (commit, debug, etc.) +# Must be set before test execution begins. +warnings.filterwarnings( + "ignore", + message="coroutine .* was never awaited", + category=RuntimeWarning +) + @contextmanager def provide_file_at_root(file_name, to_write=None, to_delete=True): @@ -47,6 +57,26 @@ def empty_github_token(): g.github['bot_token'] = original +def setup_mock_g(mock_g): + """ + Set up mock_g with explicit MagicMock objects to avoid AsyncMock warnings. + + In Python 3.13+, MagicMock returns AsyncMock for method calls that look + async-like (commit, debug, warning, etc.). This causes RuntimeWarnings + when the code calls these methods synchronously. + + This helper sets up explicit MagicMock objects for g.db and g.log to + prevent these warnings. + + :param mock_g: The mocked g object from @mock.patch + :return: mock_g for chaining + """ + from unittest.mock import MagicMock + mock_g.db = MagicMock() + mock_g.log = MagicMock() + return mock_g + + def create_mock_db_query(mock_g, extra_setup=None): """ Create a MagicMock for mock_g.db with common query chain setup. @@ -60,6 +90,7 @@ def create_mock_db_query(mock_g, extra_setup=None): """ from unittest.mock import MagicMock mock_g.db = MagicMock() + mock_g.log = MagicMock() # Also set up log to prevent AsyncMock warnings mock_query = MagicMock() mock_g.db.query.return_value = mock_query mock_query.filter.return_value = mock_query diff --git a/tests/test_ci/test_controllers.py b/tests/test_ci/test_controllers.py index f1c4ba74..03ff6c72 100644 --- a/tests/test_ci/test_controllers.py +++ b/tests/test_ci/test_controllers.py @@ -38,6 +38,13 @@ def __init__(self, platform): self.value = 'linux' +class MockStatus: + """Mock GitHub commit status object.""" + + def __init__(self, context): + self.context = context + + class MockFork: """Mock fork object.""" @@ -781,8 +788,10 @@ def __init__(self): self.commit = "test" mock_test.query.filter.return_value.all.return_value = [MockTest()] + # Use MockStatus object instead of dict - code expects .context attribute + # Use 'linux' to match MockPlatform.value mock_repo.return_value.get_commit.return_value.get_statuses.return_value = [ - {"context": f"CI - {platform_name}"}] + MockStatus("CI - linux")] data = {'action': 'closed', 'pull_request': {'number': 1234, 'draft': False}}