Skip to content

Commit

Permalink
Fix integration test fictures
Browse files Browse the repository at this point in the history
  • Loading branch information
PabloLec committed Sep 27, 2024
1 parent 12dde51 commit 6ffd5d7
Showing 1 changed file with 40 additions and 3 deletions.
43 changes: 40 additions & 3 deletions tests/integration/test_full_workflow.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import contextvars
import functools
import traceback
from asyncio import DefaultEventLoopPolicy, Task
from pathlib import Path
from unittest.mock import MagicMock

Expand All @@ -23,10 +27,43 @@
)


@pytest.mark.asyncio(scope="class")
def task_factory(loop, coro, context=None):
stack = traceback.extract_stack()
for frame in stack[-2::-1]:
package_name = Path(frame.filename).parts[-2]
if package_name != "asyncio":
if package_name == "pytest_asyncio":
# This function was called from pytest_asyncio, use shared context
break
else:
# This function was called from somewhere else, create context copy
context = None
break
return Task(coro, loop=loop, context=context)

Check failure

Code scanning / CodeQL

Wrong name for an argument in a class instantiation Error test

Keyword argument 'context' is not a supported parameter name of
Task.__init__
.


class CustomEventLoopPolicy(DefaultEventLoopPolicy):

Check failure

Code scanning / CodeQL

Missing call to `__init__` during object initialization Error test

Class CustomEventLoopPolicy may not be initialized properly as
method BaseDefaultEventLoopPolicy.__init__
is not called from its
__init__ method
.
def __init__(self, context) -> None:
super().__init__()
self.context = context

def new_event_loop(self):
loop = self._loop_factory()
loop.set_task_factory(functools.partial(task_factory, context=self.context))
return loop


@pytest.fixture(scope="session")
def event_loop_policy():
policy = CustomEventLoopPolicy(contextvars.copy_context())
yield policy
policy.get_event_loop().close()


@pytest.mark.asyncio(loop_scope="class")
@pytest.mark.incremental
class TestFullWorkflow:
@pytest_asyncio.fixture(scope="class")
@pytest_asyncio.fixture(scope="class", loop_scope="class")
def session_patch(self, session_mocker):
session_mocker.patch(
"recoverpy.lib.env_check._is_user_root",
Expand All @@ -41,7 +78,7 @@ def session_patch(self, session_mocker):
MagicMock(return_value=True),
)

@pytest_asyncio.fixture(scope="class")
@pytest_asyncio.fixture(scope="class", loop_scope="class")
async def pilot(self, session_patch):
async with RecoverpyApp().run_test() as p:
yield p
Expand Down

0 comments on commit 6ffd5d7

Please sign in to comment.