-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #300 from Eclips4/improve-example
Run code from README in tests
- Loading branch information
Showing
4 changed files
with
99 additions
and
88 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,55 @@ | ||
import sqlite3 | ||
|
||
from typing import Protocol, Iterable | ||
from sqlite3 import Connection | ||
|
||
class DAO(Protocol): | ||
... | ||
|
||
class Service: | ||
def __init__(self, dao: DAO): | ||
... | ||
|
||
class DAOImpl(DAO): | ||
def __init__(self, connection: Connection): | ||
... | ||
|
||
class SomeClient: | ||
... | ||
|
||
from dishka import Provider, Scope | ||
|
||
service_provider = Provider(scope=Scope.REQUEST) | ||
service_provider.provide(Service) | ||
service_provider.provide(DAOImpl, provides=DAO) | ||
service_provider.provide(SomeClient, scope=Scope.APP) # override provider scope | ||
|
||
from dishka import Provider, provide, Scope | ||
|
||
class ConnectionProvider(Provider): | ||
@provide(scope=Scope.REQUEST) | ||
def new_connection(self) -> Iterable[Connection]: | ||
conn = sqlite3.connect(":memory:") | ||
yield conn | ||
conn.close() | ||
|
||
|
||
from dishka import make_container | ||
|
||
container = make_container(service_provider, ConnectionProvider()) | ||
|
||
client = container.get(SomeClient) # `SomeClient` has Scope.APP, so it is accessible here | ||
client = container.get(SomeClient) # same instace of `SomeClient` | ||
|
||
|
||
# subcotaniner to access more short-living objects | ||
with container() as request_container: | ||
service = request_container.get(Service) | ||
service = request_container.get(Service) # same service instance | ||
# at this point connection will be closed as we exited context manager | ||
|
||
# new subcontainer to have a new lifespan for request processing | ||
with container() as request_container: | ||
service = request_container.get(Service) # new service instance | ||
|
||
container.close() |
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,8 @@ | ||
import runpy | ||
from pathlib import Path | ||
|
||
ROOT = Path(__file__).parent.parent.parent.resolve() | ||
QUICKSTART_EXAMPLE_PATH = ROOT / "docs/quickstart_example.py" | ||
|
||
def test_readme_example(): | ||
runpy.run_path(QUICKSTART_EXAMPLE_PATH) |