def create_app(): provider = Provider() provider.bind(Repo, SQLRepo) configure(provider=provider) app = Sanic(__name__) app.blueprint(bp) return app
def create_app(): provider = Provider() provider.bind(Repo, SQLRepo) configure(provider=provider) app = Flask(__name__) app.register_blueprint(bp) return app
def test_configure(): provider = Provider() provider.bind(int, str) configure(provider=provider) assert isinstance(Container.get(), Provider) assert Container.get().bindings[int] == str with raises(InjectException): configure(provider=provider)
def test_has_shared_state(): c1 = Container c2 = Container assert c1.get() == c2.get() p1 = Provider(cls=int, new_cls=str) c1.set(provider=p1) assert c1.get() == c2.get() p2 = Provider(cls=str, new_cls=int) c2.set(provider=p2) assert c1.get() == c2.get()
def test_has_shared_state_with_instance(): c1 = Container() c2 = Container() assert id(c1) != id(c2) assert c1.get() == c2.get() p1 = Provider(cls=int, new_cls=str) c1.set(provider=p1) assert c1.get() == c2.get() p2 = Provider(cls=str, new_cls=int) c2.set(provider=p2) assert c1.get() == c2.get()
def test_unbind(): provider = Provider() with raises(InjectException): provider.unbind(cls=int) provider.bind(int, str) provider.unbind(cls=int) assert provider.bindings == {}
def test_configure_after_clear(): provider = Provider() provider.bind(int, str) configure_after_clear(provider=provider) provider = Provider() provider.bind(str, int) configure_after_clear(provider=provider) assert isinstance(Container.get(), Provider) assert Container.get().bindings[str] == int
def test_get(): Container.clear() assert Container.get() is None Container.set(provider=Provider()) assert Container.get() is not None
def test_set(): Container.clear() assert Container.get() is None provider = Provider() Container.set(provider=provider) assert Container.get() == provider
from pythondi import Provider, configure, configure_after_clear, inject from .repo import Repo, SQLRepo class Usecase: @inject() def __init__(self, repo: Repo): self.repo = repo if __name__ == '__main__': # There is three ways to binding classes # 1. Init provider without arguments provider = Provider() # Bind one by one provider.bind(Repo, SQLRepo) # Bind all at once provider.bind(classes={Repo: SQLRepo}) # 2. Init provider with arguments provider = Provider(cls=Repo, new_cls=SQLRepo) # 3. Init provider with dictionary provider = Provider(classes={Repo: SQLRepo}) # Inject with configure configure(provider=provider) # Or if you want to fresh inject, use `configure_after_clear` configure_after_clear(provider=provider)
def test_bind_class_init(): provider = Provider(cls=int, new_cls=str) assert provider.bindings[int] == str
def test_clear_bindings(): provider = Provider() provider.bind(int, str) provider.bind(list, dict) provider.clear_bindings() assert provider.bindings == {}
def test_bind_with_classes(): provider = Provider() provider.bind(classes={int: str}) assert provider.bindings[int] == str provider.bind(int, dict) assert provider.bindings[int] == dict
def test_bind(): provider = Provider() provider.bind(int, str) assert provider.bindings[int] == str
def test_bind_class_init_with_dict(): bind = {int: str} provider = Provider(classes=bind) assert provider.bindings[int] == str
def init_di() -> None: provider = Provider() provider.bind(UserRepo, UserMySQLRepo) configure(provider=provider)