diff --git a/silkaj/tx.py b/silkaj/tx.py index 336e87f4ca84dc30ac8f93a5c8b4044e5efa125c..426b98da5369cca7f02d60aaaa57a2398dca0aad 100644 --- a/silkaj/tx.py +++ b/silkaj/tx.py @@ -26,7 +26,7 @@ from silkaj.network_tools import ClientInstance, HeadBlock from silkaj.crypto_tools import check_public_key from silkaj.tools import message_exit, CurrencySymbol, coroutine from silkaj.auth import auth_method -from silkaj.wot import is_member +from silkaj import wot from silkaj.money import ( get_sources, get_amount_from_pubkey, @@ -170,7 +170,7 @@ async def display_pubkey(tx, message, pubkey): Displays a pubkey and the eventually associated id. """ tx.append([message + " (pubkey)", pubkey]) - id = await is_member(pubkey) + id = await wot.is_member(pubkey) if id: tx.append([message + " (id)", id["uid"]]) diff --git a/tests/patched.py b/tests/patched.py new file mode 100644 index 0000000000000000000000000000000000000000..edd52942a23a190bed8411ec29df4daf49c5de9f --- /dev/null +++ b/tests/patched.py @@ -0,0 +1,37 @@ +# This file contains patched functions for testing purposes. + +from silkaj.constants import G1_SYMBOL + +## Mocked values + +mock_ud_value = 3.14159 + +pubkey_list = [ + {"pubkey": "DBM6F5ChMJzpmkUdL5zD9UXKExmZGfQ1AgPDQy4MxSBw", "uid": ""}, + {"pubkey": "4szFkvQ5tzzhwcfUtZD32hdoG2ZzhvG3ZtfR61yjnxdw", "uid": ""}, + {"pubkey": "BFb5yv8z1fowR6Z8mBXTALy5z7gHfMU976WtXhmRsUMh", "uid": "riri"}, + {"pubkey": "C1oAV9FX2y9iz2sdp7kZBFu3EBNAa6UkrrRG3EwouPeH", "uid": "fifi"}, + {"pubkey": "7Hr6oUxE6nGZxFG7gVbpMK6oUkNTh5eU686EiCXWCrBF", "uid": "loulou"}, +] + +#### Patched functions #### + +## testing tx.py ## + +# mock UDValue +async def ud_value(self): + return mock_ud_value + + +# mock is_member +async def is_member(pubkey): + for account in pubkey_list: + if account["pubkey"] == pubkey: + if account["uid"]: + return account + return False + + +# mock CurrencySymbol().symbol +async def currency_symbol(self): + return G1_SYMBOL diff --git a/tests/test_unit_tx.py b/tests/test_unit_tx.py index b1300203181175e46b4940fca8a26b231d580c90..069958ce7574e021f2159820df47ae8df91bebb5 100644 --- a/tests/test_unit_tx.py +++ b/tests/test_unit_tx.py @@ -1,11 +1,167 @@ import pytest +from silkaj.tx import ( + truncBase, + display_pubkey, + display_amount, + transaction_confirmation, +) +from silkaj.money import UDValue +from silkaj.constants import G1_SYMBOL +import patched -from silkaj.tx import truncBase - - +# truncBase() @pytest.mark.parametrize( "amount,base,expected", [(0, 0, 0), (10, 2, 0), (100, 2, 100), (306, 2, 300), (3060, 3, 3000)], ) def test_truncBase(amount, base, expected): assert truncBase(amount, base) == expected + + +# display_amount() +@pytest.mark.parametrize( + "message, amount, currency_symbol", [("Total", 1000, G1_SYMBOL)] +) +@pytest.mark.asyncio +async def test_display_amount(message, amount, currency_symbol, monkeypatch): + monkeypatch.setattr("silkaj.money.UDValue.get_ud_value", patched.ud_value) + + amount_UD = round(amount / await UDValue().ud_value, 4) + expected = [ + [ + message + " (unit|relative)", + str(amount / 100) + + " " + + currency_symbol + + " | " + + str(amount_UD) + + " UD " + + currency_symbol, + ] + ] + tx = list() + await display_amount(tx, message, amount, currency_symbol) + assert tx == expected + + +# display_pubkey() +@pytest.mark.parametrize( + "message, pubkey, id", + [ + ("From", "BFb5yv8z1fowR6Z8mBXTALy5z7gHfMU976WtXhmRsUMh", "riri"), + ("To", "DBM6F5ChMJzpmkUdL5zD9UXKExmZGfQ1AgPDQy4MxSBw", ""), + ], +) +@pytest.mark.asyncio +async def test_display_pubkey(message, pubkey, id, monkeypatch): + monkeypatch.setattr("silkaj.wot.is_member", patched.is_member) + + expected = [[message + " (pubkey)", pubkey]] + if id: + expected.append([message + " (id)", id]) + tx = list() + await display_pubkey(tx, message, pubkey) + assert tx == expected + + +# transaction_confirmation() +@pytest.mark.parametrize( + "issuer_pubkey, pubkey_balance, tx_amount, outputAddresses, outputBackChange, comment, currency_symbol", + [ + # only one receiver + [ + "DBM6F5ChMJzpmkUdL5zD9UXKExmZGfQ1AgPDQy4MxSBw", + 3000, + 1000, + ["4szFkvQ5tzzhwcfUtZD32hdoG2ZzhvG3ZtfR61yjnxdw"], + "", + "", + G1_SYMBOL, + ], + # one member receiver + [ + "DBM6F5ChMJzpmkUdL5zD9UXKExmZGfQ1AgPDQy4MxSBw", + 3000, + 1000, + ["BFb5yv8z1fowR6Z8mBXTALy5z7gHfMU976WtXhmRsUMh"], + "", + "This is a comment", + G1_SYMBOL, + ], + # many receivers and backchange + [ + "DBM6F5ChMJzpmkUdL5zD9UXKExmZGfQ1AgPDQy4MxSBw", + 3000, + 1000, + [ + "BFb5yv8z1fowR6Z8mBXTALy5z7gHfMU976WtXhmRsUMh", + "4szFkvQ5tzzhwcfUtZD32hdoG2ZzhvG3ZtfR61yjnxdw", + ], + "C1oAV9FX2y9iz2sdp7kZBFu3EBNAa6UkrrRG3EwouPeH", + "This is a comment", + G1_SYMBOL, + ], + ], +) +@pytest.mark.asyncio +async def test_transaction_confirmation( + issuer_pubkey, + pubkey_balance, + tx_amount, + outputAddresses, + outputBackChange, + comment, + currency_symbol, + monkeypatch, +): + # patched functions + monkeypatch.setattr("silkaj.wot.is_member", patched.is_member) + monkeypatch.setattr("silkaj.money.UDValue.get_ud_value", patched.ud_value) + monkeypatch.setattr( + "silkaj.tools.CurrencySymbol.get_symbol", patched.currency_symbol + ) + + # creating expected list + expected = list() + expected.append( + [ + "pubkey’s balance before tx", + str(pubkey_balance / 100) + " " + currency_symbol, + ] + ) + + await display_amount( + expected, + "total amount", + float(tx_amount * len(outputAddresses)), + currency_symbol, + ) + + expected.append( + [ + "pubkey’s balance after tx", + str(((pubkey_balance - tx_amount * len(outputAddresses)) / 100)) + + " " + + currency_symbol, + ] + ) + + await display_pubkey(expected, "from", issuer_pubkey) + for outputAddress in outputAddresses: + await display_pubkey(expected, "to", outputAddress) + await display_amount(expected, "amount", tx_amount, currency_symbol) + if outputBackChange: + await display_pubkey(expected, "Backchange", outputBackChange) + + expected.append(["comment", comment]) + + # asserting + tx = await transaction_confirmation( + issuer_pubkey, + pubkey_balance, + tx_amount, + outputAddresses, + outputBackChange, + comment, + ) + assert tx == expected