Skip to content

Commit 18c09a0

Browse files
committed
Format review on tests
1 parent b666858 commit 18c09a0

File tree

11 files changed

+374
-255
lines changed

11 files changed

+374
-255
lines changed

tests/test_cgi.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
try:
1818
from http.server import HTTPServer, CGIHTTPRequestHandler
1919
except ImportError:
20-
from BaseHTTPServer import HTTPServer
21-
from CGIHTTPServer import CGIHTTPRequestHandler
20+
from BaseHTTPServer import HTTPServer # type: ignore
21+
from CGIHTTPServer import CGIHTTPRequestHandler # type: ignore
2222

2323
# JSON-RPC library
2424
from jsonrpclib import ServerProxy
@@ -57,9 +57,9 @@ def test_server(self):
5757

5858
# Check call
5959
for _ in range(2):
60-
a, b = random.random(), random.random()
61-
result = client.add(a, b)
62-
self.assertEqual(result, a + b)
60+
rand1, rand2 = random.random(), random.random()
61+
result = client.add(rand1, rand2)
62+
self.assertEqual(result, rand1 + rand2)
6363

6464
# Close server
6565
server.shutdown()

tests/test_compatibility.py

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

9-
# Tests utilities
10-
from tests.utilities import UtilityServer
9+
# Standard library
10+
import json
11+
import unittest
1112

1213
# JSON-RPC library
1314
import jsonrpclib
1415

15-
# Standard library
16-
import json
17-
import unittest
16+
# Tests utilities
17+
from tests.utilities import UtilityServer
1818

1919
# ------------------------------------------------------------------------------
2020

@@ -23,18 +23,20 @@ class TestCompatibility(unittest.TestCase):
2323
"""
2424
Tests JSON-RPC compatibility
2525
"""
26+
2627
def setUp(self):
2728
"""
2829
Pre-test set up
2930
"""
3031
# Set up the server
31-
self.server = UtilityServer().start('', 0)
32+
self.server = UtilityServer().start("", 0)
3233
self.port = self.server.get_port()
3334

3435
# Set up the client
3536
self.history = jsonrpclib.history.History()
3637
self.client = jsonrpclib.ServerProxy(
37-
'http://localhost:{0}'.format(self.port), history=self.history)
38+
"http://localhost:{0}".format(self.port), history=self.history
39+
)
3840

