Ejemplo n.º 1
0
def test_from_class():
    container = Container()
    container.register("DepOne", DepOne)
    o = container.get("DepOne")
    assert isinstance(o, DepOne)
    o_again = container.get("DepOne")
    assert o == o_again
Ejemplo n.º 2
0
def test_default_value_override():
    const = 42
    container = Container()
    container.register_class(A)
    container.register_class(DepOne)
    container.register_class(DepTwo)
    container.register("ConstDep", lambda: const)

    a = container.get("A")
    assert a.const_d == const
Ejemplo n.º 3
0
def test_injection():
    container = Container()
    container.register("A", A)
    container.register("DepOne", DepOne)
    container.register("DepTwo", DepTwo)
    a = container.get("A")
    d1 = container.get("DepOne")
    d2 = container.get("DepTwo")
    assert a.dep1 == d1
    assert a.dep2 == d2
    assert a.dep1 != DepOne()
Ejemplo n.º 4
0
def test_annotated_dependency():
    container = Container()
    container.register_class(DepOne)
    container.register_class(DepTwo)
    container.register_class(AnnotatedDependency)

    annotated = container.get("AnnotatedDependency")
    d1 = container.get("DepOne")
    d2 = container.get("DepTwo")
    assert annotated.dep1 == d1
    assert annotated.dep2 == d2
Ejemplo n.º 5
0
def test_cyclic_dependency():
    container = Container()
    container.register("DepOne", DepOne)
    container.register("DepTwo", DepTwo)
    container.register("DepThree", DepThree)
    container.register("CyclicDep", CyclicDep)

    with pytest.raises(CyclicDependencyError) as exec_info:
        container.get("CyclicDep")

    assert exec_info.value.cycled_node_name == "CyclicDep"
Ejemplo n.º 6
0
def test_multi_level_injection():
    container = Container()
    container.register_class(B)
    container.register_class(A)
    container.register_class(DepOne)
    container.register_class(DepTwo)

    b = container.get("B")
    a = container.get("A")
    dep1 = container.get("DepOne")

    assert b.a == a
    assert b.dep1 == dep1
    assert a.dep1 == dep1
Ejemplo n.º 7
0
def test_from_factory():
    container = Container()
    const_dep = 42

    def factory_a():
        return A(DepOne(), DepTwo(), const_dep)

    container.register("AWithFactory", factory_a)
    container.register("AWithoutFactory", A)
    container.register("DepOne", DepOne)
    container.register("DepTwo", DepTwo)

    a_from_factory = container.get("AWithFactory")
    a_without_factory = container.get("AWithoutFactory")

    assert a_from_factory.const_d == const_dep
    assert a_without_factory != a_from_factory
Ejemplo n.º 8
0
def test_from_constant():
    value1 = "Value1"
    value2 = 42
    value3 = DepTwo()

    container = Container()
    container.register_constant("V1", value1)
    container.register_constant("V2", value2)
    container.register_constant("V3", value3)

    v1 = container.get("V1")
    v2 = container.get("V2")
    v3 = container.get("V3")

    assert v1 == value1
    assert v2 == value2
    assert v3 == value3
Ejemplo n.º 9
0
def test_arg_name_to_service_name(arg_name, service_name):
    assert Container.arg_name_to_service_name(arg_name) == service_name