Commit 5b990cb5 authored by matograine's avatar matograine Committed by Moul

[test] #236: add tests for tx confirmation, mock functions:

* add tests for display_pubkey(), display_amount() and transaction_confirmation() in tests/test_unit_tx.py.
* patch async functions.
* create the patched.py module containing patched/mocked functions.
* modify silkaj/tx.py to match patch requirements:
  * `from silkaj.wot import is_member` -> `from silkaj import wot`
  * `await is_member()` -> `await wot.is_member()`
parent ceb7c1a3
Pipeline #7102 passed with stages
in 3 minutes and 32 seconds
...@@ -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"]])
......
# This file contains patched functions for testing purposes.
from silkaj.constants import G1_SYMBOL
## Mocked values
mock_ud_value = 3.14159
pubkey_list = [
{"pubkey": "DBM6F5ChMJzpmkUdL5zD9UXKExmZGfQ1AgPDQy4MxSBw", "uid": ""},
{"pubkey": "4szFkvQ5tzzhwcfUtZD32hdoG2ZzhvG3ZtfR61yjnxdw", "uid": ""},
{"pubkey": "BFb5yv8z1fowR6Z8mBXTALy5z7gHfMU976WtXhmRsUMh", "uid": "riri"},
{"pubkey": "C1oAV9FX2y9iz2sdp7kZBFu3EBNAa6UkrrRG3EwouPeH", "uid": "fifi"},
{"pubkey": "7Hr6oUxE6nGZxFG7gVbpMK6oUkNTh5eU686EiCXWCrBF", "uid": "loulou"},
]
#### Patched functions ####
## testing tx.py ##
# mock UDValue
async def ud_value(self):
return mock_ud_value
# mock is_member
async def is_member(pubkey):
for account in pubkey_list:
if account["pubkey"] == pubkey:
if account["uid"]:
return account
return False
# mock CurrencySymbol().symbol
async def currency_symbol(self):
return G1_SYMBOL
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
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