def test_safe_call_success_response(): response = safe_call(Request(method="ping", id=1), Methods(ping), debug=True) assert isinstance(response, SuccessResponse) assert response.result == "pong" assert response.id == 1
def test_call_requests_with_context(): def ping_with_context(context=None): assert context is sentinel.context call_requests( Request("ping_with_context", convert_camel_case=False), Methods(ping_with_context), debug=True, )
def test_request_convert_camel_case(): request = Request(**{ "jsonrpc": "2.0", "method": "fooBar", "params": { "fooBar": 1 } }, convert_camel_case=True) assert request.method == "foo_bar"
def test_call_requests_batch_all_notifications(): """Should return a BatchResponse response, an empty list""" response = call_requests( { Request(**{ "jsonrpc": "2.0", "method": "notify_sum", "params": [1, 2, 4] }), Request(**{ "jsonrpc": "2.0", "method": "notify_hello", "params": [7] }), }, Methods(ping), debug=True, ) assert str(response) == ""
def test_request_no_args(): req = Request(method="foo") assert req.args == [] assert req.kwargs == {}
def test_notification_false(): request = Request(method="foo", id=99) assert request.is_notification is False
def test_notification_true(): request = Request(method="foo") assert request.is_notification is True
def test_request(): assert Request(method="foo").method == "foo"
def test_request_from_string(): request = Request( **json.loads('{"jsonrpc": "2.0", "method": "foo", "id": 1}')) assert request.jsonrpc == "2.0" assert request.method == "foo" assert request.id == 1
def test_request_id(): assert Request(method="foo", id=99).id == 99
def test_safe_call_invalid_args(): response = safe_call(Request(method="ping", params=[1], id=1), Methods(ping), debug=True) assert isinstance(response, InvalidParamsResponse)
def test_safe_call_method_not_found(): response = safe_call(Request(method="nonexistant", id=1), Methods(ping), debug=True) assert isinstance(response, MethodNotFoundResponse)
def test_safe_call_notification_failure(): def fail(): raise ValueError() response = safe_call(Request(method="foo"), Methods(fail), debug=True) assert isinstance(response, NotificationResponse)
def test_safe_call_notification(): response = safe_call(Request(method="ping"), Methods(ping), debug=True) assert isinstance(response, NotificationResponse)
def test_request_positional_args(): req = Request(method="foo", params=[2, 3]) assert req.args == [2, 3] assert req.kwargs == {}
def test_request_keyword_args(): req = Request(method="foo", params={"foo": "bar"}) assert req.args == [] assert req.kwargs == {"foo": "bar"}
def test_request_no_id(): request = Request({"jsonrpc": "2.0", "method": "foo"}) assert request.id is NOID