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

Run code from README in tests #300

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ from dishka import Provider, provide, Scope
class ConnectionProvider(Provider):
@provide(scope=Scope.REQUEST)
def new_connection(self) -> Iterable[Connection]:
conn = sqlite3.connect()
conn = sqlite3.connect(":memory:")
yield conn
conn.close()
```
Expand Down
2 changes: 1 addition & 1 deletion docs/quickstart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ To provide connection we might need to write some custom code:
class ConnectionProvider(Provider):
@provide(scope=Scope.REQUEST)
def new_connection(self) -> Iterable[Connection]:
conn = sqlite3.connect()
conn = sqlite3.connect(":memory:")
yield conn
conn.close()
Expand Down
70 changes: 70 additions & 0 deletions tests/unit/test_readme_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import sqlite3
from collections.abc import Iterable
from sqlite3 import Connection
from typing import Protocol

import pytest

from dishka import Provider, Scope, make_container, provide


class DAO(Protocol):
...


class Service:
def __init__(self, dao: DAO):
...


class DAOImpl(DAO):
def __init__(self, connection: Connection):
...


class SomeClient:
...


@pytest.fixture
def service_provider():
provider = Provider(scope=Scope.REQUEST)
provider.provide(Service)
provider.provide(DAOImpl, provides=DAO)
provider.provide(SomeClient, scope=Scope.APP)
return provider


@pytest.fixture
def container(service_provider):
return make_container(service_provider, ConnectionProvider())


class ConnectionProvider(Provider):
@provide(scope=Scope.REQUEST)
def new_connection(self) -> Iterable[Connection]:
conn = sqlite3.connect(":memory:")
yield conn
conn.close()


def test_get_client(container):
client_1 = container.get(SomeClient)
client_2 = container.get(SomeClient)

assert isinstance(client_1, SomeClient)
assert client_1 is client_2


def test_subcontainers(container):
with container() as request_container:
service_1 = request_container.get(Service)
service_2 = request_container.get(Service)

assert service_1 is service_2
assert isinstance(service_1, Service)

with container() as new_request_container:
service_3 = new_request_container.get(Service)

assert service_1 is not service_3