Add tests for nested deps resolution

This commit is contained in:
jazzthief 2023-05-08 17:55:01 +02:00
parent cc2304e46e
commit 5cdf114237
No known key found for this signature in database
GPG Key ID: 650DC0A0E6B6947C
2 changed files with 30 additions and 0 deletions

View File

@ -25,6 +25,11 @@ class FactoryService:
self.service = service
class NestedService:
def __init__(self, factory_service: FactoryService):
self.factory_service = factory_service
def init_service():
service = Service()
service.init()
@ -36,6 +41,7 @@ class Container(containers.DeclarativeContainer):
service = providers.Resource(init_service)
factory_service = providers.Factory(FactoryService, service)
nested_service = providers.Factory(NestedService, factory_service)
@inject
@ -46,3 +52,10 @@ def test_function(service: Service = Closing[Provide["service"]]):
@inject
def test_function_dependency(factory: FactoryService = Closing[Provide["factory_service"]]):
return factory
@inject
def test_function_nested_dependency(
nested: NestedService = Closing[Provide["nested_service"]]
):
return nested

View File

@ -306,6 +306,23 @@ def test_closing_dependency_resource():
assert result_1 is not result_2
@mark.usefixtures("resourceclosing_container")
def test_closing_nested_dependency_resource():
resourceclosing.Service.reset_counter()
result_1 = resourceclosing.test_function_nested_dependency()
assert isinstance(result_1, resourceclosing.NestedService)
assert result_1.factory_service.service.init_counter == 1
assert result_1.factory_service.service.shutdown_counter == 1
result_2 = resourceclosing.test_function_nested_dependency()
assert isinstance(result_2, resourceclosing.NestedService)
assert result_2.factory_service.service.init_counter == 2
assert result_2.factory_service.service.shutdown_counter == 2
assert result_1 is not result_2
@mark.usefixtures("resourceclosing_container")
def test_closing_resource_bypass_marker_injection():
resourceclosing.Service.reset_counter()