Skip to content

Commit

Permalink
Updated dependency overrides.
Browse files Browse the repository at this point in the history
Signed-off-by: Pavel Kirilin <[email protected]>
  • Loading branch information
s3rius committed Aug 12, 2023
1 parent 217d9bc commit 9492ba2
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 48 deletions.
22 changes: 18 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

This project was initially created to show the abillities of [taskiq-dependencies](https://github.com/taskiq-python/taskiq-dependencies) project, which is used by [taskiq](https://github.com/taskiq-python/taskiq) to provide you with the best experience of sending distributed tasks.

This project adds [FastAPI](https://github.com/tiangolo/fastapi)-like dependency injection to your [AioHTTP](https://github.com/aio-libs/aiohttp) application.
This project adds [FastAPI](https://github.com/tiangolo/fastapi)-like dependency injection to your [AioHTTP](https://github.com/aio-libs/aiohttp) application and swagger documentation based on types.

To start using dependency injection, just initialize the injector.

Expand Down Expand Up @@ -362,8 +362,8 @@ async def my_handler(var: str = Depends(Path())):
Sometimes for tests you don't want to calculate actual functions
and you want to pass another functions instead.

To do so, you can add "dependency_overrides" key to the aplication.
It's a dict that is passed as additional context to dependency resolvers.
To do so, you can add "dependency_overrides" or "values_overrides" to the aplication's state.
These values should be dicts.

Here's an example.

Expand All @@ -385,5 +385,19 @@ where you create your application. And make sure that keys
of that dict are actual function that are being replaced.

```python
my_app["dependency_overrides"] = {original_dep: 2}
my_app["values_overrides"] = {original_dep: 2}
```

But `values_overrides` only overrides values. If you want to
override functions, you have to use `dependency_overrides`. Here's an example:

```python
def replacing_function() -> int:
return 2


my_app["dependency_overrides"] = {original_dep: replacing_function}
```

The cool point about `dependency_overrides`, is that it recalculates graph and
you can use dependencies in function that replaces the original.
3 changes: 2 additions & 1 deletion aiohttp_deps/initializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ async def __call__(self, request: web.Request) -> web.StreamResponse:
{
web.Request: request,
web.Application: request.app,
**request.app.get("dependency_overrides", {}),
**request.app.get("values_overrides", {}),
},
replaced_deps=request.app.get("dependency_overrides"),
) as resolver:
return await self.original_handler(**(await resolver.resolve_kwargs()))

Expand Down
3 changes: 2 additions & 1 deletion aiohttp_deps/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ async def _iter(self) -> StreamResponse:
{
web.Request: self.request,
web.Application: self.request.app,
**self.request.app.get("dependency_overrides", {}),
**self.request.app.get("values_overrides", {}),
},
replaced_deps=self.request.app.get("dependency_overrides"),
) as ctx:
return await method(**(await ctx.resolve_kwargs())) # type: ignore
80 changes: 40 additions & 40 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 24 additions & 1 deletion tests/test_func_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,26 @@ async def handler(app: web.Application = Depends()):
assert "Application" in (await resp.json())["request"]


@pytest.mark.anyio
async def test_values_override(
my_app: web.Application,
aiohttp_client: ClientGenerator,
):
def original_dep() -> int:
return 1

async def handler(num: int = Depends(original_dep)):
return web.json_response({"request": num})

my_app.router.add_get("/", handler)
my_app["values_overrides"] = {original_dep: 2}

client = await aiohttp_client(my_app)
resp = await client.get("/")
assert resp.status == 200
assert (await resp.json())["request"] == 2


@pytest.mark.anyio
async def test_dependency_override(
my_app: web.Application,
Expand All @@ -45,11 +65,14 @@ async def test_dependency_override(
def original_dep() -> int:
return 1

def custom_dep() -> int:
return 2

async def handler(num: int = Depends(original_dep)):
return web.json_response({"request": num})

my_app.router.add_get("/", handler)
my_app["dependency_overrides"] = {original_dep: 2}
my_app["dependency_overrides"] = {original_dep: custom_dep}

client = await aiohttp_client(my_app)
resp = await client.get("/")
Expand Down
27 changes: 26 additions & 1 deletion tests/test_view_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,28 @@ async def get(self):
assert resp.status == 405


@pytest.mark.anyio
async def test_values_override(
my_app: web.Application,
aiohttp_client: ClientGenerator,
):
def original_dep() -> int:
return 1

class MyView(View):
async def get(self, num: int = Depends(original_dep)):
"""Nothing."""
return web.json_response({"request": num})

my_app.router.add_view("/", MyView)
my_app["values_overrides"] = {original_dep: 2}

client = await aiohttp_client(my_app)
resp = await client.get("/")
assert resp.status == 200
assert (await resp.json())["request"] == 2


@pytest.mark.anyio
async def test_dependency_override(
my_app: web.Application,
Expand All @@ -63,13 +85,16 @@ async def test_dependency_override(
def original_dep() -> int:
return 1

def replaced() -> int:
return 2

class MyView(View):
async def get(self, num: int = Depends(original_dep)):
"""Nothing."""
return web.json_response({"request": num})

my_app.router.add_view("/", MyView)
my_app["dependency_overrides"] = {original_dep: 2}
my_app["dependency_overrides"] = {original_dep: replaced}

client = await aiohttp_client(my_app)
resp = await client.get("/")
Expand Down

0 comments on commit 9492ba2

Please sign in to comment.