Commit 8e33f014 authored by matograine's avatar matograine

rework on tests

parent d3f7de6d
......@@ -18,10 +18,9 @@ along with Silkaj. If not, see <https://www.gnu.org/licenses/>.
from re import compile, search
import math
from time import sleep
from tabulate import tabulate
from click import command, option, FloatRange
from silkaj.cli_tools import MutuallyExclusiveOption
from tabulate import tabulate
from silkaj.network_tools import ClientInstance, HeadBlock
from silkaj.crypto_tools import check_public_key
from silkaj.tools import message_exit, CurrencySymbol, coroutine
......@@ -41,27 +40,9 @@ from duniterpy.documents.transaction import OutputSource, Unlock, SIGParameter
@command("tx", help="Send transaction")
@option(
"--amount",
type=FloatRange(0.01),
help="Quantitative value",
cls=MutuallyExclusiveOption,
mutually_exclusive=["amountud", "allsources"],
)
@option(
"--amountUD",
type=float,
help="Relative value",
cls=MutuallyExclusiveOption,
mutually_exclusive=["amount", "allsources"],
)
@option(
"--allSources",
is_flag=True,
help="Send all sources",
cls=MutuallyExclusiveOption,
mutually_exclusive=["amount", "amountud"],
)
@option("--amount", type=FloatRange(0.01), help="Quantitative value")
@option("--amountUD", type=float, help="Relative value")
@option("--allSources", is_flag=True, help="Send all sources")
@option(
"--output",
required=True,
......@@ -121,12 +102,15 @@ async def send_transaction(
async def transaction_amount(amount, amountUD, allSources):
"""
Check command line interface amount option
Return transaction amount
"""
if not (amount or amountUD or allSources):
message_exit("--amount nor --amountUD nor --allSources is set")
if amount:
return round(amount * 100)
return amount * 100
if amountUD:
return round(amountUD * await UDValue().ud_value)
return amountUD * await UDValue().ud_value
def check_transaction_values(
......@@ -146,6 +130,24 @@ def check_transaction_values(
)
async def display_amount(tx, message, amount, currency_symbol):
"""
For transaction_confirmation,
Displays an amount in unit and relative reference.
"""
amount_UD = round((amount / await UDValue().ud_value), 4)
tx.append(
[
message + " (unit | relative)",
"{unit_amount} {currency_symbol} || {UD_amount} UD".format(
unit_amount=str(amount / 100),
currency_symbol=currency_symbol,
UD_amount=str(amount_UD),
),
]
)
async def display_pubkey(tx, message, pubkey):
"""
For transaction_confirmation,
......@@ -162,25 +164,13 @@ async def display_output_and_amount(tx, outputAddresses, tx_amount, currency_sym
For transaction_confirmation,
Displays the receiver(s) and the amount(s) of one or many output(s).
"""
if len(tx_amount) == 1:
for outputAddress in outputAddresses:
await display_pubkey(tx, "to", outputAddress)
await display_amount(tx, "amount", tx_amount[0], currency_symbol)
if len(tx_amount) > 1:
c = 0
while c < len(outputAddresses):
await display_pubkey(tx, "to", outputAddresses[c])
await display_amount(tx, "amount", tx_amount[c], currency_symbol)
c += 1
for outputAddress in outputAddresses:
await display_pubkey(tx, "to", outputAddress)
await display_amount(tx, "amount", tx_amount, currency_symbol)
async def transaction_confirmation(
issuer_pubkey,
pubkey_amount,
tx_amount,
totalAmount,
outputAddresses,
outputBackChange,
comment,
issuer_pubkey, pubkey_amount, tx_amount, outputAddresses, outputBackChange, comment
):
"""
Generate transaction confirmation
......@@ -192,25 +182,31 @@ async def transaction_confirmation(
["pubkey’s balance before tx", str(pubkey_amount / 100) + " " + currency_symbol]
)
await display_amount(tx, "total amount", totalAmount, currency_symbol)
await display_amount(
tx,
"total amount",
float(tx_amount * len(outputAddresses)),
currency_symbol,
)
tx.append(
[
"pubkey’s balance after tx",
str(((pubkey_amount - totalAmount) / 100)) + " " + currency_symbol,
str(((pubkey_amount - tx_amount * len(outputAddresses)) / 100))
+ " "
+ currency_symbol,
]
)
await display_pubkey(tx, "from", issuer_pubkey)
await display_output_and_amount(tx, outputAddresses, tx_amount, currency_symbol)
if outputBackChange:
await display_pubkey(tx, "Backchange", outputBackChange)
await display_pubkey("Backchange", outputBackChange)
tx.append(["comment", comment])
return tx
async def get_list_input_for_transaction(pubkey, TXamount):
listinput, amount = await get_sources(pubkey)
......
import pytest
from silkaj.tx import truncBase, display_pubkey, display_amount, total_amount, display_output_and_amount
from silkaj.tx import (
truncBase,
display_pubkey,
display_amount,
display_output_and_amount,
)
from silkaj.money import UDValue
# truncBase()
@pytest.mark.parametrize('amount,base,expected', [
(0, 0, 0),
(10, 2, 0),
(100, 2, 100),
(306, 2, 300),
(3060, 3, 3000),
])
@pytest.mark.parametrize(
"amount,base,expected",
[(0, 0, 0), (10, 2, 0), (100, 2, 100), (306, 2, 300), (3060, 3, 3000)],
)
def test_truncBase(amount, base, expected):
assert truncBase(amount, base) == expected
#display_amount()
@pytest.mark.parametrize('message, amount, currency_symbol', [
("Total", 10, "Ğ1"),
])
# display_amount()
@pytest.mark.parametrize("message, amount, currency_symbol", [("Total", 1000, "Ğ1")])
@pytest.mark.asyncio
async def test_display_amount(message, amount, currency_symbol):
amount_UD = float(amount) * await UDValue().ud_value
expected = (message + " (unit | relative)", str(amount/100) + " " + currency_symbol + " || " + str(amount_UD) + " UD")
tx = await display_amount ([], message, amount, currency_symbol) == expected
amount_UD = round(amount / await UDValue().ud_value, 4)
expected = [
[
message + " (unit | relative)",
str(amount / 100)
+ " "
+ currency_symbol
+ " || "
+ str(amount_UD)
+ " UD",
]
]
tx = list()
await display_amount(tx, message, amount, currency_symbol)
assert tx == expected
#display_pubkey()
@pytest.mark.parametrize('message, pubkey, id', [
# display_pubkey()
@pytest.mark.parametrize(
"message, pubkey, id",
[
("From", "CmFKubyqbmJWbhyH2eEPVSSs4H4NeXGDfrETzEnRFtPd", "Matograine"),
("To", "9qJjmuYi4Sti89Lm81G7AoFo5QLpmxd1NSMqN4Z1BwhS", ""),
("To", "9qJjmuYi4Sti89Lm81G7AoFo5QLpmxd1NSMqN4Z1BwhS", "TRUC"),
])
],
)
@pytest.mark.asyncio
async def test_display_pubkey(message, pubkey, id):
if id == "":
expected = [[message + " (pubkey)", pubkey]]
if id == "TRUC":
expected = "DEBUG : test passes even if it shoundn't"
else:
expected = [[message + " (pubkey)", pubkey], [message + " (id)", id]]
assert display_pubkey(message, pubkey) == expected
tx = list()
await display_pubkey(tx, message, pubkey)
print("DEBUG : tx : ", tx) # to see if display_pubkey gets tgi ID
assert tx == expected
#display_output_and_amount()
@pytest.mark.parametrize('outputAddresses, tx_amount, currency_symbol, id', [
("CmFKubyqbmJWbhyH2eEPVSSs4H4NeXGDfrETzEnRFtPd", 10, "Ğ1", "Matograine"),
(["d88fPFbDdJXJANHH7hedFMaRyGcnVZj9c5cDaE76LRN"], 10, "Ğ1", ""),
(["CmFKubyqbmJWbhyH2eEPVSSs4H4NeXGDfrETzEnRFtPd", "d88fPFbDdJXJANHH7hedFMaRyGcnVZj9c5cDaE76LRN"], 10, "Ğ1", ["Matograine", ""]),
(["CmFKubyqbmJWbhyH2eEPVSSs4H4NeXGDfrETzEnRFtPd", "d88fPFbDdJXJANHH7hedFMaRyGcnVZj9c5cDaE76LRN"], [10, 23.65], "Ğ1", ["Matograine", ""]),
])
# display_output_and_amount()
@pytest.mark.parametrize(
"outputAddresses, tx_amount, currency_symbol, ids",
[
(["CmFKubyqbmJWbhyH2eEPVSSs4H4NeXGDfrETzEnRFtPd"], 1000, "Ğ1", ["Matograine"]),
(["d88fPFbDdJXJANHH7hedFMaRyGcnVZj9c5cDaE76LRN"], 1000, "Ğ1", [""]),
(
[
"CmFKubyqbmJWbhyH2eEPVSSs4H4NeXGDfrETzEnRFtPd",
"d88fPFbDdJXJANHH7hedFMaRyGcnVZj9c5cDaE76LRN",
],
1000,
"Ğ1",
["Matograine", ""],
),
],
)
@pytest.mark.asyncio
async def test_display_output_and_amount(outputAddresses, tx_amount, currency_symbol, id):
async def test_display_output_and_amount(
outputAddresses, tx_amount, currency_symbol, ids
):
amount_UD = round(tx_amount / await UDValue().ud_value, 4)
expected = list()
if len(outputAddresses) == 1:
amount_UD = float(tx_amount) * await UDValue().ud_value
if id == "":
expected = [["to (pubkey)", outputAddresses], ["amount (unit | relative)", str(tx_amount/100) + " " + currency_symbol + " || " + str(amount_UD) + " UD"]]
elif id != "":
expected = [["to (pubkey)", outputAddresses], ["to (id)", id], ["amount (unit | relative)", str(tx_amount/100) + " " + currency_symbol + " || " + str(amount_UD) + " UD"]]
elif len(outputAddresses) != 1 and len(tx_amount) == 1:
amount_UD = float(tx_amount) * await UDValue().ud_value
a = 0
while a < len(outputAddresses):
if id(a) == []:
expected = expected.append(["to (pubkey)", outputAddresses(a)], ["amount (unit | relative)", str(tx_amount/100) + " " + currency_symbol + " || " + str(amount_UD) + " UD"])
a = a+1
elif id(a) != []:
expected = expected.append(["to (pubkey)", outputAddresses(a)], ["to (id)", id(a)], ["amount (unit | relative)", str(tx_amount/100) + " " + currency_symbol + " || " + str(amount_UD) + " UD"])
a = a+1
elif len(outputAddresses) != 1 and len(tx_amount) != 1:
a = 0
while a < len(outputAddresses):
amount_UD = float(tx_amount(a)) * await UDValue().ud_value
if id(a) == "":
expected = expected.append(["to (pubkey)", outputAddresses(a)], ["amount (unit | relative)", str(tx_amount(a)/100) + " " + currency_symbol + " || " + str(amount_UD) + " UD"])
a = a+1
elif id(a) != "":
expected = expected.append(["to (pubkey)", outputAddresses(a)], ["to (id)", id(a)], ["amount (unit | relative)", str(tx_amount(a)/100) + " " + currency_symbol + " || " + str(amount_UD) + " UD"])
a = a+1
assert display_output_and_amount(outputAddresses, tx_amount, currency_symbol) == expected
if ids(0) == "":
expected.append(
["to (pubkey)", outputAddresses[0]],
[
"amount (unit | relative)",
str(tx_amount / 100)
+ " "
+ currency_symbol
+ " || "
+ str(amount_UD)
+ " UD",
],
)
else:
expected.append(
["to (pubkey)", outputAddresses],
["to (id)", ids(0)],
[
"amount (unit | relative)",
str(tx_amount / 100)
+ " "
+ currency_symbol
+ " || "
+ str(amount_UD)
+ " UD",
],
)
else:
for outputAddress, id in zip(outputAddresses, ids):
if id == "":
expected.append(
["to (pubkey)", outputAddress],
[
"amount (unit | relative)",
str(tx_amount / 100)
+ " "
+ currency_symbol
+ " || "
+ str(amount_UD)
+ " UD",
],
)
else:
expected.append(
["to (pubkey)", outputAddress],
["to (id)", id],
[
"amount (unit | relative)",
str(tx_amount / 100)
+ " "
+ currency_symbol
+ " || "
+ str(amount_UD)
+ " UD",
],
)
tx = list()
await display_output_and_amount(tx, outputAddresses, tx_amount, currency_symbol)
assert tx == expected
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment