Esempio n. 1
0
def test_bind_name_to_instance():
    apple = Apple()
    bindings = Bindings()
    bindings.bind("apple").to_instance(apple)
    
    injector = Injector(bindings)
    assert injector.get("apple") is apple
Esempio n. 2
0
def test_bind_type_to_provider():
    apple = Apple()
    bindings = Bindings()
    bindings.bind(Apple).to_provider(lambda injector: apple)
    
    injector = Injector(bindings)
    assert injector.get(Apple) is apple
Esempio n. 3
0
def test_can_bind_to_names():
    apple_to_inject = Apple()
    bindings = Bindings()
    bindings.bind("apple").to_instance(apple_to_inject)
    bindings.bind("another_apple").to_key("apple")
    
    injector = Injector(bindings)
    assert injector.get("another_apple") is apple_to_inject
Esempio n. 4
0
def test_classes_that_inherit_from_injectable_have_members_injected():
    class Foo(Base):
        _tag_fetcher = dependency("tag_fetcher")
    
    tag_fetcher = {'some': 'object'}
    
    bindings = Bindings()
    bindings.bind("tag_fetcher").to_instance(tag_fetcher)
    injector = Injector(bindings)
    assert injector.get(Foo)._tag_fetcher is tag_fetcher
Esempio n. 5
0
def test_scoped_injectables_cannot_depend_on_injectables_in_separate_scope():
    Name = zuice.key("Name")
    
    class Greeter(zuice.Base):
        _name = zuice.dependency(Name)
    
    bindings = Bindings()
    bindings.bind(Greeter).singleton()
    injector = Injector(bindings)
    error = assert_raises(NoSuchBindingException, lambda: injector.get(Greeter, {Name: "Bob"}))
Esempio n. 6
0
def test_get_raises_exception_if_no_such_binding_exists():
    injector = Injector(Bindings())
    assert_raises(NoSuchBindingException, lambda: injector.get("apple"))
    
    apple = Apple()
    bindings = Bindings()
    bindings.bind("apple").to_provider(lambda injector: apple)
    
    injector = Injector(bindings)
    assert injector.get("apple") is apple
    assert_raises(NoSuchBindingException, lambda: injector.get("banana"))
Esempio n. 7
0
def test_original_bindings_are_prefered_to_zero_arg_constructors():
    class Unit(object):
        pass
    
    unit = Unit()
    
    bindings = Bindings()
    bindings.bind(Unit).to_instance(unit)
    
    injector = Injector(bindings)
    assert injector.get(Unit, {zuice.key("a"): "a"}) is unit
Esempio n. 8
0
 def test_cached_singleton_is_available_from_non_singleton_scope(self):
     x = [0]
     class Counter(object):
         def __init__(self):
            self.x = x[0] = x[0] + 1
     
     bindings = Bindings()
     bindings.bind(Counter).singleton()
     injector = Injector(bindings)
     
     assert_equal(1, injector.get(Counter, {"name": "Bob"}).x)
     assert_equal(1, injector.get(Counter, {"name": "Jim"}).x)
Esempio n. 9
0
 def test_bindings_can_change_lifetime_to_singleton(self):
     x = [0]
     class Counter(object):
         def __init__(self):
            self.x = x[0] = x[0] + 1
     
     bindings = Bindings()
     bindings.bind(Counter).singleton()
     injector = Injector(bindings)
     
     assert_equal(1, injector.get(Counter).x)
     assert_equal(1, injector.get(Counter).x)
Esempio n. 10
0
 def test_bindings_can_change_lifetime_of_provider_to_singleton(self):
     x = [0]
     
     def count(injector):
         x[0] += 1
         return x[0]
     
     counter = zuice.key("counter")
     bindings = Bindings()
     bindings.bind(counter).to_provider(count).singleton()
     injector = Injector(bindings)
     assert_equal(1, injector.get(counter))
     assert_equal(1, injector.get(counter))
Esempio n. 11
0
def test_subclassing_injectable_objects_allows_injected_attributes_to_be_overwritten():
    class Parent(Base):
        _fetcher = dependency('tag_fetcher')
        
    class Child(Parent):
        _fetcher = dependency('post_fetcher')

    post_fetcher = {'another': 'object'}
    
    bindings = Bindings()
    bindings.bind("post_fetcher").to_instance(post_fetcher)
    injector = Injector(bindings)
    child = injector.get(Child)
    
    assert child._fetcher is post_fetcher
Esempio n. 12
0
def test_unscoped_injectables_are_available_in_any_scope():
    Greeting = zuice.key("Greeting")
    Name = zuice.key("Name")
    
    class Greeter(zuice.Base):
        _greeting = zuice.dependency(Greeting)
        _name = zuice.dependency(Name)
        
        def hello(self):
            return "{0} {1}".format(self._greeting, self._name)
    
    bindings = Bindings()
    bindings.bind(Greeting).to_instance("Hello")
    injector = Injector(bindings)
    greeter = injector.get(Greeter, {Name: "Bob"})
    assert greeter.hello() == "Hello Bob"
Esempio n. 13
0
def test_injectable_injects_attributes_of_sub_classes():
    class Parent(Base):
        _tag_fetcher = dependency('tag_fetcher')
        
    class Child(Parent):
        _blog_post_fetcher = dependency('post_fetcher')

    tag_fetcher = {'some': 'object'}
    post_fetcher = {'another': 'object'}
    
    bindings = Bindings()
    bindings.bind("tag_fetcher").to_instance(tag_fetcher)
    bindings.bind("post_fetcher").to_instance(post_fetcher)
    injector = Injector(bindings)
    child = injector.get(Child)
    
    assert child._tag_fetcher is tag_fetcher
    assert child._blog_post_fetcher is post_fetcher
Esempio n. 14
0
def test_changing_bindings_after_creating_injector_does_not_change_injector():
    bindings = Bindings()
    injector = Injector(bindings)
    bindings.bind("apple").to_instance(Apple())
    assert_raises(NoSuchBindingException, lambda: injector.get("apple"))