From e46ed618e9cadfb5b6e1384c4ca396a293896a98 Mon Sep 17 00:00:00 2001
From: matograine <tom.ngr@zaclys.net>
Date: Tue, 28 Jan 2020 18:28:00 +0100
Subject: [PATCH] [feat] #111: modify transaction_confirmation() function

    * use display_amount() to display pubkey's amounts

* modify test_transaction_confirmation() to match new behavior
    * add new test case
    * remove useless float()
---
 silkaj/tx.py          | 36 ++++++++++++-------------
 tests/test_unit_tx.py | 63 ++++++++++++++++++++++++++-----------------
 2 files changed, 56 insertions(+), 43 deletions(-)

diff --git a/silkaj/tx.py b/silkaj/tx.py
index 064dd026..9e39fc96 100644
--- a/silkaj/tx.py
+++ b/silkaj/tx.py
@@ -203,7 +203,12 @@ def check_transaction_values(
 
 
 async def transaction_confirmation(
-    issuer_pubkey, pubkey_amount, tx_amount, outputAddresses, outputBackChange, comment
+    issuer_pubkey,
+    pubkey_amount,
+    tx_amounts,
+    outputAddresses,
+    outputBackChange,
+    comment,
 ):
     """
     Generate transaction confirmation
@@ -211,35 +216,30 @@ async def transaction_confirmation(
 
     currency_symbol = await CurrencySymbol().symbol
     ud_value = await money.UDValue().ud_value
+    total_tx_amount = sum(tx_amounts)
     tx = list()
-    tx.append(
-        ["pubkey’s balance before tx", str(pubkey_amount / 100) + " " + currency_symbol]
+    # display account situation
+    display_amount(
+        tx, "pubkey's balance before tx", pubkey_amount, ud_value, currency_symbol,
+    )
+    display_amount(
+        tx, "total transaction amount", total_tx_amount, ud_value, currency_symbol,
     )
-
     display_amount(
         tx,
-        "total amount",
-        float(tx_amount * len(outputAddresses)),
+        "pubkey's balance after tx",
+        (pubkey_amount - total_tx_amount),
         ud_value,
         currency_symbol,
     )
-
-    tx.append(
-        [
-            "pubkey’s balance after tx",
-            str(((pubkey_amount - tx_amount * len(outputAddresses)) / 100))
-            + " "
-            + currency_symbol,
-        ]
-    )
-
     await display_pubkey(tx, "from", issuer_pubkey)
-    for outputAddress in outputAddresses:
+    # display outputs and amounts
+    for outputAddress, tx_amount in zip(outputAddresses, tx_amounts):
         await display_pubkey(tx, "to", outputAddress)
         display_amount(tx, "amount", tx_amount, ud_value, currency_symbol)
+    # display last informations
     if outputBackChange:
         await display_pubkey(tx, "Backchange", outputBackChange)
-
     tx.append(["comment", comment])
     return tx
 
diff --git a/tests/test_unit_tx.py b/tests/test_unit_tx.py
index 14217184..2a5b6db9 100644
--- a/tests/test_unit_tx.py
+++ b/tests/test_unit_tx.py
@@ -76,13 +76,13 @@ async def test_display_pubkey(message, pubkey, id, monkeypatch):
 
 # transaction_confirmation()
 @pytest.mark.parametrize(
-    "issuer_pubkey, pubkey_balance, tx_amount, outputAddresses, outputBackChange, comment, currency_symbol",
+    "issuer_pubkey, pubkey_balance, tx_amounts, outputAddresses, outputBackChange, comment, currency_symbol",
     [
         # only one receiver
         [
             "DBM6F5ChMJzpmkUdL5zD9UXKExmZGfQ1AgPDQy4MxSBw",
             3000,
-            1000,
+            [1000],
             ["4szFkvQ5tzzhwcfUtZD32hdoG2ZzhvG3ZtfR61yjnxdw"],
             "",
             "",
@@ -92,7 +92,7 @@ async def test_display_pubkey(message, pubkey, id, monkeypatch):
         [
             "DBM6F5ChMJzpmkUdL5zD9UXKExmZGfQ1AgPDQy4MxSBw",
             3000,
-            1000,
+            [1000],
             ["BFb5yv8z1fowR6Z8mBXTALy5z7gHfMU976WtXhmRsUMh"],
             "",
             "This is a comment",
@@ -102,7 +102,7 @@ async def test_display_pubkey(message, pubkey, id, monkeypatch):
         [
             "DBM6F5ChMJzpmkUdL5zD9UXKExmZGfQ1AgPDQy4MxSBw",
             3000,
-            1000,
+            [1000, 1000],
             [
                 "BFb5yv8z1fowR6Z8mBXTALy5z7gHfMU976WtXhmRsUMh",
                 "4szFkvQ5tzzhwcfUtZD32hdoG2ZzhvG3ZtfR61yjnxdw",
@@ -111,13 +111,26 @@ async def test_display_pubkey(message, pubkey, id, monkeypatch):
             "This is a comment",
             G1_SYMBOL,
         ],
+        # many receivers and outputs
+        [
+            "DBM6F5ChMJzpmkUdL5zD9UXKExmZGfQ1AgPDQy4MxSBw",
+            3000,
+            [1000, 250],
+            [
+                "BFb5yv8z1fowR6Z8mBXTALy5z7gHfMU976WtXhmRsUMh",
+                "4szFkvQ5tzzhwcfUtZD32hdoG2ZzhvG3ZtfR61yjnxdw",
+            ],
+            "",
+            "This is a comment",
+            G1_SYMBOL,
+        ],
     ],
 )
 @pytest.mark.asyncio
 async def test_transaction_confirmation(
     issuer_pubkey,
     pubkey_balance,
-    tx_amount,
+    tx_amounts,
     outputAddresses,
     outputBackChange,
     comment,
@@ -134,44 +147,44 @@ async def test_transaction_confirmation(
     # creating expected list
     ud_value = await UDValue().ud_value
     expected = list()
-    expected.append(
-        [
-            "pubkey’s balance before tx",
-            str(pubkey_balance / 100) + " " + currency_symbol,
-        ]
+    total_tx_amount = sum(tx_amounts)
+    # display account situation
+    display_amount(
+        expected,
+        "pubkey's balance before tx",
+        pubkey_balance,
+        ud_value,
+        currency_symbol,
     )
-
     display_amount(
         expected,
-        "total amount",
-        float(tx_amount * len(outputAddresses)),
+        "total transaction amount",
+        total_tx_amount,
         ud_value,
         currency_symbol,
     )
-
-    expected.append(
-        [
-            "pubkey’s balance after tx",
-            str(((pubkey_balance - tx_amount * len(outputAddresses)) / 100))
-            + " "
-            + currency_symbol,
-        ]
+    display_amount(
+        expected,
+        "pubkey's balance after tx",
+        (pubkey_balance - total_tx_amount),
+        ud_value,
+        currency_symbol,
     )
-
     await display_pubkey(expected, "from", issuer_pubkey)
-    for outputAddress in outputAddresses:
+    # display recipients and amounts
+    for outputAddress, tx_amount in zip(outputAddresses, tx_amounts):
         await display_pubkey(expected, "to", outputAddress)
         display_amount(expected, "amount", tx_amount, ud_value, currency_symbol)
+    # display backchange and comment
     if outputBackChange:
         await display_pubkey(expected, "Backchange", outputBackChange)
-
     expected.append(["comment", comment])
 
     # asserting
     tx = await transaction_confirmation(
         issuer_pubkey,
         pubkey_balance,
-        tx_amount,
+        tx_amounts,
         outputAddresses,
         outputBackChange,
         comment,
-- 
GitLab