コード例 #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
コード例 #2
0
    def test_function_custom_name(self):
        def foo():
            pass

        methods = Methods()
        methods.add(foo, 'foobar')
        self.assertIs(foo, methods['foobar'])
コード例 #3
0
ファイル: test_request.py プロジェクト: bcb/jsonrpcserver
 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'])
コード例 #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
コード例 #5
0
    def test_function(self):
        def foo():
            pass  #pylint:disable=multiple-statements

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

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

        methods = Methods()
        methods.add(foo)
        self.assertIs(foo, methods['foo'])
コード例 #11
0
ファイル: test_methods.py プロジェクト: benizl/jsonrpcserver
 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__())
コード例 #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__())
コード例 #13
0
ファイル: test_methods.py プロジェクト: benizl/jsonrpcserver
 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'])
コード例 #14
0
ファイル: test_request.py プロジェクト: bcb/jsonrpcserver
 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'))
コード例 #15
0
ファイル: test_methods.py プロジェクト: mihasol/jsonrpcserver
def test_lookup():
    def foo():
        pass

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

    assert lookup(methods, "foo") is foo
コード例 #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__())
コード例 #17
0
 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'))
コード例 #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__())
コード例 #19
0
ファイル: test_request.py プロジェクト: spumer/jsonrpcserver
 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'))
コード例 #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")
コード例 #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"])
コード例 #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'])
コード例 #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'])
コード例 #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'])
コード例 #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'])
コード例 #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'])
コード例 #27
0
    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)
コード例 #28
0
ファイル: test_methods.py プロジェクト: benizl/jsonrpcserver
 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__())
コード例 #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__())
コード例 #30
0
ファイル: test_request.py プロジェクト: spumer/jsonrpcserver
 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'])
コード例 #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__())
コード例 #32
0
ファイル: test_methods.py プロジェクト: mihasol/jsonrpcserver
def test_add_static_method_custom_name():
    class FooClass:
        @staticmethod
        def foo():
            return "bar"

    assert Methods(custom=FooClass.foo).items["custom"] == FooClass.foo
コード例 #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'])
コード例 #34
0
ファイル: test_methods.py プロジェクト: mihasol/jsonrpcserver
def test_add_static_method():
    class FooClass:
        @staticmethod
        def foo():
            return "bar"

    assert Methods(FooClass.foo).items["foo"] is FooClass.foo
コード例 #35
0
ファイル: test_methods.py プロジェクト: mihasol/jsonrpcserver
def test_add_function_custom_name_via_decorator():
    methods = Methods()

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

    assert methods.items["bar"] is foo
コード例 #36
0
ファイル: test_methods.py プロジェクト: benizl/jsonrpcserver
 def test_no_name(self):
     methods = Methods()
     with self.assertRaises(AttributeError):
         methods.add(None)
コード例 #37
0
ファイル: test_methods.py プロジェクト: benizl/jsonrpcserver
 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__ !
コード例 #38
0
ファイル: test_request.py プロジェクト: bcb/jsonrpcserver
 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'])
コード例 #39
0
ファイル: test_request.py プロジェクト: bcb/jsonrpcserver
 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'])
コード例 #40
0
ファイル: test_methods.py プロジェクト: benizl/jsonrpcserver
 def test_lambda_custom_name(self):
     add = lambda x, y: x + y
     methods = Methods()
     methods.add(add, 'add')
     self.assertIs(add, methods['add'])
コード例 #41
0
ファイル: test_methods.py プロジェクト: benizl/jsonrpcserver
 def test_lambda_renamed(self):
     add = lambda x, y: x + y
     add.__name__ = 'add'
     methods = Methods()
     methods.add(add)
     self.assertIs(add, methods['add'])
コード例 #42
0
ファイル: test_methods.py プロジェクト: benizl/jsonrpcserver
 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'])
コード例 #43
0
ファイル: test_methods.py プロジェクト: benizl/jsonrpcserver
 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)
コード例 #44
0
ファイル: test_methods.py プロジェクト: benizl/jsonrpcserver
 def test_function(self):
     def foo(): pass #pylint:disable=multiple-statements
     methods = Methods()
     methods.add(foo)
     self.assertIs(foo, methods['foo'])
コード例 #45
0
ファイル: test_methods.py プロジェクト: benizl/jsonrpcserver
 def test_function_custom_name(self):
     def foo(): pass #pylint:disable=multiple-statements
     methods = Methods()
     methods.add(foo, 'foobar')
     self.assertIs(foo, methods['foobar'])
コード例 #46
0
ファイル: test_methods.py プロジェクト: benizl/jsonrpcserver
 def test_non_callable(self):
     methods = Methods()
     with self.assertRaises(TypeError):
         methods.add(None, 'ping')
コード例 #47
0
ファイル: test_methods.py プロジェクト: benizl/jsonrpcserver
 def test_partial_custom_name(self):
     six = partial(lambda x: x + 1, 5)
     methods = Methods()
     methods.add(six, 'six')
     self.assertIs(six, methods['six'])