From 18764dd399c549f517662b9440cbb88fc14eb2ce Mon Sep 17 00:00:00 2001 From: melvin Date: Fri, 29 Mar 2024 11:01:14 -0400 Subject: [PATCH] fix error when a sync FastAPI dep is used --- fastapi_events/handlers/local.py | 2 +- tests/handlers/test_local_handler.py | 33 ++++++++++++++++++++++++---- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/fastapi_events/handlers/local.py b/fastapi_events/handlers/local.py index 4a0501c..ccac2aa 100644 --- a/fastapi_events/handlers/local.py +++ b/fastapi_events/handlers/local.py @@ -147,7 +147,7 @@ async def solve_dependencies( solved = await call(**sub_values) else: loop = asyncio.get_event_loop() - solved = await loop.run_in_executor(None, functools.partial(call, event, **sub_values)) + solved = await loop.run_in_executor(None, functools.partial(call, **sub_values)) if sub_dependant.name is not None: values[sub_dependant.name] = solved diff --git a/tests/handlers/test_local_handler.py b/tests/handlers/test_local_handler.py index 639270e..dbf3ec2 100644 --- a/tests/handlers/test_local_handler.py +++ b/tests/handlers/test_local_handler.py @@ -176,11 +176,11 @@ async def handle_events(event: Event): assert spans_created[-1].attributes[SpanAttributes.HANDLER] == "fastapi_events.handlers.local.LocalHandler" -def test_local_handler_with_fastapi_dependencies( +def test_local_handler_with_async_fastapi_dependencies( setup_test ): """ - to verify the support of FastAPI dependencies + to verify the support of async FastAPI dependencies Relevant Github issue: #41 """ app, handler = setup_test() @@ -207,11 +207,11 @@ async def handle_event_with_dependency( client.get("/events?event=TEST_EVENT") -def test_local_handler_with_nested_dependencies( +def test_local_handler_with_nested_async_dependencies( setup_test ): """ - to verify the support of nested FastAPI dependencies + to verify the support of nested async FastAPI dependencies Relevant Github issue: #41 """ app, handler = setup_test() @@ -242,3 +242,28 @@ async def handle_event_with_dependency( client = TestClient(app) client.get("/events?event=TEST_EVENT") + + +def test_local_handler_with_sync_fastapi_dependencies( + setup_test +): + """ + to verify the support of sync FastAPI dependencies + Relevant Github issue: #60 + """ + app, handler = setup_test() + + _mock_dependency = MagicMock() + + def get_repo(): + return _mock_dependency + + @handler.register(event_name="TEST_EVENT") + async def handle_event_with_sync_dependency( + event: Event, + repo=Depends(get_repo) + ): + assert repo == _mock_dependency + + client = TestClient(app) + client.get("/events?event=TEST_EVENT")