示例#1
0
def test_use_inject_annotation(wire, wired_method_builder):
    f = wired_method_builder(wire())
    with pytest.raises(TypeError):
        f()

    f = wired_method_builder(wire(), annotation=Provide[MyService])
    assert f() is world.get(MyService)
示例#2
0
def test_dependencies(wire, wired_method_builder):
    f = wired_method_builder(wire(methods=['f'], dependencies=('y',)))
    assert f() is world.get('y')

    f = wired_method_builder(wire(methods=['f'], dependencies=dict(x='z')))
    assert f() is world.get('z')

    f = wired_method_builder(wire(methods=['f'], dependencies=dict(y='z')))
    with pytest.raises(TypeError):
        f()

    f = wired_method_builder(wire(methods=['f'], dependencies=lambda arg: arg.name * 2))
    assert f() is world.get('xx')
示例#3
0
def wire_(class_=None, auto_wire=True, **kwargs):
    if auto_wire is True:
        m = ['__init__']
    elif auto_wire is False:
        m = []
    else:
        m = auto_wire

    return wire(class_=class_, methods=m, **kwargs)
示例#4
0
def test_wiring_auto_provide(wire, wired_method_builder, annotation):
    f = wired_method_builder(wire(methods=['f']),
                             annotation=annotation)
    with pytest.raises(TypeError):
        f()

    # Boolean
    f = wired_method_builder(wire(methods=['f'], auto_provide=False),
                             annotation=annotation)
    with pytest.raises(TypeError):
        f()

    f = wired_method_builder(wire(methods=['f'], auto_provide=True),
                             annotation=annotation)
    if annotation is MyService:
        assert f() is world.get(MyService)
    else:
        with pytest.raises(TypeError):
            f()

    # List
    f = wired_method_builder(wire(methods=['f'], auto_provide=[MyService]),
                             annotation=annotation)
    if annotation is MyService:
        assert f() is world.get(MyService)
    else:
        with pytest.raises(TypeError):
            f()

    class Unknown:
        pass

    f = wired_method_builder(wire(methods=['f'], auto_provide=[Unknown]),
                             annotation=annotation)
    with pytest.raises(TypeError):
        f()

    # Function
    f = wired_method_builder(wire(methods=['f'],
                                  auto_provide=lambda cls: issubclass(cls, MyService)),
                             annotation=annotation)
    if annotation is MyService:
        assert f() is world.get(MyService)
    else:
        with pytest.raises(TypeError):
            f()

    f = wired_method_builder(wire(methods=['f'], auto_provide=lambda cls: False),
                             annotation=annotation)
    with pytest.raises(TypeError):
        f()
示例#5
0
def test_invalid_class(obj):
    with pytest.raises(TypeError):
        wire(obj)
示例#6
0
def test_invalid_class(obj):
    with pytest.raises(TypeError):
        wire(obj, methods=['__init__'])
示例#7
0
def test_validation(wire, kwargs, expectation):
    with expectation:
        wire(**kwargs)