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

    provider.bind(int, str)
    provider.unbind(cls=int)
    assert provider.bindings == {}
Esempio n. 7
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
Esempio n. 8
0
def test_get():
    Container.clear()
    assert Container.get() is None
    Container.set(provider=Provider())
    assert Container.get() is not None
Esempio n. 9
0
def test_set():
    Container.clear()
    assert Container.get() is None
    provider = Provider()
    Container.set(provider=provider)
    assert Container.get() == provider
Esempio n. 10
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)
Esempio n. 11
0
def test_bind_class_init():
    provider = Provider(cls=int, new_cls=str)
    assert provider.bindings[int] == str
Esempio n. 12
0
def test_clear_bindings():
    provider = Provider()
    provider.bind(int, str)
    provider.bind(list, dict)
    provider.clear_bindings()
    assert provider.bindings == {}
Esempio n. 13
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
Esempio n. 14
0
def test_bind():
    provider = Provider()
    provider.bind(int, str)
    assert provider.bindings[int] == str
Esempio n. 15
0
def test_bind_class_init_with_dict():
    bind = {int: str}
    provider = Provider(classes=bind)
    assert provider.bindings[int] == str
Esempio n. 16
0
def init_di() -> None:
    provider = Provider()
    provider.bind(UserRepo, UserMySQLRepo)
    configure(provider=provider)