From 841743673c7bad028ddb0b57c4f421fb8fa04454 Mon Sep 17 00:00:00 2001 From: yakimka Date: Fri, 3 Jan 2025 18:13:11 +0200 Subject: [PATCH] Fix tests --- .../test_pytest_integration.py | 24 +++++++++++++++++++ .../test_starlette_integration.py | 12 +++++----- tests/test_yield_dep.py | 19 +++++++++++++++ 3 files changed, 49 insertions(+), 6 deletions(-) diff --git a/tests/test_integrations/test_pytest_integration.py b/tests/test_integrations/test_pytest_integration.py index 693ca52..7c65b75 100644 --- a/tests/test_integrations/test_pytest_integration.py +++ b/tests/test_integrations/test_pytest_integration.py @@ -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( """ diff --git a/tests/test_integrations/test_starlette_integration.py b/tests/test_integrations/test_starlette_integration.py index 506eb9f..e8a6774 100644 --- a/tests/test_integrations/test_starlette_integration.py +++ b/tests/test_integrations/test_starlette_integration.py @@ -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 diff --git a/tests/test_yield_dep.py b/tests/test_yield_dep.py index 01f4940..21826d0 100644 --- a/tests/test_yield_dep.py +++ b/tests/test_yield_dep.py @@ -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 = []