From 005b705dcaeb43d0b6f987bb8600ea0c439c3a00 Mon Sep 17 00:00:00 2001 From: matograine <tom.ngr@zaclys.net> Date: Sat, 14 Nov 2020 12:42:44 +0100 Subject: [PATCH] [test] #335 : two functions are testing transaction_amount() * make one test only errors * move it from test_unit_tx.py to test_tx.py --- tests/test_tx.py | 56 ++++++++++++++++++++++- tests/test_unit_tx.py | 101 ------------------------------------------ 2 files changed, 55 insertions(+), 102 deletions(-) diff --git a/tests/test_tx.py b/tests/test_tx.py index 214c91de..9f871c92 100644 --- a/tests/test_tx.py +++ b/tests/test_tx.py @@ -21,7 +21,11 @@ from click.testing import CliRunner from silkaj.tx import transaction_amount from silkaj.money import UDValue from silkaj.cli import cli -from silkaj.constants import MINIMAL_ABSOLUTE_TX_AMOUNT, FAILURE_EXIT_STATUS +from silkaj.constants import ( + MINIMAL_ABSOLUTE_TX_AMOUNT, + FAILURE_EXIT_STATUS, + CENT_MULT_TO_UNIT, +) from patched.money import patched_ud_value from patched.test_constants import mock_ud_value @@ -65,6 +69,56 @@ async def test_transaction_amount(monkeypatch): assert trial[3] == await transaction_amount(trial[0], trial[1], trial[2]) +# transaction_amount errors() +@pytest.mark.parametrize( + "amounts, UDs_amounts, outputAddresses, expected", + [ + ( + None, + [0.00002], + ["DBM6F5ChMJzpmkUdL5zD9UXKExmZGfQ1AgPDQy4MxSBw"], + "Error: amount 0.00002 is too low.", + ), + ( + [10, 56], + None, + ["DBM6F5ChMJzpmkUdL5zD9UXKExmZGfQ1AgPDQy4MxSBw"], + "Error: The number of passed recipients is not the same as the passed amounts.", + ), + ( + None, + [1, 45], + ["DBM6F5ChMJzpmkUdL5zD9UXKExmZGfQ1AgPDQy4MxSBw"], + "Error: The number of passed recipients is not the same as the passed amounts.", + ), + ], +) +@pytest.mark.asyncio +async def test_transaction_amount_errors( + amounts, UDs_amounts, outputAddresses, expected, capsys, monkeypatch +): + # patched functions + monkeypatch.setattr("silkaj.money.UDValue.get_ud_value", patched_ud_value) + + def too_little_amount(amounts, multiplicator): + for amount in amounts: + if amount * multiplicator < MINIMAL_ABSOLUTE_TX_AMOUNT * CENT_MULT_TO_UNIT: + return True + return False + + # run tests + if amounts: + given_amounts = amounts + if UDs_amounts: + given_amounts = UDs_amounts + # check program exit on error + with pytest.raises(SystemExit) as pytest_exit: + # read output to check error. + await transaction_amount(amounts, UDs_amounts, outputAddresses) + assert expected == capsys.readouterr() + assert pytest_exit.type == SystemExit + + def test_tx_passed_amount_cli(): """One option""" result = CliRunner().invoke(cli, ["tx", "--amount", "1"]) diff --git a/tests/test_unit_tx.py b/tests/test_unit_tx.py index 3c294e96..995648de 100644 --- a/tests/test_unit_tx.py +++ b/tests/test_unit_tx.py @@ -200,107 +200,6 @@ def test_compute_amounts(): ) -# transaction_amount() -@pytest.mark.parametrize( - "amounts, UDs_amounts, outputAddresses, expected", - [ - ([10], None, ["DBM6F5ChMJzpmkUdL5zD9UXKExmZGfQ1AgPDQy4MxSBw"], [1000]), - ( - [10, 2.37], - None, - [ - "DBM6F5ChMJzpmkUdL5zD9UXKExmZGfQ1AgPDQy4MxSBw", - "4szFkvQ5tzzhwcfUtZD32hdoG2ZzhvG3ZtfR61yjnxdw", - ], - [1000, 237], - ), - ( - [10], - None, - [ - "DBM6F5ChMJzpmkUdL5zD9UXKExmZGfQ1AgPDQy4MxSBw", - "4szFkvQ5tzzhwcfUtZD32hdoG2ZzhvG3ZtfR61yjnxdw", - ], - [1000, 1000], - ), - (None, [1.263], ["DBM6F5ChMJzpmkUdL5zD9UXKExmZGfQ1AgPDQy4MxSBw"], [397]), - ( - None, - [0.5, 10], - [ - "DBM6F5ChMJzpmkUdL5zD9UXKExmZGfQ1AgPDQy4MxSBw", - "4szFkvQ5tzzhwcfUtZD32hdoG2ZzhvG3ZtfR61yjnxdw", - ], - [157, 3140], - ), - ( - None, - [0.5], - [ - "DBM6F5ChMJzpmkUdL5zD9UXKExmZGfQ1AgPDQy4MxSBw", - "4szFkvQ5tzzhwcfUtZD32hdoG2ZzhvG3ZtfR61yjnxdw", - ], - [157, 157], - ), - ( - None, - [0.00002], - [ - "DBM6F5ChMJzpmkUdL5zD9UXKExmZGfQ1AgPDQy4MxSBw", - ], - "Error: amount 0.00002 is too low.", - ), - ( - [10, 56], - None, - ["DBM6F5ChMJzpmkUdL5zD9UXKExmZGfQ1AgPDQy4MxSBw"], - "Error: The number of passed recipients is not the same as the passed amounts.", - ), - ( - None, - [1, 45], - "DBM6F5ChMJzpmkUdL5zD9UXKExmZGfQ1AgPDQy4MxSBw", - "Error: The number of passed recipients is not the same as the passed amounts.", - ), - ], -) -@pytest.mark.asyncio -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) - - def too_little_amount(amounts, multiplicator): - for amount in amounts: - if amount * multiplicator < MINIMAL_ABSOLUTE_TX_AMOUNT * CENT_MULT_TO_UNIT: - return True - return False - - # run tests - if amounts: - given_amounts = amounts - if UDs_amounts: - given_amounts = UDs_amounts - # test errors - if ( - (len(given_amounts) > 1 and len(outputAddresses) != len(given_amounts)) - or (UDs_amounts and too_little_amount(given_amounts, mock_ud_value)) - or (amounts and too_little_amount(given_amounts, CENT_MULT_TO_UNIT)) - ): - # 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) - assert expected == capsys.readouterr() - assert pytest_exit.type == SystemExit - # test good values - else: - assert expected == await tx.transaction_amount( - amounts, UDs_amounts, outputAddresses - ) - - # generate_transaction_document() # expected results -- GitLab