5
5
import unittest
6
6
7
7
from dependency_injector import (
8
+ containers ,
8
9
providers ,
9
10
errors ,
10
11
)
@@ -498,14 +499,44 @@ def setUp(self):
498
499
self .example_b_factory = providers .Factory (self .ExampleB )
499
500
self .factory_aggregate = providers .FactoryAggregate (
500
501
example_a = self .example_a_factory ,
501
- example_b = self .example_b_factory )
502
+ example_b = self .example_b_factory ,
503
+ )
502
504
503
505
def test_is_provider (self ):
504
506
self .assertTrue (providers .is_provider (self .factory_aggregate ))
505
507
506
508
def test_is_delegated_provider (self ):
507
509
self .assertTrue (providers .is_delegated (self .factory_aggregate ))
508
510
511
+ def test_init_with_non_string_keys (self ):
512
+ factory = providers .FactoryAggregate ({
513
+ self .ExampleA : self .example_a_factory ,
514
+ self .ExampleB : self .example_b_factory ,
515
+ })
516
+
517
+ object_a = factory (self .ExampleA , 1 , 2 , init_arg3 = 3 , init_arg4 = 4 )
518
+ object_b = factory (self .ExampleB , 11 , 22 , init_arg3 = 33 , init_arg4 = 44 )
519
+
520
+ self .assertIsInstance (object_a , self .ExampleA )
521
+ self .assertEqual (object_a .init_arg1 , 1 )
522
+ self .assertEqual (object_a .init_arg2 , 2 )
523
+ self .assertEqual (object_a .init_arg3 , 3 )
524
+ self .assertEqual (object_a .init_arg4 , 4 )
525
+
526
+ self .assertIsInstance (object_b , self .ExampleB )
527
+ self .assertEqual (object_b .init_arg1 , 11 )
528
+ self .assertEqual (object_b .init_arg2 , 22 )
529
+ self .assertEqual (object_b .init_arg3 , 33 )
530
+ self .assertEqual (object_b .init_arg4 , 44 )
531
+
532
+ self .assertEqual (
533
+ factory .factories ,
534
+ {
535
+ self .ExampleA : self .example_a_factory ,
536
+ self .ExampleB : self .example_b_factory ,
537
+ },
538
+ )
539
+
509
540
def test_init_with_not_a_factory (self ):
510
541
with self .assertRaises (errors .Error ):
511
542
providers .FactoryAggregate (
@@ -528,7 +559,37 @@ def test_init_optional_factories(self):
528
559
self .assertIsInstance (provider ('example_a' ), self .ExampleA )
529
560
self .assertIsInstance (provider ('example_b' ), self .ExampleB )
530
561
531
- def test_set_provides_returns_self (self ):
562
+ def test_set_factories_with_non_string_keys (self ):
563
+ factory = providers .FactoryAggregate ()
564
+ factory .set_factories ({
565
+ self .ExampleA : self .example_a_factory ,
566
+ self .ExampleB : self .example_b_factory ,
567
+ })
568
+
569
+ object_a = factory (self .ExampleA , 1 , 2 , init_arg3 = 3 , init_arg4 = 4 )
570
+ object_b = factory (self .ExampleB , 11 , 22 , init_arg3 = 33 , init_arg4 = 44 )
571
+
572
+ self .assertIsInstance (object_a , self .ExampleA )
573
+ self .assertEqual (object_a .init_arg1 , 1 )
574
+ self .assertEqual (object_a .init_arg2 , 2 )
575
+ self .assertEqual (object_a .init_arg3 , 3 )
576
+ self .assertEqual (object_a .init_arg4 , 4 )
577
+
578
+ self .assertIsInstance (object_b , self .ExampleB )
579
+ self .assertEqual (object_b .init_arg1 , 11 )
580
+ self .assertEqual (object_b .init_arg2 , 22 )
581
+ self .assertEqual (object_b .init_arg3 , 33 )
582
+ self .assertEqual (object_b .init_arg4 , 44 )
583
+
584
+ self .assertEqual (
585
+ factory .factories ,
586
+ {
587
+ self .ExampleA : self .example_a_factory ,
588
+ self .ExampleB : self .example_b_factory ,
589
+ },
590
+ )
591
+
592
+ def test_set_factories_returns_self (self ):
532
593
provider = providers .FactoryAggregate ()
533
594
self .assertIs (provider .set_factories (example_a = self .example_a_factory ), provider )
534
595
@@ -603,6 +664,24 @@ def test_deepcopy(self):
603
664
self .assertIsInstance (self .factory_aggregate .example_b , type (provider_copy .example_b ))
604
665
self .assertIs (self .factory_aggregate .example_b .cls , provider_copy .example_b .cls )
605
666
667
+ def test_deepcopy_with_non_string_keys (self ):
668
+ factory_aggregate = providers .FactoryAggregate ({
669
+ self .ExampleA : self .example_a_factory ,
670
+ self .ExampleB : self .example_b_factory ,
671
+ })
672
+ provider_copy = providers .deepcopy (factory_aggregate )
673
+
674
+ self .assertIsNot (factory_aggregate , provider_copy )
675
+ self .assertIsInstance (provider_copy , type (factory_aggregate ))
676
+
677
+ self .assertIsNot (factory_aggregate .factories [self .ExampleA ], provider_copy .factories [self .ExampleA ])
678
+ self .assertIsInstance (factory_aggregate .factories [self .ExampleA ], type (provider_copy .factories [self .ExampleA ]))
679
+ self .assertIs (factory_aggregate .factories [self .ExampleA ].cls , provider_copy .factories [self .ExampleA ].cls )
680
+
681
+ self .assertIsNot (factory_aggregate .factories [self .ExampleB ], provider_copy .factories [self .ExampleB ])
682
+ self .assertIsInstance (factory_aggregate .factories [self .ExampleB ], type (provider_copy .factories [self .ExampleB ]))
683
+ self .assertIs (factory_aggregate .factories [self .ExampleB ].cls , provider_copy .factories [self .ExampleB ].cls )
684
+
606
685
def test_repr (self ):
607
686
self .assertEqual (repr (self .factory_aggregate ),
608
687
'<dependency_injector.providers.'
0 commit comments