Ejemplo n.º 1
0
def test_sub_container():
    class Dependency(Protocol):
        x: int

    class Dependency1:
        x = 1

    class Dependency2:
        x = 2

    class Client:
        def __init__(self, dependency: Dependency) -> None:
            self.x = dependency.x

    container = Container()

    sub_container = container.sub_container()
    sub_container.register(Dependency, value=Dependency1())

    with pytest.raises(ResolverError) as e:
        container.resolve(Client)

    client1 = sub_container.resolve(Client)

    assert client1.x == 1

    container.register(Dependency, value=Dependency2())
    client2 = container.resolve(Client)

    assert client2.x == 2
Ejemplo n.º 2
0
def handler(event, context, container=None):
    try:
        container = container or Container()
        return container.thingLambdaMapper().updateThing(event)
    finally:
        if container:
            container.shutdown()
Ejemplo n.º 3
0
def test_basic_register_resolve():
    class TestDependency:
        def __init__(self):
            self.x = 1

    container = Container()
    container.register(TestDependency)

    assert container.resolve(TestDependency).x == 1
Ejemplo n.º 4
0
def test_resolving_with_wrong_parameters():
    class Dependency:
        x = 1

    class Client:
        def __init__(self, dependency: Dependency) -> None:
            self._dependency = dependency

    container = Container()

    with pytest.raises(ResolverError):
        client = container.resolve(Client, non_existing_parameter=Dependency())
Ejemplo n.º 5
0
def test_decorator_dependency():
    class Dependency:
        def __init__(self):
            self.x = 1

    container = Container()

    @container.inject
    def my_function(dependency: Depends(Dependency), value: int) -> int:
        return value + dependency.x

    assert my_function(value=1) == 2
Ejemplo n.º 6
0
def test_dependency_without_registered_dependencies():
    class TestDependency:
        def __init__(self):
            self.x = 1

    container = Container()

    dependency_1 = container.resolve(TestDependency)
    dependency_2 = container.resolve(TestDependency)

    assert dependency_1.x == 1
    assert dependency_2.x == 1
    assert dependency_1 is dependency_2
Ejemplo n.º 7
0
def test_register_dependency_with_default_value():
    class TestDependency:
        def __init__(self, x):
            self.x = x

    class AnotherTestDependency:
        def __init__(self, dependency: TestDependency):
            self.x = dependency.x + 1

    container = Container()
    container.register(TestDependency, value=TestDependency(x=2))

    assert container.resolve(AnotherTestDependency).x == 3
Ejemplo n.º 8
0
def test_simple_hierarchy_depedency():
    class TestDependency:
        def __init__(self):
            self.x = 1

    class AnotherTestDependency:
        def __init__(self, dependency: TestDependency):
            self.x = dependency.x + 1

    container = Container()
    container.register(AnotherTestDependency)

    assert container.resolve(AnotherTestDependency).x == 2
Ejemplo n.º 9
0
def test_multiple_basic_register_resolve():
    class TestDependency:
        def __init__(self):
            self.x = 1

    class AnotherTestDependency:
        def __init__(self):
            self.x = 2

    container = Container()
    container.register(TestDependency)
    container.register(AnotherTestDependency)

    assert container.resolve(TestDependency).x == 1
    assert container.resolve(AnotherTestDependency).x == 2
Ejemplo n.º 10
0
def test_simple_hierarchy_depedency_without_registerd_dependency():
    class TestDependency:
        def __init__(self):
            self.x = 1

    class AnotherTestDependency:
        def __init__(self, dependency: TestDependency):
            self.x = dependency.x + 1

    container = Container()

    dependency_1 = container.resolve(AnotherTestDependency)
    dependency_2 = container.resolve(AnotherTestDependency)

    assert dependency_1 is dependency_2
    assert dependency_1.x == 2
Ejemplo n.º 11
0
def test_register_dependency_by_factory():
    class Dependency:
        def __init__(self, x):
            self.x = x

    def dependency_factory() -> Dependency:
        return Dependency(1)

    class AnotherDependency:
        def __init__(self, dependency: Dependency):
            self.x = dependency.x

    container = Container()
    container.register(Dependency, factory=dependency_factory)

    assert container.resolve(AnotherDependency).x == 1
Ejemplo n.º 12
0
def test_sub_container_2():
    class Dependency(Protocol):
        x: int

    class Dependency1:
        x = 1

    class Client:
        def __init__(self, dependency: Dependency) -> None:
            self.x = dependency.x

    container = Container()
    container.register(Dependency, value=Dependency1())

    sub_container = container.sub_container()
    client = sub_container.resolve(Client)

    assert client.x == 1
def test_dummy_celery_injector():
    container = Container()

    class Dependency:
        def __init__(self):
            self.x = 1

    class Celery:
        def task(self, fn):
            return fn

    celery = Celery()

    @celery_task(celery, container)
    def my_function(dependency: Depends(Dependency)):
        return dependency.x

    assert my_function() == 1
Ejemplo n.º 14
0
def test_value_and_factory_register_exception():
    class Dependency:
        def __init__(self, x):
            self.x = x

    def dependency_factory() -> Dependency:
        return Dependency(1)

    class AnotherDependency:
        def __init__(self, dependency: Dependency):
            self.x = dependency.x

    container = Container()

    with pytest.raises(ContainerRegisterError):
        container.register(Dependency,
                           factory=dependency_factory,
                           value=Dependency(2))
Ejemplo n.º 15
0
def test_multiple_dependencies():
    class Dependency1:
        def __init__(self):
            self.x = 1

    class Dependency2:
        def __init__(self):
            self.x = 2

    class Dependency3:
        def __init__(self, dependency: Dependency1):
            self.x = dependency.x + 1

    class Dependency4:
        def __init__(self, dependency_3: Dependency3,
                     dependency_2: Dependency2):
            self.x = dependency_3.x + dependency_2.x

    container = Container()

    assert container.resolve(Dependency4).x == 4
Ejemplo n.º 16
0
def test_resolving_with_parameters():
    class FirstDependency:
        x = 1

    class Dependency(Protocol):
        y: int

    class ConcreteDependency:
        y = 2

    class Client:
        def __init__(self, first_dependency: FirstDependency,
                     second_dependency: Dependency) -> None:
            self.x = first_dependency.x
            self.y = second_dependency.y

        def add(self) -> int:
            return self.x + self.y

    container = Container()
    client = container.resolve(Client, second_dependency=ConcreteDependency)

    assert client.add() == 3
 def setUp(self):
     self.principal = {
         'organizationId': 'ORG001',
         'roles': ['ROLE_THING_USER']
     }
     self.container = Container()
Ejemplo n.º 18
0
def container():
    return Container()