Passed
Push — master ( 55adfd...84b7e9 )
by Michael
03:30
created

test_args.test_builtin_funcs()   A

Complexity

Conditions 2

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 11
rs 9.95
c 0
b 0
f 0
cc 2
nop 1
1
import os
2
import sys
3
4
import pytest
5
6
import aiohttp_rpc
7
from aiohttp_rpc import errors
8
9
10
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
11
from tests import utils
12
13
14
@pytest.mark.asyncio
15
async def test_args(aiohttp_client):
16
    def method(a=1):
17
        return [1, 2, a]
18
19
    rpc_server = aiohttp_rpc.JsonRpcServer()
20
    rpc_server.add_method(method)
21
22
    assert await rpc_server.call('method') == [1, 2, 1]
23
    assert await rpc_server.call('method', args=[1]) == [1, 2, 1]
24
25
    client = await utils.make_client(aiohttp_client, rpc_server)
26
27
    async with aiohttp_rpc.JsonRpcClient('/rpc', session=client) as rpc:
28
        assert await rpc.call('method') == [1, 2, 1]
29
        assert await rpc.call('method', 1) == [1, 2, 1]
30
31
32
@pytest.mark.asyncio
33
async def test_kwargs(aiohttp_client):
34
    def method(a=1, *, b=2):
35
        return [1, a, b]
36
37
    rpc_server = aiohttp_rpc.JsonRpcServer()
38
    rpc_server.add_method(method)
39
40
    with pytest.raises(errors.InvalidParams):
41
        await rpc_server.call('method', args=[1, 2])
42
43
    assert await rpc_server.call('method', kwargs={'a': 1, 'b': 2}) == [1, 1, 2]
44
    assert await rpc_server.call('method', args=[2], kwargs={'b': 2}) == [1, 2, 2]
45
46
    client = await utils.make_client(aiohttp_client, rpc_server)
47
48
    async with aiohttp_rpc.JsonRpcClient('/rpc', session=client) as rpc:
49
        assert await rpc.call('method', a=1, b=2) == [1, 1, 2]
50
51
        with pytest.raises(errors.InvalidParams):
52
            await rpc.call('method', 2, b=2)
53
54
55
@pytest.mark.asyncio
56
async def test_varargs(aiohttp_client):
57
    def method(a=1, *args):
58
        return [a, *args]
59
60
    rpc_server = aiohttp_rpc.JsonRpcServer()
61
    rpc_server.add_method(method)
62
63
    assert await rpc_server.call('method') == [1]
64
    assert await rpc_server.call('method', args=[2]) == [2]
65
    assert await rpc_server.call('method', args=[2, 3]) == [2, 3]
66
67
    client = await utils.make_client(aiohttp_client, rpc_server)
68
69
    async with aiohttp_rpc.JsonRpcClient('/rpc', session=client) as rpc:
70
        assert await rpc.call('method') == [1]
71
        assert await rpc.call('method', 2) == [2]
72
        assert await rpc.call('method', 2, 3) == [2, 3]
73
74
75
@pytest.mark.asyncio
76
async def test_varkw(aiohttp_client):
77
    def method(a=1, **kwargs):
78
        return [a, kwargs]
79
80
    rpc_server = aiohttp_rpc.JsonRpcServer()
81
    rpc_server.add_method(method)
82
83
    with pytest.raises(errors.InvalidParams):
84
        await rpc_server.call('method', args=[1, 2])
85
86
    assert await rpc_server.call('method', kwargs={'a': 1, 'b': 2}) == [1, {'b': 2}]
87
88
    client = await utils.make_client(aiohttp_client, rpc_server)
89
90
    async with aiohttp_rpc.JsonRpcClient('/rpc', session=client) as rpc:
91
        with pytest.raises(errors.InvalidParams):
92
            await rpc.call('method', 1, 2)
93
94
        assert await rpc.call('method', a=1, b=2) == [1, {'b': 2}]
95
96
97
@pytest.mark.asyncio
98
async def test_extra_kwargs(aiohttp_client):
99
    def method(rpc_request):
100
        return rpc_request.__class__.__name__
101
102
    def method_2(*, rpc_request):
103
        return rpc_request.__class__.__name__
104
105
    rpc_server = aiohttp_rpc.JsonRpcServer(middlewares=(aiohttp_rpc.middlewares.extra_args_middleware,))
106
    rpc_server.add_method(method)
107
    rpc_server.add_method(method_2)
108
109
    assert await rpc_server.call('method', extra_args={'rpc_request': 123}), 123
110
    assert await rpc_server.call('method_2', extra_args={'rpc_request': 123}), 123
111
112
    client = await utils.make_client(aiohttp_client, rpc_server)
113
114
    async with aiohttp_rpc.JsonRpcClient('/rpc', session=client) as rpc:
115
        assert await rpc.call('method') == 'JsonRpcRequest'
116
        assert await rpc.call('method_2') == 'JsonRpcRequest'
117
118
119
@pytest.mark.asyncio
120
async def test_builtin_funcs(aiohttp_client):
121
    rpc_server = aiohttp_rpc.JsonRpcServer(middlewares=(aiohttp_rpc.middlewares.extra_args_middleware,))
122
    rpc_server.add_method(sum)
123
    rpc_server.add_method(aiohttp_rpc.JsonRpcMethod('', zip, prepare_result=list))
124
125
    client = await utils.make_client(aiohttp_client, rpc_server)
126
127
    async with aiohttp_rpc.JsonRpcClient('/rpc', session=client) as rpc:
128
        assert await rpc.sum([1, 2, 3]) == 6
129
        assert await rpc.zip(['a', 'b'], [1, 2]) == [['a', 1], ['b', 2]]
130