Ejemplo n.º 1
0
def test_custom_scope():
    @haps.scope('custom')
    class CustomScopedCls:
        pass

    class CustomScope(InstanceScope):
        get_object_called = False

        def get_object(self, type_):
            CustomScope.get_object_called = True
            return super(CustomScope, self).get_object(type_)

    haps.Container.configure(
        [haps.Egg(CustomScopedCls, CustomScopedCls, None, CustomScopedCls)])
    haps.Container().register_scope('custom', CustomScope)

    class AnotherClass:
        @haps.inject
        def __init__(self, csc: CustomScopedCls):
            self.csc = csc

    some_instance = AnotherClass()
    assert isinstance(some_instance.csc, CustomScopedCls)
    assert CustomScope.get_object_called
Ejemplo n.º 2
0
def test_configure_class_and_get_object(some_class):
    haps.Container.configure(
        [haps.Egg(some_class, some_class, None, some_class)])

    some_instance = haps.Container().get_object(some_class)
    assert isinstance(some_instance, some_class)
Ejemplo n.º 3
0
def test_configure():
    haps.Container.configure([])

    assert haps.Container()
Ejemplo n.º 4
0
def test_not_configured():
    with pytest.raises(exceptions.NotConfigured):
        haps.Container()