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

[test] #282: split patched.py

* create patched directory
* create files matching the silkaj modules
    * money.py
    * tools.py
    * wot.py
    * blockchain_tools.py
* delete patched.py
* change calls in the tests
parent 910f8b59
No related branches found
No related tags found
No related merge requests found
## mock head_block()
async def patched_head_block(self):
mocked_head_block = {
"number": 48000,
"unitbase": 0,
"currency": "g1",
"hash": "0000010D30B1284D34123E036B7BE0A449AE9F2B928A77D7D20E3BDEAC7EE14C",
}
return mocked_head_block
......@@ -33,51 +33,23 @@ pubkey_list = [
{"pubkey": "7Hr6oUxE6nGZxFG7gVbpMK6oUkNTh5eU686EiCXWCrBF", "uid": "loulou"},
]
#### Patched functions ####
## testing tx.py ##
# mock UDValue
async def ud_value(self):
async def patched_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
## mock head_block()
async def head_block(self):
mocked_head_block = {
"number": 48000,
"unitbase": 0,
"currency": "g1",
"hash": "0000010D30B1284D34123E036B7BE0A449AE9F2B928A77D7D20E3BDEAC7EE14C",
}
return mocked_head_block
# mock get_sources()
async def get_sources(pubkey):
async def patched_get_sources(pubkey):
"""
Returns transaction sources.
This function does not cover all possibilities : no other unlock conditions than SIG(pubkey).
if pubkey == DBM6F5ChMJzpmkUdL5zD9UXKExmZGfQ1AgPDQy4MxSBw : 3 TXsources, amount = 600
if pubkey == 4szFkvQ5tzzhwcfUtZD32hdoG2ZzhvG3ZtfR61yjnxdw : 53 TXsources, amount = 143100
if pubkey == BFb5yv8z1fowR6Z8mBXTALy5z7gHfMU976WtXhmRsUMh : 10 UDsources, amount = 3140
if pubkey == C1oAV9FX2y9iz2sdp7kZBFu3EBNAa6UkrrRG3EwouPeH : 50 UDsources and 20 TXsources, amount = 36700
This function doesn't cover all possibilities : only SIG() unlock condition.
for pubkey DBM6F5ChMJzpmkUdL5zD9UXKExmZGfQ1AgPDQy4MxSBw : 3 TX, amount = 600
for pubkey 4szFkvQ5tzzhwcfUtZD32hdoG2ZzhvG3ZtfR61yjnxdw : 53 TX, amount = 143100
for pubkey BFb5yv8z1fowR6Z8mBXTALy5z7gHfMU976WtXhmRsUMh : 10 UD, amount = 3140
for pubkey C1oAV9FX2y9iz2sdp7kZBFu3EBNAa6UkrrRG3EwouPeH : 50 UD and 20 TX, amount = 36700
else : 0 sources, amount = 0
For convenience, the hash is always the same. This should change for other testing purposes.
Same hash for each TX for convenience. This may change for other testing purposes.
"""
def listinput_UD(listinput, amount, pubkey, max_ud, total):
......
from silkaj.constants import G1_SYMBOL
# mock CurrencySymbol().symbol
async def patched_currency_symbol(self):
return G1_SYMBOL
pubkey_list = [
{"pubkey": "DBM6F5ChMJzpmkUdL5zD9UXKExmZGfQ1AgPDQy4MxSBw", "uid": ""},
{"pubkey": "4szFkvQ5tzzhwcfUtZD32hdoG2ZzhvG3ZtfR61yjnxdw", "uid": ""},
{"pubkey": "BFb5yv8z1fowR6Z8mBXTALy5z7gHfMU976WtXhmRsUMh", "uid": "riri"},
{"pubkey": "C1oAV9FX2y9iz2sdp7kZBFu3EBNAa6UkrrRG3EwouPeH", "uid": "fifi"},
{"pubkey": "7Hr6oUxE6nGZxFG7gVbpMK6oUkNTh5eU686EiCXWCrBF", "uid": "loulou"},
]
# mock is_member
async def patched_is_member(pubkey):
for account in pubkey_list:
if account["pubkey"] == pubkey:
if account["uid"]:
return account
return False
......@@ -27,7 +27,7 @@ from duniterpy.documents import Membership, block_uid
from duniterpy.api import bma
from duniterpy.key import SigningKey
import patched
from patched.blockchain_tools import patched_head_block
from silkaj import auth, wot
from silkaj.cli import cli
from silkaj.network_tools import ClientInstance
......@@ -133,7 +133,7 @@ async def patched_wot_requirements_no_pending(pubkey, identity_uid):
def test_membership_cmd(dry_run, confirmation, exit_code, monkeypatch):
# Monkeypatch and Mock
monkeypatch.setattr(auth, "auth_method", patched_auth_method)
monkeypatch.setattr(HeadBlock, "get_head", patched.head_block)
monkeypatch.setattr(HeadBlock, "get_head", patched_head_block)
monkeypatch.setattr(wot, "choose_identity", patched_choose_identity)
patched_display_confirmation_table = AsyncMock()
......
......@@ -2,14 +2,15 @@ import pytest
from silkaj.tui import display_pubkey, display_amount
from silkaj.constants import G1_SYMBOL
import patched
from patched.wot import patched_is_member
from patched.money import mock_ud_value
# display_amount()
@pytest.mark.parametrize(
"message, amount, currency_symbol", [("Total", 1000, G1_SYMBOL)]
)
def test_display_amount(message, amount, currency_symbol, monkeypatch):
ud_value = patched.mock_ud_value
ud_value = mock_ud_value
amount_UD = round(amount / ud_value, 2)
expected = [
[
......@@ -38,7 +39,7 @@ def test_display_amount(message, amount, currency_symbol, monkeypatch):
)
@pytest.mark.asyncio
async def test_display_pubkey(message, pubkey, id, monkeypatch):
monkeypatch.setattr("silkaj.wot.is_member", patched.is_member)
monkeypatch.setattr("silkaj.wot.is_member", patched_is_member)
expected = [[message + " (pubkey)", pubkey]]
if id:
......
......@@ -22,7 +22,8 @@ from silkaj.tx import transaction_amount
from silkaj.money import UDValue
from silkaj.cli import cli
from silkaj.constants import MINIMAL_TX_AMOUNT, FAILURE_EXIT_STATUS
import patched
from patched.money import mock_ud_value, patched_ud_value
@pytest.mark.asyncio
......@@ -30,8 +31,8 @@ async def test_transaction_amount(monkeypatch):
"""test passed amounts passed tx command
float ≠ 100 does not give the exact value"""
monkeypatch.setattr(UDValue, "get_ud_value", patched.ud_value)
udvalue = patched.mock_ud_value
monkeypatch.setattr(UDValue, "get_ud_value", patched_ud_value)
udvalue = mock_ud_value
trials = (
# tests for --amount (unit)
([141.89], None, ["A"], [14189]),
......
......@@ -36,7 +36,10 @@ from duniterpy.documents.transaction import (
)
from duniterpy.documents.block_uid import BlockUID
import patched
from patched.wot import patched_is_member
from patched.money import patched_get_sources, patched_ud_value, mock_ud_value
from patched.tools import patched_currency_symbol
from patched.blockchain_tools import patched_head_block
# truncBase()
......@@ -112,14 +115,14 @@ async def test_transaction_confirmation(
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.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
"silkaj.tools.CurrencySymbol.get_symbol", patched_currency_symbol
)
# creating expected list
ud_value = await UDValue().ud_value
ud_value = mock_ud_value
expected = list()
total_tx_amount = sum(tx_amounts)
# display account situation
......@@ -271,8 +274,8 @@ async def test_transaction_amount(
amounts, UDs_amounts, outputAddresses, expected, capsys, monkeypatch
):
# patched functions
monkeypatch.setattr("silkaj.money.UDValue.get_ud_value", patched.ud_value)
udvalue = patched.mock_ud_value
monkeypatch.setattr("silkaj.money.UDValue.get_ud_value", patched_ud_value)
udvalue = mock_ud_value
def too_little_amount(amounts, multiplicator):
for amount in amounts:
......@@ -408,7 +411,7 @@ async def test_generate_transaction_document(
):
# patch Head_block
monkeypatch.setattr(
"silkaj.blockchain_tools.HeadBlock.get_head", patched.head_block
"silkaj.blockchain_tools.HeadBlock.get_head", patched_head_block
)
assert result == await generate_transaction_document(
......@@ -455,7 +458,7 @@ async def test_get_list_input_for_transaction(
"""
# patched functions
monkeypatch.setattr("silkaj.money.get_sources", patched.get_sources)
monkeypatch.setattr("silkaj.money.get_sources", patched_get_sources)
# testing error exit
if isinstance(expected, str):
with pytest.raises(SystemExit) as pytest_exit:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment