[tests] don't override __init__() in individual tests
[bitcoinplatinum.git] / test / functional / receivedby.py
blob48eb1c51b51c638bf28f355040f40cb00904c32a
1 #!/usr/bin/env python3
2 # Copyright (c) 2014-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 """Test the listreceivedbyaddress RPC."""
7 from test_framework.test_framework import BitcoinTestFramework
8 from test_framework.util import *
10 def get_sub_array_from_array(object_array, to_match):
11 '''
12 Finds and returns a sub array from an array of arrays.
13 to_match should be a unique idetifier of a sub array
14 '''
15 for item in object_array:
16 all_match = True
17 for key,value in to_match.items():
18 if item[key] != value:
19 all_match = False
20 if not all_match:
21 continue
22 return item
23 return []
25 class ReceivedByTest(BitcoinTestFramework):
26 def set_test_params(self):
27 self.enable_mocktime()
29 def run_test(self):
30 '''
31 listreceivedbyaddress Test
32 '''
33 # Send from node 0 to 1
34 addr = self.nodes[1].getnewaddress()
35 txid = self.nodes[0].sendtoaddress(addr, 0.1)
36 self.sync_all()
38 #Check not listed in listreceivedbyaddress because has 0 confirmations
39 assert_array_result(self.nodes[1].listreceivedbyaddress(),
40 {"address":addr},
41 { },
42 True)
43 #Bury Tx under 10 block so it will be returned by listreceivedbyaddress
44 self.nodes[1].generate(10)
45 self.sync_all()
46 assert_array_result(self.nodes[1].listreceivedbyaddress(),
47 {"address":addr},
48 {"address":addr, "account":"", "amount":Decimal("0.1"), "confirmations":10, "txids":[txid,]})
49 #With min confidence < 10
50 assert_array_result(self.nodes[1].listreceivedbyaddress(5),
51 {"address":addr},
52 {"address":addr, "account":"", "amount":Decimal("0.1"), "confirmations":10, "txids":[txid,]})
53 #With min confidence > 10, should not find Tx
54 assert_array_result(self.nodes[1].listreceivedbyaddress(11),{"address":addr},{ },True)
56 #Empty Tx
57 addr = self.nodes[1].getnewaddress()
58 assert_array_result(self.nodes[1].listreceivedbyaddress(0,True),
59 {"address":addr},
60 {"address":addr, "account":"", "amount":0, "confirmations":0, "txids":[]})
62 '''
63 getreceivedbyaddress Test
64 '''
65 # Send from node 0 to 1
66 addr = self.nodes[1].getnewaddress()
67 txid = self.nodes[0].sendtoaddress(addr, 0.1)
68 self.sync_all()
70 #Check balance is 0 because of 0 confirmations
71 balance = self.nodes[1].getreceivedbyaddress(addr)
72 if balance != Decimal("0.0"):
73 raise AssertionError("Wrong balance returned by getreceivedbyaddress, %0.2f"%(balance))
75 #Check balance is 0.1
76 balance = self.nodes[1].getreceivedbyaddress(addr,0)
77 if balance != Decimal("0.1"):
78 raise AssertionError("Wrong balance returned by getreceivedbyaddress, %0.2f"%(balance))
80 #Bury Tx under 10 block so it will be returned by the default getreceivedbyaddress
81 self.nodes[1].generate(10)
82 self.sync_all()
83 balance = self.nodes[1].getreceivedbyaddress(addr)
84 if balance != Decimal("0.1"):
85 raise AssertionError("Wrong balance returned by getreceivedbyaddress, %0.2f"%(balance))
87 '''
88 listreceivedbyaccount + getreceivedbyaccount Test
89 '''
90 #set pre-state
91 addrArr = self.nodes[1].getnewaddress()
92 account = self.nodes[1].getaccount(addrArr)
93 received_by_account_json = get_sub_array_from_array(self.nodes[1].listreceivedbyaccount(),{"account":account})
94 if len(received_by_account_json) == 0:
95 raise AssertionError("No accounts found in node")
96 balance_by_account = self.nodes[1].getreceivedbyaccount(account)
98 txid = self.nodes[0].sendtoaddress(addr, 0.1)
99 self.sync_all()
101 # listreceivedbyaccount should return received_by_account_json because of 0 confirmations
102 assert_array_result(self.nodes[1].listreceivedbyaccount(),
103 {"account":account},
104 received_by_account_json)
106 # getreceivedbyaddress should return same balance because of 0 confirmations
107 balance = self.nodes[1].getreceivedbyaccount(account)
108 if balance != balance_by_account:
109 raise AssertionError("Wrong balance returned by getreceivedbyaccount, %0.2f"%(balance))
111 self.nodes[1].generate(10)
112 self.sync_all()
113 # listreceivedbyaccount should return updated account balance
114 assert_array_result(self.nodes[1].listreceivedbyaccount(),
115 {"account":account},
116 {"account":received_by_account_json["account"], "amount":(received_by_account_json["amount"] + Decimal("0.1"))})
118 # getreceivedbyaddress should return updates balance
119 balance = self.nodes[1].getreceivedbyaccount(account)
120 if balance != balance_by_account + Decimal("0.1"):
121 raise AssertionError("Wrong balance returned by getreceivedbyaccount, %0.2f"%(balance))
123 #Create a new account named "mynewaccount" that has a 0 balance
124 self.nodes[1].getaccountaddress("mynewaccount")
125 received_by_account_json = get_sub_array_from_array(self.nodes[1].listreceivedbyaccount(0,True),{"account":"mynewaccount"})
126 if len(received_by_account_json) == 0:
127 raise AssertionError("No accounts found in node")
129 # Test includeempty of listreceivedbyaccount
130 if received_by_account_json["amount"] != Decimal("0.0"):
131 raise AssertionError("Wrong balance returned by listreceivedbyaccount, %0.2f"%(received_by_account_json["amount"]))
133 # Test getreceivedbyaccount for 0 amount accounts
134 balance = self.nodes[1].getreceivedbyaccount("mynewaccount")
135 if balance != Decimal("0.0"):
136 raise AssertionError("Wrong balance returned by getreceivedbyaccount, %0.2f"%(balance))
138 if __name__ == '__main__':
139 ReceivedByTest().main()