diff --git a/tests/test_membership.py b/tests/test_membership.py index 4cb19fbf1d3ba45417a72927e5e20ab4b90ae698..b8150f44aef5cae47d7c496075e9ce5ae4be1a13 100644 --- a/tests/test_membership.py +++ b/tests/test_membership.py @@ -42,13 +42,6 @@ from silkaj.constants import DATE, FAILURE_EXIT_STATUS, SUCCESS_EXIT_STATUS from silkaj.network_tools import ClientInstance 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" identity_timestamp = block_uid( @@ -62,7 +55,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, @@ -85,7 +78,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", @@ -111,7 +104,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, @@ -134,16 +127,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"] @@ -170,13 +160,13 @@ 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) date_idty_pub = pendulum.from_timestamp(block["time"], tz="local").format(DATE) table.append( ["Identity published", date_idty_pub], ) - params = await BlockchainParams().params + params = BlockchainParams().params membership_validity = ( pendulum.now().add(seconds=params["msValidity"]).diff_for_humans() ) @@ -191,9 +181,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 e08bb86f3c32fb4429fada220c626b37d643ec34..4323033bbb9d4bfb78b947934a97d2e39e40188d 100644 --- a/tests/test_money.py +++ b/tests/test_money.py @@ -25,8 +25,7 @@ from silkaj.money import get_sources from silkaj.wot import display_pubkey_and_checksum -@pytest.mark.asyncio -async def test_get_sources(monkeypatch): +def test_get_sources(monkeypatch): """ test that get_source() will : * only use simple SIG txs @@ -34,7 +33,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", @@ -60,7 +59,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", @@ -100,9 +99,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 3f6ec040f49844277ed8a585dfd6ae70807bb6d5..3a08651ff17ecc9755e83622e9ef0301bd16d055 100644 --- a/tests/test_tui.py +++ b/tests/test_tui.py @@ -54,14 +54,14 @@ def test_display_amount(message, amount, currency_symbol): ], ) @pytest.mark.asyncio -async def test_display_pubkey(message, pubkey, id, monkeypatch): +def test_display_pubkey(message, pubkey, id, monkeypatch): monkeypatch.setattr(wot_tools, "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 6cc7b1baa2cb8888ff65e5d06d888dfa7e896a75..939f3c559b0b2b458c4f19250aadb7fdde5aa60c 100644 --- a/tests/test_tx.py +++ b/tests/test_tx.py @@ -14,6 +14,7 @@ # along with Silkaj. If not, see <https://www.gnu.org/licenses/>. import sys +from unittest.mock import Mock import pytest from click import pass_context @@ -33,12 +34,6 @@ from silkaj.constants import ( PUBKEY_MIN_LENGTH, ) -# 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 @@ -51,8 +46,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""" @@ -86,7 +80,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() @@ -113,8 +107,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 @@ -134,7 +127,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 @@ -235,7 +228,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 b92810e2998d7f30a54a04e27823843da4772113..4a19d2ccb96391000c25bf5c6d711a00f9350cb4 100644 --- a/tests/test_tx_history.py +++ b/tests/test_tx_history.py @@ -40,8 +40,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 a60d1f8ff1085c60dd184e26de3afcd7cb4491ad..98d3d94cacd2245e6710009afb12f52dc39b6671 100644 --- a/tests/test_unit_tx.py +++ b/tests/test_unit_tx.py @@ -13,7 +13,7 @@ # You should have received a copy of the GNU Affero General Public License # along with Silkaj. If not, see <https://www.gnu.org/licenses/>. -import sys +from unittest.mock import Mock import pytest from click import pass_context @@ -50,12 +50,6 @@ from silkaj.cli import cli from silkaj.constants import CENT_MULT_TO_UNIT, G1_SYMBOL, MINIMAL_ABSOLUTE_TX_AMOUNT from silkaj.tui import display_amount, display_pubkey -# 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 key_fifi = patched_auth_method("fifi") @@ -118,8 +112,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, @@ -158,18 +151,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, @@ -309,8 +302,7 @@ result1 = Transaction( ) ], ) -@pytest.mark.asyncio -async def test_generate_transaction_document( +def test_generate_transaction_document( issuers, tx_amounts, listinput_and_amount, @@ -323,7 +315,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, @@ -438,8 +430,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 ): """ @@ -454,16 +445,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 @@ -842,8 +829,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, @@ -854,7 +840,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 @@ -863,12 +849,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, [ @@ -879,7 +865,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, @@ -1040,8 +1026,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) @@ -1081,7 +1067,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, @@ -1090,7 +1076,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, @@ -1099,7 +1085,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() @@ -1233,8 +1219,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, @@ -1252,10 +1237,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: @@ -1263,15 +1248,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, @@ -1296,7 +1281,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, @@ -1312,10 +1297,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 2bff53a7e7f6c3c6add76eb72dc6857405c21cf4..223e49c08f678f4658b12a303e7eaeba2838228e 100644 --- a/tests/test_verify_blocks.py +++ b/tests/test_verify_blocks.py @@ -38,20 +38,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() @@ -104,12 +103,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 8cecfa42eda00cf52761aa5e7fb62b2868afbc68..a67d1918d82fbffbff9bfc33d26d2206fcb9de3e 100644 --- a/tests/test_wot.py +++ b/tests/test_wot.py @@ -49,7 +49,7 @@ tutu = identity_card( ) -async def patched_lookup_one(pubkey_uid): +def patched_lookup_one(pubkey_uid): return [ { "pubkey": pubkey_titi_tata, @@ -59,7 +59,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, @@ -69,7 +69,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, @@ -84,7 +84,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, @@ -99,7 +99,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, @@ -141,13 +141,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_tools, "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"] @@ -156,7 +155,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: