def test_examples_positionals(): def subtract(minuend, subtrahend): return minuend - subtrahend response = dispatch_pure( '{"jsonrpc": "2.0", "method": "subtract", "params": [42, 23], "id": 1}', 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": [23, 42], "id": 2}', Methods(subtract), convert_camel_case=False, context=NOCONTEXT, debug=True, serialize=default_serialize, deserialize=default_deserialize, ) assert isinstance(response, SuccessResponse) assert response.result == -19
def test_function_custom_name(self): def foo(): pass methods = Methods() methods.add(foo, 'foobar') self.assertIs(foo, methods['foobar'])
def test_methods_partials(self): multiply = lambda x, y: x * y double = partial(multiply, 2) methods = Methods() methods.add(double, 'double') req = Request({'jsonrpc': '2.0', 'method': 'double', 'params': [3], 'id': 1}) self.assertEqual(6, req.call(methods)['result'])
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, serialize=default_serialize, deserialize=default_deserialize, ) assert isinstance(response, SuccessResponse) assert response.result == 19
def test_function(self): def foo(): pass #pylint:disable=multiple-statements methods = Methods() methods.add(foo) self.assertIs(foo, methods['foo'])
def test_examples_nameds(): def subtract(context: Context, **kwargs): return SuccessResponse( kwargs["minuend"] - kwargs["subtrahend"], id=context.request.id ) response = dispatch_pure( '{"jsonrpc": "2.0", "method": "subtract", "params": {"subtrahend": 23, "minuend": 42}, "id": 3}', Methods(subtract), extra=None, 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), extra=None, serialize=default_serialize, deserialize=default_deserialize, ) assert isinstance(response, SuccessResponse) assert response.result == 19
def test_function_custom_name(self): def foo(): pass #pylint:disable=multiple-statements methods = Methods() methods.add(foo, 'foobar') self.assertIs(foo, methods['foobar'])
def test(self): def foo(): pass methods = Methods() methods.add_method(foo) self.assertIs(foo, methods['foo'])
def test_function(self): def foo(): pass methods = Methods() methods.add(foo) self.assertIs(foo, methods['foo'])
def test_instance_method(self): class FooClass(object): #pylint:disable=too-few-public-methods def foo(self): # pylint: disable=no-self-use return 'bar' methods = Methods() methods.add(FooClass().foo) self.assertEqual('bar', methods['foo'].__call__())
def test_static_method(self): class FooClass(object): #pylint:disable=too-few-public-methods @staticmethod def foo(): return 'bar' methods = Methods() methods.add(FooClass.foo) self.assertIs(FooClass.foo, methods['foo'])
def test_methods_object(self): def cat(): pass # pylint: disable=multiple-statements def dog(): pass # pylint: disable=multiple-statements methods = Methods() methods.add(cat) methods.add(dog) self.assertIs(cat, Request._get_method(methods, 'cat')) self.assertIs(dog, Request._get_method(methods, 'dog'))
def test_lookup(): def foo(): pass methods = Methods() methods.items["foo"] = foo assert lookup(methods, "foo") is foo
def test_instance_method(self): class FooClass(object): def foo(self): return "bar" methods = Methods() methods.add(FooClass().foo) self.assertEqual("bar", methods["foo"].__call__())
def test_methods_object(self): def cat(): pass def dog(): pass methods = Methods() methods.add(cat) methods.add(dog) self.assertIs(cat, get_method(methods, 'cat')) self.assertIs(dog, get_method(methods, 'dog'))
def test_instance_method(self): class FooClass(object): def foo(self): return 'bar' methods = Methods() methods.add(FooClass().foo) self.assertEqual('bar', methods['foo'].__call__())
def test_dispatch(self): def foo(): return "bar" methods = Methods() methods.add(foo) request = {"jsonrpc": "2.0", "method": "foo", "id": 1} response = methods.dispatch(request) self.assertEqual(response["result"], "bar")
def test_static_method_custom_name(self): class FooClass(object): @staticmethod def foo(): return "bar" methods = Methods() methods.add(FooClass.foo, "custom") self.assertIs(FooClass.foo, methods["custom"])
def test_static_method_custom_name(self): class FooClass(object): #pylint:disable=too-few-public-methods @staticmethod def foo(): return 'bar' methods = Methods() methods.add(FooClass.foo, 'custom') self.assertIs(FooClass.foo, methods['custom'])
def test_static_method(self): class FooClass(object): @staticmethod def foo(): return 'bar' methods = Methods() methods.add(FooClass.foo) self.assertIs(FooClass.foo, methods['foo'])
def test_static_method_custom_name(self): class FooClass(object): @staticmethod def foo(): return 'bar' methods = Methods() methods.add(FooClass.foo, 'custom') self.assertIs(FooClass.foo, methods['custom'])
def test_positionals(self): methods = Methods() methods.add(lambda x: x * x, 'square') req = Request({ 'jsonrpc': '2.0', 'method': 'square', 'params': [3], 'id': 1 }) self.assertEqual(9, req.call(methods)['result'])
def test_object_method(self): methods = Methods() methods.add(FooClass().foo, 'foo') req = Request({ 'jsonrpc': '2.0', 'method': 'foo', 'params': [1, 2], 'id': 1 }) response = req.call(methods) self.assertIsInstance(response, RequestResponse) self.assertEqual('bar', response['result'])
def __init__(self, evnetsEmmitter: EventsEmmitterHub = None, **kwargs: Any): Methods.__init__(self, **kwargs) self.evnetsEmmitter = evnetsEmmitter self.register("list")(self.list_functions) self.register("help")(self.help_functions) if evnetsEmmitter: self.register("rpc.on")(evnetsEmmitter.rpc_on) self.register("rpc.off")(evnetsEmmitter.rpc_off)
def test_instance_method_custom_name(self): class Foo(object): #pylint:disable=too-few-public-methods def __init__(self, name): self.name = name def get_name(self): return self.name obj1 = Foo('a') obj2 = Foo('b') methods = Methods() methods.add(obj1.get_name, 'custom1') methods.add(obj2.get_name, 'custom2') # Can't use assertIs, so check the outcome is as expected self.assertEqual('a', methods['custom1'].__call__()) self.assertEqual('b', methods['custom2'].__call__())
def test_instance_method_custom_name(self): class Foo(object): def __init__(self, name): self.name = name def get_name(self): return self.name obj1 = Foo("a") obj2 = Foo("b") methods = Methods() methods.add(obj1.get_name, "custom1") methods.add(obj2.get_name, "custom2") # Can't use assertIs, so check the outcome is as expected self.assertEqual("a", methods["custom1"].__call__()) self.assertEqual("b", methods["custom2"].__call__())
def test_methods_functions_with_decorator(self): methods = Methods() @methods.add def foo(): # pylint: disable=redefined-outer-name,unused-variable return 'bar' req = Request({'jsonrpc': '2.0', 'method': 'foo', 'id': 1}) self.assertEqual('bar', req.call(methods)['result'])
def test_add_static_method_custom_name(): class FooClass: @staticmethod def foo(): return "bar" assert Methods(custom=FooClass.foo).items["custom"] == FooClass.foo
def test_methods_functions_with_decorator(self): methods = Methods() @methods.add def foo(): return 'bar' req = Request({'jsonrpc': '2.0', 'method': 'foo', 'id': 1}) self.assertEqual('bar', req.call(methods)['result'])
def test_add_static_method(): class FooClass: @staticmethod def foo(): return "bar" assert Methods(FooClass.foo).items["foo"] is FooClass.foo
def test_add_function_custom_name_via_decorator(): methods = Methods() @methods.add(name='bar') def foo(): pass assert methods.items["bar"] is foo
def test_no_name(self): methods = Methods() with self.assertRaises(AttributeError): methods.add(None)
def test_partial_no_name(self): six = partial(lambda x: x + 1, 5) methods = Methods() with self.assertRaises(AttributeError): methods.add(six) # Partial has no __name__ !
def test_methods_lambdas(self): methods = Methods() methods.add(lambda: 'bar', 'foo') req = Request({'jsonrpc': '2.0', 'method': 'foo', 'id': 1}) self.assertEqual('bar', req.call(methods)['result'])
def test_positionals(self): methods = Methods() methods.add(lambda x: x * x, 'square') req = Request({'jsonrpc': '2.0', 'method': 'square', 'params': [3], 'id': 1}) self.assertEqual(9, req.call(methods)['result'])
def test_lambda_custom_name(self): add = lambda x, y: x + y methods = Methods() methods.add(add, 'add') self.assertIs(add, methods['add'])
def test_lambda_renamed(self): add = lambda x, y: x + y add.__name__ = 'add' methods = Methods() methods.add(add) self.assertIs(add, methods['add'])
def test_partial_renamed(self): six = partial(lambda x: x + 1, 5) six.__name__ = 'six' methods = Methods() methods.add(six) self.assertIs(six, methods['six'])
def test_lambda_no_name(self): add = lambda x, y: x + y methods = Methods() methods.add(add) # Lambda's __name__ will be '<lambda>'! self.assertNotIn('add', methods)
def test_non_callable(self): methods = Methods() with self.assertRaises(TypeError): methods.add(None, 'ping')
def test_partial_custom_name(self): six = partial(lambda x: x + 1, 5) methods = Methods() methods.add(six, 'six') self.assertIs(six, methods['six'])