def test_it_now_works(): container = Container() container[AbstractFoo] = Foo # This is/was the root cause of the issue container[Foo] = Foo assert isinstance(container[AbstractFoo], Foo)
def test_it_writes_a_warning_every_time_reflection_is_used(caplog): c = Container(log_undefined_deps=True) with caplog.at_level(logging.INFO): _something = c[AThing] assert len(caplog.records) == 1 assert ( caplog.records[0].message == "Undefined dependency. Using reflection for <class 'tests.test_undefinied_dep_logging.AThing'>" ) assert caplog.records[0].undefined_dependency == AThing
def test_plain(benchmark): container = Container() container[SomeOtherThingAsAsingleton] = Singleton( SomeOtherThingAsAsingleton) @bind_to_container(container, shared=[SomeService]) def do_work(thing: AThingIMightNeed = injectable): thing.do_it() def do_pretend_work(): for _ in range(10): do_work() return True assert benchmark(do_pretend_work)
import pytest from lagom import Container, bind_to_container, magic_bind_to_container from lagom.exceptions import ClassesCannotBeDecorated container = Container() @bind_to_container(container) def _do_something(): """ The doc string :return: """ pass @magic_bind_to_container(container) def _do_something_magic(): """ The magic doc string :return: """ pass def test_doc_strings_are_preserved(): assert _do_something.__doc__ assert _do_something_magic.__doc__ assert "The doc string" in _do_something.__doc__ assert "The magic doc string" in _do_something_magic.__doc__
def django_integration(): return DjangoIntegration(Container(), models=[FakeDjangoModel])
def container(): return Container()
class Dep: def __init__(self, contents: str): self.contents = contents class OtherDep: def __init__(self, dep: Dep): self.dep = dep class OtherDepNeedingALambda: def __init__(self, dep): self.dep = dep level_one = Container() level_one[Dep] = Dep("level one") level_one[OtherDepNeedingALambda] = lambda c: OtherDepNeedingALambda(c[Dep]) level_two = level_one.clone() level_two[Dep] = Dep("level two") level_three = level_two.clone() level_three[Dep] = Dep("level three") def test_container_invocation_level_controls_the_dep_loaded_for_reflected_constructions( ): assert level_one[OtherDep].dep.contents == "level one" assert level_two[OtherDep].dep.contents == "level two" assert level_three[OtherDep].dep.contents == "level three"
def container(): c = Container() return c
from services.users_service import DummyUsersService, UsersService from utils.dep_container import DepContainer def setup_container(c: Container): c[UsersService] = DummyUsersService c[AdminService] = DummyAdminService c[TokenService] = DummyTokenService c[SecretService] = Singleton(DummySecretService) app = FastAPI() @app.get("/") def index(): return { "hello": "world", } app.include_router(token.router) app.include_router(users.router, prefix='/users') app.include_router(admin.router, prefix='/admin') # Inject with Lagom Container # Open http://localhost:8000/users c = Container() app.dependency_overrides = DepContainer(c).to_dependency_overrides() setup_container(c)
def test_by_default_it_doesnt_log(caplog): c = Container() with caplog.at_level(logging.INFO): _something = c[AThing] assert len(caplog.records) == 0
def test_an_alias_doesnt_skip_a_defined_constructor(): container = Container() container[Something] = lambda: Something("the correct value") container[SomethingInterface] = Something # type: ignore assert container[SomethingInterface].value == "the correct value"