-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
100 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import anyio | ||
import pytest | ||
|
||
from esmerald.param_functions import Requires | ||
from esmerald.utils.dependencies import async_resolve_dependencies, resolve_dependencies | ||
|
||
|
||
def get_user(): | ||
return {"id": 1, "name": "Alice"} | ||
|
||
|
||
def get_current_user(user=Requires(get_user)): | ||
return user | ||
|
||
|
||
async def get_async_user(): | ||
await anyio.sleep(0.1) | ||
return {"id": 2, "name": "Bob"} | ||
|
||
|
||
async def async_endpoint(current_user=Requires(get_async_user)): | ||
return {"message": "Hello", "user": current_user} | ||
|
||
|
||
def endpoint(current_user=Requires(get_current_user)): | ||
return {"message": "Hello", "user": current_user} | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_required_dependency_async(): | ||
async_result = await async_resolve_dependencies(async_endpoint) | ||
|
||
assert async_result == {"message": "Hello", "user": {"id": 2, "name": "Bob"}} | ||
|
||
|
||
def test_required_dependency(): | ||
result = resolve_dependencies(endpoint) | ||
assert result == {"message": "Hello", "user": {"id": 1, "name": "Alice"}} |