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

[test] #213: create test for handle_intermediary_transaction()

* two test cases:
    * with two outputs and a backchange, without intermediary transaction
    * with one output and no backchange, with an intermediary transaction
* add asynctest in the dev dependencies in pyproject.toml
* add key_fifi in utils_tests.py
parent 1d8f8aac
No related branches found
No related tags found
No related merge requests found
...@@ -28,6 +28,7 @@ pytest-cov = "^2.7" ...@@ -28,6 +28,7 @@ pytest-cov = "^2.7"
pytest-asyncio = "^0.10.0" pytest-asyncio = "^0.10.0"
coverage-badge = "^1.0" coverage-badge = "^1.0"
pytest-sugar = "^0.9.2" pytest-sugar = "^0.9.2"
asynctest = "^0.13.0"
[tool.poetry.scripts] [tool.poetry.scripts]
silkaj = "silkaj.cli:cli" silkaj = "silkaj.cli:cli"
......
...@@ -9,7 +9,7 @@ async def patched_ud_value(self): ...@@ -9,7 +9,7 @@ async def patched_ud_value(self):
return mock_ud_value return mock_ud_value
# mock get_sources() # mock get_sources and use a count
async def patched_get_sources(pubkey): async def patched_get_sources(pubkey):
""" """
Returns transaction sources lists. Returns transaction sources lists.
......
mock_ud_value = 314
from duniterpy.key import SigningKey
from silkaj.tools import message_exit
async def patched_generate_and_send_transaction(
key,
issuers,
tx_amounts,
listinput_and_amount,
outputAddresses,
Comment,
OutputbackChange,
):
if not (
(
isinstance(key, SigningKey)
and isinstance(issuers, str)
and isinstance(tx_amounts, list)
and isinstance(listinput_and_amount, tuple)
and isinstance(outputAddresses, list)
and isinstance(Comment, str)
and isinstance(OutputBackchange, str)
)
and len(tx_amounts) == len(outputAddresses)
and sum(tx_amounts) <= listinput_and_amount[2]
and key.pubkey() == issuers
):
message_exit(
"Test error : patched_generate_and_send_transaction() : Parameters are not coherent"
)
pass
import pytest import pytest
from asynctest.mock import Mock, CoroutineMock
from silkaj.tx import ( from silkaj.tx import (
truncBase, truncBase,
transaction_confirmation, transaction_confirmation,
...@@ -6,6 +7,7 @@ from silkaj.tx import ( ...@@ -6,6 +7,7 @@ from silkaj.tx import (
transaction_amount, transaction_amount,
generate_transaction_document, generate_transaction_document,
get_list_input_for_transaction, get_list_input_for_transaction,
handle_intermediaries_transactions,
) )
from silkaj.tui import display_amount, display_pubkey from silkaj.tui import display_amount, display_pubkey
from silkaj.money import UDValue from silkaj.money import UDValue
...@@ -20,10 +22,10 @@ from duniterpy.documents.transaction import ( ...@@ -20,10 +22,10 @@ from duniterpy.documents.transaction import (
from duniterpy.documents.block_uid import BlockUID from duniterpy.documents.block_uid import BlockUID
from patched.wot import patched_is_member from patched.wot import patched_is_member
from patched.money import patched_get_sources, patched_ud_value, mock_ud_value from patched.money import patched_get_sources, patched_ud_value
from patched.tools import patched_currency_symbol from patched.tools import patched_currency_symbol
from patched.network_tools import patched_head_block from patched.network_tools import patched_head_block
from utils_tests import pubkey_list, mock_ud_value, key_fifi
# truncBase() # truncBase()
@pytest.mark.parametrize( @pytest.mark.parametrize(
...@@ -439,7 +441,7 @@ async def test_get_list_input_for_transaction( ...@@ -439,7 +441,7 @@ async def test_get_list_input_for_transaction(
# patched functions # patched functions
monkeypatch.setattr("silkaj.money.get_sources", patched_get_sources) monkeypatch.setattr("silkaj.money.get_sources", patched_get_sources)
# reset patched_get_sources counter # reset get_sources count
patched_get_sources.counter = 0 patched_get_sources.counter = 0
# testing error exit # testing error exit
if isinstance(expected, str): if isinstance(expected, str):
...@@ -451,3 +453,386 @@ async def test_get_list_input_for_transaction( ...@@ -451,3 +453,386 @@ async def test_get_list_input_for_transaction(
else: else:
result = await get_list_input_for_transaction(pubkey, TXamount) result = await get_list_input_for_transaction(pubkey, TXamount)
assert (len(result[0]), result[1], result[2]) == expected assert (len(result[0]), result[1], result[2]) == expected
# handle_intermediaries_transactions()
@pytest.mark.parametrize(
"key, issuers, tx_amounts, outputAddresses, Comment, OutputbackChange, expected_listinput_amount",
[
# test 1 : with two amounts/outputs and an outputbackchange, no need for intermediary transaction.
(
key_fifi,
"HcRgKh4LwbQVYuAc3xAdCynYXpKoiPE6qdxCMa8JeHat",
(100, 100),
(
"DBM6F5ChMJzpmkUdL5zD9UXKExmZGfQ1AgPDQy4MxSBw",
"4szFkvQ5tzzhwcfUtZD32hdoG2ZzhvG3ZtfR61yjnxdw",
),
"Test comment",
"HcRgKh4LwbQVYuAc3xAdCynYXpKoiPE6qdxCMa8JeHat",
(
[
InputSource(
amount=100,
base=0,
source="T",
origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7",
index=0,
),
InputSource(
amount=200,
base=0,
source="T",
origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7",
index=1,
),
],
300,
False,
),
),
# test 2 : with one amounts/outputs and no outputbackchange, need for intermediary transaction.
(
key_fifi,
"HcRgKh4LwbQVYuAc3xAdCynYXpKoiPE6qdxCMa8JeHat",
(90000,),
("4szFkvQ5tzzhwcfUtZD32hdoG2ZzhvG3ZtfR61yjnxdw",),
"Test comment",
"HcRgKh4LwbQVYuAc3xAdCynYXpKoiPE6qdxCMa8JeHat",
(
[
InputSource(
amount=100,
base=0,
source="T",
origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7",
index=0,
),
InputSource(
amount=200,
base=0,
source="T",
origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7",
index=1,
),
InputSource(
amount=300,
base=0,
source="T",
origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7",
index=2,
),
InputSource(
amount=400,
base=0,
source="T",
origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7",
index=3,
),
InputSource(
amount=500,
base=0,
source="T",
origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7",
index=4,
),
InputSource(
amount=600,
base=0,
source="T",
origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7",
index=5,
),
InputSource(
amount=700,
base=0,
source="T",
origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7",
index=6,
),
InputSource(
amount=800,
base=0,
source="T",
origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7",
index=7,
),
InputSource(
amount=900,
base=0,
source="T",
origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7",
index=8,
),
InputSource(
amount=1000,
base=0,
source="T",
origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7",
index=9,
),
InputSource(
amount=1100,
base=0,
source="T",
origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7",
index=10,
),
InputSource(
amount=1200,
base=0,
source="T",
origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7",
index=11,
),
InputSource(
amount=1300,
base=0,
source="T",
origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7",
index=12,
),
InputSource(
amount=1400,
base=0,
source="T",
origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7",
index=13,
),
InputSource(
amount=1500,
base=0,
source="T",
origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7",
index=14,
),
InputSource(
amount=1600,
base=0,
source="T",
origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7",
index=15,
),
InputSource(
amount=1700,
base=0,
source="T",
origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7",
index=16,
),
InputSource(
amount=1800,
base=0,
source="T",
origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7",
index=17,
),
InputSource(
amount=1900,
base=0,
source="T",
origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7",
index=18,
),
InputSource(
amount=2000,
base=0,
source="T",
origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7",
index=19,
),
InputSource(
amount=2100,
base=0,
source="T",
origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7",
index=20,
),
InputSource(
amount=2200,
base=0,
source="T",
origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7",
index=21,
),
InputSource(
amount=2300,
base=0,
source="T",
origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7",
index=22,
),
InputSource(
amount=2400,
base=0,
source="T",
origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7",
index=23,
),
InputSource(
amount=2500,
base=0,
source="T",
origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7",
index=24,
),
InputSource(
amount=2600,
base=0,
source="T",
origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7",
index=25,
),
InputSource(
amount=2700,
base=0,
source="T",
origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7",
index=26,
),
InputSource(
amount=2800,
base=0,
source="T",
origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7",
index=27,
),
InputSource(
amount=2900,
base=0,
source="T",
origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7",
index=28,
),
InputSource(
amount=3000,
base=0,
source="T",
origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7",
index=29,
),
InputSource(
amount=3100,
base=0,
source="T",
origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7",
index=30,
),
InputSource(
amount=3200,
base=0,
source="T",
origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7",
index=31,
),
InputSource(
amount=3300,
base=0,
source="T",
origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7",
index=32,
),
InputSource(
amount=3400,
base=0,
source="T",
origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7",
index=33,
),
InputSource(
amount=3500,
base=0,
source="T",
origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7",
index=34,
),
InputSource(
amount=3600,
base=0,
source="T",
origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7",
index=35,
),
InputSource(
amount=3700,
base=0,
source="T",
origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7",
index=36,
),
InputSource(
amount=3800,
base=0,
source="T",
origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7",
index=37,
),
InputSource(
amount=3900,
base=0,
source="T",
origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7",
index=38,
),
InputSource(
amount=4000,
base=0,
source="T",
origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7",
index=39,
),
],
82000,
True,
),
),
],
)
@pytest.mark.asyncio
async def test_handle_intermediaries_transactions(
key,
issuers,
tx_amounts,
outputAddresses,
Comment,
OutputbackChange,
expected_listinput_amount,
monkeypatch,
):
# patched functions
patched_generate_and_send_transaction = CoroutineMock(return_value=None)
monkeypatch.setattr("silkaj.money.get_sources", patched_get_sources)
monkeypatch.setattr(
"silkaj.tx.generate_and_send_transaction", patched_generate_and_send_transaction
)
patched_get_sources.counter = 0
await handle_intermediaries_transactions(
key, issuers, tx_amounts, outputAddresses, Comment, OutputbackChange
)
# patched functions
monkeypatch.setattr("silkaj.money.get_sources", patched_get_sources)
# reset patched_get_sources counter
patched_get_sources.counter = 0
if expected_listinput_amount[2] == True:
patched_generate_and_send_transaction.assert_any_await(
key,
issuers,
[expected_listinput_amount[1],],
expected_listinput_amount,
[issuers],
"Change operation",
)
else:
patched_generate_and_send_transaction.assert_awaited_once_with(
key,
issuers,
tx_amounts,
expected_listinput_amount,
outputAddresses,
Comment,
issuers,
)
...@@ -22,3 +22,6 @@ def insecure_key(pass_salt): ...@@ -22,3 +22,6 @@ def insecure_key(pass_salt):
pubkey(1) = BUhLyJT17bzDVXW66xxfk1F7947vytmwJVadTaWb8sJS pubkey(1) = BUhLyJT17bzDVXW66xxfk1F7947vytmwJVadTaWb8sJS
""" """
return SigningKey.from_credentials(pass_salt, pass_salt) return SigningKey.from_credentials(pass_salt, pass_salt)
key_fifi = insecure_key("fifi")
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment