integration test fix

This commit is contained in:
Lex Berezhny 2020-03-20 18:24:24 -04:00
parent 93fc883b90
commit d6d83a5c76
4 changed files with 43 additions and 33 deletions

View file

@ -454,7 +454,7 @@ class Account:
def get_balance(self, confirmations=0, include_claims=False, read_only=False, **constraints): def get_balance(self, confirmations=0, include_claims=False, read_only=False, **constraints):
if not include_claims: if not include_claims:
constraints.update({'txo_type__in': (0, TXO_TYPES['purchase'])}) constraints.update({'txo_type__in': (TXO_TYPES['other'], TXO_TYPES['purchase'])})
if confirmations > 0: if confirmations > 0:
height = self.ledger.headers.height - (confirmations-1) height = self.ledger.headers.height - (confirmations-1)
constraints.update({'height__lte': height, 'height__gt': 0}) constraints.update({'height__lte': height, 'height__gt': 0})
@ -569,14 +569,13 @@ class Account:
total = await get_total_balance() total = await get_total_balance()
if reserved_subtotals: if reserved_subtotals:
claims_balance = await get_total_balance(txo_type__in=CLAIM_TYPES) claims_balance = await get_total_balance(txo_type__in=CLAIM_TYPES)
for amount, spent, from_me, to_me, height in await self.get_support_summary(): for txo in await self.get_support_summary():
if confirmations > 0 and not 0 < height <= self.ledger.headers.height - (confirmations - 1): if confirmations > 0 and not 0 < txo.tx_ref.height <= self.ledger.headers.height - (confirmations - 1):
continue continue
if not spent and to_me: if txo.is_my_input:
if from_me: supports_balance += txo.amount
supports_balance += amount
else: else:
tips_balance += amount tips_balance += txo.amount
reserved = claims_balance + supports_balance + tips_balance reserved = claims_balance + supports_balance + tips_balance
else: else:
reserved = await self.get_balance( reserved = await self.get_balance(
@ -634,7 +633,7 @@ class Account:
return self.ledger.get_support_count(wallet=self.wallet, accounts=[self], **constraints) return self.ledger.get_support_count(wallet=self.wallet, accounts=[self], **constraints)
def get_support_summary(self): def get_support_summary(self):
return self.ledger.db.get_supports_summary(account_id=self.id) return self.ledger.db.get_supports_summary(wallet=self.wallet, accounts=[self])
async def release_all_outputs(self): async def release_all_outputs(self):
await self.ledger.db.release_all_outputs(self) await self.ledger.db.release_all_outputs(self)

View file

@ -839,7 +839,7 @@ class Database(SQLiteMixin):
return txos return txos
async def get_txo_count(self, unspent=False, **constraints): def _clean_txo_constraints_for_aggregation(self, unspent, constraints):
constraints.pop('include_is_my_input', None) constraints.pop('include_is_my_input', None)
constraints.pop('include_is_my_output', None) constraints.pop('include_is_my_output', None)
constraints.pop('wallet', None) constraints.pop('wallet', None)
@ -849,9 +849,17 @@ class Database(SQLiteMixin):
constraints.pop('order_by', None) constraints.pop('order_by', None)
if unspent: if unspent:
self.constrain_unspent(constraints) self.constrain_unspent(constraints)
async def get_txo_count(self, unspent=False, **constraints):
self._clean_txo_constraints_for_aggregation(unspent, constraints)
count = await self.select_txos('COUNT(*) as total', **constraints) count = await self.select_txos('COUNT(*) as total', **constraints)
return count[0]['total'] return count[0]['total']
async def get_txo_sum(self, unspent=False, **constraints):
self._clean_txo_constraints_for_aggregation(unspent, constraints)
result = await self.select_txos('SUM(amount) as total', **constraints)
return result[0]['total']
def get_utxos(self, read_only=False, **constraints): def get_utxos(self, read_only=False, **constraints):
return self.get_txos(unspent=True, read_only=read_only, **constraints) return self.get_txos(unspent=True, read_only=read_only, **constraints)
@ -1019,13 +1027,11 @@ class Database(SQLiteMixin):
" )", (account.public_key.address, ) " )", (account.public_key.address, )
) )
def get_supports_summary(self, account_id, read_only=False): def get_supports_summary(self, read_only=False, **constraints):
return self.db.execute_fetchall(f""" return self.get_txos(
select txo.amount, exists(select * from txi where txi.txoid=txo.txoid) as spent, txo_type=TXO_TYPES['support'],
(txo.txid in unspent=True, is_my_output=True,
(select txi.txid from txi join account_address a on txi.address = a.address include_is_my_input=True,
where a.account = ?)) as from_me, no_tx=True, read_only=read_only,
(txo.address in (select address from account_address where account=?)) as to_me, **constraints
tx.height )
from txo join tx using (txid) where txo_type={TXO_TYPES['support']}
""", (account_id, account_id), read_only=read_only)

View file

