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)
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')
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)
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()
def test_invalid_class(obj): with pytest.raises(TypeError): wire(obj)
def test_invalid_class(obj): with pytest.raises(TypeError): wire(obj, methods=['__init__'])
def test_validation(wire, kwargs, expectation): with expectation: wire(**kwargs)