@@ -190,6 +190,64 @@ class ContainerWithArgsAndKwArgs(containers.DeclarativeContainer):
190190
191191 self .assertIsNot (service1 .client , service2 .client )
192192
193+ def test_injection_error (self ):
194+ async def init_resource ():
195+ raise Exception ('Something went wrong' )
196+
197+ class Container (containers .DeclarativeContainer ):
198+ resource_with_error = providers .Resource (init_resource )
199+
200+ client = providers .Factory (
201+ Client ,
202+ resource1 = resource_with_error ,
203+ resource2 = None ,
204+ )
205+
206+ container = Container ()
207+
208+ with self .assertRaises (Exception ) as context :
209+ self ._run (container .client ())
210+ self .assertEqual (str (context .exception ), 'Something went wrong' )
211+
212+ def test_injection_runtime_error_async_provides (self ):
213+ async def create_client (* args , ** kwargs ):
214+ raise Exception ('Something went wrong' )
215+
216+ class Container (containers .DeclarativeContainer ):
217+ resource = providers .Resource (init_resource , providers .Object (RESOURCE1 ))
218+
219+ client = providers .Factory (
220+ create_client ,
221+ resource1 = resource ,
222+ resource2 = None ,
223+ )
224+
225+ container = Container ()
226+
227+ with self .assertRaises (Exception ) as context :
228+ self ._run (container .client ())
229+ self .assertEqual (str (context .exception ), 'Something went wrong' )
230+
231+ def test_injection_call_error_async_provides (self ):
232+ async def create_client (): # <-- no args defined
233+ ...
234+
235+ class Container (containers .DeclarativeContainer ):
236+ resource = providers .Resource (init_resource , providers .Object (RESOURCE1 ))
237+
238+ client = providers .Factory (
239+ create_client ,
240+ resource1 = resource ,
241+ resource2 = None ,
242+ )
243+
244+ container = Container ()
245+
246+ with self .assertRaises (TypeError ) as context :
247+ self ._run (container .client ())
248+ self .assertIn ("create_client() got" , str (context .exception ))
249+ self .assertIn ("unexpected keyword argument" , str (context .exception ))
250+
193251 def test_attributes_injection (self ):
194252 class ContainerWithAttributes (containers .DeclarativeContainer ):
195253 resource1 = providers .Resource (init_resource , providers .Object (RESOURCE1 ))
@@ -236,6 +294,53 @@ class ContainerWithAttributes(containers.DeclarativeContainer):
236294
237295 self .assertIsNot (service1 .client , service2 .client )
238296
297+ def test_attributes_injection_attribute_error (self ):
298+ class ClientWithException (Client ):
299+ @property
300+ def attribute_set_error (self ):
301+ return None
302+
303+ @attribute_set_error .setter
304+ def attribute_set_error (self , value ):
305+ raise Exception ('Something went wrong' )
306+
307+ class Container (containers .DeclarativeContainer ):
308+ resource = providers .Resource (init_resource , providers .Object (RESOURCE1 ))
309+
310+ client = providers .Factory (
311+ ClientWithException ,
312+ resource1 = resource ,
313+ resource2 = resource ,
314+ )
315+ client .add_attributes (attribute_set_error = 123 )
316+
317+ container = Container ()
318+
319+ with self .assertRaises (Exception ) as context :
320+ self ._run (container .client ())
321+ self .assertEqual (str (context .exception ), 'Something went wrong' )
322+
323+ def test_attributes_injection_runtime_error (self ):
324+ async def init_resource ():
325+ raise Exception ('Something went wrong' )
326+
327+ class Container (containers .DeclarativeContainer ):
328+ resource = providers .Resource (init_resource )
329+
330+ client = providers .Factory (
331+ Client ,
332+ resource1 = None ,
333+ resource2 = None ,
334+ )
335+ client .add_attributes (resource1 = resource )
336+ client .add_attributes (resource2 = resource )
337+
338+ container = Container ()
339+
340+ with self .assertRaises (Exception ) as context :
341+ self ._run (container .client ())
342+ self .assertEqual (str (context .exception ), 'Something went wrong' )
343+
239344 def test_async_instance_and_sync_attributes_injection (self ):
240345 class ContainerWithAttributes (containers .DeclarativeContainer ):
241346 resource1 = providers .Resource (init_resource , providers .Object (RESOURCE1 ))
0 commit comments