예제 #1
0
def test_ambiguous_dependency(some_class):
    class NewClass(some_class):
        pass

    class NewClass2(some_class):
        pass

    with pytest.raises(ConfigurationError) as e:
        haps.Container.configure([
            haps.Egg(some_class, some_class, None, NewClass),
            haps.Egg(some_class, some_class, None, NewClass2)
        ])

    assert e.value.args[0] == f'Ambiguous implementation {repr(some_class)}'
예제 #2
0
def test_inject_class_by_operator(some_class):
    haps.Container.configure(
        [haps.Egg(some_class, some_class, None, some_class)])

    some_class_instance = haps.DI() >> some_class

    assert isinstance(some_class_instance, some_class)
예제 #3
0
def test_inject_class(some_class):
    haps.Container.configure(
        [haps.Egg(some_class, some_class, None, some_class)])

    class AnotherClass:
        @haps.inject
        def __init__(self, some_class_instance: some_class):
            self.some_class_instance = some_class_instance

    some_instance = AnotherClass()

    assert isinstance(some_instance.some_class_instance, some_class)
예제 #4
0
def test_dependencies_with_profiles(some_class, profiles, expected):
    class NewClass(some_class):
        pass

    class NewClass2(some_class):
        pass

    class NewClass3(some_class):
        pass

    Configuration().set('haps.profiles', profiles)
    haps.Container.configure([
        haps.Egg(some_class, NewClass, None, NewClass),
        haps.Egg(some_class, NewClass2, None, NewClass2, 'test'),
        haps.Egg(some_class, NewClass3, None, NewClass3, 'prod')
    ])

    class AnotherClass:
        some_instance: some_class = haps.Inject()

    some_instance = AnotherClass()

    assert type(some_instance.some_instance).__name__ == expected
예제 #5
0
def test_not_existing_scope():
    @haps.scope('custom')
    class CustomScopedCls:
        pass

    haps.Container.configure(
        [haps.Egg(CustomScopedCls, CustomScopedCls, None, CustomScopedCls)])

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

    with pytest.raises(exceptions.UnknownScope):
        AnotherClass()
예제 #6
0
def test_named_configuration_property_injection(some_class):
    class NewClass(some_class):
        pass

    class NewClass2(some_class):
        pass

    haps.Container.configure([
        haps.Egg(some_class, NewClass, None, NewClass),
        haps.Egg(some_class, NewClass2, 'extra', NewClass2)
    ])

    class AnotherClass:
        some_instance: some_class = haps.Inject()
        some_extra_instance: some_class = haps.Inject('extra')

    some_instance = AnotherClass()

    assert isinstance(some_instance.some_instance, NewClass)
    assert isinstance(some_instance.some_extra_instance, NewClass2)
    instance1 = some_instance.some_instance
    instance2 = some_instance.some_extra_instance

    assert instance1 is not instance2
예제 #7
0
def test_inject_into_function_with_optional_args(some_class):
    haps.Container.configure(
        [haps.Egg(some_class, some_class, None, some_class)])

    class NotRegistered:
        pass

    @haps.inject
    def func(some_class_instance: some_class, no_annotation,
             not_registered: NotRegistered):
        return some_class_instance, no_annotation, not_registered

    not_reg = NotRegistered()
    sci, na, nr = func(no_annotation=5, not_registered=not_reg)

    assert isinstance(sci, some_class)
    assert na == 5
    assert nr is not_reg
예제 #8
0
def test_inject_class_using_property_instance_annotation(some_class):
    class NewClass(some_class):
        pass

    haps.Container.configure(
        [haps.Egg(some_class, some_class, None, NewClass)])

    class AnotherClass:
        injected_instance: some_class = haps.Inject()

    some_instance = AnotherClass()

    assert hasattr(some_instance, 'injected_instance')
    assert isinstance(some_instance.injected_instance, NewClass)
    instance1 = some_instance.injected_instance
    instance2 = some_instance.injected_instance

    assert instance1 is instance2
예제 #9
0
def test_inject_class_using_init_annotation(some_class):
    class NewClass(some_class):
        pass

    haps.Container.configure(
        [haps.Egg(some_class, some_class, None, NewClass)])

    class AnotherClass:
        @haps.inject
        def __init__(self, injected_instance: some_class):
            self.injected_instance = injected_instance

    some_instance = AnotherClass()

    assert hasattr(some_instance, 'injected_instance')
    assert isinstance(some_instance.injected_instance, NewClass)
    instance1 = some_instance.injected_instance
    instance2 = some_instance.injected_instance

    assert instance1 is instance2
예제 #10
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
예제 #11
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)