Skip to content

Commit 5cdf114

Browse files
committed
Add tests for nested deps resolution
1 parent cc2304e commit 5cdf114

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

tests/unit/samples/wiringstringids/resourceclosing.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ def __init__(self, service: Service):
2525
self.service = service
2626

2727

28+
class NestedService:
29+
def __init__(self, factory_service: FactoryService):
30+
self.factory_service = factory_service
31+
32+
2833
def init_service():
2934
service = Service()
3035
service.init()
@@ -36,6 +41,7 @@ class Container(containers.DeclarativeContainer):
3641

3742
service = providers.Resource(init_service)
3843
factory_service = providers.Factory(FactoryService, service)
44+
nested_service = providers.Factory(NestedService, factory_service)
3945

4046

4147
@inject
@@ -46,3 +52,10 @@ def test_function(service: Service = Closing[Provide["service"]]):
4652
@inject
4753
def test_function_dependency(factory: FactoryService = Closing[Provide["factory_service"]]):
4854
return factory
55+
56+
57+
@inject
58+
def test_function_nested_dependency(
59+
nested: NestedService = Closing[Provide["nested_service"]]
60+
):
61+
return nested

tests/unit/wiring/string_ids/test_main_py36.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,23 @@ def test_closing_dependency_resource():
306306
assert result_1 is not result_2
307307

308308

309+
@mark.usefixtures("resourceclosing_container")
310+
def test_closing_nested_dependency_resource():
311+
resourceclosing.Service.reset_counter()
312+
313+
result_1 = resourceclosing.test_function_nested_dependency()
314+
assert isinstance(result_1, resourceclosing.NestedService)
315+
assert result_1.factory_service.service.init_counter == 1
316+
assert result_1.factory_service.service.shutdown_counter == 1
317+
318+
result_2 = resourceclosing.test_function_nested_dependency()
319+
assert isinstance(result_2, resourceclosing.NestedService)
320+
assert result_2.factory_service.service.init_counter == 2
321+
assert result_2.factory_service.service.shutdown_counter == 2
322+
323+
assert result_1 is not result_2
324+
325+
309326
@mark.usefixtures("resourceclosing_container")
310327
def test_closing_resource_bypass_marker_injection():
311328
resourceclosing.Service.reset_counter()

0 commit comments

Comments
 (0)