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)
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)
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)
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)
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)
def test_environment_gets_most_specific(self): class ConcreteComponentSub(ConcreteComponent): pass with Environment(ConcreteComponent, ConcreteComponentSub): c = Environment.provide(AbstractComponent) self.assertIsInstance(c, ConcreteComponentSub)
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)
def test(): with self.assertRaises(NoEnvironment): Environment.provide(AbstractComponent)
def test(): with Environment(AlternativeComponent): c1 = Environment.provide(AbstractComponent) self.assertIsInstance(c1, AlternativeComponent)
def test(): component = Environment.provide(SomeComponent) self.assertIsInstance(component, SomeComponent)
def test_environment_provides_concrete_subclass(self): with Environment(ConcreteComponent): c = Environment.provide(AbstractComponent) self.assertIsInstance(c, AbstractComponent) self.assertIsInstance(c, ConcreteComponent)
def test_environment_cant_provide_abstract_component(self): with Environment(): with self.assertRaises(UnregisteredDependency): Environment.provide(AbstractComponent)
def test_environment_provides_concrete_component(self): with Environment(): c = Environment.provide(SomeComponent) self.assertIsInstance(c, SomeComponent)
def test_fails_with_ambiguous_dependencies(self): with Environment(ConcreteComponent, AlternativeComponent): with self.assertRaises(AmbiguousDependencies): Environment.provide(AbstractComponent)
def test_instances_are_cached(self): with Environment(): s1 = Environment.provide(SomeComponent, self) s2 = Environment.provide(SomeComponent, self) self.assertIs(s1, s2)
def test_singleton_is_always_same_instance(self): with Environment(): s1 = Environment.provide(SomeSingleton, self) s2 = Environment.provide(SomeSingleton, object()) self.assertIs(s1, s2)