-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add `enter` helper * Update code after rebase * Change `enter` realisation * Add `helpers.enter` context manager for resolving dependencies in pytest fixtures * Fix `enter` typehints
- Loading branch information
Showing
6 changed files
with
209 additions
and
3 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
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,85 @@ | ||
from picodi import Provide, SingletonScope, dependency, inject | ||
from picodi.helpers import enter | ||
|
||
|
||
def get_42(): | ||
return 42 | ||
|
||
|
||
def test_enter_sync_gen(closeable): | ||
def dep(): | ||
yield 42 | ||
closeable.close() | ||
|
||
with enter(dep) as val: | ||
assert val == 42 | ||
assert closeable.is_closed is False | ||
|
||
assert closeable.is_closed is True | ||
|
||
|
||
async def test_enter_async_gen(closeable): | ||
async def dep(): | ||
yield 42 | ||
closeable.close() | ||
|
||
async with enter(dep) as val: | ||
assert val == 42 | ||
assert closeable.is_closed is False | ||
|
||
assert closeable.is_closed is True | ||
|
||
|
||
def test_enter_injected_sync_gen(closeable): | ||
@inject | ||
def dep(num: int = Provide(get_42)): | ||
yield num | ||
closeable.close() | ||
|
||
with enter(dep) as val: | ||
assert val == 42 | ||
assert closeable.is_closed is False | ||
|
||
assert closeable.is_closed is True | ||
|
||
|
||
async def test_enter_injected_async_gen(closeable): | ||
@inject | ||
async def dep(num: int = Provide(get_42)): | ||
yield num | ||
closeable.close() | ||
|
||
async with enter(dep) as val: | ||
assert val == 42 | ||
assert closeable.is_closed is False | ||
|
||
assert closeable.is_closed is True | ||
|
||
|
||
def test_singleton_sync_gen_not_closed(closeable): | ||
@dependency(scope_class=SingletonScope) | ||
def dep(): | ||
yield 42 | ||
closeable.close() | ||
|
||
with enter(dep) as val: | ||
assert val == 42 | ||
assert closeable.is_closed is False | ||
|
||
assert closeable.is_closed is False | ||
|
||
|
||
def test_enter_regular_dependency(): | ||
def dep(): | ||
return 42 | ||
|
||
with enter(dep) as val: | ||
assert val == 42 | ||
|
||
|
||
async def test_enter_regular_dependency_async(): | ||
async def dep(): | ||
return 42 | ||
|
||
async with enter(dep) as val: | ||
assert val == 42 |