Skip to content
Snippets Groups Projects
Commit acc30245 authored by matograine's avatar matograine Committed by Moul
Browse files

[test] #111 : Send multiple amounts

* create a unit test for get_list_input_for_transaction()
* create a patched get_sources() function
parent c0b4e9e4
No related branches found
No related tags found
No related merge requests found
# This file contains patched functions for testing purposes.
from silkaj.constants import G1_SYMBOL
from silkaj.money import amount_in_current_base
from duniterpy.documents.transaction import InputSource
## Mocked values
......@@ -35,3 +37,72 @@ async def is_member(pubkey):
# mock CurrencySymbol().symbol
async def currency_symbol(self):
return G1_SYMBOL
# mock get_sources()
async def get_sources(pubkey):
"""
Returns transaction sources.
This function does not cover all possibilities : no other unlock conditions than SIG(pubkey).
if pubkey == DBM6F5ChMJzpmkUdL5zD9UXKExmZGfQ1AgPDQy4MxSBw : 3 TXsources, amount = 600
if pubkey == 4szFkvQ5tzzhwcfUtZD32hdoG2ZzhvG3ZtfR61yjnxdw : 53 TXsources, amount = 143100
if pubkey == BFb5yv8z1fowR6Z8mBXTALy5z7gHfMU976WtXhmRsUMh : 10 UDsources, amount = 3140
if pubkey == C1oAV9FX2y9iz2sdp7kZBFu3EBNAa6UkrrRG3EwouPeH : 50 UDsources and 20 TXsources, amount = 36700
else : 0 sources, amount = 0
For convenience, the hash is always the same. This should change for other testing purposes.
"""
def listinput_UD(listinput, amount, pubkey, max_ud, total):
while max_ud > 0 and total > 0:
listinput.append(
InputSource(
amount=mock_ud_value,
base=0,
source="D",
origin_id=pubkey,
index=max_ud,
)
)
amount += amount_in_current_base(listinput[-1])
max_ud -= 1
total -= 1
def listinput_TX(listinput, amount, max_tx, total):
orig_max = max_tx + 1
while max_tx > 0 and total > 0:
listinput.append(
InputSource(
amount=(orig_max - max_tx) * 100,
base=0,
source="T",
origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7",
index=max_tx,
)
)
amount += amount_in_current_base(listinput[-1])
max_tx -= 1
total -= 1
listinput, n = list(), 0
amount = 0
if pubkey == "DBM6F5ChMJzpmkUdL5zD9UXKExmZGfQ1AgPDQy4MxSBw":
max_tx = 3
max_ud = 0
elif pubkey == "4szFkvQ5tzzhwcfUtZD32hdoG2ZzhvG3ZtfR61yjnxdw":
max_tx = 53
max_ud = 0
elif pubkey == "BFb5yv8z1fowR6Z8mBXTALy5z7gHfMU976WtXhmRsUMh":
max_tx = 0
max_ud = 10
elif pubkey == "C1oAV9FX2y9iz2sdp7kZBFu3EBNAa6UkrrRG3EwouPeH":
max_tx = 20
max_ud = 50
else:
max_tx = 0
max_ud = 0
total = max_tx + max_ud
listinput_TX(listinput, amount, max_tx, total)
listinput_UD(listinput, amount, pubkey, max_ud, total)
return listinput, amount
import pytest
from silkaj.tx import truncBase, transaction_confirmation, transaction_amount
from silkaj.tx import (
truncBase,
transaction_confirmation,
transaction_amount,
get_list_input_for_transaction,
)
from silkaj.tui import display_pubkey, display_amount
from silkaj.money import UDValue
from silkaj.constants import G1_SYMBOL
......@@ -266,3 +271,50 @@ async def test_transaction_amount(
# test good values
else:
assert expected == await transaction_amount(amount, amountUD, outputAddresses)
# get_list_input_for_transaction()
@pytest.mark.parametrize(
"pubkey, TXamount, expected",
[
("DBM6F5ChMJzpmkUdL5zD9UXKExmZGfQ1AgPDQy4MxSBw", 200, (2, 300, False)),
("DBM6F5ChMJzpmkUdL5zD9UXKExmZGfQ1AgPDQy4MxSBw", 600, (3, 600, False)),
(
"DBM6F5ChMJzpmkUdL5zD9UXKExmZGfQ1AgPDQy4MxSBw",
800,
"Error: you don't have enough money",
),
("4szFkvQ5tzzhwcfUtZD32hdoG2ZzhvG3ZtfR61yjnxdw", 143100, (40, 82000, True)),
("BFb5yv8z1fowR6Z8mBXTALy5z7gHfMU976WtXhmRsUMh", 200, (1, 314, False)),
("BFb5yv8z1fowR6Z8mBXTALy5z7gHfMU976WtXhmRsUMh", 3140, (10, 3140, False)),
(
"BFb5yv8z1fowR6Z8mBXTALy5z7gHfMU976WtXhmRsUMh",
5000,
"Error: you don't have enough money",
),
("C1oAV9FX2y9iz2sdp7kZBFu3EBNAa6UkrrRG3EwouPeH", 2900, (8, 3600, False)),
("C1oAV9FX2y9iz2sdp7kZBFu3EBNAa6UkrrRG3EwouPeH", 22500, (25, 22570, False)),
("C1oAV9FX2y9iz2sdp7kZBFu3EBNAa6UkrrRG3EwouPeH", 29000, (40, 27280, True)),
],
)
@pytest.mark.asyncio
async def test_get_list_input_for_transaction(
pubkey, TXamount, expected, monkeypatch, capsys
):
"""
expected is [len(listinput), amount, IntermediateTransaction] or "Error"
see patched.get_sources() to compute expected values.
"""
# patched functions
monkeypatch.setattr("silkaj.money.get_sources", patched.get_sources)
# testing error exit
if isinstance(expected, str):
with pytest.raises(SystemExit) as pytest_exit:
result = await get_list_input_for_transaction(pubkey, TXamount)
assert expected == capsys.readouterr()
assert pytest_exit.type == SystemExit
# testing good values
else:
result = await get_list_input_for_transaction(pubkey, TXamount)
assert (len(result[0]), result[1], result[2]) == expected
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment