rpc_named_arguments.py raw
1 #!/usr/bin/env python3
2 # Copyright (c) 2016-present 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 """Test using named arguments for RPCs."""
6
7 from test_framework.test_framework import BitcoinTestFramework
8 from test_framework.util import (
9 assert_equal,
10 assert_raises_rpc_error,
11 )
12
13
14 class NamedArgumentTest(BitcoinTestFramework):
15 def set_test_params(self):
16 self.num_nodes = 1
17
18 def run_test(self):
19 node = self.nodes[0]
20 h = node.help(command='getblockchaininfo')
21 assert h.startswith('getblockchaininfo\n')
22
23 assert_raises_rpc_error(-8, 'Unknown named parameter', node.help, random='getblockchaininfo')
24
25 h = node.getblockhash(height=0)
26 node.getblock(blockhash=h)
27
28 assert_equal(node.echojson(), [])
29 assert_equal(node.echojson(arg0=0, arg9=9), [0] + [None] * 8 + [9])
30 assert_equal(node.echojson(arg1=1), [None, 1])
31 assert_equal(node.echojson(arg9=None), [] if self.options.usecli else [None] * 10)
32 assert_equal(node.echojson(arg0=0, arg3=3, arg9=9), [0] + [None] * 2 + [3] + [None] * 5 + [9])
33 assert_equal(node.echojson(0, 1, arg3=3, arg5=5), [0, 1, None, 3, None, 5])
34 assert_raises_rpc_error(-8, "Parameter arg1 specified twice both as positional and named argument", node.echojson, 0, 1, arg1=1)
35 assert_raises_rpc_error(-8, "Parameter arg1 specified twice both as positional and named argument", node.echojson, 0, None, 2, arg1=1)
36
37 if __name__ == '__main__':
38 NamedArgumentTest(__file__).main()
39