Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support synchronous fastapi dependency #61

Merged
merged 1 commit into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
Loading