1+ """Main wiring tests for Annotated attribute and argument injection."""
2+
3+ from decimal import Decimal
4+ import typing
5+
6+ from dependency_injector import errors
7+ from dependency_injector .wiring import Closing , Provide , Provider , wire
8+ from pytest import fixture , mark , raises
9+
10+ from samples .wiring import module_annotated as module , package , resourceclosing
11+ from samples .wiring .service import Service
12+ from samples .wiring .container import Container , SubContainer
13+
14+ @fixture (autouse = True )
15+ def container ():
16+ container = Container (config = {"a" : {"b" : {"c" : 10 }}})
17+ container .wire (
18+ modules = [module ],
19+ packages = [package ],
20+ )
21+ yield container
22+ container .unwire ()
23+
24+ @fixture
25+ def subcontainer ():
26+ container = SubContainer ()
27+ container .wire (
28+ modules = [module ],
29+ packages = [package ],
30+ )
31+ yield container
32+ container .unwire ()
33+
34+ @fixture
35+ def resourceclosing_container ():
36+ container = resourceclosing .Container ()
37+ container .wire (modules = [resourceclosing ])
38+ yield container
39+ container .unwire ()
40+
41+ def test_module_attributes_wiring ():
42+ assert isinstance (module .service , Service )
43+ assert isinstance (module .service_provider (), Service )
44+ assert isinstance (module .__annotations__ ['undefined' ], typing ._AnnotatedAlias )
45+
46+ def test_class_wiring ():
47+ test_class_object = module .TestClass ()
48+ assert isinstance (test_class_object .service , Service )
49+
50+ def test_class_wiring_context_arg (container : Container ):
51+ test_service = container .service ()
52+ test_class_object = module .TestClass (service = test_service )
53+ assert test_class_object .service is test_service
54+
55+ def test_class_method_wiring ():
56+ test_class_object = module .TestClass ()
57+ service = test_class_object .method ()
58+ assert isinstance (service , Service )
59+
60+ def test_class_classmethod_wiring ():
61+ service = module .TestClass .class_method ()
62+ assert isinstance (service , Service )
63+
64+ def test_instance_classmethod_wiring ():
65+ instance = module .TestClass ()
66+ service = instance .class_method ()
67+ assert isinstance (service , Service )
68+
69+ def test_class_staticmethod_wiring ():
70+ service = module .TestClass .static_method ()
71+ assert isinstance (service , Service )
72+
73+ def test_instance_staticmethod_wiring ():
74+ instance = module .TestClass ()
75+ service = instance .static_method ()
76+ assert isinstance (service , Service )
77+
78+ def test_class_attribute_wiring ():
79+ assert isinstance (module .TestClass .service , Service )
80+ assert isinstance (module .TestClass .service_provider (), Service )
81+ assert isinstance (module .TestClass .__annotations__ ['undefined' ], typing ._AnnotatedAlias )
82+
83+ def test_function_wiring ():
84+ service = module .test_function ()
85+ assert isinstance (service , Service )
86+
87+ def test_function_wiring_context_arg (container : Container ):
88+ test_service = container .service ()
89+ service = module .test_function (service = test_service )
90+ assert service is test_service
91+
92+ def test_function_wiring_provider ():
93+ service = module .test_function_provider ()
94+ assert isinstance (service , Service )
95+
96+ def test_function_wiring_provider_context_arg (container : Container ):
97+ test_service = container .service ()
98+ service = module .test_function_provider (service_provider = lambda : test_service )
99+ assert service is test_service
100+
101+ def test_configuration_option ():
102+ (
103+ value_int ,
104+ value_float ,
105+ value_str ,
106+ value_decimal ,
107+ value_required ,
108+ value_required_int ,
109+ value_required_float ,
110+ value_required_str ,
111+ value_required_decimal ,
112+ ) = module .test_config_value ()
113+
114+ assert value_int == 10
115+ assert value_float == 10.0
116+ assert value_str == "10"
117+ assert value_decimal == Decimal (10 )
118+ assert value_required == 10
119+ assert value_required_int == 10
120+ assert value_required_float == 10.0
121+ assert value_required_str == "10"
122+ assert value_required_decimal == Decimal (10 )
123+
124+ def test_configuration_option_required_undefined (container : Container ):
125+ container .config .reset_override ()
126+ with raises (errors .Error , match = "Undefined configuration option \" config.a.b.c\" " ):
127+ module .test_config_value_required_undefined ()
128+
129+ def test_provide_provider ():
130+ service = module .test_provide_provider ()
131+ assert isinstance (service , Service )
132+
133+ def test_provider_provider ():
134+ service = module .test_provider_provider ()
135+ assert isinstance (service , Service )
136+
137+ def test_provided_instance (container : Container ):
138+ class TestService :
139+ foo = {"bar" : lambda : 10 }
140+
141+ with container .service .override (TestService ()):
142+ some_value = module .test_provided_instance ()
143+ assert some_value == 10
144+
145+ def test_subcontainer ():
146+ some_value = module .test_subcontainer_provider ()
147+ assert some_value == 1
148+
149+ def test_config_invariant (container : Container ):
150+ config = {
151+ "option" : {
152+ "a" : 1 ,
153+ "b" : 2 ,
154+ },
155+ "switch" : "a" ,
156+ }
157+ container .config .from_dict (config )
158+
159+ value_default = module .test_config_invariant ()
160+ assert value_default == 1
161+
162+ with container .config .switch .override ("a" ):
163+ value_a = module .test_config_invariant ()
164+ assert value_a == 1
165+
166+ with container .config .switch .override ("b" ):
167+ value_b = module .test_config_invariant ()
168+ assert value_b == 2
169+
170+ def test_class_decorator ():
171+ service = module .test_class_decorator ()
172+ assert isinstance (service , Service )
173+
174+ def test_container ():
175+ service = module .test_container ()
176+ assert isinstance (service , Service )
0 commit comments