From fec79c3ae1f9b24e4eda3db96cd6df6630e5f6a9 Mon Sep 17 00:00:00 2001
From: matograine <tom.ngr@zaclys.net>
Date: Sun, 22 Mar 2020 15:58:24 +0100
Subject: [PATCH] [mod] #213: import whole modules in tx.py For unit test
 purposes, we need to do:

`from silkaj import module`

instead of

`from silkaj.module import function1, function2`

Otherwise, we can't patch functions.
---
 silkaj/tx.py | 79 ++++++++++++++++++++++++++--------------------------
 1 file changed, 40 insertions(+), 39 deletions(-)

diff --git a/silkaj/tx.py b/silkaj/tx.py
index bcc5f499..55abf275 100644
--- a/silkaj/tx.py
+++ b/silkaj/tx.py
@@ -19,22 +19,22 @@ from re import compile, search
 import math
 from asyncio import sleep
 from tabulate import tabulate
-from click import command, option, FloatRange
-
-from silkaj.cli_tools import MutuallyExclusiveOption
-from silkaj.network_tools import ClientInstance
-from silkaj.blockchain_tools import HeadBlock
-from silkaj.crypto_tools import check_public_key
-from silkaj.tools import message_exit, CurrencySymbol, coroutine
-from silkaj.auth import auth_method
+from click import command, option, FloatRange, group
+
+from silkaj import cli_tools
+from silkaj import network_tools as nt
+from silkaj import crypto_tools as ct
+from silkaj import blockchain_tools as bt
+from silkaj import tools
+from silkaj import auth
 from silkaj import money
+from silkaj import tui
 from silkaj.constants import (
     SOURCES_PER_TX,
     MINIMAL_TX_AMOUNT,
     CENT_MULT_TO_UNIT,
     ASYNC_SLEEP,
 )
-from silkaj.tui import display_amount, display_pubkey
 
 from duniterpy.api.bma.tx import process
 from duniterpy.documents import BlockUID, Transaction
@@ -51,7 +51,7 @@ from duniterpy.documents.transaction import OutputSource, Unlock, SIGParameter
     help="Quantitative amount(s):\n-a <amount>\nMinimum amount is {0}".format(
         MINIMAL_TX_AMOUNT
     ),
-    cls=MutuallyExclusiveOption,
+    cls=cli_tools.MutuallyExclusiveOption,
     mutually_exclusive=["amountsud", "allsources"],
 )
 @option(
@@ -61,14 +61,14 @@ from duniterpy.documents.transaction import OutputSource, Unlock, SIGParameter
     multiple=True,
     type=float,
     help="Relative amount(s):\n-d <amount_UD>",
-    cls=MutuallyExclusiveOption,
+    cls=cli_tools.MutuallyExclusiveOption,
     mutually_exclusive=["amounts", "allsources"],
 )
 @option(
     "--allSources",
     is_flag=True,
     help="Send all sources to one recipient",
-    cls=MutuallyExclusiveOption,
+    cls=cli_tools.MutuallyExclusiveOption,
     mutually_exclusive=["amounts", "amountsud"],
 )
 @option(
@@ -88,7 +88,7 @@ Sending to many recipients is possible:\n\
     help="Pubkey recipient to send the rest of the transaction: <pubkey[:checksum]>",
 )
 @option("--yes", "-y", is_flag=True, help="Assume yes. Do not prompt confirmation")
-@coroutine
+@tools.coroutine
 async def send_transaction(
     amounts, amountsud, allsources, recipients, comment, outputbackchange, yes
 ):
