Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_add_lambda_no_name():
lmb = lambda x, y: x + y
methods = Methods(lmb)
# The lambda's __name__ will be ''!
assert "" in methods.items
def test_safe_call_method_not_found():
response = safe_call(
Request(method="nonexistant", id=1),
Methods(ping),
debug=True,
serialize=default_serialize,
)
assert isinstance(response, MethodNotFoundResponse)
def test_add_partial_custom_name():
six = partial(lambda x: x + 1, 5)
assert Methods(six=six).items["six"] is six
def test_add_instance_method():
class FooClass:
def foo(self):
return "bar"
assert Methods(FooClass().foo).items["foo"].__call__() is "bar"
def test_safe_call_notification():
response = safe_call(
Request(method="ping"), Methods(ping), debug=True, serialize=default_serialize
)
assert isinstance(response, NotificationResponse)
def test_dispatch_basic_logging():
response = dispatch(
'{"jsonrpc": "2.0", "method": "ping", "id": 1}',
Methods(ping),
basic_logging=True,
)
def test_methods_functions(self):
methods = Methods()
methods.add(foo)
req = Request({"jsonrpc": "2.0", "method": "foo", "id": 1})
self.assertEqual("bar", req.call(methods)["result"])
def test_dispatch_pure_invalid_params():
def foo(colour):
assert colour in ("orange", "red", "yellow"), "Invalid colour"
response = dispatch_pure(
'{"jsonrpc": "2.0", "method": "foo", "params": ["blue"], "id": 1}',
Methods(foo),
convert_camel_case=False,
context=NOCONTEXT,
debug=True,
serialize=default_serialize,
deserialize=default_deserialize,
)
assert isinstance(response, InvalidParamsResponse)
def test_examples_nameds():
def subtract(**kwargs):
return kwargs["minuend"] - kwargs["subtrahend"]
response = dispatch_pure(
'{"jsonrpc": "2.0", "method": "subtract", "params": {"subtrahend": 23, "minuend": 42}, "id": 3}',
Methods(subtract),
convert_camel_case=False,
context=NOCONTEXT,
debug=True,
serialize=default_serialize,
deserialize=default_deserialize,
)
assert isinstance(response, SuccessResponse)
assert response.result == 19
# Second example
response = dispatch_pure(
'{"jsonrpc": "2.0", "method": "subtract", "params": {"minuend": 42, "subtrahend": 23}, "id": 4}',
Methods(subtract),
convert_camel_case=False,
context=NOCONTEXT,
debug=True,
def build_jrpc_methods(api: API) -> jsonrpcserver.methods.Methods:
""" Builds the Methods object for jrpcserver from an api instance """
methods = jsonrpcserver.methods.Methods()
def _scrape(meth):
return inspect.iscoroutinefunction(meth)\
and not meth.__name__.startswith('_')
# If we do inspect.getmembers() on the api instance, then it will access
# property objects, which calls their getters. Since those getters are
# now async, that will create and orphan a coroutine. Instead, we can
# do inspect.getmembers() on the API _class_, so that properties aren't
# called, and then pull the object from the _instance_ to actually bind
# into our method list
for mname, mobj in inspect.getmembers(
api.__class__, _scrape):
wrapper = _build_serializable_method(mname, getattr(api, mname))
methods.add(**{mname: wrapper})
return methods