-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathtest_main.py
56 lines (39 loc) · 1.51 KB
/
test_main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
"""Test main.py"""
from returns.result import Success
from jsonrpcserver.main import (
dispatch_to_json,
dispatch_to_response,
dispatch_to_serializable,
)
from jsonrpcserver.methods import method
from jsonrpcserver.response import SuccessResponse
from jsonrpcserver.result import Ok, Result
# pylint: disable=missing-function-docstring
# pylint: disable=missing-function-docstring
def ping() -> Result:
return Ok("pong")
def test_dispatch_to_response() -> None:
assert dispatch_to_response(
'{"jsonrpc": "2.0", "method": "ping", "id": 1}', {"ping": ping}
) == Success(SuccessResponse("pong", 1))
def test_dispatch_to_response_with_global_methods() -> None:
@method
def ping() -> Result: # pylint: disable=redefined-outer-name
return Ok("pong")
response = dispatch_to_response('{"jsonrpc": "2.0", "method": "ping", "id": 1}')
assert response == Success(SuccessResponse("pong", 1))
def test_dispatch_to_serializable() -> None:
assert dispatch_to_serializable(
'{"jsonrpc": "2.0", "method": "ping", "id": 1}', {"ping": ping}
) == {"jsonrpc": "2.0", "result": "pong", "id": 1}
def test_dispatch_to_json() -> None:
assert (
dispatch_to_json(
'{"jsonrpc": "2.0", "method": "ping", "id": 1}', {"ping": ping}
)
== '{"jsonrpc": "2.0", "result": "pong", "id": 1}'
)
def test_dispatch_to_json_notification() -> None:
assert (
dispatch_to_json('{"jsonrpc": "2.0", "method": "ping"}', {"ping": ping}) == ""
)