-
Notifications
You must be signed in to change notification settings - Fork 53
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 #30 from reagento/feature/real_world_tests
Real world example tests
- Loading branch information
Showing
7 changed files
with
83 additions
and
6 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,3 @@ | ||
fastapi==0.109.0 | ||
aiogram==3.3.0 | ||
uvicorn==0.27.0 |
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 @@ | ||
-r requirements.txt | ||
|
||
pytest==7.* | ||
pytest-asyncio==0.23.* | ||
pytest-repeat==0.9.* | ||
|
||
httpx==0.26.* | ||
asgi_lifespan==2.1.* |
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,49 @@ | ||
""" | ||
In this test example we mock our interactors to check if web view | ||
works correctly. Other option was to only adapters or just use real providers. | ||
We use `fastapi.testclient.TestClient` to send requests to the app | ||
Additionally we need `asgi_lifespan.LifespanManager` to correctly enter | ||
app scope as it is done in real application | ||
""" | ||
from unittest.mock import Mock | ||
|
||
import pytest | ||
import pytest_asyncio | ||
from asgi_lifespan import LifespanManager | ||
from fastapi.testclient import TestClient | ||
|
||
from dishka import Provider, Scope, provide | ||
from dishka.integrations.fastapi import ( | ||
DishkaApp, | ||
) | ||
from main_web import create_fastapi_app | ||
from myapp.use_cases import AddProductsInteractor | ||
|
||
|
||
class FakeInteractorProvider(Provider): | ||
@provide(scope=Scope.REQUEST) | ||
def add_products(self) -> AddProductsInteractor: | ||
return Mock() | ||
|
||
|
||
@pytest.fixture | ||
def interactor_provider(): | ||
return FakeInteractorProvider() | ||
|
||
|
||
@pytest_asyncio.fixture | ||
async def client(interactor_provider): | ||
app = DishkaApp( | ||
providers=[interactor_provider], | ||
app=create_fastapi_app(), | ||
) | ||
async with LifespanManager(app): | ||
yield TestClient(app) | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_index(client): | ||
res = client.get("/") | ||
assert res.status_code == 200 | ||
assert res.json() == "Ok" |
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