diff --git a/tests/test_membership.py b/tests/test_membership.py index 7ae7f5fb64930299c0d25e54034c93e96d3b79c5..d4b0985bb6ca1f1fa88b2c89bb6a3604603c8c1f 100644 --- a/tests/test_membership.py +++ b/tests/test_membership.py @@ -50,12 +50,6 @@ from silkaj.constants import ( ) from silkaj.tui import display_pubkey_and_checksum -# AsyncMock available from Python 3.8. asynctest is used for Py < 3.8 -if sys.version_info[1] > 7: - from unittest.mock import AsyncMock -else: - from asynctest.mock import CoroutineMock as AsyncMock - # Values and patches pubkey = "EA7Dsw39ShZg4SpURsrgMaMqrweJPUFPYHwZA8e92e3D" @@ -70,7 +64,7 @@ def patched_auth_method(): return SigningKey.from_credentials(identity_uid, identity_uid) -async def patched_choose_identity(pubkey): +def patched_choose_identity(pubkey): return ( {"uid": identity_uid, "meta": {"timestamp": identity_timestamp}}, pubkey, @@ -93,7 +87,7 @@ def test_membership_cmd(dry_run, display, confirmation, exit_code, monkeypatch): monkeypatch.setattr(HeadBlock, "get_head", patched_head_block) monkeypatch.setattr(wot, "choose_identity", patched_choose_identity) - patched_display_confirmation_table = AsyncMock() + patched_display_confirmation_table = Mock() monkeypatch.setattr( membership, "display_confirmation_table", @@ -119,7 +113,7 @@ def test_membership_cmd(dry_run, display, confirmation, exit_code, monkeypatch): result = CliRunner().invoke(cli, args=command, input=confirmations) # Assert functions are called - patched_display_confirmation_table.assert_awaited_once_with( + patched_display_confirmation_table.assert_called_once_with( identity_uid, pubkey, identity_timestamp, @@ -142,16 +136,13 @@ def test_membership_cmd(dry_run, display, confirmation, exit_code, monkeypatch): "patched_wot_requirements", [patched_wot_requirements_no_pending, patched_wot_requirements_one_pending], ) -@pytest.mark.asyncio -async def test_display_confirmation_table( - patched_wot_requirements, monkeypatch, capsys -): +def test_display_confirmation_table(patched_wot_requirements, monkeypatch, capsys): monkeypatch.setattr(bma.wot, "requirements", patched_wot_requirements) monkeypatch.setattr(bma.blockchain, "parameters", patched_params) monkeypatch.setattr(bma.blockchain, "block", patched_block) client = ClientInstance().client - identities_requirements = await client(bma.wot.requirements, pubkey) + identities_requirements = client(bma.wot.requirements, pubkey) for identity_requirements in identities_requirements["identities"]: if identity_requirements["uid"] == identity_uid: membership_expires = identity_requirements["membershipExpiresIn"] @@ -178,12 +169,12 @@ async def test_display_confirmation_table( table.append(["Block Identity", str(identity_timestamp)[:45] + "…"]) - block = await client(bma.blockchain.block, identity_timestamp.number) + block = client(bma.blockchain.block, identity_timestamp.number) table.append( ["Identity published", pendulum.from_timestamp(block["time"]).format("LL")], ) - params = await BlockchainParams().params + params = BlockchainParams().params membership_validity = ( pendulum.now().add(seconds=params["msValidity"]).diff_for_humans() ) @@ -198,9 +189,7 @@ async def test_display_confirmation_table( expected = tabulate(table, tablefmt="fancy_grid") + "\n" - await membership.display_confirmation_table( - identity_uid, pubkey, identity_timestamp - ) + membership.display_confirmation_table(identity_uid, pubkey, identity_timestamp) captured = capsys.readouterr() assert expected == captured.out diff --git a/tests/test_money.py b/tests/test_money.py index eaaa746789681fce1612e09ea5c89d81783ac6ff..1c8590f31b55fa846ce9814500b83704c3da39bd 100644 --- a/tests/test_money.py +++ b/tests/test_money.py @@ -27,8 +27,7 @@ from silkaj.constants import FAILURE_EXIT_STATUS from silkaj.money import get_sources -@pytest.mark.asyncio -async def test_get_sources(monkeypatch): +def test_get_sources(monkeypatch): """ test that get_source() will : * only use simple SIG txs @@ -36,7 +35,7 @@ async def test_get_sources(monkeypatch): * return pending txs in first positions of the sources list """ - async def patched_tx_sources(self, pubkey): + def patched_tx_sources(self, pubkey): return { "currency": "g1-test", "pubkey": "AhRMHUxMPXSeG7qXZrE6qCdjwK9p2bu5Eqei7xAWVEDK", @@ -62,7 +61,7 @@ async def test_get_sources(monkeypatch): ], } - async def patched_tx_pending(self, pubkey): + def patched_tx_pending(self, pubkey): return { "currency": "g1-test", "pubkey": "AhRMHUxMPXSeG7qXZrE6qCdjwK9p2bu5Eqei7xAWVEDK", @@ -102,9 +101,7 @@ async def test_get_sources(monkeypatch): monkeypatch.setattr(bma_tx, "sources", patched_tx_sources) monkeypatch.setattr(bma_tx, "pending", patched_tx_pending) - listinput, balance = await get_sources( - "AhRMHUxMPXSeG7qXZrE6qCdjwK9p2bu5Eqei7xAWVEDK" - ) + listinput, balance = get_sources("AhRMHUxMPXSeG7qXZrE6qCdjwK9p2bu5Eqei7xAWVEDK") assert len(listinput) == 2 # test SIG() only source is used assert balance == 10000 # 10 in unitbase 3 diff --git a/tests/test_tui.py b/tests/test_tui.py index ca39bd96a7d9f2e15f7a67cf4acb2dd045c5bb6c..80870c6c62373e5a496f107b9f85f82225e64fb6 100644 --- a/tests/test_tui.py +++ b/tests/test_tui.py @@ -53,15 +53,14 @@ def test_display_amount(message, amount, currency_symbol): ("To", "DBM6F5ChMJzpmkUdL5zD9UXKExmZGfQ1AgPDQy4MxSBw", ""), ], ) -@pytest.mark.asyncio -async def test_display_pubkey(message, pubkey, id, monkeypatch): +def test_display_pubkey(message, pubkey, id, monkeypatch): monkeypatch.setattr("silkaj.wot.is_member", patched_is_member) expected = [[message + " (pubkey:checksum)", display_pubkey_and_checksum(pubkey)]] if id: expected.append([message + " (id)", id]) tx = list() - await display_pubkey(tx, message, pubkey) + display_pubkey(tx, message, pubkey) assert tx == expected diff --git a/tests/test_tx.py b/tests/test_tx.py index c6b1909b0c4dd4d1520cc39d89b52aa899de1b0a..278d0ccdc8fc65c16514b8c3f4b38f3364c5a4b8 100644 --- a/tests/test_tx.py +++ b/tests/test_tx.py @@ -17,6 +17,7 @@ along with Silkaj. If not, see <https://www.gnu.org/licenses/>. import sys import pytest +from unittest.mock import Mock from click.testing import CliRunner from click import pass_context @@ -35,13 +36,6 @@ from patched.test_constants import mock_ud_value from patched.auth import patched_auth_method from patched.tx import patched_gen_confirmation_table -# AsyncMock available from Python 3.8. asynctest is used for Py < 3.8 -if sys.version_info[1] > 7: - from unittest.mock import AsyncMock -else: - from asynctest.mock import CoroutineMock as AsyncMock - - # create test auths @pass_context def patched_auth_method_truc(ctx): @@ -53,8 +47,7 @@ def patched_auth_method_riri(ctx): return patched_auth_method("riri") -@pytest.mark.asyncio -async def test_transaction_amount(monkeypatch): +def test_transaction_amount(monkeypatch): """test passed amounts passed tx command float ≠ 100 does not give the exact value""" @@ -88,7 +81,7 @@ async def test_transaction_amount(monkeypatch): ) for trial in trials: - assert trial[3] == await tx.transaction_amount(trial[0], trial[1], trial[2]) + assert trial[3] == tx.transaction_amount(trial[0], trial[1], trial[2]) # transaction_amount errors() @@ -115,8 +108,7 @@ async def test_transaction_amount(monkeypatch): ), ], ) -@pytest.mark.asyncio -async def test_transaction_amount_errors( +def test_transaction_amount_errors( amounts, UDs_amounts, outputAddresses, expected, capsys, monkeypatch ): # patched functions @@ -136,7 +128,7 @@ async def test_transaction_amount_errors( # check program exit on error with pytest.raises(SystemExit) as pytest_exit: # read output to check error. - await tx.transaction_amount(amounts, UDs_amounts, outputAddresses) + tx.transaction_amount(amounts, UDs_amounts, outputAddresses) assert expected == capsys.readouterr() assert pytest_exit.type == SystemExit @@ -237,7 +229,7 @@ def test_tx_passed_all_sources_empty( # patch functions monkeypatch.setattr(auth, "auth_method", auth_method) monkeypatch.setattr(money, "get_sources", patched_get_sources) - patched_gen_confirmation_table = AsyncMock() + patched_gen_confirmation_table = Mock() monkeypatch.setattr(tx, "gen_confirmation_table", patched_gen_confirmation_table) result = CliRunner().invoke(cli, args=arguments) diff --git a/tests/test_tx_history.py b/tests/test_tx_history.py index 362ff6a5fd5fe6353aed356bcf52d27ff5d5bd58..eecab68177241ca99deaef1efb41df61af8989e3 100644 --- a/tests/test_tx_history.py +++ b/tests/test_tx_history.py @@ -42,8 +42,7 @@ MAX_FULL_PUBKEY_LENGTH_WITH_CHECKSUM = ( ) # char `:` ==> 44 + 3 + 1 = 48 -@pytest.mark.asyncio -async def test_tx_history_generate_table_and_pubkey_uid_display(monkeypatch): +def test_tx_history_generate_table_and_pubkey_uid_display(monkeypatch): def min_pubkey_length_with_uid(pubkey): # uid is at least one char : "<uid> - <pubkey>" adds min 4 chars to <pubkey> return pubkey + 4 @@ -56,10 +55,10 @@ async def test_tx_history_generate_table_and_pubkey_uid_display(monkeypatch): pubkey = "d88fPFbDdJXJANHH7hedFMaRyGcnVZj9c5cDaE76LRN" received_txs, sent_txs = list(), list() - await patched_get_transactions_history(client, pubkey, received_txs, sent_txs) + patched_get_transactions_history(client, pubkey, received_txs, sent_txs) # simple table - txs_list = await tx_history.generate_table( + txs_list = tx_history.generate_table( received_txs, sent_txs, pubkey, @@ -75,7 +74,7 @@ async def test_tx_history_generate_table_and_pubkey_uid_display(monkeypatch): assert len(tx_list[1]) == SHORT_PUBKEY_LENGTH_WITH_CHECKSUM # with uids - txs_list_uids = await tx_history.generate_table( + txs_list_uids = tx_history.generate_table( received_txs, sent_txs, pubkey, @@ -99,7 +98,7 @@ async def test_tx_history_generate_table_and_pubkey_uid_display(monkeypatch): assert len(txs_list_uids[4][1]) == SHORT_PUBKEY_LENGTH_WITH_CHECKSUM # with full pubkeys - txs_list_full = await tx_history.generate_table( + txs_list_full = tx_history.generate_table( received_txs, sent_txs, pubkey, @@ -120,7 +119,7 @@ async def test_tx_history_generate_table_and_pubkey_uid_display(monkeypatch): ) # with full pubkeys and uids - txs_list_uids_full = await tx_history.generate_table( + txs_list_uids_full = tx_history.generate_table( received_txs, sent_txs, pubkey, diff --git a/tests/test_unit_tx.py b/tests/test_unit_tx.py index 0eee16e5ae95938fe1df9f82503c7d8099de21f7..8aa46f9301615ab05ddb80a7266148e56ce77eb2 100644 --- a/tests/test_unit_tx.py +++ b/tests/test_unit_tx.py @@ -17,6 +17,7 @@ along with Silkaj. If not, see <https://www.gnu.org/licenses/>. import sys import pytest +from unittest.mock import Mock from silkaj import tx, wot, money, tools, blockchain_tools, auth, network_tools from silkaj.tui import display_pubkey, display_amount @@ -49,11 +50,6 @@ from patched.tx import ( patched_handle_intermediaries_transactions, ) -# AsyncMock available from Python 3.8. asynctest is used for Py < 3.8 -if sys.version_info[1] > 7: - from unittest.mock import AsyncMock -else: - from asynctest.mock import CoroutineMock as AsyncMock # Values # fifi: HcRgKh4LwbQVYuAc3xAdCynYXpKoiPE6qdxCMa8JeHat : 53 TX, amount = 5300 @@ -117,8 +113,7 @@ def test_truncBase(amount, base, expected): ], ], ) -@pytest.mark.asyncio -async def test_gen_confirmation_table( +def test_gen_confirmation_table( issuer_pubkey, pubkey_balance, tx_amounts, @@ -157,18 +152,18 @@ async def test_gen_confirmation_table( mock_ud_value, G1_SYMBOL, ) - await display_pubkey(expected, "From", issuer_pubkey) + display_pubkey(expected, "From", issuer_pubkey) # display recipients and amounts for outputAddress, tx_amount in zip(outputAddresses, tx_amounts): - await display_pubkey(expected, "To", outputAddress) + display_pubkey(expected, "To", outputAddress) display_amount(expected, "Amount", tx_amount, mock_ud_value, G1_SYMBOL) # display backchange and comment if outputBackChange: - await display_pubkey(expected, "Backchange", outputBackChange) + display_pubkey(expected, "Backchange", outputBackChange) expected.append(["Comment", comment]) # asserting - table_list = await tx.gen_confirmation_table( + table_list = tx.gen_confirmation_table( issuer_pubkey, pubkey_balance, tx_amounts, @@ -308,8 +303,7 @@ result1 = Transaction( ) ], ) -@pytest.mark.asyncio -async def test_generate_transaction_document( +def test_generate_transaction_document( issuers, tx_amounts, listinput_and_amount, @@ -322,7 +316,7 @@ async def test_generate_transaction_document( # patch Head_block monkeypatch.setattr(blockchain_tools.HeadBlock, "get_head", patched_head_block) - assert result == await tx.generate_transaction_document( + assert result == tx.generate_transaction_document( issuers, tx_amounts, listinput_and_amount, @@ -437,8 +431,7 @@ async def test_generate_transaction_document( ("BdanxHdwRRzCXZpiqvTVTX4gyyh6qFTYjeCWCkLwDifx", 9100, 91, (1, 9600, False)), ], ) -@pytest.mark.asyncio -async def test_get_list_input_for_transaction( +def test_get_list_input_for_transaction( pubkey, TXamount, outputs_number, expected, monkeypatch, capsys ): """ @@ -453,16 +446,12 @@ async def test_get_list_input_for_transaction( # testing error exit if isinstance(expected, str): with pytest.raises(SystemExit) as pytest_exit: - result = await tx.get_list_input_for_transaction( - pubkey, TXamount, outputs_number - ) + result = tx.get_list_input_for_transaction(pubkey, TXamount, outputs_number) assert expected == capsys.readouterr().out assert pytest_exit.type == SystemExit # testing good values else: - result = await tx.get_list_input_for_transaction( - pubkey, TXamount, outputs_number - ) + result = tx.get_list_input_for_transaction(pubkey, TXamount, outputs_number) assert (len(result[0]), result[1], result[2]) == expected @@ -841,8 +830,7 @@ async def test_get_list_input_for_transaction( ), ], ) -@pytest.mark.asyncio -async def test_handle_intermediaries_transactions( +def test_handle_intermediaries_transactions( key, issuers, tx_amounts, @@ -853,7 +841,7 @@ async def test_handle_intermediaries_transactions( monkeypatch, ): # patched functions - patched_generate_and_send_transaction = AsyncMock(return_value=None) + patched_generate_and_send_transaction = Mock(return_value=None) monkeypatch.setattr(money, "get_sources", patched_get_sources) monkeypatch.setattr( tx, "generate_and_send_transaction", patched_generate_and_send_transaction @@ -862,12 +850,12 @@ async def test_handle_intermediaries_transactions( patched_get_sources.counter = 0 # testing - await tx.handle_intermediaries_transactions( + tx.handle_intermediaries_transactions( key, issuers, tx_amounts, outputAddresses, Comment, OutputbackChange ) if expected_listinput_amount[2] == True: - patched_generate_and_send_transaction.assert_any_await( + patched_generate_and_send_transaction.assert_any_call( key, issuers, [ @@ -878,7 +866,7 @@ async def test_handle_intermediaries_transactions( "Change operation", ) else: - patched_generate_and_send_transaction.assert_awaited_once_with( + patched_generate_and_send_transaction.assert_called_once_with( key, issuers, tx_amounts, @@ -1039,8 +1027,8 @@ def test_send_transaction( return args_list # mocking functions - patched_gen_confirmation_table = AsyncMock(return_value=None) - patched_handle_intermediaries_transactions = AsyncMock(return_value=None) + patched_gen_confirmation_table = Mock(return_value=None) + patched_handle_intermediaries_transactions = Mock(return_value=None) # patching functions monkeypatch.setattr(auth, "auth_method", patched_auth_method_tx) @@ -1080,7 +1068,7 @@ def test_send_transaction( print(result.output) if confirmation_answer: - patched_gen_confirmation_table.assert_any_await( + patched_gen_confirmation_table.assert_called_once_with( key_fifi.pubkey, total_amount, tx_amounts, @@ -1089,7 +1077,7 @@ def test_send_transaction( comment, ) if yes or confirmation_answer == "yes": - patched_handle_intermediaries_transactions.assert_any_await( + patched_handle_intermediaries_transactions.assert_called_once_with( key_fifi, key_fifi.pubkey, tx_amounts, @@ -1098,7 +1086,7 @@ def test_send_transaction( outputbackchange, ) elif confirmation_answer == "no": - patched_handle_intermediaries_transactions.assert_not_awaited() + patched_handle_intermediaries_transactions.assert_not_called() # generate_and_send_transaction() @@ -1232,8 +1220,7 @@ def test_send_transaction( ), ], ) -@pytest.mark.asyncio -async def test_generate_and_send_transaction( +def test_generate_and_send_transaction( key, issuers, tx_amounts, @@ -1251,10 +1238,10 @@ async def test_generate_and_send_transaction( def __init__(self): self.status = client_return - async def __call__(self, *args, **kwargs): + def __call__(self, *args, **kwargs): return self - async def text(self): + def text(self): return "Tests for Silkaj : Fake Return !" class patched_ClientInstance: @@ -1262,15 +1249,15 @@ async def test_generate_and_send_transaction( self.client = FakeReturn() # mock functions - tx.generate_transaction_document = AsyncMock() + tx.generate_transaction_document = Mock() # patched functions monkeypatch.setattr(blockchain_tools.HeadBlock, "get_head", patched_head_block) monkeypatch.setattr(network_tools, "ClientInstance", patched_ClientInstance) # write the test function - async def function_testing(): - await tx.generate_and_send_transaction( + def function_testing(): + tx.generate_and_send_transaction( key, issuers, tx_amounts, @@ -1295,7 +1282,7 @@ async def test_generate_and_send_transaction( ) ) - tx.generate_transaction_document.assert_awaited_once_with( + tx.generate_transaction_document.assert_called_once_with( issuers, tx_amounts, listinput_and_amount, @@ -1311,10 +1298,10 @@ async def test_generate_and_send_transaction( ### test function and catch SystemExit if needed ### if client_return == 200: - await function_testing() + function_testing() else: with pytest.raises(SystemExit) as pytest_exit: - await function_testing() + function_testing() assert pytest_exit.type == SystemExit diff --git a/tests/test_verify_blocks.py b/tests/test_verify_blocks.py index 5a80474906836afe89aebdba610f8c0e5e32bdf6..9134e2fd67b0b9f68e2005ccab5a7ff8556eef13 100644 --- a/tests/test_verify_blocks.py +++ b/tests/test_verify_blocks.py @@ -42,20 +42,19 @@ G1_INVALID_BLOCK_SIG = 15144 HEAD_BLOCK = 48000 -async def current(self): +def current(self): return {"number": HEAD_BLOCK} @pytest.mark.parametrize( "from_block, to_block", [(2, 1), (20000, 15000), (0, HEAD_BLOCK + 1), (300000, 0)] ) -@pytest.mark.asyncio -async def test_check_passed_blocks_range(from_block, to_block, capsys, monkeypatch): +def test_check_passed_blocks_range(from_block, to_block, capsys, monkeypatch): monkeypatch.setattr(bma.blockchain, "current", current) client = Client(EndPoint().BMA_ENDPOINT) # https://medium.com/python-pandemonium/testing-sys-exit-with-pytest-10c6e5f7726f with pytest.raises(SystemExit) as pytest_wrapped_e: - await check_passed_blocks_range(client, from_block, to_block) + check_passed_blocks_range(client, from_block, to_block) assert pytest_wrapped_e.type == SystemExit assert pytest_wrapped_e.value.code == FAILURE_EXIT_STATUS captured = capsys.readouterr() @@ -108,12 +107,10 @@ def test_get_chunk_size(from_block, to_block, chunks_from, chunk_from): @pytest.mark.parametrize("chunk_size, chunk_from", [(2, 1), (5, 10)]) -@pytest.mark.asyncio -async def test_get_chunks(chunk_size, chunk_from): +def test_get_chunks(chunk_size, chunk_from): client = Client(EndPoint().BMA_ENDPOINT) - chunk = await get_chunk(client, chunk_size, chunk_from) + chunk = get_chunk(client, chunk_size, chunk_from) assert chunk[0]["number"] + chunk_size - 1 == chunk[-1]["number"] - await client.close() invalid_signature = "fJusVDRJA8akPse/sv4uK8ekUuvTGj1OoKYVdMQQAACs7OawDfpsV6cEMPcXxrQTCTRMrTN/rRrl20hN5zC9DQ==" diff --git a/tests/test_wot.py b/tests/test_wot.py index 08a1b265e73c9ff32454fec9f02cbce44b3ab5aa..b38d23bd4306ed2f884a03d9024dc9538e31ec04 100644 --- a/tests/test_wot.py +++ b/tests/test_wot.py @@ -52,7 +52,7 @@ tutu = identity_card( ) -async def patched_lookup_one(pubkey_uid): +def patched_lookup_one(pubkey_uid): return [ { "pubkey": pubkey_titi_tata, @@ -62,7 +62,7 @@ async def patched_lookup_one(pubkey_uid): ] -async def patched_lookup_two(pubkey_uid): +def patched_lookup_two(pubkey_uid): return [ { "pubkey": pubkey_titi_tata, @@ -72,7 +72,7 @@ async def patched_lookup_two(pubkey_uid): ] -async def patched_lookup_three(pubkey_uid): +def patched_lookup_three(pubkey_uid): return [ { "pubkey": pubkey_titi_tata, @@ -87,7 +87,7 @@ async def patched_lookup_three(pubkey_uid): ] -async def patched_lookup_four(pubkey_uid): +def patched_lookup_four(pubkey_uid): return [ { "pubkey": pubkey_titi_tata, @@ -102,7 +102,7 @@ async def patched_lookup_four(pubkey_uid): ] -async def patched_lookup_five(pubkey_uid): +def patched_lookup_five(pubkey_uid): return [ { "pubkey": pubkey_titi_tata, @@ -144,13 +144,12 @@ def patched_prompt_tutu(message): ("titi", pubkey_toto_tutu, patched_prompt_toto, patched_lookup_five), ], ) -@pytest.mark.asyncio -async def test_choose_identity( +def test_choose_identity( selected_uid, pubkey, patched_prompt, patched_lookup, capsys, monkeypatch ): monkeypatch.setattr(wot, "wot_lookup", patched_lookup) monkeypatch.setattr(click, "prompt", patched_prompt) - identity_card, get_pubkey, signed = await wot.choose_identity(pubkey) + identity_card, get_pubkey, signed = wot.choose_identity(pubkey) expected_pubkey = pubkey.split(":")[0] assert expected_pubkey == get_pubkey assert selected_uid == identity_card["uid"] @@ -159,7 +158,7 @@ async def test_choose_identity( # Check it is displayed for more than one identity # Check the uids and ids are in captured = capsys.readouterr() - lookups = await patched_lookup("") + lookups = patched_lookup("") # only one pubkey and one uid on this pubkey if len(lookups) == 1 and len(lookups[0]["uids"]) == 1: