From 805f3f445acbf0888de5cb83531b33281281ef07 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 * money.py * tools.py * wot.py * blockchain_tools.py * delete patched.py * change calls in the tests --- tests/patched/blockchain_tools.py | 9 ++++++ tests/{patched.py => patched/money.py} | 44 +++++--------------------- tests/patched/tools.py | 6 ++++ tests/patched/wot.py | 15 +++++++++ tests/test_membership.py | 4 +-- tests/test_tui.py | 7 ++-- tests/test_tx.py | 7 ++-- tests/test_unit_tx.py | 21 ++++++------ 8 files changed, 60 insertions(+), 53 deletions(-) create mode 100644 tests/patched/blockchain_tools.py rename tests/{patched.py => patched/money.py} (72%) create mode 100644 tests/patched/tools.py create mode 100644 tests/patched/wot.py diff --git a/tests/patched/blockchain_tools.py b/tests/patched/blockchain_tools.py new file mode 100644 index 00000000..150a4187 --- /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 7c0eff47..e26ce8ab 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 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_membership.py b/tests/test_membership.py index 62128235..6a9c8605 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 @@ -134,7 +134,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 dfabcc28..9161786a 100644 --- a/tests/test_tui.py +++ b/tests/test_tui.py @@ -19,14 +19,15 @@ import pytest from silkaj.tui import display_pubkey, display_amount, display_pubkey_and_checksum from silkaj.constants import G1_SYMBOL, SHORT_PUBKEY_SIZE -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): - ud_value = patched.mock_ud_value + ud_value = mock_ud_value amount_UD = round(amount / ud_value, 2) expected = [ [ @@ -55,7 +56,7 @@ def test_display_amount(message, amount, currency_symbol): ) @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:checksum)", display_pubkey_and_checksum(pubkey)]] if id: diff --git a/tests/test_tx.py b/tests/test_tx.py index 4c17cff5..93999519 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 ba0e09f8..36b3214a 100644 --- a/tests/test_unit_tx.py +++ b/tests/test_unit_tx.py @@ -40,7 +40,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() @@ -116,14 +119,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 @@ -275,8 +278,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: @@ -412,7 +415,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( @@ -459,7 +462,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