Skip to content

Commit 5864e9c

Browse files
committed
[tests] remove direct testing on JSONRPCException from individual test cases
1 parent e93fff1 commit 5864e9c

File tree

5 files changed

+26
-26
lines changed

5 files changed

+26
-26
lines changed

test/functional/import-rescan.py

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@
1919
happened previously.
2020
"""
2121

22-
from test_framework.authproxy import JSONRPCException
2322
from test_framework.test_framework import BitcoinTestFramework
24-
from test_framework.util import (connect_nodes, sync_blocks, assert_equal, set_node_times)
23+
from test_framework.util import (assert_raises_jsonrpc, connect_nodes, sync_blocks, assert_equal, set_node_times)
2524

2625
import collections
2726
import enum
@@ -35,21 +34,26 @@
3534
class Variant(collections.namedtuple("Variant", "call data rescan prune")):
3635
"""Helper for importing one key and verifying scanned transactions."""
3736

37+
def try_rpc(self, func, *args, **kwargs):
38+
if self.expect_disabled:
39+
assert_raises_jsonrpc(-4, "Rescan is disabled in pruned mode", func, *args, **kwargs)
40+
else:
41+
return func(*args, **kwargs)
42+
3843
def do_import(self, timestamp):
3944
"""Call one key import RPC."""
4045

4146
if self.call == Call.single:
4247
if self.data == Data.address:
43-
response, error = try_rpc(self.node.importaddress, self.address["address"], self.label,
44-
self.rescan == Rescan.yes)
48+
response = self.try_rpc(self.node.importaddress, self.address["address"], self.label,
49+
self.rescan == Rescan.yes)
4550
elif self.data == Data.pub:
46-
response, error = try_rpc(self.node.importpubkey, self.address["pubkey"], self.label,
47-
self.rescan == Rescan.yes)
51+
response = self.try_rpc(self.node.importpubkey, self.address["pubkey"], self.label,
52+
self.rescan == Rescan.yes)
4853
elif self.data == Data.priv:
49-
response, error = try_rpc(self.node.importprivkey, self.key, self.label, self.rescan == Rescan.yes)
54+
response = self.try_rpc(self.node.importprivkey, self.key, self.label, self.rescan == Rescan.yes)
5055
assert_equal(response, None)
51-
assert_equal(error, {'message': 'Rescan is disabled in pruned mode',
52-
'code': -4} if self.expect_disabled else None)
56+
5357
elif self.call == Call.multi:
5458
response = self.node.importmulti([{
5559
"scriptPubKey": {
@@ -179,13 +183,5 @@ def run_test(self):
179183
else:
180184
variant.check()
181185

182-
183-
def try_rpc(func, *args, **kwargs):
184-
try:
185-
return func(*args, **kwargs), None
186-
except JSONRPCException as e:
187-
return None, e.error
188-
189-
190186
if __name__ == "__main__":
191187
ImportRescanTest().main()

test/functional/importmulti.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -423,11 +423,11 @@ def run_test (self):
423423

424424
# Bad or missing timestamps
425425
self.log.info("Should throw on invalid or missing timestamp values")
426-
assert_raises_message(JSONRPCException, 'Missing required timestamp field for key',
426+
assert_raises_jsonrpc(-3, 'Missing required timestamp field for key',
427427
self.nodes[1].importmulti, [{
428428
"scriptPubKey": address['scriptPubKey'],
429429
}])
430-
assert_raises_message(JSONRPCException, 'Expected number or "now" timestamp value for key. got type string',
430+
assert_raises_jsonrpc(-3, 'Expected number or "now" timestamp value for key. got type string',
431431
self.nodes[1].importmulti, [{
432432
"scriptPubKey": address['scriptPubKey'],
433433
"timestamp": "",

test/functional/segwit.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -452,11 +452,7 @@ def run_test(self):
452452
for i in importlist:
453453
# import all generated addresses. The wallet already has the private keys for some of these, so catch JSON RPC
454454
# exceptions and continue.
455-
try:
456-
self.nodes[0].importaddress(i,"",False,True)
457-
except JSONRPCException as exp:
458-
assert_equal(exp.error["message"], "The wallet already contains the private key for this address or script")
459-
assert_equal(exp.error["code"], -4)
455+
try_rpc(-4, "The wallet already contains the private key for this address or script", self.nodes[0].importaddress, i, "", False, True)
460456

461457
self.nodes[0].importaddress(script_to_p2sh(op0)) # import OP_0 as address only
462458
self.nodes[0].importaddress(multisig_without_privkey_address) # Test multisig_without_privkey

test/functional/signrawtransactions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def script_verification_error_test(self):
8282
assert_equal(decodedRawTx["vin"][i]["vout"], inp["vout"])
8383

8484
# Make sure decoderawtransaction throws if there is extra data
85-
assert_raises(JSONRPCException, self.nodes[0].decoderawtransaction, rawTx + "00")
85+
assert_raises_jsonrpc(-22, "TX decode failed", self.nodes[0].decoderawtransaction, rawTx + "00")
8686

8787
rawTxSigned = self.nodes[0].signrawtransaction(rawTx, scripts, privKeys)
8888

test/functional/test_framework/util.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,13 @@ def assert_raises_jsonrpc(code, message, fun, *args, **kwds):
9999
args*: positional arguments for the function.
100100
kwds**: named arguments for the function.
101101
"""
102+
assert try_rpc(code, message, fun, *args, **kwds), "No exception raised"
103+
104+
def try_rpc(code, message, fun, *args, **kwds):
105+
"""Tries to run an rpc command.
106+
107+
Test against error code and message if the rpc fails.
108+
Returns whether a JSONRPCException was raised."""
102109
try:
103110
fun(*args, **kwds)
104111
except JSONRPCException as e:
@@ -107,10 +114,11 @@ def assert_raises_jsonrpc(code, message, fun, *args, **kwds):
107114
raise AssertionError("Unexpected JSONRPC error code %i" % e.error["code"])
108115
if (message is not None) and (message not in e.error['message']):
109116
raise AssertionError("Expected substring not found:" + e.error['message'])
117+
return True
110118
except Exception as e:
111119
raise AssertionError("Unexpected exception raised: " + type(e).__name__)
112120
else:
113-
raise AssertionError("No exception raised")
121+
return False
114122

115123
def assert_is_hex_string(string):
116124
try:

0 commit comments

Comments
 (0)