Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yakimka committed Jan 3, 2025
1 parent cd87904 commit 8417436
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 6 deletions.
24 changes: 24 additions & 0 deletions tests/test_integrations/test_pytest_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,30 @@ def test_hello_default():
assert "marker don't support positional arguments" in "".join(result.outlines)


def test_cant_use_init_dependencies_without_kwargs(pytester):
pytester.makeconftest(
"""
pytest_plugins = ["picodi.integrations._pytest"]
"""
)

pytester.makepyfile(
"""
import pytest
from picodi import Provide, inject, dependency, SingletonScope
@pytest.mark.picodi_init_dependencies
def test_hello_default():
pass
"""
)

result = pytester.runpytest()

assert "marker must have keyword arguments" in "".join(result.outlines)


def test_fixtures_executes_in_strict_order(pytester):
pytester.makeconftest(
"""
Expand Down
12 changes: 6 additions & 6 deletions tests/test_integrations/test_starlette_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@

@pytest.fixture()
def make_app():
def maker(dependencies_for_init: InitDependencies | None = None):
def maker(dependencies_for_init: InitDependencies):
def sync_view(request: Request) -> PlainTextResponse: # noqa: U100
return PlainTextResponse("sync view")

async def async_view(request: Request) -> PlainTextResponse: # noqa: U100
return PlainTextResponse("async view")

kwargs = {}
if dependencies_for_init:
kwargs["dependencies_for_init"] = dependencies_for_init

return Starlette(
routes=[Route("/sync-view", sync_view), Route("/async-view", async_view)],
middleware=[Middleware(RequestScopeMiddleware, **kwargs)],
middleware=[
Middleware(
RequestScopeMiddleware, dependencies_for_init=dependencies_for_init
)
],
)

return maker
Expand Down
19 changes: 19 additions & 0 deletions tests/test_yield_dep.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,25 @@ async def my_async_singleton_scope_dep(number: int = Provide(get_42)):
assert called == 1


def test_can_init_injected_singleton_scope_dep_argument_passed_as_callable():
called = 0

def get_42():
return 42

@dependency(scope_class=SingletonScope)
@inject
def my_singleton_scope_dep(number: int = Provide(get_42)):
assert number == 42
nonlocal called
called += 1
return number

init_dependencies(lambda: [my_singleton_scope_dep])

assert called == 1


async def test_can_resolve_yield_in_yield_with_correct_scopes():
context_calls = []

Expand Down

0 comments on commit 8417436

Please sign in to comment.