-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.py
53 lines (39 loc) · 2.13 KB
/
test.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
# -*- coding: utf-8 -*-
import unittest
import json
from simplejsonrpc import *
class TestJSONRPCService(unittest.TestCase):
def testService(self):
test_service = SimpleJSONRPCService(api_version=1)
@jsonremote(test_service, name='test', doc='Test method')
def test(request):
return "ok"
# test method should be registered by now
self.assertTrue('test' in test_service.api()['methods'])
# the call should complete successfully
self.assertEqual(
json.dumps(json.loads(test_service(json.dumps({"jsonrpc": "2.0", "method": "test", "params": [], "id": 1}))), sort_keys=True),
json.dumps({"jsonrpc": "2.0", "id": 1, "result": "ok"}, sort_keys=True))
# Test a second api
test_service_2 = SimpleJSONRPCService(api_version=2)
@jsonremote(test_service_2, name='test', doc='Test method')
def test_2(request):
return "ok2"
# test method should be registered by now
self.assertTrue('test' in test_service_2.api()['methods'])
# the call should complete successfully
self.assertEqual(
json.dumps(json.loads(test_service_2(json.dumps({"jsonrpc": "2.0", "method": "test", "params": [], "id": 1}))), sort_keys=True),
json.dumps({"jsonrpc": "2.0", "id": 1, "result": "ok2"}, sort_keys=True))
def testExceptions(self):
test_service = SimpleJSONRPCService()
@jsonremote(test_service, name='test', doc='Test method')
def test(request):
raise JSONRPCException("Oh snap, this can happen.", 12345, {"data":"oh boy..."})
# the cexception should match
self.assertEqual(
json.dumps(json.loads(test_service(json.dumps({"jsonrpc": "2.0", "method": "test", "params": [], "id": 1}))), sort_keys=True),
json.dumps({"jsonrpc": "2.0", "id": 1, "error": {"message": "Oh snap, this can happen.", "code": 12345, "data": {"data": "oh boy..."}}}, sort_keys=True))
if __name__ == '__main__':
suite = unittest.TestLoader().loadTestsFromTestCase(TestJSONRPCService)
unittest.TextTestRunner(verbosity=2).run(suite)