From 1cb34ad2638ba78dbeab65cd37a8e62056fe8928 Mon Sep 17 00:00:00 2001 From: matograine <tom.ngr@zaclys.net> Date: Tue, 28 Jan 2020 19:04:44 +0100 Subject: [PATCH] [test] #111: create a unit test for get_list_input_for_transaction() * create a patched get_sources() function --- tests/patched.py | 71 +++++++++++++++++++++++++++++++++++++++++++ tests/test_unit_tx.py | 48 +++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+) diff --git a/tests/patched.py b/tests/patched.py index b7cfcfce..4fcc2c7d 100644 --- a/tests/patched.py +++ b/tests/patched.py @@ -1,6 +1,8 @@ # 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 @@ -46,3 +48,72 @@ async def head_block(self): "hash": "0000010D30B1284D34123E036B7BE0A449AE9F2B928A77D7D20E3BDEAC7EE14C", } return mocked_head_block + + +# 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 diff --git a/tests/test_unit_tx.py b/tests/test_unit_tx.py index 2a5b6db9..1a6f352f 100644 --- a/tests/test_unit_tx.py +++ b/tests/test_unit_tx.py @@ -5,6 +5,7 @@ from silkaj.tx import ( compute_amounts, transaction_amount, generate_transaction_document, + get_list_input_for_transaction, ) from silkaj.tui import display_pubkey, display_amount from silkaj.money import UDValue @@ -439,3 +440,50 @@ async def test_generate_transaction_document( Comment, OutputbackChange, ) + + +# 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 -- GitLab