[tests] remove direct testing on JSONRPCException from individual test cases

This commit is contained in:
John Newbery 2017-07-12 10:29:02 -04:00 committed by Alexander Block
parent 3712f8d053
commit 3602d31395
4 changed files with 25 additions and 21 deletions

View File

@ -19,9 +19,8 @@ importing nodes pick up the new transactions regardless of whether rescans
happened previously. happened previously.
""" """
from test_framework.authproxy import JSONRPCException
from test_framework.test_framework import BitcoinTestFramework from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import (connect_nodes, sync_blocks, assert_equal, set_node_times) from test_framework.util import (assert_raises_jsonrpc, connect_nodes, sync_blocks, assert_equal, set_node_times)
import collections import collections
import enum import enum
@ -36,21 +35,26 @@ Rescan = enum.Enum("Rescan", "no yes late_timestamp")
class Variant(collections.namedtuple("Variant", "call data rescan prune")): class Variant(collections.namedtuple("Variant", "call data rescan prune")):
"""Helper for importing one key and verifying scanned transactions.""" """Helper for importing one key and verifying scanned transactions."""
def try_rpc(self, func, *args, **kwargs):
if self.expect_disabled:
assert_raises_jsonrpc(-4, "Rescan is disabled in pruned mode", func, *args, **kwargs)
else:
return func(*args, **kwargs)
def do_import(self, timestamp): def do_import(self, timestamp):
"""Call one key import RPC.""" """Call one key import RPC."""
if self.call == Call.single: if self.call == Call.single:
if self.data == Data.address: if self.data == Data.address:
response, error = try_rpc(self.node.importaddress, self.address["address"], self.label, response = self.try_rpc(self.node.importaddress, self.address["address"], self.label,
self.rescan == Rescan.yes) self.rescan == Rescan.yes)
elif self.data == Data.pub: elif self.data == Data.pub:
response, error = try_rpc(self.node.importpubkey, self.address["pubkey"], self.label, response = self.try_rpc(self.node.importpubkey, self.address["pubkey"], self.label,
self.rescan == Rescan.yes) self.rescan == Rescan.yes)
elif self.data == Data.priv: elif self.data == Data.priv:
response, error = try_rpc(self.node.importprivkey, self.key, self.label, self.rescan == Rescan.yes) response = self.try_rpc(self.node.importprivkey, self.key, self.label, self.rescan == Rescan.yes)
assert_equal(response, None) assert_equal(response, None)
assert_equal(error, {'message': 'Rescan is disabled in pruned mode',
'code': -4} if self.expect_disabled else None)
elif self.call == Call.multi: elif self.call == Call.multi:
response = self.node.importmulti([{ response = self.node.importmulti([{
"scriptPubKey": { "scriptPubKey": {
@ -182,13 +186,5 @@ class ImportRescanTest(BitcoinTestFramework):
else: else:
variant.check() variant.check()
def try_rpc(func, *args, **kwargs):
try:
return func(*args, **kwargs), None
except JSONRPCException as e:
return None, e.error
if __name__ == "__main__": if __name__ == "__main__":
ImportRescanTest().main() ImportRescanTest().main()

View File

@ -448,11 +448,11 @@ class ImportMultiTest (BitcoinTestFramework):
# Bad or missing timestamps # Bad or missing timestamps
self.log.info("Should throw on invalid or missing timestamp values") self.log.info("Should throw on invalid or missing timestamp values")
assert_raises_message(JSONRPCException, 'Missing required timestamp field for key', assert_raises_jsonrpc(-3, 'Missing required timestamp field for key',
self.nodes[1].importmulti, [{ self.nodes[1].importmulti, [{
"scriptPubKey": address['scriptPubKey'], "scriptPubKey": address['scriptPubKey'],
}]) }])
assert_raises_message(JSONRPCException, 'Expected number or "now" timestamp value for key. got type string', assert_raises_jsonrpc(-3, 'Expected number or "now" timestamp value for key. got type string',
self.nodes[1].importmulti, [{ self.nodes[1].importmulti, [{
"scriptPubKey": address['scriptPubKey'], "scriptPubKey": address['scriptPubKey'],
"timestamp": "", "timestamp": "",

View File

@ -82,7 +82,7 @@ class SignRawTransactionsTest(BitcoinTestFramework):
assert_equal(decodedRawTx["vin"][i]["vout"], inp["vout"]) assert_equal(decodedRawTx["vin"][i]["vout"], inp["vout"])
# Make sure decoderawtransaction throws if there is extra data # Make sure decoderawtransaction throws if there is extra data
assert_raises(JSONRPCException, self.nodes[0].decoderawtransaction, rawTx + "00") assert_raises_jsonrpc(-22, "TX decode failed", self.nodes[0].decoderawtransaction, rawTx + "00")
rawTxSigned = self.nodes[0].signrawtransaction(rawTx, scripts, privKeys) rawTxSigned = self.nodes[0].signrawtransaction(rawTx, scripts, privKeys)

View File

@ -101,6 +101,13 @@ def assert_raises_jsonrpc(code, message, fun, *args, **kwds):
args*: positional arguments for the function. args*: positional arguments for the function.
kwds**: named arguments for the function. kwds**: named arguments for the function.
""" """
assert try_rpc(code, message, fun, *args, **kwds), "No exception raised"
def try_rpc(code, message, fun, *args, **kwds):
"""Tries to run an rpc command.
Test against error code and message if the rpc fails.
Returns whether a JSONRPCException was raised."""
try: try:
fun(*args, **kwds) fun(*args, **kwds)
except JSONRPCException as e: except JSONRPCException as e:
@ -109,10 +116,11 @@ def assert_raises_jsonrpc(code, message, fun, *args, **kwds):
raise AssertionError("Unexpected JSONRPC error code %i" % e.error["code"]) raise AssertionError("Unexpected JSONRPC error code %i" % e.error["code"])
if (message is not None) and (message not in e.error['message']): if (message is not None) and (message not in e.error['message']):
raise AssertionError("Expected substring not found:" + e.error['message']) raise AssertionError("Expected substring not found:" + e.error['message'])
return True
except Exception as e: except Exception as e:
raise AssertionError("Unexpected exception raised: " + type(e).__name__) raise AssertionError("Unexpected exception raised: " + type(e).__name__)
else: else:
raise AssertionError("No exception raised") return False
def assert_is_hex_string(string): def assert_is_hex_string(string):
try: try: