Skip to content
Snippets Groups Projects
Commit 005b705d authored by matograine's avatar matograine
Browse files

[test] #335 : two functions are testing transaction_amount()

  * make one test only errors
  * move it from test_unit_tx.py to test_tx.py
parent ca5b662f
No related branches found
No related tags found
1 merge request!130#213: Write unit tests for the transaction command
......@@ -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"])
......
......@@ -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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment