Skip to content

Commit 6066822

Browse files
committed
Small code review of internal tests (added pydoc)
1 parent f7f1cd3 commit 6066822

File tree

2 files changed

+70
-9
lines changed

2 files changed

+70
-9
lines changed

tests/test_internal.py

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@
77
:license: Apache License 2.0
88
"""
99

10+
# Standard library
11+
import json
12+
import unittest
13+
1014
# Tests utilities
1115
from tests.utilities import UtilityServer
1216

1317
# JSON-RPC library
1418
import jsonrpclib
1519

16-
# Standard library
17-
import json
18-
import unittest
19-
2020
# ------------------------------------------------------------------------------
2121

2222

@@ -41,39 +41,65 @@ def tearDown(self):
4141
self.server.stop()
4242

4343
def get_client(self):
44-
return jsonrpclib.ServerProxy('http://localhost:{0}'.format(self.port),
45-
history=self.history)
44+
"""
45+
Utility method to get a proxy to the test server
46+
"""
47+
return jsonrpclib.ServerProxy(
48+
"http://localhost:{0}".format(self.port),
49+
history=self.history
50+
)
4651

4752
def get_multicall_client(self):
53+
"""
54+
Utility method to get a multi-call proxy to the test server
55+
"""
4856
server = self.get_client()
4957
return jsonrpclib.MultiCall(server)
5058

5159
def test_connect(self):
60+
"""
61+
Simple Ping() test
62+
"""
5263
client = self.get_client()
5364
result = client.ping()
5465
self.assertTrue(result)
5566

5667
def test_single_args(self):
68+
"""
69+
Positional arguments test
70+
"""
5771
client = self.get_client()
5872
result = client.add(5, 10)
5973
self.assertTrue(result == 15)
6074

6175
def test_single_kwargs(self):
76+
"""
77+
Keyword-arguments test
78+
"""
6279
client = self.get_client()
6380
result = client.add(x=5, y=10)
6481
self.assertTrue(result == 15)
6582

6683
def test_single_kwargs_and_args(self):
84+
"""
85+
Mixed positional/keyword args failure test
86+
"""
6787
client = self.get_client()
6888
self.assertRaises(jsonrpclib.ProtocolError,
6989
client.add, (5,), {'y': 10})
7090

7191
def test_single_notify(self):
92+
"""
93+
Notification test
94+
"""
7295
client = self.get_client()
7396
result = client._notify.add(5, 10)
7497
self.assertTrue(result is None)
7598

7699
def test_single_namespace(self):
100+
"""
101+
Namespace test
102+
"""
77103
client = self.get_client()
78104
client.namespace.sum(1, 2, 4)
79105
request = json.loads(self.history.request)
@@ -89,6 +115,9 @@ def test_single_namespace(self):
89115
self.assertTrue(verify_response == response)
90116

91117
def test_multicall_success(self):
118+
"""
119+
Multi-call test
120+
"""
92121
multicall = self.get_multicall_client()
93122
multicall.ping()
94123
multicall.add(5, 10)
@@ -99,13 +128,19 @@ def test_multicall_success(self):
99128
self.assertTrue(result == correct[i])
100129

101130
def test_multicall_success_2(self):
131+
"""
132+
Another multi-call test
133+
"""
102134
multicall = self.get_multicall_client()
103135
for i in range(3):
104136
multicall.add(5, i)
105137
result = multicall()
106138
self.assertTrue(result[2] == 7)
107139

108140
def test_multicall_failure(self):
141+
"""
142+
Multi-call test with failures
143+
"""
109144
multicall = self.get_multicall_client()
110145
multicall.ping()
111146
multicall.add(x=5, y=10, z=10)

tests/utilities.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
:license: Apache License 2.0
77
"""
88

9-
# JSON-RPC library
10-
from jsonrpclib.SimpleJSONRPCServer import SimpleJSONRPCServer
11-
129
# Standard library
1310
import threading
1411

12+
# JSON-RPC library
13+
from jsonrpclib.SimpleJSONRPCServer import SimpleJSONRPCServer
14+
1515
# ------------------------------------------------------------------------------
1616
# Test methods
1717

@@ -24,28 +24,53 @@ def subtract(minuend, subtrahend):
2424

2525

2626
def add(x, y):
27+
"""
28+
Sample addition, positional arguments
29+
"""
2730
return x + y
2831

2932

3033
def update(*args):
34+
"""
35+
Sample with a list of optional arguments (returns the list)
36+
"""
3137
return args
3238

3339

3440
def summation(*args):
41+
"""
42+
Sample with a list of optional arguments (returns the sum of the arguments)
43+
"""
3544
return sum(args)
3645

3746

3847
def notify_hello(*args):
48+
"""
49+
Sample with a list of optional arguments (returns the list), meant to be
50+
called as a notification
51+
"""
3952
return args
4053

4154

4255
def get_data():
56+
"""
57+
Returns a list with a string and an integer
58+
"""
4359
return ['hello', 5]
4460

4561

4662
def ping():
63+
"""
64+
No argument, returns a boolean
65+
"""
4766
return True
4867

68+
def fail():
69+
"""
70+
No argument, raises an exception
71+
"""
72+
raise ValueError("Everything I do fails")
73+
4974
# ------------------------------------------------------------------------------
5075
# Server utility class
5176

@@ -83,6 +108,7 @@ def start(self, addr, port):
83108
server.register_function(add)
84109
server.register_function(ping)
85110
server.register_function(summation, 'namespace.sum')
111+
server.register_function(fail)
86112

87113
# Serve in a thread
88114
self._thread = threading.Thread(target=server.serve_forever)

0 commit comments

Comments
 (0)