From 5181d5427c88b80f555c4430b148923331bed540 Mon Sep 17 00:00:00 2001
From: matograine <tom.ngr@zaclys.net>
Date: Wed, 22 Jan 2020 18:47:08 +0100
Subject: [PATCH] [enh] #111 : create compute_amounts() function * Uses a list
 of user amounts and a multiplicator (should be CENT_MULT_TO_UNIT or
 UD_Value). * Multiplies the given amounts(UD) to get usable int() amounts for
 Duniter. * Makes sure that each amountUD is superior to the minimal amount.  
   * Absolute amounts are in FloatRange(MINIMAL_AMOUNT), so they have already
 been checked.     * This check should be updated when solving issue #306 /
 DUBP v13. * Returns list of amounts.

---
 silkaj/tx.py          | 19 +++++++++++++++++++
 tests/test_unit_tx.py | 36 +++++++++++++++++++++++++++++++++++-
 2 files changed, 54 insertions(+), 1 deletion(-)

diff --git a/silkaj/tx.py b/silkaj/tx.py
index c7e930fa..49924a7f 100644
--- a/silkaj/tx.py
+++ b/silkaj/tx.py
@@ -141,6 +141,25 @@ async def send_transaction(
         await client.close()
 
 
+def compute_amounts(amounts, multiplicator):
+    """
+    Computes the amounts(UD) and returns a list.
+    Multiplicator should be either CENT_MULT_TO_UNIT or UD_Value.
+    If relative amount, check that amount is superior to minimal amount.
+    """
+    # Create amounts list
+    amounts_list = list()
+    for amount in amounts:
+        computed_amount = amount * multiplicator
+        # check if relative amounts are high enough
+        if (multiplicator != CENT_MULT_TO_UNIT) and (
+            computed_amount < (MINIMAL_TX_AMOUNT * CENT_MULT_TO_UNIT)
+        ):
+            message_exit("Error: amount {0} is too low.".format(amount))
+        amounts_list.append(round(computed_amount))
+    return amounts_list
+
+
 async def transaction_amount(amount, amountUD, allSources):
     """
     Return transaction amount
diff --git a/tests/test_unit_tx.py b/tests/test_unit_tx.py
index eca98c11..1d314185 100644
--- a/tests/test_unit_tx.py
+++ b/tests/test_unit_tx.py
@@ -1,5 +1,5 @@
 import pytest
-from silkaj.tx import truncBase, transaction_confirmation
+from silkaj.tx import truncBase, transaction_confirmation, compute_amounts
 from silkaj.tui import display_pubkey, display_amount
 from silkaj.money import UDValue
 from silkaj.constants import G1_SYMBOL
@@ -161,3 +161,37 @@ async def test_transaction_confirmation(
         comment,
     )
     assert tx == expected
+
+
+# compute_amounts()
+def test_compute_amounts_errors(capsys):
+    trials = (((0.0031, 1,), 314,),)
+    for trial in trials:
+        # check program exit on error
+        with pytest.raises(SystemExit) as pytest_exit:
+            # read output to check error.
+            compute_amounts(
+                trial[0], trial[1],
+            )
+            expected_error = "Error: amount {0} is too low.".format(trial[0][0])
+            assert capsys.readouterr() == expected_error
+        assert pytest_exit.type == SystemExit
+
+
+def test_compute_amounts():
+    ud_value = 314
+    assert compute_amounts((10.0, 2.0, 0.01, 0.011, 0.019), 100) == [
+        1000,
+        200,
+        1,
+        1,
+        2,
+    ]
+    assert compute_amounts([0.0032], ud_value,) == [1]
+    assert compute_amounts([1.00], ud_value,) == [314]
+    assert compute_amounts([1.01], ud_value,) == [317]
+    assert compute_amounts([1.99], ud_value,) == [625]
+    assert compute_amounts([1.001], ud_value,) == [314]
+    assert compute_amounts([1.009], ud_value,) == [317]
+    # This case will not happen in real use, but this particular function will allow it.
+    assert compute_amounts([0.0099], 100,) == [1]
-- 
GitLab