def test_get_result_invalid_id(self): batchresponse = BatchResponse() response = {"jsonrpc": "2.0", "result": 19, "id": 1} batchresponse.add_result(response) self.assertEqual(batchresponse.get_result(5), None)
def test_str(self): batchresponse = BatchResponse() response = {"jsonrpc": "2.0", "result": 19, "id": 1} batchresponse.add_result(response) self.assertEqual(batchresponse.__str__(), str(batchresponse.results))
def test_get_result(self): batchresponse = BatchResponse() response = {"jsonrpc": "2.0", "result": 19, "id": 1} batchresponse.add_result(response) self.assertEqual(batchresponse.get_result(1), 19)
def test_add_result(self): batchresponse = BatchResponse() response = {"jsonrpc": "2.0", "result": 19, "id": 1} result = {1: 19} batchresponse.add_result(response) self.assertEqual(batchresponse.results, result)
def test_process_batch(self): responses = ('[{"jsonrpc": "2.0", "result": 19, "id": 1},' '{"jsonrpc": "2.0", "result": "test", "id": 2},' '{"jsonrpc": "2.0", "result": 12.12, "id": 5}]') batchresponse = BatchResponse() batchresponse.process_batch(responses) expected = {1: 19, 2: 'test', 5: 12.12} self.assertEqual(batchresponse.results, expected)
def test_process_batch_invalid_string(self): responses = ( '[{"jsonrpffc":adf "2.0", "result": 19, "id": 1},' '{"jsonrpc": "2.0",9420 "result": "test", "id": 2},asdff388' '{"jsonrpc": "2.0"fee, "result": 12.12, "id": 5}]') batchresponse = BatchResponse() batchresponse.process_batch(responses) try: self.assertLogs() except: # assertLogs isn't available in python < 3.4 pass
def test_add_result_error(self): batchresponse = BatchResponse() response = { "jsonrpc": "2.0", "error": { "code": -32600, "message": "Invalid Request" }, "id": 1 } result = {1: {"code": -32600, "message": "Invalid Request"}} batchresponse.add_result(response) self.assertEqual(batchresponse.results, result)
def call_procedures(self, calls): """ Send a batch of requests to a json-rpc server. The server handles each request, and then sends back a batch response. If we don't get a valid (or any) response from the server, None will be returned. usage: call_procedures(batch_call) args: calls -- BatchResponse object containing several requests returns: a BatchResponse object (can be None) """ batch = BatchResponse() responses = self._connector.send_rpc_message(calls.format_request()) batch.process_batch(responses) return batch