2
2
3
3
from decimal import Decimal
4
4
5
- from dependency_injector import errors
6
- from dependency_injector .wiring import Closing , Provide , Provider , wire
7
5
from pytest import fixture , mark , raises
8
-
9
6
from samples .wiringstringids import module , package , resourceclosing
10
- from samples .wiringstringids .service import Service
11
7
from samples .wiringstringids .container import Container , SubContainer
8
+ from samples .wiringstringids .service import Service
9
+
10
+ from dependency_injector import errors
11
+ from dependency_injector .wiring import Closing , Provide , Provider , wire
12
12
13
13
14
14
@fixture (autouse = True )
@@ -34,10 +34,11 @@ def subcontainer():
34
34
35
35
36
36
@fixture
37
- def resourceclosing_container ():
37
+ def resourceclosing_container (request ):
38
38
container = resourceclosing .Container ()
39
39
container .wire (modules = [resourceclosing ])
40
- yield container
40
+ with container .reset_singletons ():
41
+ yield container
41
42
container .unwire ()
42
43
43
44
@@ -274,42 +275,65 @@ def test_wire_multiple_containers():
274
275
275
276
@mark .usefixtures ("resourceclosing_container" )
276
277
def test_closing_resource ():
277
- resourceclosing .Service .reset_counter ()
278
-
279
278
result_1 = resourceclosing .test_function ()
280
279
assert isinstance (result_1 , resourceclosing .Service )
281
280
assert result_1 .init_counter == 1
282
281
assert result_1 .shutdown_counter == 1
282
+ assert result_1 .dependencies == {"_list" : [1 , 2 ], "_dict" : {"a" : 3 , "b" : 4 }}
283
283
284
284
result_2 = resourceclosing .test_function ()
285
285
assert isinstance (result_2 , resourceclosing .Service )
286
286
assert result_2 .init_counter == 2
287
287
assert result_2 .shutdown_counter == 2
288
+ assert result_1 .dependencies == {"_list" : [1 , 2 ], "_dict" : {"a" : 3 , "b" : 4 }}
288
289
289
290
assert result_1 is not result_2
290
291
291
292
292
293
@mark .usefixtures ("resourceclosing_container" )
293
294
def test_closing_dependency_resource ():
294
- resourceclosing .Service .reset_counter ()
295
-
296
295
result_1 = resourceclosing .test_function_dependency ()
297
296
assert isinstance (result_1 , resourceclosing .FactoryService )
298
- assert result_1 .service .init_counter == 1
299
- assert result_1 .service .shutdown_counter == 1
297
+ assert result_1 .service .init_counter == 2
298
+ assert result_1 .service .shutdown_counter == 2
300
299
301
300
result_2 = resourceclosing .test_function_dependency ()
301
+
302
302
assert isinstance (result_2 , resourceclosing .FactoryService )
303
- assert result_2 .service .init_counter == 2
304
- assert result_2 .service .shutdown_counter == 2
303
+ assert result_2 .service .init_counter == 4
304
+ assert result_2 .service .shutdown_counter == 4
305
+
306
+
307
+ @mark .usefixtures ("resourceclosing_container" )
308
+ def test_closing_dependency_resource_kwargs ():
309
+ result_1 = resourceclosing .test_function_dependency_kwargs ()
310
+ assert isinstance (result_1 , resourceclosing .FactoryService )
311
+ assert result_1 .service .init_counter == 2
312
+ assert result_1 .service .shutdown_counter == 2
313
+
314
+ result_2 = resourceclosing .test_function_dependency_kwargs ()
315
+ assert isinstance (result_2 , resourceclosing .FactoryService )
316
+ assert result_2 .service .init_counter == 4
317
+ assert result_2 .service .shutdown_counter == 4
318
+
319
+
320
+ @mark .usefixtures ("resourceclosing_container" )
321
+ def test_closing_nested_dependency_resource ():
322
+ result_1 = resourceclosing .test_function_nested_dependency ()
323
+ assert isinstance (result_1 , resourceclosing .NestedService )
324
+ assert result_1 .factory_service .service .init_counter == 2
325
+ assert result_1 .factory_service .service .shutdown_counter == 2
326
+
327
+ result_2 = resourceclosing .test_function_nested_dependency ()
328
+ assert isinstance (result_2 , resourceclosing .NestedService )
329
+ assert result_2 .factory_service .service .init_counter == 4
330
+ assert result_2 .factory_service .service .shutdown_counter == 4
305
331
306
332
assert result_1 is not result_2
307
333
308
334
309
335
@mark .usefixtures ("resourceclosing_container" )
310
336
def test_closing_resource_bypass_marker_injection ():
311
- resourceclosing .Service .reset_counter ()
312
-
313
337
result_1 = resourceclosing .test_function (service = Closing [Provide ["service" ]])
314
338
assert isinstance (result_1 , resourceclosing .Service )
315
339
assert result_1 .init_counter == 1
@@ -325,7 +349,6 @@ def test_closing_resource_bypass_marker_injection():
325
349
326
350
@mark .usefixtures ("resourceclosing_container" )
327
351
def test_closing_resource_context ():
328
- resourceclosing .Service .reset_counter ()
329
352
service = resourceclosing .Service ()
330
353
331
354
result_1 = resourceclosing .test_function (service = service )
0 commit comments