diff --git a/tests/patched/blockchain_tools.py b/tests/patched/blockchain_tools.py new file mode 100644 index 0000000000000000000000000000000000000000..150a418766a5ebf8cf242ca76001672cd3cd44db --- /dev/null +++ b/tests/patched/blockchain_tools.py @@ -0,0 +1,9 @@ +## mock head_block() +async def patched_head_block(self): + mocked_head_block = { + "number": 48000, + "unitbase": 0, + "currency": "g1", + "hash": "0000010D30B1284D34123E036B7BE0A449AE9F2B928A77D7D20E3BDEAC7EE14C", + } + return mocked_head_block diff --git a/tests/patched.py b/tests/patched/money.py similarity index 72% rename from tests/patched.py rename to tests/patched/money.py index 7c0eff471dfe1643f6f6391177021620226da694..e26ce8ab125b303a7675e71134619c65877feb46 100644 --- a/tests/patched.py +++ b/tests/patched/money.py @@ -33,51 +33,23 @@ pubkey_list = [ {"pubkey": "7Hr6oUxE6nGZxFG7gVbpMK6oUkNTh5eU686EiCXWCrBF", "uid": "loulou"}, ] -#### Patched functions #### - -## testing tx.py ## # mock UDValue -async def ud_value(self): +async def patched_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 - - -## mock head_block() -async def head_block(self): - mocked_head_block = { - "number": 48000, - "unitbase": 0, - "currency": "g1", - "hash": "0000010D30B1284D34123E036B7BE0A449AE9F2B928A77D7D20E3BDEAC7EE14C", - } - return mocked_head_block - - # mock get_sources() -async def get_sources(pubkey): +async def patched_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 + 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 - For convenience, the hash is always the same. This should change for other testing purposes. + Same hash for each TX for convenience. This may change for other testing purposes. """ def listinput_UD(listinput, amount, pubkey, max_ud, total): diff --git a/tests/patched/tools.py b/tests/patched/tools.py new file mode 100644 index 0000000000000000000000000000000000000000..05d3963827a53351871926d45edfd02938bcf9e2 --- /dev/null +++ b/tests/patched/tools.py @@ -0,0 +1,6 @@ +from silkaj.constants import G1_SYMBOL + + +# mock CurrencySymbol().symbol +async def patched_currency_symbol(self): + return G1_SYMBOL diff --git a/tests/patched/wot.py b/tests/patched/wot.py new file mode 100644 index 0000000000000000000000000000000000000000..0605bc927903e7d7214827abe304abfecad7e94e --- /dev/null +++ b/tests/patched/wot.py @@ -0,0 +1,15 @@ +pubkey_list = [ + {"pubkey": "DBM6F5ChMJzpmkUdL5zD9UXKExmZGfQ1AgPDQy4MxSBw", "uid": ""}, + {"pubkey": "4szFkvQ5tzzhwcfUtZD32hdoG2ZzhvG3ZtfR61yjnxdw", "uid": ""}, + {"pubkey": "BFb5yv8z1fowR6Z8mBXTALy5z7gHfMU976WtXhmRsUMh", "uid": "riri"}, + {"pubkey": "C1oAV9FX2y9iz2sdp7kZBFu3EBNAa6UkrrRG3EwouPeH", "uid": "fifi"}, + {"pubkey": "7Hr6oUxE6nGZxFG7gVbpMK6oUkNTh5eU686EiCXWCrBF", "uid": "loulou"}, +] + +# mock is_member +async def patched_is_member(pubkey): + for account in pubkey_list: + if account["pubkey"] == pubkey: + if account["uid"]: + return account + return False diff --git a/tests/test_membership.py b/tests/test_membership.py index 24789b6660460ddc8b5782e2acc6385f9f0df65e..d90db6a22332e507d21c3bea1d88996b3adab1f0 100644 --- a/tests/test_membership.py +++ b/tests/test_membership.py @@ -27,7 +27,7 @@ from duniterpy.documents import Membership, block_uid from duniterpy.api import bma from duniterpy.key import SigningKey -import patched +from patched.blockchain_tools import patched_head_block from silkaj import auth, wot from silkaj.cli import cli from silkaj.network_tools import ClientInstance @@ -133,7 +133,7 @@ async def patched_wot_requirements_no_pending(pubkey, identity_uid): def test_membership_cmd(dry_run, confirmation, exit_code, monkeypatch): # Monkeypatch and Mock monkeypatch.setattr(auth, "auth_method", patched_auth_method) - monkeypatch.setattr(HeadBlock, "get_head", patched.head_block) + monkeypatch.setattr(HeadBlock, "get_head", patched_head_block) monkeypatch.setattr(wot, "choose_identity", patched_choose_identity) patched_display_confirmation_table = AsyncMock() diff --git a/tests/test_tui.py b/tests/test_tui.py index d665c2a358eae9aa8d7eaaea98f360c025db620c..a29fe77cf4bc379fad6ff14516120718fdd71ea7 100644 --- a/tests/test_tui.py +++ b/tests/test_tui.py @@ -2,14 +2,15 @@ import pytest from silkaj.tui import display_pubkey, display_amount from silkaj.constants import G1_SYMBOL -import patched +from patched.wot import patched_is_member +from patched.money import mock_ud_value # display_amount() @pytest.mark.parametrize( "message, amount, currency_symbol", [("Total", 1000, G1_SYMBOL)] ) def test_display_amount(message, amount, currency_symbol, monkeypatch): - ud_value = patched.mock_ud_value + ud_value = mock_ud_value amount_UD = round(amount / ud_value, 2) expected = [ [ @@ -38,7 +39,7 @@ def test_display_amount(message, amount, currency_symbol, monkeypatch): ) @pytest.mark.asyncio async def test_display_pubkey(message, pubkey, id, monkeypatch): - monkeypatch.setattr("silkaj.wot.is_member", patched.is_member) + monkeypatch.setattr("silkaj.wot.is_member", patched_is_member) expected = [[message + " (pubkey)", pubkey]] if id: diff --git a/tests/test_tx.py b/tests/test_tx.py index 4c17cff5ff3a6c093d6aefb146c2ab47ed1d3387..939995196155104e282a6fa3eac8907535104256 100644 --- a/tests/test_tx.py +++ b/tests/test_tx.py @@ -22,7 +22,8 @@ from silkaj.tx import transaction_amount from silkaj.money import UDValue from silkaj.cli import cli from silkaj.constants import MINIMAL_TX_AMOUNT, FAILURE_EXIT_STATUS -import patched + +from patched.money import mock_ud_value, patched_ud_value @pytest.mark.asyncio @@ -30,8 +31,8 @@ async def test_transaction_amount(monkeypatch): """test passed amounts passed tx command float ≠100 does not give the exact value""" - monkeypatch.setattr(UDValue, "get_ud_value", patched.ud_value) - udvalue = patched.mock_ud_value + monkeypatch.setattr(UDValue, "get_ud_value", patched_ud_value) + udvalue = mock_ud_value trials = ( # tests for --amount (unit) ([141.89], None, ["A"], [14189]), diff --git a/tests/test_unit_tx.py b/tests/test_unit_tx.py index b5d259cb200e98d63a6a9b982937645a3233b4e9..28162f5dc467a9f3123f44e8073701b9e534a1aa 100644 --- a/tests/test_unit_tx.py +++ b/tests/test_unit_tx.py @@ -36,7 +36,10 @@ from duniterpy.documents.transaction import ( ) from duniterpy.documents.block_uid import BlockUID -import patched +from patched.wot import patched_is_member +from patched.money import patched_get_sources, patched_ud_value, mock_ud_value +from patched.tools import patched_currency_symbol +from patched.blockchain_tools import patched_head_block # truncBase() @@ -112,14 +115,14 @@ async def test_transaction_confirmation( 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.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 + "silkaj.tools.CurrencySymbol.get_symbol", patched_currency_symbol ) # creating expected list - ud_value = await UDValue().ud_value + ud_value = mock_ud_value expected = list() total_tx_amount = sum(tx_amounts) # display account situation @@ -271,8 +274,8 @@ async def test_transaction_amount( amounts, UDs_amounts, outputAddresses, expected, capsys, monkeypatch ): # patched functions - monkeypatch.setattr("silkaj.money.UDValue.get_ud_value", patched.ud_value) - udvalue = patched.mock_ud_value + monkeypatch.setattr("silkaj.money.UDValue.get_ud_value", patched_ud_value) + udvalue = mock_ud_value def too_little_amount(amounts, multiplicator): for amount in amounts: @@ -408,7 +411,7 @@ async def test_generate_transaction_document( ): # patch Head_block monkeypatch.setattr( - "silkaj.blockchain_tools.HeadBlock.get_head", patched.head_block + "silkaj.blockchain_tools.HeadBlock.get_head", patched_head_block ) assert result == await generate_transaction_document( @@ -455,7 +458,7 @@ async def test_get_list_input_for_transaction( """ # patched functions - monkeypatch.setattr("silkaj.money.get_sources", patched.get_sources) + monkeypatch.setattr("silkaj.money.get_sources", patched_get_sources) # testing error exit if isinstance(expected, str): with pytest.raises(SystemExit) as pytest_exit: