diff --git a/tests/patched/money.py b/tests/patched/money.py index a0da99fdfc52268dca84c7176e3362986a88cff0..77ef6ef93b9cf20d3dada17f9911e6b3e10159fd 100644 --- a/tests/patched/money.py +++ b/tests/patched/money.py @@ -17,13 +17,10 @@ along with Silkaj. If not, see <https://www.gnu.org/licenses/>. # This file contains patched functions for testing purposes. -from silkaj.constants import G1_SYMBOL +from silkaj.constants import G1_SYMBOL, SOURCES_PER_TX from silkaj.money import amount_in_current_base from duniterpy.documents.transaction import InputSource - - -# mock UDValue -mock_ud_value = 314 +from patched.test_constants import mock_ud_value async def patched_ud_value(self): @@ -35,16 +32,26 @@ async def patched_get_sources(pubkey): """ Returns transaction sources. This function doesn't cover all possibilities : only SIG() unlock condition. - for pubkey DBM6F5ChMJzpmkUdL5zD9UXKExmZGfQ1AgPDQy4MxSBw : 3 TX, amount = 600 - for pubkey 4szFkvQ5tzzhwcfUtZD32hdoG2ZzhvG3ZtfR61yjnxdw : 53 TX, amount = 143100 - for pubkey BFb5yv8z1fowR6Z8mBXTALy5z7gHfMU976WtXhmRsUMh : 10 UD, amount = 3140 - for pubkey C1oAV9FX2y9iz2sdp7kZBFu3EBNAa6UkrrRG3EwouPeH : 50 UD and 20 TX, amount = 36700 - else : 0 sources, amount = 0 + + Can be called many times (depending on pubkey). + If so, it will mock intermediary tx for the first 40 inputs. + Tests using this function should reset the counter at the begining of each test case. + See source_dict.py for inputs lists. + + all UTXO have the same amount : 100 + all UD have the same amount : 314 + + for pubkey CtM5RZHopnSRAAoWNgTWrUhDEmspcCAxn6fuCEWDWudp : 3 TX, balance = 300 + for pubkey HcRgKh4LwbQVYuAc3xAdCynYXpKoiPE6qdxCMa8JeHat : 53 TX, balance = 5300 + for pubkey 2sq4w8yYVDWNxVWZqGWWDriFf5z7dn7iLahDCvEEotuY : 10 UD, balance = 3140 + for pubkey 9cwBBgXcSVMT74xiKYygX6FM5yTdwd3NABj1CfHbbAmp : 50 UD and 20 TX, balance = 17700 + else : 0 sources, balance = 0 Same hash for each TX for convenience. This may change for other testing purposes. """ - def listinput_UD(listinput, amount, pubkey, max_ud, total): - while max_ud > 0 and total > 0: + def listinput_UD(listinput, balance, pubkey, max_ud): + a = 0 + while a < max_ud: listinput.append( InputSource( amount=mock_ud_value, @@ -54,46 +61,73 @@ async def patched_get_sources(pubkey): index=max_ud, ) ) - amount += amount_in_current_base(listinput[-1]) - max_ud -= 1 - total -= 1 + balance += amount_in_current_base(listinput[-1]) + a += 1 - def listinput_TX(listinput, amount, max_tx, total): - orig_max = max_tx + 1 - while max_tx > 0 and total > 0: + def listinput_TX(listinput, balance, max_tx): + a = 0 + while a < max_tx: listinput.append( InputSource( - amount=(orig_max - max_tx) * 100, + amount=100, base=0, source="T", origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7", index=max_tx, ) ) - amount += amount_in_current_base(listinput[-1]) - max_tx -= 1 - total -= 1 + balance += amount_in_current_base(listinput[-1]) + a += 1 listinput, n = list(), 0 - amount = 0 + balance = 0 if pubkey == "CtM5RZHopnSRAAoWNgTWrUhDEmspcCAxn6fuCEWDWudp": - max_tx = 3 max_ud = 0 + max_tx = 3 elif pubkey == "HcRgKh4LwbQVYuAc3xAdCynYXpKoiPE6qdxCMa8JeHat": - max_tx = 53 - max_ud = 0 + if patched_get_sources.counter == 0: + max_ud = 0 + max_tx = 53 + elif patched_get_sources.counter == 1: + listinput.append( + InputSource( + amount=100 * SOURCES_PER_TX, # 100 * 40 = 4000 + base=0, + source="T", + origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7", + index=93, + ) + ) + max_ud = 0 + max_tx = 6 elif pubkey == "2sq4w8yYVDWNxVWZqGWWDriFf5z7dn7iLahDCvEEotuY": - max_tx = 0 max_ud = 10 + max_tx = 0 elif pubkey == "9cwBBgXcSVMT74xiKYygX6FM5yTdwd3NABj1CfHbbAmp": - max_tx = 20 - max_ud = 50 + if patched_get_sources.counter == 0: + max_ud = 50 + max_tx = 20 + elif patched_get_sources.counter == 1: + listinput.append( + InputSource( + amount=mock_ud_value * SOURCES_PER_TX, # 40 UD = 40*314 = 12560 + base=0, + source="T", + origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7", + index=93, + ) + ) + max_ud = 4 + max_tx = 20 else: - max_tx = 0 max_ud = 0 + max_tx = 0 + + listinput_UD(listinput, balance, pubkey, max_ud) + listinput_TX(listinput, balance, max_tx) + + patched_get_sources.counter += 1 + return listinput, balance - total = max_tx + max_ud - listinput_TX(listinput, amount, max_tx, total) - listinput_UD(listinput, amount, pubkey, max_ud, total) - return listinput, amount +patched_get_sources.counter = 0 diff --git a/tests/patched/test_constants.py b/tests/patched/test_constants.py new file mode 100644 index 0000000000000000000000000000000000000000..8c3ef80a2cfe6027e7864d29e3a1cb32939a237f --- /dev/null +++ b/tests/patched/test_constants.py @@ -0,0 +1,3 @@ +# this file contains only constant values for testing (no function) to prevent circular dependencies + +mock_ud_value = 314 diff --git a/tests/test_tui.py b/tests/test_tui.py index 93a94cab1c072f581d1d335f591de71051f0abd3..76272240ff55b5c2f9e1d033a79272b86159d988 100644 --- a/tests/test_tui.py +++ b/tests/test_tui.py @@ -20,7 +20,7 @@ from silkaj.tui import display_pubkey, display_amount, display_pubkey_and_checks from silkaj.constants import G1_SYMBOL, SHORT_PUBKEY_SIZE from patched.wot import patched_is_member -from patched.money import mock_ud_value +from patched.test_constants import mock_ud_value # display_amount() @pytest.mark.parametrize( diff --git a/tests/test_tx.py b/tests/test_tx.py index 469baf066d7c91360a7e38bcb243711a1f247210..214c91debc58827ac0e26258b7300de9f4a9c5b8 100644 --- a/tests/test_tx.py +++ b/tests/test_tx.py @@ -23,7 +23,8 @@ from silkaj.money import UDValue from silkaj.cli import cli from silkaj.constants import MINIMAL_ABSOLUTE_TX_AMOUNT, FAILURE_EXIT_STATUS -from patched.money import mock_ud_value, patched_ud_value +from patched.money import patched_ud_value +from patched.test_constants import mock_ud_value @pytest.mark.asyncio diff --git a/tests/test_unit_tx.py b/tests/test_unit_tx.py index 953e9b9ae80ee70f4c8cb4db040b57dbd52c6d0f..026b0970129b5a523822d85d1540521444800c81 100644 --- a/tests/test_unit_tx.py +++ b/tests/test_unit_tx.py @@ -41,7 +41,8 @@ from duniterpy.documents.transaction import ( from duniterpy.documents.block_uid import BlockUID from patched.wot import patched_is_member -from patched.money import patched_get_sources, patched_ud_value, mock_ud_value +from patched.money import patched_get_sources, patched_ud_value +from patched.test_constants import mock_ud_value from patched.tools import patched_currency_symbol from patched.blockchain_tools import patched_head_block @@ -429,24 +430,45 @@ async def test_generate_transaction_document( @pytest.mark.parametrize( "pubkey, TXamount, expected", [ - ("CtM5RZHopnSRAAoWNgTWrUhDEmspcCAxn6fuCEWDWudp", 200, (2, 300, False)), - ("CtM5RZHopnSRAAoWNgTWrUhDEmspcCAxn6fuCEWDWudp", 600, (3, 600, False)), + # less than 1 source + ("CtM5RZHopnSRAAoWNgTWrUhDEmspcCAxn6fuCEWDWudp", 99, (1, 100, False)), + # exactly one source + ("CtM5RZHopnSRAAoWNgTWrUhDEmspcCAxn6fuCEWDWudp", 100, (1, 100, False)), + # more than 1 source and no interm tx + ("CtM5RZHopnSRAAoWNgTWrUhDEmspcCAxn6fuCEWDWudp", 150, (2, 200, False)), + # all sources + ("CtM5RZHopnSRAAoWNgTWrUhDEmspcCAxn6fuCEWDWudp", 300, (3, 300, False)), + # too high amount ( "CtM5RZHopnSRAAoWNgTWrUhDEmspcCAxn6fuCEWDWudp", - 800, + 301, "Error: you don't have enough money", ), - ("HcRgKh4LwbQVYuAc3xAdCynYXpKoiPE6qdxCMa8JeHat", 143100, (40, 82000, True)), + # need for an intermediary tx + ("HcRgKh4LwbQVYuAc3xAdCynYXpKoiPE6qdxCMa8JeHat", 4100, (40, 4000, True)), + # no need for an intermediary tx, but the function still does it + ("HcRgKh4LwbQVYuAc3xAdCynYXpKoiPE6qdxCMa8JeHat", 4000, (40, 4000, True)), + # less than 1 UD source ("2sq4w8yYVDWNxVWZqGWWDriFf5z7dn7iLahDCvEEotuY", 200, (1, 314, False)), + # exactly 1 UD source + ("2sq4w8yYVDWNxVWZqGWWDriFf5z7dn7iLahDCvEEotuY", 314, (1, 314, False)), + # all sources with UD sources ("2sq4w8yYVDWNxVWZqGWWDriFf5z7dn7iLahDCvEEotuY", 3140, (10, 3140, False)), + # too high amount ( "2sq4w8yYVDWNxVWZqGWWDriFf5z7dn7iLahDCvEEotuY", 5000, "Error: you don't have enough money", ), - ("9cwBBgXcSVMT74xiKYygX6FM5yTdwd3NABj1CfHbbAmp", 2900, (8, 3600, False)), - ("9cwBBgXcSVMT74xiKYygX6FM5yTdwd3NABj1CfHbbAmp", 22500, (25, 22570, False)), - ("9cwBBgXcSVMT74xiKYygX6FM5yTdwd3NABj1CfHbbAmp", 29000, (40, 27280, True)), + # mix UD and TX source + ("9cwBBgXcSVMT74xiKYygX6FM5yTdwd3NABj1CfHbbAmp", 2500, (8, 2512, False)), + ("9cwBBgXcSVMT74xiKYygX6FM5yTdwd3NABj1CfHbbAmp", 7800, (25, 7850, False)), + # need for interm tx + ("9cwBBgXcSVMT74xiKYygX6FM5yTdwd3NABj1CfHbbAmp", 12561, (40, 12560, True)), + # no need for interm tx but the function still does it + ("9cwBBgXcSVMT74xiKYygX6FM5yTdwd3NABj1CfHbbAmp", 12247, (40, 12560, True)), + # exactly 39 sources + ("9cwBBgXcSVMT74xiKYygX6FM5yTdwd3NABj1CfHbbAmp", 12246, (39, 12246, False)), ], ) @pytest.mark.asyncio @@ -455,11 +477,13 @@ async def test_get_list_input_for_transaction( ): """ expected is [len(listinput), amount, IntermediateTransaction] or "Error" - see patched.get_sources() to compute expected values. + see patched_get_sources() to compute expected values. """ # patched functions monkeypatch.setattr("silkaj.money.get_sources", patched_get_sources) + # reset patched_get_sources counter + patched_get_sources.counter = 0 # testing error exit if isinstance(expected, str): with pytest.raises(SystemExit) as pytest_exit: