mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2025-07-06 13:23:15 +03:00
Add test
This commit is contained in:
parent
c08ac48931
commit
5474bd2ad1
31
tests/unit/samples/wiringsamples/resourceclosing.py
Normal file
31
tests/unit/samples/wiringsamples/resourceclosing.py
Normal file
|
@ -0,0 +1,31 @@
|
|||
from dependency_injector import containers, providers
|
||||
from dependency_injector.wiring import Provide, Closing
|
||||
|
||||
|
||||
class Service:
|
||||
init_counter: int = 0
|
||||
shutdown_counter: int = 0
|
||||
|
||||
@classmethod
|
||||
def init(cls):
|
||||
cls.init_counter += 1
|
||||
|
||||
@classmethod
|
||||
def shutdown(cls):
|
||||
cls.shutdown_counter += 1
|
||||
|
||||
|
||||
def init_service():
|
||||
service = Service()
|
||||
service.init()
|
||||
yield service
|
||||
service.shutdown()
|
||||
|
||||
|
||||
class Container(containers.DeclarativeContainer):
|
||||
|
||||
service = providers.Resource(init_service)
|
||||
|
||||
|
||||
def test_function(service: Service = Closing[Provide[Container.service]]):
|
||||
return service
|
|
@ -174,3 +174,22 @@ class WiringTest(unittest.TestCase):
|
|||
|
||||
self.assertIsInstance(service, Service)
|
||||
self.assertEqual(some_value, 1)
|
||||
|
||||
def test_closing_resource(self):
|
||||
from wiringsamples import resourceclosing
|
||||
|
||||
container = resourceclosing.Container()
|
||||
container.wire(modules=[resourceclosing])
|
||||
self.addCleanup(container.unwire)
|
||||
|
||||
result_1 = resourceclosing.test_function()
|
||||
self.assertIsInstance(result_1, resourceclosing.Service)
|
||||
self.assertEqual(result_1.init_counter, 1)
|
||||
self.assertEqual(result_1.shutdown_counter, 1)
|
||||
|
||||
result_2 = resourceclosing.test_function()
|
||||
self.assertIsInstance(result_2, resourceclosing.Service)
|
||||
self.assertEqual(result_2.init_counter, 2)
|
||||
self.assertEqual(result_2.shutdown_counter, 2)
|
||||
|
||||
self.assertIsNot(result_1, result_2)
|
||||
|
|
Loading…
Reference in New Issue
Block a user