def test_duplicate_result(): p = Plugin(autopatch=False) def test1(request): request.set_result(1) # MARKER1 request.set_result(1) req = Request(p, req_id=1, method="test1", params=[]) ba = p._bind_kwargs(test1, {}, req) with pytest.raises( ValueError, match=r'current state is RequestState\.FINISHED(.*\n.*)*MARKER1'): test1(*ba.args) def test2(request): request.set_exception(1) # MARKER2 request.set_exception(1) req = Request(p, req_id=2, method="test2", params=[]) ba = p._bind_kwargs(test2, {}, req) with pytest.raises( ValueError, match=r'current state is RequestState\.FAILED(.*\n*.*)*MARKER2'): test2(*ba.args) def test3(request): request.set_exception(1) # MARKER3 request.set_result(1) req = Request(p, req_id=3, method="test3", params=[]) ba = p._bind_kwargs(test3, {}, req) with pytest.raises( ValueError, match=r'current state is RequestState\.FAILED(.*\n*.*)*MARKER3'): test3(*ba.args) def test4(request): request.set_result(1) # MARKER4 request.set_exception(1) req = Request(p, req_id=4, method="test4", params=[]) ba = p._bind_kwargs(test4, {}, req) with pytest.raises( ValueError, match=r'current state is RequestState\.FINISHED(.*\n*.*)*MARKER4'): test4(*ba.args)
def test_argument_coercion(): p = Plugin(autopatch=False) def test1(msat: Millisatoshi): assert isinstance(msat, Millisatoshi) ba = p._bind_kwargs(test1, {"msat": "100msat"}, None) test1(*ba.args) ba = p._bind_pos(test1, ["100msat"], None) test1(*ba.args, **ba.kwargs)
def test_bind_kwargs(): p = Plugin(autopatch=False) req = object() params = {'name': 'World'} def test1(name): assert name == 'World' bound = p._bind_kwargs(test1, params, req) test1(*bound.args, **bound.kwargs) def test2(name, plugin): assert name == 'World' assert plugin == p bound = p._bind_kwargs(test2, params, req) test2(*bound.args, **bound.kwargs) def test3(plugin, name): assert name == 'World' assert plugin == p bound = p._bind_kwargs(test3, params, req) test3(*bound.args, **bound.kwargs) def test4(plugin, name, request): assert name == 'World' assert plugin == p assert request == req bound = p._bind_kwargs(test4, params, req) test4(*bound.args, **bound.kwargs) def test5(request, name, plugin): assert name == 'World' assert plugin == p assert request == req bound = p._bind_kwargs(test5, params, req) test5(*bound.args, **bound.kwargs) def test6(request, name, plugin, answer=42): assert name == 'World' assert plugin == p assert request == req assert answer == 42 bound = p._bind_kwargs(test6, params, req) test6(*bound.args, **bound.kwargs) # Now mix in a catch-all parameter that needs to be assigned def test6(request, name, plugin, *args, **kwargs): assert name == 'World' assert plugin == p assert request == req assert args == () assert kwargs == {'answer': 42} bound = p._bind_kwargs(test6, {'name': 'World', 'answer': 42}, req) test6(*bound.args, **bound.kwargs)