From acbb94482e8f548a545effafb77ab11a7e673c4c Mon Sep 17 00:00:00 2001 From: matograine <tom.ngr@zaclys.net> Date: Tue, 17 Mar 2020 13:54:10 +0100 Subject: [PATCH] [test] #282: split patched.py * create patched directory * create files matching the silkaj modules * delete patched.py * change calls in the tests --- tests/{patched.py => patched/money.py} | 35 ++------------------------ tests/patched/network_tools.py | 9 +++++++ tests/patched/tools.py | 6 +++++ tests/patched/wot.py | 15 +++++++++++ tests/test_tui.py | 7 +++--- tests/test_unit_tx.py | 22 +++++++++------- 6 files changed, 49 insertions(+), 45 deletions(-) rename tests/{patched.py => patched/money.py} (79%) create mode 100644 tests/patched/network_tools.py create mode 100644 tests/patched/tools.py create mode 100644 tests/patched/wot.py diff --git a/tests/patched.py b/tests/patched/money.py similarity index 79% rename from tests/patched.py rename to tests/patched/money.py index 4fcc2c7d..03c8914c 100644 --- a/tests/patched.py +++ b/tests/patched/money.py @@ -1,6 +1,3 @@ -# 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 @@ -16,42 +13,14 @@ 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). diff --git a/tests/patched/network_tools.py b/tests/patched/network_tools.py new file mode 100644 index 00000000..150a4187 --- /dev/null +++ b/tests/patched/network_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/tools.py b/tests/patched/tools.py new file mode 100644 index 00000000..05d39638 --- /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 00000000..0605bc92 --- /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_tui.py b/tests/test_tui.py index 5aa227ac..0c760cef 100644 --- a/tests/test_tui.py +++ b/tests/test_tui.py @@ -2,7 +2,8 @@ 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() @@ -10,7 +11,7 @@ import patched "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, 4) expected = [ [ @@ -39,7 +40,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_unit_tx.py b/tests/test_unit_tx.py index cd00bfd8..ff42a68b 100644 --- a/tests/test_unit_tx.py +++ b/tests/test_unit_tx.py @@ -7,6 +7,7 @@ from silkaj.tx import ( generate_transaction_document, get_list_input_for_transaction, ) +from silkaj.tui import display_amount, display_pubkey from silkaj.money import UDValue from silkaj.constants import G1_SYMBOL, CENT_MULT_TO_UNIT, MINIMAL_TX_AMOUNT from duniterpy.documents.transaction import ( @@ -18,7 +19,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.network_tools import patched_head_block # truncBase() @@ -94,14 +98,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 @@ -254,8 +258,8 @@ async def test_transaction_amount( amounts, amountUDs, 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: @@ -388,7 +392,7 @@ async def test_generate_transaction_document( monkeypatch, ): # patch Head_block - monkeypatch.setattr("silkaj.network_tools.HeadBlock.get_head", patched.head_block) + monkeypatch.setattr("silkaj.network_tools.HeadBlock.get_head", patched_head_block) assert result == await generate_transaction_document( issuers, @@ -434,7 +438,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: -- GitLab