Skip to content

Commit

Permalink
allow sending plain objects as replacements (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
pksol authored May 24, 2022
1 parent c209588 commit 3b0000d
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ A fixture which allows easy replacement of fastapi dependencies for testing
## Installation

```bash
pip install -U pytest-fastapi-deps
pip install pytest-fastapi-deps
```

or install with `Poetry`
Expand Down Expand Up @@ -76,21 +76,22 @@ def my_second_override():
def test_get_override_two_dep(fastapi_dep):
with fastapi_dep(app).override(
{
first_dep: lambda: {"my": "override"},
first_dep: "plain_override_object",
second_dep: my_second_override,
}
):
response = client.get("/depends")
assert response.status_code == 200
assert response.json() == {
"first_dep": {"my": "override"},
"first_dep": "plain_override_object",
"second_dep": {"another": "override"},
}
```

Note how easy it is: you add the `fastapi_dep` fixture, initialize it with the fastapi
`app` and send a dictionary of overrides: the keys are the original functions while the
values are replacement functions.
values are plain objects that would be returned or replacement functions that would be
called.

If your use case is to replace the dependencies for the entire duration of your test,
you can use pytest [indirect parameters](https://docs.pytest.org/en/latest/example/parametrize.html#indirect-parametrization) to simplify the body of your test:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ build-backend = "poetry.core.masonry.api"

[tool.poetry]
name = "pytest-fastapi-deps"
version = "0.1.5"
version = "0.2.0"
description = "A fixture which allows easy replacement of fastapi dependencies for testing"
readme = "README.md"
authors = ["Peter Kogan <[email protected]>"]
Expand Down
6 changes: 5 additions & 1 deletion pytest_fastapi_deps/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,13 @@ def __enter__(self):
if dep in self._app.dependency_overrides:
# Save existing overrides
self._old_overrides[dep] = self._app.dependency_overrides[dep]
self._app.dependency_overrides[dep] = new_dep
self._app.dependency_overrides[dep] = self._callable_replacement(new_dep)
return self

@staticmethod
def _callable_replacement(new_dep):
return new_dep if callable(new_dep) else lambda: new_dep

def __exit__(self, *args: typing.Any) -> None:
for dep in self.overrides.keys():
if dep in self._old_overrides:
Expand Down
10 changes: 10 additions & 0 deletions tests/examples/test_public_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ def test_get_override_single_dep(fastapi_dep):
}


def test_get_override_single_dep_plain_object(fastapi_dep):
with fastapi_dep(app).override({first_dep: "override"}):
response = client.get("/depends")
assert response.status_code == 200
assert response.json() == {
"first_dep": "override",
"second_dep": {"skip": 20, "limit": 50},
}


def test_get_override_unrelated_dep(fastapi_dep):
with fastapi_dep(app).override(
{
Expand Down

0 comments on commit 3b0000d

Please sign in to comment.