Example #1
0
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
Example #2
0
    def test_function_custom_name(self):
        def foo():
            pass

        methods = Methods()
        methods.add(foo, 'foobar')
        self.assertIs(foo, methods['foobar'])
Example #3
0
 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'])
Example #4
0
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
Example #5
0
    def test_function(self):
        def foo():
            pass  #pylint:disable=multiple-statements

        methods = Methods()
        methods.add(foo)
        self.assertIs(foo, methods['foo'])
Example #6
0
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
Example #7
0
    def test_function_custom_name(self):
        def foo():
            pass  #pylint:disable=multiple-statements

        methods = Methods()
        methods.add(foo, 'foobar')
        self.assertIs(foo, methods['foobar'])
Example #8
0
 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'])
Example #9
0
    def test(self):
        def foo():
            pass

        methods = Methods()
        methods.add_method(foo)
        self.assertIs(foo, methods['foo'])
Example #10
0
    def test_function(self):
        def foo():
            pass

        methods = Methods()
        methods.add(foo)
        self.assertIs(foo, methods['foo'])
Example #11
0
 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__())
Example #12
0
    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__())
Example #13
0
 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'])
Example #14
0
 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'))
Example #15
0
def test_lookup():
    def foo():
        pass

    methods = Methods()
    methods.items["foo"] = foo

    assert lookup(methods, "foo") is foo
Example #16
0
    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'))
Example #18
0
    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__())
Example #19
0
 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'))
Example #20
0
    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")
Example #21
0
    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"])
Example #22
0
    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'])
Example #23
0
    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'])
Example #24
0
    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'])
Example #25
0
 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'])
Example #26
0
 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)
Example #28
0
 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__())
Example #29
0
    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__())
Example #30
0
 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'])
Example #31
0
    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__())
Example #32
0
def test_add_static_method_custom_name():
    class FooClass:
        @staticmethod
        def foo():
            return "bar"

    assert Methods(custom=FooClass.foo).items["custom"] == FooClass.foo
Example #33
0
 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'])
Example #34
0
def test_add_static_method():
    class FooClass:
        @staticmethod
        def foo():
            return "bar"

    assert Methods(FooClass.foo).items["foo"] is FooClass.foo
Example #35
0
def test_add_function_custom_name_via_decorator():
    methods = Methods()

    @methods.add(name='bar')
    def foo():
        pass

    assert methods.items["bar"] is foo
Example #36
0
 def test_no_name(self):
     methods = Methods()
     with self.assertRaises(AttributeError):
         methods.add(None)
Example #37
0
 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__ !
Example #38
0
 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'])
Example #39
0
 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'])
Example #40
0
 def test_lambda_custom_name(self):
     add = lambda x, y: x + y
     methods = Methods()
     methods.add(add, 'add')
     self.assertIs(add, methods['add'])
Example #41
0
 def test_lambda_renamed(self):
     add = lambda x, y: x + y
     add.__name__ = 'add'
     methods = Methods()
     methods.add(add)
     self.assertIs(add, methods['add'])
Example #42
0
 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'])
Example #43
0
 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)
Example #44
0
 def test_function(self):
     def foo(): pass #pylint:disable=multiple-statements
     methods = Methods()
     methods.add(foo)
     self.assertIs(foo, methods['foo'])
Example #45
0
 def test_function_custom_name(self):
     def foo(): pass #pylint:disable=multiple-statements
     methods = Methods()
     methods.add(foo, 'foobar')
     self.assertIs(foo, methods['foobar'])
Example #46
0
 def test_non_callable(self):
     methods = Methods()
     with self.assertRaises(TypeError):
         methods.add(None, 'ping')
Example #47
0
 def test_partial_custom_name(self):
     six = partial(lambda x: x + 1, 5)
     methods = Methods()
     methods.add(six, 'six')
     self.assertIs(six, methods['six'])