@ -122,13 +122,13 @@ class BasicTransactionTests(IntegrationTestCase):
await self.blockchain.generate(1) await self.blockchain.generate(1)
await self.ledger.wait(tx) # confirmed await self.ledger.wait(tx) # confirmed
tx = (await account1.get_transactions())[1] tx = (await account1.get_transactions(include_is_my_input=True, include_is_my_output=True))[1]
self.assertEqual(satoshis_to_coins(tx.inputs[0].amount), '1.1') self.assertEqual(satoshis_to_coins(tx.inputs[0].amount), '1.1')
self.assertEqual(satoshis_to_coins(tx.inputs[1].amount), '1.1') self.assertEqual(satoshis_to_coins(tx.inputs[1].amount), '1.1')
self.assertEqual(satoshis_to_coins(tx.outputs[0].amount), '2.0') self.assertEqual(satoshis_to_coins(tx.outputs[0].amount), '2.0')
self.assertEqual(tx.outputs[0].get_address(self.ledger), address2) self.assertEqual(tx.outputs[0].get_address(self.ledger), address2)
self.assertFalse(tx.outputs[0].is_change) self.assertTrue(tx.outputs[0].is_internal_transfer)
self.assertTrue(tx.outputs[1].is_change) self.assertTrue(tx.outputs[1].is_internal_transfer)
async def test_history_edge_cases(self): async def test_history_edge_cases(self):
await self.blockchain.generate(300) await self.blockchain.generate(300)

View file

@ -82,6 +82,11 @@ class WalletCommands(CommandTestCase):
async def test_granular_balances(self): async def test_granular_balances(self):
account2 = await self.daemon.jsonrpc_account_create("Tip-er") account2 = await self.daemon.jsonrpc_account_create("Tip-er")
wallet2 = await self.daemon.jsonrpc_wallet_create('foo', create_account=True)
account3 = wallet2.default_account
address3 = await self.daemon.jsonrpc_address_unused(account3.id, wallet2.id)
await self.confirm_tx(await self.blockchain.send_to_address(address3, 1))
await self.generate(1)
account_balance = self.daemon.jsonrpc_account_balance account_balance = self.daemon.jsonrpc_account_balance
wallet_balance = self.daemon.jsonrpc_wallet_balance wallet_balance = self.daemon.jsonrpc_wallet_balance
@ -128,7 +133,7 @@ class WalletCommands(CommandTestCase):
# tip received # tip received
support1 = await self.support_create( support1 = await self.support_create(
self.get_claim_id(stream1), '0.3', tip=True, funding_account_ids=[account2.id] self.get_claim_id(stream1), '0.3', tip=True, wallet_id=wallet2.id
) )
self.assertEqual(await account_balance(), { self.assertEqual(await account_balance(), {
'total': '9.27741', 'total': '9.27741',
@ -137,8 +142,8 @@ class WalletCommands(CommandTestCase):
'reserved_subtotals': {'claims': '1.0', 'supports': '2.0', 'tips': '0.3'} 'reserved_subtotals': {'claims': '1.0', 'supports': '2.0', 'tips': '0.3'}
}) })
self.assertEqual(await wallet_balance(), { self.assertEqual(await wallet_balance(), {
'total': '9.977268', 'total': '10.27741',
'available': '6.677268', 'available': '6.97741',
'reserved': '3.3', 'reserved': '3.3',
'reserved_subtotals': {'claims': '1.0', 'supports': '2.0', 'tips': '0.3'} 'reserved_subtotals': {'claims': '1.0', 'supports': '2.0', 'tips': '0.3'}
}) })
@ -153,8 +158,8 @@ class WalletCommands(CommandTestCase):
'reserved_subtotals': {'claims': '1.0', 'supports': '2.0', 'tips': '0.0'} 'reserved_subtotals': {'claims': '1.0', 'supports': '2.0', 'tips': '0.0'}
}) })
self.assertEqual(await wallet_balance(), { self.assertEqual(await wallet_balance(), {
'total': '9.977161', 'total': '10.277303',
'available': '6.977161', 'available': '7.277303',
'reserved': '3.0', 'reserved': '3.0',
'reserved_subtotals': {'claims': '1.0', 'supports': '2.0', 'tips': '0.0'} 'reserved_subtotals': {'claims': '1.0', 'supports': '2.0', 'tips': '0.0'}
}) })
@ -165,17 +170,17 @@ class WalletCommands(CommandTestCase):
# tip another claim # tip another claim
await self.support_create( await self.support_create(
self.get_claim_id(stream2), '0.2', tip=True, funding_account_ids=[self.account.id] self.get_claim_id(stream2), '0.2', tip=True, wallet_id=wallet2.id
) )
self.assertEqual(await account_balance(), { self.assertEqual(await account_balance(), {
'total': '9.077157', 'total': '9.277303',
'available': '6.077157', 'available': '6.277303',
'reserved': '3.0', 'reserved': '3.0',
'reserved_subtotals': {'claims': '1.0', 'supports': '2.0', 'tips': '0.0'} 'reserved_subtotals': {'claims': '1.0', 'supports': '2.0', 'tips': '0.0'}
}) })
self.assertEqual(await wallet_balance(), { self.assertEqual(await wallet_balance(), {
'total': '9.938908', 'total': '10.439196',
'available': '6.638908', 'available': '7.139196',
'reserved': '3.3', 'reserved': '3.3',
'reserved_subtotals': {'claims': '1.1', 'supports': '2.0', 'tips': '0.2'} 'reserved_subtotals': {'claims': '1.1', 'supports': '2.0', 'tips': '0.2'}
}) })