3941
def tearDown(self):
4042
"""
@@ -56,12 +58,12 @@ def test_positional(self):
5658
request = json.loads(self.history.request)
5759
response = json.loads(self.history.response)
5860
verify_request = {
59-
"jsonrpc": "2.0", "method": "subtract",
60-
"params": [42, 23], "id": request['id']
61-
}
62-
verify_response = {
63-
"jsonrpc": "2.0", "result": 19, "id": request['id']
61+
"jsonrpc": "2.0",
62+
"method": "subtract",
63+
"params": [42, 23],
64+
"id": request["id"],
6465
}
66+
verify_response = {"jsonrpc": "2.0", "result": 19, "id": request["id"]}
6567
self.assertTrue(request == verify_request)
6668
self.assertTrue(response == verify_response)
6769

@@ -74,13 +76,12 @@ def test_named(self):
7476
request = json.loads(self.history.request)
7577
response = json.loads(self.history.response)
7678
verify_request = {
77-
"jsonrpc": "2.0", "method": "subtract",
79+
"jsonrpc": "2.0",
80+
"method": "subtract",
7881
"params": {"subtrahend": 23, "minuend": 42},
79-
"id": request['id']
80-
}
81-
verify_response = {
82-
"jsonrpc": "2.0", "result": 19, "id": request['id']
82+
"id": request["id"],
8383
}
84+
verify_response = {"jsonrpc": "2.0", "result": 19, "id": request["id"]}
8485
self.assertTrue(request == verify_request)
8586
self.assertTrue(response == verify_response)
8687

@@ -91,106 +92,123 @@ def test_notification(self):
9192
request = json.loads(self.history.request)
9293
response = self.history.response
9394
verify_request = {
94-
"jsonrpc": "2.0", "method": "update", "params": [1, 2, 3, 4, 5]
95+
"jsonrpc": "2.0",
96+
"method": "update",
97+
"params": [1, 2, 3, 4, 5],
9598
}
96-
verify_response = ''
99+
verify_response = ""
97100
self.assertTrue(request == verify_request)
98101
self.assertTrue(response == verify_response)
99102

100103
def test_non_existent_method(self):
104+
""" Testing behaviour when calling a non-existent method """
101105
self.assertRaises(jsonrpclib.ProtocolError, self.client.foobar)
102106
request = json.loads(self.history.request)
103107
response = json.loads(self.history.response)
104108
verify_request = {
105-
"jsonrpc": "2.0", "method": "foobar", "id": request['id']
109+
"jsonrpc": "2.0",
110+
"method": "foobar",
111+
"id": request["id"],
106112
}
107113
verify_response = {
108114
"jsonrpc": "2.0",
109-
"error":
110-
{"code": -32601, "message": response['error']['message']},
111-
"id": request['id']
115+
"error": {"code": -32601, "message": response["error"]["message"]},
116+
"id": request["id"],
112117
}
113118
self.assertTrue(request == verify_request)
114119
self.assertTrue(response == verify_response)
115120

116121
def test_special_method(self):
117-
self.assertRaises(AttributeError, getattr, self.client, '__special_method__')
122+
""" Tests behaviour on dunder methods """
123+
self.assertRaises(
124+
AttributeError, getattr, self.client, "__special_method__"
125+
)
118126
self.assertIsNone(self.history.request)
119127

120128
def test_invalid_json(self):
121-
invalid_json = '{"jsonrpc": "2.0", "method": "foobar, ' + \
122-
'"params": "bar", "baz]'
129+
""" Tests behaviour on invalid JSON request """
130+
invalid_json = (
131+
'{"jsonrpc": "2.0", "method": "foobar, ' + '"params": "bar", "baz]'
132+
)
123133
self.client._run_request(invalid_json)
124134
response = json.loads(self.history.response)
125135
verify_response = json.loads(
126-
'{"jsonrpc": "2.0", "error": {"code": -32700,' +
127-
' "message": "Parse error."}, "id": null}'
136+
'{"jsonrpc": "2.0", "error": {"code": -32700,'
137+
+ ' "message": "Parse error."}, "id": null}'
128138
)
129-
verify_response['error']['message'] = response['error']['message']
139+
verify_response["error"]["message"] = response["error"]["message"]
130140
self.assertTrue(response == verify_response)
131141

132142
def test_invalid_request(self):
143+
""" Tests incomplete request """
133144
invalid_request = '{"jsonrpc": "2.0", "method": 1, "params": "bar"}'
134145
self.client._run_request(invalid_request)
135146
response = json.loads(self.history.response)
136147
verify_response = json.loads(
137-
'{"jsonrpc": "2.0", "error": {"code": -32600, ' +
138-
'"message": "Invalid Request."}, "id": null}'
148+
'{"jsonrpc": "2.0", "error": {"code": -32600, '
149+
+ '"message": "Invalid Request."}, "id": null}'
139150
)
140-
verify_response['error']['message'] = response['error']['message']
151+
verify_response["error"]["message"] = response["error"]["message"]
141152
self.assertTrue(response == verify_response)
142153

143154
def test_batch_invalid_json(self):
144-
invalid_request = '[ {"jsonrpc": "2.0", "method": "sum", ' + \
145-
'"params": [1,2,4], "id": "1"},{"jsonrpc": "2.0", "method" ]'
155+
""" Tests invalid JSON request on batch call """
156+
invalid_request = (
157+
'[ {"jsonrpc": "2.0", "method": "sum", '
158+
+ '"params": [1,2,4], "id": "1"},{"jsonrpc": "2.0", "method" ]'
159+
)
146160
self.client._run_request(invalid_request)
147161
response = json.loads(self.history.response)
148162
verify_response = json.loads(
149-
'{"jsonrpc": "2.0", "error": {"code": -32700,' +
150-
'"message": "Parse error."}, "id": null}'
163+
'{"jsonrpc": "2.0", "error": {"code": -32700,'
164+
+ '"message": "Parse error."}, "id": null}'
151165
)
152-
verify_response['error']['message'] = response['error']['message']
166+
verify_response["error"]["message"] = response["error"]["message"]
153167
self.assertTrue(response == verify_response)
154168

155169
def test_empty_array(self):
156-
invalid_request = '[]'
170+
""" Tests empty array as request """
171+
invalid_request = "[]"
157172
self.client._run_request(invalid_request)
158173
response = json.loads(self.history.response)
159174
verify_response = json.loads(
160-
'{"jsonrpc": "2.0", "error": {"code": -32600, ' +
161-
'"message": "Invalid Request."}, "id": null}'
175+
'{"jsonrpc": "2.0", "error": {"code": -32600, '
176+
+ '"message": "Invalid Request."}, "id": null}'
162177
)
163-
verify_response['error']['message'] = response['error']['message']
178+
verify_response["error"]["message"] = response["error"]["message"]
164179
self.assertTrue(response == verify_response)
165180

166181
def test_nonempty_array(self):
167-
invalid_request = '[1,2]'
182+
""" Tests array as request """
183+
invalid_request = "[1,2]"
168184
request_obj = json.loads(invalid_request)
169185
self.client._run_request(invalid_request)
170186
response = json.loads(self.history.response)
171187
self.assertTrue(len(response) == len(request_obj))
172188
for resp in response:
173189
verify_resp = json.loads(
174-
'{"jsonrpc": "2.0", "error": {"code": -32600, ' +
175-
'"message": "Invalid Request."}, "id": null}'
190+
'{"jsonrpc": "2.0", "error": {"code": -32600, '
191+
+ '"message": "Invalid Request."}, "id": null}'
176192
)
177-
verify_resp['error']['message'] = resp['error']['message']
193+
verify_resp["error"]["message"] = resp["error"]["message"]
178194
self.assertTrue(resp == verify_resp)
179195

180196
def test_batch(self):
197+
""" Tests batch call """
181198
multicall = jsonrpclib.MultiCall(self.client)
182199
multicall.sum(1, 2, 4)
183200
multicall._notify.notify_hello(7)
184201
multicall.subtract(42, 23)
185-
multicall.foo.get(name='myself')
202+
multicall.foo.get(name="myself")
186203
multicall.get_data()
187204
job_requests = [j.request() for j in multicall._job_list]
188205
job_requests.insert(3, '{"foo": "boo"}')
189-
json_requests = '[%s]' % ','.join(job_requests)
206+
json_requests = "[%s]" % ",".join(job_requests)
190207
requests = json.loads(json_requests)
191208
responses = self.client._run_request(json_requests)
192209

193-
verify_requests = json.loads("""[
210+
verify_requests = json.loads(
211+
"""[
194212
{"jsonrpc": "2.0", "method": "sum", "params": [1,2,4], "id": "1"},
195213
{"jsonrpc": "2.0", "method": "notify_hello", "params": [7]},
196214
{"jsonrpc": "2.0", "method": "subtract",
@@ -199,10 +217,12 @@ def test_batch(self):
199217
{"jsonrpc": "2.0", "method": "foo.get",
200218
"params": {"name": "myself"}, "id": "5"},
201219
{"jsonrpc": "2.0", "method": "get_data", "id": "9"}
202-
]""")
220+
]"""
221+
)
203222

204223
# Thankfully, these are in order so testing is pretty simple.
205-
verify_responses = json.loads("""[
224+
verify_responses = json.loads(
225+
"""[
206226
{"jsonrpc": "2.0", "result": 7, "id": "1"},
207227
{"jsonrpc": "2.0", "result": 19, "id": "2"},
208228
{"jsonrpc": "2.0",
@@ -212,51 +232,50 @@ def test_batch(self):
212232
"error": {"code": -32601, "message": "Method not found."},
213233
"id": "5"},
214234
{"jsonrpc": "2.0", "result": ["hello", 5], "id": "9"}
215-
]""")
235+
]"""
236+
)
216237

217238
self.assertTrue(len(requests) == len(verify_requests))
218239
self.assertTrue(len(responses) == len(verify_responses))
219240

220241
responses_by_id = {}
221242
response_i = 0
222243

223-
for i in range(len(requests)):
224-
verify_request = verify_requests[i]
225-
request = requests[i]
244+
for verify_request, request in zip(verify_requests, requests):
226245
response = None
227-
if request.get('method') != 'notify_hello':
228-
req_id = request.get('id')
229-
if 'id' in verify_request:
230-
verify_request['id'] = req_id
246+
if request.get("method") != "notify_hello":
247+
req_id = request.get("id")
248+
if "id" in verify_request:
249+
verify_request["id"] = req_id
231250
verify_response = verify_responses[response_i]
232-
verify_response['id'] = req_id
251+
verify_response["id"] = req_id
233252
responses_by_id[req_id] = verify_response
234253
response_i += 1
235254
response = verify_response
236255
self.assertTrue(request == verify_request)
237256

238257
for response in responses:
239-
verify_response = responses_by_id.get(response.get('id'))
240-
if 'error' in verify_response:
241-
verify_response['error']['message'] = \
242-
response['error']['message']
258+
verify_response = responses_by_id.get(response.get("id"))
259+
if "error" in verify_response:
260+
verify_response["error"]["message"] = response["error"][
261+
"message"
262+
]
243263
self.assertTrue(response == verify_response)
244264

245265
def test_batch_notifications(self):
266+
""" Tests batch notifications """
246267
multicall = jsonrpclib.MultiCall(self.client)
247268
multicall._notify.notify_sum(1, 2, 4)
248269
multicall._notify.notify_hello(7)
249270
result = multicall()
250271
self.assertTrue(len(result) == 0)
251272
valid_request = json.loads(
252-
'[{"jsonrpc": "2.0", "method": "notify_sum", ' +
253-
'"params": [1,2,4]},{"jsonrpc": "2.0", ' +
254-
'"method": "notify_hello", "params": [7]}]'
273+
'[{"jsonrpc": "2.0", "method": "notify_sum", '
274+
+ '"params": [1,2,4]},{"jsonrpc": "2.0", '
275+
+ '"method": "notify_hello", "params": [7]}]'
255276
)
256277
request = json.loads(self.history.request)
257278
self.assertTrue(len(request) == len(valid_request))
258-
for i in range(len(request)):
259-
req = request[i]
260-
valid_req = valid_request[i]
279+
for req, valid_req in zip(request, valid_request):
261280
self.assertTrue(req == valid_req)
262-
self.assertTrue(self.history.response == '')
281+
self.assertTrue(self.history.response == "")

0 commit comments

Comments
 (0)