Add mock tests for accounts, addresses and balance.
Add method for getting a single balance.
This commit is contained in:
parent
5e96380d7b
commit
56d7443616
|
@ -9,14 +9,17 @@ class Account(object):
|
||||||
self.index = index
|
self.index = index
|
||||||
self._backend = backend
|
self._backend = backend
|
||||||
|
|
||||||
def get_balance(self):
|
def get_balances(self):
|
||||||
return self._backend.get_balance(account=self.index)
|
return self._backend.get_balances(account=self.index)
|
||||||
|
|
||||||
|
def get_balance(self, unlocked=False):
|
||||||
|
return self._backend.get_balances(account=self.index)[1 if unlocked else 0]
|
||||||
|
|
||||||
def get_address(self):
|
def get_address(self):
|
||||||
"""
|
"""
|
||||||
Return account's main address.
|
Return account's main address.
|
||||||
"""
|
"""
|
||||||
return self._backend.get_address(account=self.index)[0]
|
return self._backend.get_addresses(account=self.index)[0]
|
||||||
|
|
||||||
def get_addresses(self):
|
def get_addresses(self):
|
||||||
return self._backend.get_addresses(account=self.index)
|
return self._backend.get_addresses(account=self.index)
|
||||||
|
|
|
@ -49,7 +49,7 @@ class JSONRPC(object):
|
||||||
addresses[_addr['address_index']] = address(_addr['address'])
|
addresses[_addr['address_index']] = address(_addr['address'])
|
||||||
return addresses
|
return addresses
|
||||||
|
|
||||||
def get_balance(self, account=0):
|
def get_balances(self, account=0):
|
||||||
_balance = self.raw_request('getbalance', {'account_index': account})
|
_balance = self.raw_request('getbalance', {'account_index': account})
|
||||||
return (from_atomic(_balance['balance']), from_atomic(_balance['unlocked_balance']))
|
return (from_atomic(_balance['balance']), from_atomic(_balance['unlocked_balance']))
|
||||||
|
|
||||||
|
|
|
@ -22,8 +22,11 @@ class Wallet(object):
|
||||||
idx += 1
|
idx += 1
|
||||||
|
|
||||||
# Following methods operate on default account (index=0)
|
# Following methods operate on default account (index=0)
|
||||||
def get_balance(self):
|
def get_balances(self):
|
||||||
return self.accounts[0].get_balance()
|
return self.accounts[0].get_balances()
|
||||||
|
|
||||||
|
def get_balance(self, unlocked=False):
|
||||||
|
return self.accounts[0].get_balance(unlocked=unlocked)
|
||||||
|
|
||||||
def get_address(self, index=0):
|
def get_address(self, index=0):
|
||||||
return self.accounts[0].get_addresses()[0]
|
return self.accounts[0].get_addresses()[0]
|
||||||
|
|
|
@ -1,2 +1,3 @@
|
||||||
from .address import AddressTestCase, TestnetAddressTestCase
|
from .address import AddressTestCase, TestnetAddressTestCase
|
||||||
from .numbers import NumbersTestCase
|
from .numbers import NumbersTestCase
|
||||||
|
from .wallet import SubaddrWalletTestCase
|
||||||
|
|
|
@ -0,0 +1,125 @@
|
||||||
|
from decimal import Decimal
|
||||||
|
import unittest
|
||||||
|
from unittest.mock import patch, Mock
|
||||||
|
|
||||||
|
from monero.wallet import Wallet
|
||||||
|
from monero.backends.jsonrpc import JSONRPC
|
||||||
|
|
||||||
|
class SubaddrWalletTestCase(unittest.TestCase):
|
||||||
|
@patch('monero.backends.jsonrpc.requests.post')
|
||||||
|
def test_get_balance(self, mock_post):
|
||||||
|
mock_post.return_value.status_code = 200
|
||||||
|
mock_post.return_value.json.return_value = {'id': 0,
|
||||||
|
'jsonrpc': '2.0',
|
||||||
|
'result': {'subaddress_accounts': [{'account_index': 0,
|
||||||
|
'balance': 224916129245183,
|
||||||
|
'base_address': '9vgV48wWAPTWik5QSUSoGYicdvvsbSNHrT9Arsx1XBTz6VrWPSgfmnUKSPZDMyX4Ms8R9TkhB4uFqK9s5LUBbV6YQN2Q9ag',
|
||||||
|
'label': 'Primary account',
|
||||||
|
'unlocked_balance': 224916129245183},
|
||||||
|
{'account_index': 1,
|
||||||
|
'balance': 3981420960933,
|
||||||
|
'base_address': 'BaCBwYSK9BGSuKxb2msXEj4mmpvZYJexYHfqx7kNPDrXDePVXSfoofxGquhXxpA4uxawcnVnouusMDgP74CACa7e9siimpj',
|
||||||
|
'label': 'Untitled account',
|
||||||
|
'unlocked_balance': 3981420960933},
|
||||||
|
{'account_index': 2,
|
||||||
|
'balance': 7256159239955,
|
||||||
|
'base_address': 'BgCseuY3jFJAZS7kt9mrNg7fEG3bo5BV91CTyKbYu9GFiU6hUZhvdNWCTUdQNPNcA4PyFApsFr3EsQDEDfT3tQSY1mVZeP2',
|
||||||
|
'label': 'Untitled account',
|
||||||
|
'unlocked_balance': 7256159239955}],
|
||||||
|
'total_balance': 236153709446071,
|
||||||
|
'total_unlocked_balance': 236153709446071}}
|
||||||
|
self.wallet = Wallet(JSONRPC())
|
||||||
|
mock_post.return_value.json.return_value = {'id': 0,
|
||||||
|
'jsonrpc': '2.0',
|
||||||
|
'result': {'balance': 224916129245183,
|
||||||
|
'per_subaddress': [{'address': '9vgV48wWAPTWik5QSUSoGYicdvvsbSNHrT9Arsx1XBTz6VrWPSgfmnUKSPZDMyX4Ms8R9TkhB4uFqK9s5LUBbV6YQN2Q9ag',
|
||||||
|
'address_index': 0,
|
||||||
|
'balance': 189656129245183,
|
||||||
|
'label': 'Primary account',
|
||||||
|
'num_unspent_outputs': 2,
|
||||||
|
'unlocked_balance': 189656129245183},
|
||||||
|
{'address': 'BfJ5W7dZGaYih6J63YvhiDSKpVUUZbVrEhLRCY6L6TdnEfzJmwP6aUJZQQnzLQ2NMTKMAC8hiJsoiNC7jbEUZ8tmBoJcnN1',
|
||||||
|
'address_index': 5,
|
||||||
|
'balance': 260000000000,
|
||||||
|
'label': '(Untitled address)',
|
||||||
|
'num_unspent_outputs': 1,
|
||||||
|
'unlocked_balance': 260000000000},
|
||||||
|
{'address': 'BbkS4mn6gcgUidn2znLa2J6eSBkbGjGX4doeDCKAzT2A3t1cjbquQGjhYgiMHiKTrY8ojk6Zjqi1ufvfuPwyKv4hNnMruro',
|
||||||
|
'address_index': 7,
|
||||||
|
'balance': 35000000000000,
|
||||||
|
'label': '(Untitled address)',
|
||||||
|
'num_unspent_outputs': 5,
|
||||||
|
'unlocked_balance': 35000000000000}],
|
||||||
|
'unlocked_balance': 224916129245183}}
|
||||||
|
locked = self.wallet.get_balance()
|
||||||
|
unlocked = self.wallet.get_balance(unlocked=True)
|
||||||
|
balances = self.wallet.get_balances()
|
||||||
|
self.assertEqual(balances[0], locked)
|
||||||
|
self.assertEqual(balances[1], unlocked)
|
||||||
|
self.assertEqual(locked, Decimal('224.916129245183'))
|
||||||
|
|
||||||
|
@patch('monero.backends.jsonrpc.requests.post')
|
||||||
|
def test_get_address(self, mock_post):
|
||||||
|
mock_post.return_value.status_code = 200
|
||||||
|
mock_post.return_value.json.return_value = {'id': 0,
|
||||||
|
'jsonrpc': '2.0',
|
||||||
|
'result': {'subaddress_accounts': [{'account_index': 0,
|
||||||
|
'balance': 224916129245183,
|
||||||
|
'base_address': '9vgV48wWAPTWik5QSUSoGYicdvvsbSNHrT9Arsx1XBTz6VrWPSgfmnUKSPZDMyX4Ms8R9TkhB4uFqK9s5LUBbV6YQN2Q9ag',
|
||||||
|
'label': 'Primary account',
|
||||||
|
'unlocked_balance': 224916129245183},
|
||||||
|
{'account_index': 1,
|
||||||
|
'balance': 3981420960933,
|
||||||
|
'base_address': 'BaCBwYSK9BGSuKxb2msXEj4mmpvZYJexYHfqx7kNPDrXDePVXSfoofxGquhXxpA4uxawcnVnouusMDgP74CACa7e9siimpj',
|
||||||
|
'label': 'Untitled account',
|
||||||
|
'unlocked_balance': 3981420960933},
|
||||||
|
{'account_index': 2,
|
||||||
|
'balance': 7256159239955,
|
||||||
|
'base_address': 'BgCseuY3jFJAZS7kt9mrNg7fEG3bo5BV91CTyKbYu9GFiU6hUZhvdNWCTUdQNPNcA4PyFApsFr3EsQDEDfT3tQSY1mVZeP2',
|
||||||
|
'label': 'Untitled account',
|
||||||
|
'unlocked_balance': 7256159239955}],
|
||||||
|
'total_balance': 236153709446071,
|
||||||
|
'total_unlocked_balance': 236153709446071}}
|
||||||
|
self.wallet = Wallet(JSONRPC())
|
||||||
|
mock_post.return_value.json.return_value = {'id': 0,
|
||||||
|
'jsonrpc': '2.0',
|
||||||
|
'result': {'address': '9vgV48wWAPTWik5QSUSoGYicdvvsbSNHrT9Arsx1XBTz6VrWPSgfmnUKSPZDMyX4Ms8R9TkhB4uFqK9s5LUBbV6YQN2Q9ag',
|
||||||
|
'addresses': [{'address': '9vgV48wWAPTWik5QSUSoGYicdvvsbSNHrT9Arsx1XBTz6VrWPSgfmnUKSPZDMyX4Ms8R9TkhB4uFqK9s5LUBbV6YQN2Q9ag',
|
||||||
|
'address_index': 0,
|
||||||
|
'label': 'Primary account',
|
||||||
|
'used': True},
|
||||||
|
{'address': 'BbBjyYoYNNwFfL8RRVRTMiZUofBLpjRxdNnd5E4LyGcAK5CEsnL3gmE5QkrDRta7RPficGHcFdR6rUwWcjnwZVvCE3tLxhJ',
|
||||||
|
'address_index': 1,
|
||||||
|
'label': '',
|
||||||
|
'used': True},
|
||||||
|
{'address': 'BgzZVoJP6Vx5WP87r7NRCCRcFwiUha8uTgnjGGitHYTJEmRuz6Jq2oE9icDCGYMHXZcnR8T35Z8NoVXkfMnF9ikJNfcwwsy',
|
||||||
|
'address_index': 2,
|
||||||
|
'label': '(Untitled address)',
|
||||||
|
'used': False},
|
||||||
|
{'address': 'Bck7sYz1vvUghNNTR6rrpxfRDegswezggB9mWQkXgjwxKRTo1feiJopStdJAHtMJoSEdsYppWvQ6vbGbArWxP32xCG2TsVZ',
|
||||||
|
'address_index': 3,
|
||||||
|
'label': '(Untitled address)',
|
||||||
|
'used': True},
|
||||||
|
{'address': 'BYCcWM1gZHdCnh3Cb1KfWrAU1SjBWMV3KhUoeRy7V2Lw2F2hHeuzouP2NECBaTUgnyYAzEe8s5vpA7qmWYfjVfxeHoHWPnb',
|
||||||
|
'address_index': 4,
|
||||||
|
'label': '(Untitled address)',
|
||||||
|
'used': False},
|
||||||
|
{'address': 'BfJ5W7dZGaYih6J63YvhiDSKpVUUZbVrEhLRCY6L6TdnEfzJmwP6aUJZQQnzLQ2NMTKMAC8hiJsoiNC7jbEUZ8tmBoJcnN1',
|
||||||
|
'address_index': 5,
|
||||||
|
'label': '(Untitled address)',
|
||||||
|
'used': True},
|
||||||
|
{'address': 'BaJwiPYwnN6DV8yBeh4FjjCqRoPfdkWppSzVXTPBJo35fDyU8caxLchATGJg7TKB24Q8nM8P1iWSt4DMwec8Pg7bSbFDAir',
|
||||||
|
'address_index': 6,
|
||||||
|
'label': '(Untitled address)',
|
||||||
|
'used': False},
|
||||||
|
{'address': 'BbkS4mn6gcgUidn2znLa2J6eSBkbGjGX4doeDCKAzT2A3t1cjbquQGjhYgiMHiKTrY8ojk6Zjqi1ufvfuPwyKv4hNnMruro',
|
||||||
|
'address_index': 7,
|
||||||
|
'label': '(Untitled address)',
|
||||||
|
'used': True}]}}
|
||||||
|
waddr = self.wallet.get_address()
|
||||||
|
a0addr = self.wallet.accounts[0].get_address()
|
||||||
|
self.assertEqual(waddr, a0addr)
|
||||||
|
self.assertEqual(
|
||||||
|
waddr,
|
||||||
|
'9vgV48wWAPTWik5QSUSoGYicdvvsbSNHrT9Arsx1XBTz6VrWPSgfmnUKSPZDMyX4Ms8R9TkhB4uFqK9s5LUBbV6YQN2Q9ag')
|
||||||
|
self.assertEqual(len(self.wallet.accounts[0].get_addresses()), 8)
|
Loading…
Reference in New Issue