@@ -96,16 +96,16 @@ async def send_transaction(
     Main function
     """
     if not (amounts or amountsud or allsources):
-        message_exit("Error: amount, amountUD or allSources is not set.")
+        tools.message_exit("Error: amount, amountUD or allSources is not set.")
     if allsources and len(recipients) > 1:
-        message_exit(
+        tools.message_exit(
             "Error: the --allSources option can only be used with one recipient."
         )
     # compute amounts and amountsud
     if not allsources:
         tx_amounts = await transaction_amount(amounts, amountsud, recipients)
 
-    key = auth_method()
+    key = auth.auth_method()
     issuer_pubkey = key.pubkey
 
     pubkey_amount = await money.get_amount_from_pubkey(issuer_pubkey)
@@ -148,7 +148,7 @@ async def send_transaction(
             outputbackchange,
         )
     else:
-        client = ClientInstance().client
+        client = nt.ClientInstance().client
         await client.close()
 
 
@@ -164,7 +164,7 @@ async def transaction_amount(amounts, UDs_amounts, outputAddresses):
         UD_value = await money.UDValue().ud_value
         amounts_list = compute_amounts(UDs_amounts, UD_value)
     if len(amounts_list) != len(outputAddresses) and len(amounts_list) != 1:
-        message_exit(
+        tools.message_exit(
             "Error: The number of passed recipients is not the same as the passed amounts."
         )
     # In case one amount is passed with multiple recipients
@@ -188,7 +188,7 @@ def compute_amounts(amounts, multiplicator):
         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))
+            tools.message_exit("Error: amount {0} is too low.".format(amount))
         amounts_list.append(round(computed_amount))
     return amounts_list
 
@@ -204,16 +204,16 @@ def check_transaction_values(
     """
     checkComment(comment)
     for i, outputAddress in enumerate(outputAddresses):
-        outputAddresses[i] = check_public_key(outputAddress, True)
+        outputAddresses[i] = ct.check_public_key(outputAddress, True)
         if not outputAddresses[i]:
-            message_exit(outputAddress)
+            tools.message_exit(outputAddress)
     if outputBackChange:
         pubkey = outputBackChange
-        outputBackChange = check_public_key(outputBackChange, True)
+        outputBackChange = ct.check_public_key(outputBackChange, True)
         if not outputBackChange:
-            message_exit(pubkey)
+            tools.message_exit(pubkey)
     if enough_source:
-        message_exit(
+        tools.message_exit(
             issuer_pubkey + " pubkey doesn’t have enough money for this transaction."
         )
     return outputBackChange
@@ -231,41 +231,41 @@ async def transaction_confirmation(
     Generate transaction confirmation
     """
 
-    currency_symbol = await CurrencySymbol().symbol
+    currency_symbol = await tools.CurrencySymbol().symbol
     ud_value = await money.UDValue().ud_value
     total_tx_amount = sum(tx_amounts)
     tx = list()
     # display account situation
-    display_amount(
+    tui.display_amount(
         tx,
         "pubkey's balance before tx",
         pubkey_amount,
         ud_value,
         currency_symbol,
     )
-    display_amount(
+    tui.display_amount(
         tx,
         "total transaction amount",
         total_tx_amount,
         ud_value,
         currency_symbol,
     )
-    display_amount(
+    tui.display_amount(
         tx,
         "pubkey's balance after tx",
         (pubkey_amount - total_tx_amount),
         ud_value,
         currency_symbol,
     )
-    await display_pubkey(tx, "from", issuer_pubkey)
+    await tui.display_pubkey(tx, "from", issuer_pubkey)
     # display outputs and amounts
     for outputAddress, tx_amount in zip(outputAddresses, tx_amounts):
-        await display_pubkey(tx, "to", outputAddress)
+        await tui.display_pubkey(tx, "to", outputAddress)
         await sleep(ASYNC_SLEEP)
-        display_amount(tx, "amount", tx_amount, ud_value, currency_symbol)
+        tui.display_amount(tx, "amount", tx_amount, ud_value, currency_symbol)
     # display last informations
     if outputBackChange:
-        await display_pubkey(tx, "Backchange", outputBackChange)
+        await tui.display_pubkey(tx, "Backchange", outputBackChange)
     tx.append(["comment", comment])
     return tx
 
@@ -288,7 +288,7 @@ async def get_list_input_for_transaction(pubkey, TXamount):
         if TXamount <= 0:
             break
     if TXamount > 0 and not intermediatetransaction:
-        message_exit("Error: you don't have enough money")
+        tools.message_exit("Error: you don't have enough money")
     return listinputfinal, totalAmountInput, intermediatetransaction
 
 
@@ -300,7 +300,7 @@ async def handle_intermediaries_transactions(
     Comment="",
     OutputbackChange=None,
 ):
-    client = ClientInstance().client
+    client = nt.ClientInstance().client
     while True:
         listinput_and_amount = await get_list_input_for_transaction(
             issuers, sum(tx_amounts)
@@ -356,7 +356,7 @@ async def generate_and_send_transaction(
         display_sent_tx(outputAddress, tx_amount)
     print("   - Total:   " + str(sum(tx_amounts) / 100))
 
-    client = ClientInstance().client
+    client = nt.ClientInstance().client
     transaction = await generate_transaction_document(
         issuers,
         tx_amounts,
@@ -366,11 +366,12 @@ async def generate_and_send_transaction(
         OutputbackChange,
     )
     transaction.sign([key])
+
     response = await client(process, transaction.signed_raw())
     if response.status == 200:
         print("Transaction successfully sent.")
     else:
-        message_exit(
+        tools.message_exit(
             "Error while publishing transaction: {0}".format(await response.text())
         )
 
@@ -392,7 +393,7 @@ async def generate_transaction_document(
     totalAmountInput = listinput_and_amount[1]
     total_tx_amount = sum(tx_amounts)
 
-    head_block = await HeadBlock().head_block
+    head_block = await bt.HeadBlock().head_block
     currency_name = head_block["currency"]
     blockstamp_current = BlockUID(head_block["number"], head_block["hash"])
     curentUnitBase = head_block["unitbase"]
@@ -461,12 +462,12 @@ def generate_output(listoutput, unitbase, rest, recipient_address):
 
 def checkComment(Comment):
     if len(Comment) > 255:
-        message_exit("Error: Comment is too long")
+        tools.message_exit("Error: Comment is too long")
     regex = compile(
         "^[0-9a-zA-Z\\ \\-\\_\\:\\/\\;\\*\\[\\]\\(\\)\\?\\!\\^\\+\\=\\@\\&\\~\\#\\{\\}\\|\\\\<\\>\\%\\.]*$"
     )
     if not search(regex, Comment):
-        message_exit("Error: the format of the comment is invalid")
+        tools.message_exit("Error: the format of the comment is invalid")
 
 
 def truncBase(amount, base):
-- 
GitLab