Skip to content

Commit

Permalink
fix error when a sync FastAPI dep is used
Browse files Browse the repository at this point in the history
  • Loading branch information
melvinkcx committed Mar 29, 2024
1 parent 8bb530f commit 18764dd
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
2 changes: 1 addition & 1 deletion fastapi_events/handlers/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 29 additions & 4 deletions tests/handlers/test_local_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -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")

0 comments on commit 18764dd

Please sign in to comment.