Exemple #1
0
    def test_manual_pushing_and_popping_contexts(self):
        gimme.add(2)
        gimme.context()

        gimme.add(1)
        assert gimme.that(int) == 1
        gimme.pop_context()

        assert gimme.that(int) == 2
Exemple #2
0
def test_circular_dependencies(repo):
    class A:
        def __init__(self, c: "C"):
            ...

    class B:
        def __init__(self, a: A):
            ...

    @gimme.dependency
    class C:
        def __init__(self, b: B):
            ...

    with pytest.raises(gimme.exceptions.CircularDependency) as err:
        gimme.that(A)
    assert str(err.value) == "A -> C -> B"
Exemple #3
0
def test_can_get_attrs_class():
    @attr.dataclass
    class MyAttr:
        dep: Dependency
        no_init: int = attr.ib(init=False)
        a: int = 4
        b: list = attr.ib(factory=list)

    inst = gimme.that(MyAttr)
    assert isinstance(inst.dep, Dependency)
Exemple #4
0
    def test_resolve_from_multiple_layers(self):
        class MyClass:
            def __init__(self, a: int, b: str):
                self.a = a
                self.b = b

        gimme.add(2)
        with gimme.context():
            gimme.add("bla")
            obj = gimme.that(MyClass)
        assert (obj.a, obj.b) == (2, "bla")
Exemple #5
0
def test_can_get_dataclass():
    import dataclasses

    @dataclasses.dataclass
    class MyDataclass:
        dep: Dependency
        no_init: int = dataclasses.field(init=False)
        a: int = 4
        b: list = attr.ib(factory=list)

    inst = gimme.that(MyDataclass)
    assert isinstance(inst.dep, Dependency)
Exemple #6
0
def test_can_resolve_circular_dependencies_using_deferred_resolution():
    class A:
        b = gimme.later("B")

        def __init__(self):
            ...

    @gimme.dependency
    class B:
        def __init__(self, a: A):
            self.a = a

    obj = gimme.that(A)
    assert isinstance(obj, A)
    assert isinstance(obj.b, B)
Exemple #7
0
def test_can_get_class_with_requirement():
    inst = gimme.that(HasDependency)
    assert isinstance(inst, HasDependency)
    assert isinstance(inst.dep, Dependency)
Exemple #8
0
def test_can_get_class_by_str_if_registered_as_dependency():
    assert isinstance(gimme.that("RegisteredClass"), RegisteredClass)
Exemple #9
0
def test_can_get_class_without_dependencies():
    assert isinstance(gimme.that(SimpleClass), SimpleClass)
Exemple #10
0
 def test_can_context_dependent_dependency(self):
     gimme.add(2)
     with gimme.context():
         gimme.add(1)
         assert gimme.that(int) == 1
     assert gimme.that(int) == 2
Exemple #11
0
 def test_can_add_and_retrieve_object(self, repo):
     obj = object()
     gimme.add(obj)
     assert gimme.that(object) is obj