Skip to content

Commit 2ca9dcd

Browse files
committed
test: Add test for RPC named arguments
Add RPC testcase for RPC named arguments.
1 parent fba1a61 commit 2ca9dcd

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

qa/pull-tester/rpc-tests.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@
151151
'signmessages.py',
152152
'nulldummy.py',
153153
'import-rescan.py',
154+
'rpcnamedargs.py',
154155
]
155156
if ENABLE_ZMQ:
156157
testScripts.append('zmq_test.py')

qa/rpc-tests/rpcnamedargs.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) 2016 The Bitcoin Core developers
3+
# Distributed under the MIT software license, see the accompanying
4+
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
5+
6+
from decimal import Decimal
7+
8+
from test_framework.test_framework import BitcoinTestFramework
9+
from test_framework.authproxy import JSONRPCException
10+
from test_framework.util import (
11+
assert_equal,
12+
assert_raises_jsonrpc,
13+
assert_is_hex_string,
14+
assert_is_hash_string,
15+
start_nodes,
16+
connect_nodes_bi,
17+
)
18+
19+
20+
class NamedArgumentTest(BitcoinTestFramework):
21+
"""
22+
Test named arguments on RPC calls.
23+
"""
24+
25+
def __init__(self):
26+
super().__init__()
27+
self.setup_clean_chain = False
28+
self.num_nodes = 1
29+
30+
def setup_network(self, split=False):
31+
self.nodes = start_nodes(self.num_nodes, self.options.tmpdir)
32+
self.is_network_split = False
33+
self.sync_all()
34+
35+
def run_test(self):
36+
node = self.nodes[0]
37+
h = node.help(command='getinfo')
38+
assert(h.startswith('getinfo\n'))
39+
40+
assert_raises_jsonrpc(-8, node.help, random='getinfo')
41+
42+
h = node.getblockhash(index=0)
43+
node.getblock(hash=h)
44+
45+
assert_equal(node.echo(), [])
46+
assert_equal(node.echo(arg0=0,arg9=9), [0] + [None]*8 + [9])
47+
assert_equal(node.echo(arg1=1), [None, 1])
48+
assert_equal(node.echo(arg9=None), [None]*10)
49+
assert_equal(node.echo(arg0=0,arg3=3,arg9=9), [0] + [None]*2 + [3] + [None]*5 + [9])
50+
51+
if __name__ == '__main__':
52+
NamedArgumentTest().main()

qa/rpc-tests/test_framework/util.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,18 @@ def assert_raises_message(exc, message, fun, *args, **kwds):
546546
else:
547547
raise AssertionError("No exception raised")
548548

549+
def assert_raises_jsonrpc(code, fun, *args, **kwds):
550+
'''Check for specific JSONRPC exception code'''
551+
try:
552+
fun(*args, **kwds)
553+
except JSONRPCException as e:
554+
if e.error["code"] != code:
555+
raise AssertionError("Unexpected JSONRPC error code %i" % e.error["code"])
556+
except Exception as e:
557+
raise AssertionError("Unexpected exception raised: "+type(e).__name__)
558+
else:
559+
raise AssertionError("No exception raised")
560+
549561
def assert_is_hex_string(string):
550562
try:
551563
int(string, 16)

0 commit comments

Comments
 (0)