Exemple #1
0
def create_app():
    provider = Provider()
    provider.bind(Repo, SQLRepo)
    configure(provider=provider)
    app = Sanic(__name__)
    app.blueprint(bp)
    return app
Exemple #2
0
def create_app():
    provider = Provider()
    provider.bind(Repo, SQLRepo)
    configure(provider=provider)
    app = Flask(__name__)
    app.register_blueprint(bp)
    return app
Exemple #3
0
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)
Exemple #4
0
def test_unbind():
    provider = Provider()
    with raises(InjectException):
        provider.unbind(cls=int)

    provider.bind(int, str)
    provider.unbind(cls=int)
    assert provider.bindings == {}
Exemple #5
0
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
Exemple #6
0
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)
Exemple #7
0
def test_clear_bindings():
    provider = Provider()
    provider.bind(int, str)
    provider.bind(list, dict)
    provider.clear_bindings()
    assert provider.bindings == {}
Exemple #8
0
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
Exemple #9
0
def test_bind():
    provider = Provider()
    provider.bind(int, str)
    assert provider.bindings[int] == str
Exemple #10
0
def init_di() -> None:
    provider = Provider()
    provider.bind(UserRepo, UserMySQLRepo)
    configure(provider=provider)