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

issue #236 : adding tests

* adding tests for display_pubkey(), display_amount() and transaction_confirmation() in tests/test_unit_tx.py.
* patching async functions.
* creating the patched.py module containing patched functions.
* modifying silkaj/tx.py to match patch requirements :
	* `from silkaj.wot import is_member` -> `from silkaj import wot`
        * `awit is_member()` -> `await wot.is_member()`
parent 820ad606
No related branches found
No related tags found
No related merge requests found
Pipeline #7076 failed
...@@ -26,7 +26,7 @@ from silkaj.network_tools import ClientInstance, HeadBlock ...@@ -26,7 +26,7 @@ from silkaj.network_tools import ClientInstance, HeadBlock
from silkaj.crypto_tools import check_public_key from silkaj.crypto_tools import check_public_key
from silkaj.tools import message_exit, CurrencySymbol, coroutine from silkaj.tools import message_exit, CurrencySymbol, coroutine
from silkaj.auth import auth_method from silkaj.auth import auth_method
from silkaj.wot import is_member from silkaj import wot
from silkaj.money import ( from silkaj.money import (
get_sources, get_sources,
get_amount_from_pubkey, get_amount_from_pubkey,
...@@ -170,7 +170,7 @@ async def display_pubkey(tx, message, pubkey): ...@@ -170,7 +170,7 @@ async def display_pubkey(tx, message, pubkey):
Displays a pubkey and the eventually associated id. Displays a pubkey and the eventually associated id.
""" """
tx.append([message + " (pubkey)", pubkey]) tx.append([message + " (pubkey)", pubkey])
id = await is_member(pubkey) id = await wot.is_member(pubkey)
if id: if id:
tx.append([message + " (id)", id["uid"]]) tx.append([message + " (id)", id["uid"]])
......
import pytest import pytest
from silkaj.tx import (
truncBase,
display_pubkey,
display_amount,
transaction_confirmation,
)
from silkaj.money import UDValue
from silkaj.constants import G1_SYMBOL
import patched
from silkaj.tx import truncBase # truncBase()
@pytest.mark.parametrize( @pytest.mark.parametrize(
"amount,base,expected", "amount,base,expected",
[(0, 0, 0), (10, 2, 0), (100, 2, 100), (306, 2, 300), (3060, 3, 3000)], [(0, 0, 0), (10, 2, 0), (100, 2, 100), (306, 2, 300), (3060, 3, 3000)],
) )
def test_truncBase(amount, base, expected): def test_truncBase(amount, base, expected):
assert truncBase(amount, base) == expected assert truncBase(amount, base) == expected
# display_amount()
@pytest.mark.parametrize(
"message, amount, currency_symbol", [("Total", 1000, G1_SYMBOL)]
)
@pytest.mark.asyncio
async def test_display_amount(message, amount, currency_symbol, monkeypatch):
monkeypatch.setattr("silkaj.money.UDValue.get_ud_value", patched.ud_value)
amount_UD = round(amount / await UDValue().ud_value, 4)
expected = [
[
message + " (unit|relative)",
str(amount / 100)
+ " "
+ currency_symbol
+ " | "
+ str(amount_UD)
+ " UD "
+ currency_symbol,
]
]
tx = list()
await display_amount(tx, message, amount, currency_symbol)
assert tx == expected
# display_pubkey()
@pytest.mark.parametrize(
"message, pubkey, id",
[
("From", "BFb5yv8z1fowR6Z8mBXTALy5z7gHfMU976WtXhmRsUMh", "riri"),
("To", "DBM6F5ChMJzpmkUdL5zD9UXKExmZGfQ1AgPDQy4MxSBw", ""),
],
)
@pytest.mark.asyncio
async def test_display_pubkey(message, pubkey, id, monkeypatch):
monkeypatch.setattr("silkaj.wot.is_member", patched.is_member)
expected = [[message + " (pubkey)", pubkey]]
if id:
expected.append([message + " (id)", id])
tx = list()
await display_pubkey(tx, message, pubkey)
assert tx == expected
# transaction_confirmation()
@pytest.mark.parametrize(
"issuer_pubkey, pubkey_balance, tx_amount, outputAddresses, outputBackChange, comment, currency_symbol",
[
# only one receiver
[
"DBM6F5ChMJzpmkUdL5zD9UXKExmZGfQ1AgPDQy4MxSBw",
3000,
1000,
["4szFkvQ5tzzhwcfUtZD32hdoG2ZzhvG3ZtfR61yjnxdw"],
"",
"",
G1_SYMBOL,
],
# one member receiver
[
"DBM6F5ChMJzpmkUdL5zD9UXKExmZGfQ1AgPDQy4MxSBw",
3000,
1000,
["BFb5yv8z1fowR6Z8mBXTALy5z7gHfMU976WtXhmRsUMh"],
"",
"This is a comment",
G1_SYMBOL,
],
# many receivers and backchange
[
"DBM6F5ChMJzpmkUdL5zD9UXKExmZGfQ1AgPDQy4MxSBw",
3000,
1000,
[
"BFb5yv8z1fowR6Z8mBXTALy5z7gHfMU976WtXhmRsUMh",
"4szFkvQ5tzzhwcfUtZD32hdoG2ZzhvG3ZtfR61yjnxdw",
],
"C1oAV9FX2y9iz2sdp7kZBFu3EBNAa6UkrrRG3EwouPeH",
"This is a comment",
G1_SYMBOL,
],
],
)
@pytest.mark.asyncio
async def test_transaction_confirmation(
issuer_pubkey,
pubkey_balance,
tx_amount,
outputAddresses,
outputBackChange,
comment,
currency_symbol,
monkeypatch,
):
# patched functions
monkeypatch.setattr("silkaj.wot.is_member", patched.is_member)
monkeypatch.setattr("silkaj.money.UDValue.get_ud_value", patched.ud_value)
monkeypatch.setattr(
"silkaj.tools.CurrencySymbol.get_symbol", patched.currency_symbol
)
# creating expected list
expected = list()
expected.append(
[
"pubkey’s balance before tx",
str(pubkey_balance / 100) + " " + currency_symbol,
]
)
await display_amount(
expected,
"total amount",
float(tx_amount * len(outputAddresses)),
currency_symbol,
)
expected.append(
[
"pubkey’s balance after tx",
str(((pubkey_balance - tx_amount * len(outputAddresses)) / 100))
+ " "
+ currency_symbol,
]
)
await display_pubkey(expected, "from", issuer_pubkey)
for outputAddress in outputAddresses:
await display_pubkey(expected, "to", outputAddress)
await display_amount(expected, "amount", tx_amount, currency_symbol)
if outputBackChange:
await display_pubkey(expected, "Backchange", outputBackChange)
expected.append(["comment", comment])
# asserting
tx = await transaction_confirmation(
issuer_pubkey,
pubkey_balance,
tx_amount,
outputAddresses,
outputBackChange,
comment,
)
assert tx == expected
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment