Ejemplo n.º 1
0
 def test_nested_environments(self):
     with Environment(ConcreteComponent):
         c = Environment.provide(AbstractComponent)
         self.assertIsInstance(c, ConcreteComponent)
         with Environment(AlternativeComponent):
             c = Environment.provide(AbstractComponent)
             self.assertIsInstance(c, AlternativeComponent)
Ejemplo n.º 2
0
 def test_more_than_one_instance_in_cache(self):
     with Environment():
         s1 = Environment.provide(SomeComponent, self)
         s2 = Environment.provide(SomeOtherComponent, self)
         s3 = Environment.provide(SomeComponent, self)
         s4 = Environment.provide(SomeOtherComponent, self)
         self.assertIs(s1, s3)
         self.assertIs(s2, s4)
Ejemplo n.º 3
0
 def test_environment_provides_correct_implementation(self):
     with Environment(ConcreteComponent):
         c = Environment.provide(AbstractComponent)
         self.assertIsInstance(c, AbstractComponent)
         self.assertIsInstance(c, ConcreteComponent)
     with Environment(AlternativeComponent):
         c = Environment.provide(AbstractComponent)
         self.assertIsInstance(c, AbstractComponent)
         self.assertIsInstance(c, AlternativeComponent)
Ejemplo n.º 4
0
    def test_subtype_is_singleton(self):
        class SomeComponentSingleton(SomeComponent, Singleton):
            pass

        with Environment(SomeComponentSingleton):
            s1 = Environment.provide(SomeComponent, object())
            s2 = Environment.provide(SomeComponent, object())
            self.assertIs(s1, s2)
            s3 = Environment.provide(SomeComponentSingleton, object())
            self.assertIs(s1, s3)
Ejemplo n.º 5
0
 def test_nested_environments(self):
     key = Key()
     with Environment(ConcreteComponent):
         c1 = Environment.provide(AbstractComponent, self)
         self.assertIsInstance(c1, ConcreteComponent)
         with Environment(AlternativeComponent):
             c2 = Environment.provide(AbstractComponent, self)
             c3 = Environment.provide(AbstractComponent, key)
             self.assertIs(c1, c2)
             self.assertIsInstance(c3, AlternativeComponent)
         c4 = Environment.provide(AbstractComponent, key)
         c5 = Environment.provide(AbstractComponent, self)
         self.assertIsNot(c3, c4)
         self.assertIs(c1, c5)
Ejemplo n.º 6
0
    def test_environment_gets_most_specific(self):
        class ConcreteComponentSub(ConcreteComponent):
            pass

        with Environment(ConcreteComponent, ConcreteComponentSub):
            c = Environment.provide(AbstractComponent)
            self.assertIsInstance(c, ConcreteComponentSub)
Ejemplo n.º 7
0
    def test_new_environment_in_thread(self):
        def test():
            with Environment(AlternativeComponent):
                c1 = Environment.provide(AbstractComponent)
                self.assertIsInstance(c1, AlternativeComponent)

        with Environment(ConcreteComponent):
            threading.Thread(target=test).start()
            c2 = Environment.provide(AbstractComponent)
            self.assertIsInstance(c2, ConcreteComponent)
Ejemplo n.º 8
0
 def test():
     with self.assertRaises(NoEnvironment):
         Environment.provide(AbstractComponent)
Ejemplo n.º 9
0
 def test():
     with Environment(AlternativeComponent):
         c1 = Environment.provide(AbstractComponent)
         self.assertIsInstance(c1, AlternativeComponent)
Ejemplo n.º 10
0
 def test():
     component = Environment.provide(SomeComponent)
     self.assertIsInstance(component, SomeComponent)
Ejemplo n.º 11
0
 def test_environment_provides_concrete_subclass(self):
     with Environment(ConcreteComponent):
         c = Environment.provide(AbstractComponent)
         self.assertIsInstance(c, AbstractComponent)
         self.assertIsInstance(c, ConcreteComponent)
Ejemplo n.º 12
0
 def test_environment_cant_provide_abstract_component(self):
     with Environment():
         with self.assertRaises(UnregisteredDependency):
             Environment.provide(AbstractComponent)
Ejemplo n.º 13
0
 def test_environment_provides_concrete_component(self):
     with Environment():
         c = Environment.provide(SomeComponent)
         self.assertIsInstance(c, SomeComponent)
Ejemplo n.º 14
0
 def test_fails_with_ambiguous_dependencies(self):
     with Environment(ConcreteComponent, AlternativeComponent):
         with self.assertRaises(AmbiguousDependencies):
             Environment.provide(AbstractComponent)
Ejemplo n.º 15
0
 def test_instances_are_cached(self):
     with Environment():
         s1 = Environment.provide(SomeComponent, self)
         s2 = Environment.provide(SomeComponent, self)
         self.assertIs(s1, s2)
Ejemplo n.º 16
0
 def test_singleton_is_always_same_instance(self):
     with Environment():
         s1 = Environment.provide(SomeSingleton, self)
         s2 = Environment.provide(SomeSingleton, object())
         self.assertIs(s1, s2)