1
0
Fork 0

Parametrize Flexpool tests

Signed-off-by: Julien Riou <julien@riou.xyz>
This commit is contained in:
Julien Riou 2021-02-16 09:29:55 +01:00
parent 4cd278c60e
commit 9d75604869
No known key found for this signature in database
GPG key ID: FF42D23B580C89F7

View file

@ -1,5 +1,6 @@
from datetime import datetime, timedelta
import pytest
from companion.pools.flexpool import FlexpoolHandler, Transaction
@ -8,113 +9,58 @@ class TestFlexpoolHandler:
handler = FlexpoolHandler()
assert handler.pool_name == 'flexpool'
def test_new_balance_with_notification(self, mocker):
"""Old balance is 1, new balance is 2, should send notification"""
notifier = mocker.Mock()
notifier.notify_balance = mocker.Mock()
handler = FlexpoolHandler(notifier=notifier)
miner = mocker.patch('flexpoolapi.miner')
miner().balance.return_value = 2
mocker.patch('companion.pools.flexpool.FlexpoolHandler._watch_miner_payments')
mocker.patch('companion.pools.flexpool.Miner.get_payements')
last_balance, last_transaction = handler.watch_miner(address='addr', last_balance=1)
assert last_balance == 2
notifier.notify_balance.assert_called_once()
def test_new_balance_after_payment_with_notification(self, mocker):
"""Old balance is 0, new balance is 1 (old > new), should send notification"""
notifier = mocker.Mock()
notifier.notify_balance = mocker.Mock()
handler = FlexpoolHandler(notifier=notifier)
miner = mocker.patch('flexpoolapi.miner')
miner().balance.return_value = 0
mocker.patch('companion.pools.flexpool.FlexpoolHandler._watch_miner_payments')
mocker.patch('companion.pools.flexpool.Miner.get_payements')
last_balance, last_transaction = handler.watch_miner(address='addr', last_balance=1)
assert last_balance == 0
notifier.notify_balance.assert_called_once()
def test_very_new_balance_with_notification(self, mocker):
"""Old balance doesn't exist, new balance is 1, should send notification"""
notifier = mocker.Mock()
notifier.notify_balance = mocker.Mock()
handler = FlexpoolHandler(notifier=notifier)
miner = mocker.patch('flexpoolapi.miner')
miner().balance.return_value = 1
mocker.patch('companion.pools.flexpool.FlexpoolHandler._watch_miner_payments')
mocker.patch('companion.pools.flexpool.Miner.get_payements')
last_balance, last_transaction = handler.watch_miner(address='addr')
assert last_balance == 1
notifier.notify_balance.assert_called_once()
def test_same_balance_without_notification(self, mocker):
"""Old balance and new balance are the same, should not send notification"""
notifier = mocker.Mock()
notifier.notify_balance = mocker.Mock()
handler = FlexpoolHandler(notifier=notifier)
miner = mocker.patch('flexpoolapi.miner')
miner().balance.return_value = 1
mocker.patch('companion.pools.flexpool.FlexpoolHandler._watch_miner_payments')
mocker.patch('companion.pools.flexpool.Miner.get_payements')
last_balance, last_transaction = handler.watch_miner(address='addr', last_balance=1)
assert last_balance == 1
notifier.notify_balance.assert_not_called()
def test_new_payment_with_notification(self, mocker):
"""One transaction saved (trx1), two transactions detected (trx1, trx2), should send notification"""
notifier = mocker.Mock()
notifier.notify_payment = mocker.Mock()
handler = FlexpoolHandler(notifier=notifier)
mocker.patch('flexpoolapi.miner')
mocker.patch('companion.pools.flexpool.FlexpoolHandler._watch_miner_balance')
get_payements = mocker.patch('companion.pools.flexpool.Miner.get_payements')
get_payements.return_value = [
Transaction(txid='trx1', amount=1, time=datetime.now(), duration=timedelta(minutes=1)),
Transaction(txid='trx2', amount=1, time=datetime.now(), duration=timedelta(minutes=1))
@pytest.mark.parametrize(
'old_balance,new_balance,should_notify',
[
pytest.param(1, 2, True, id='new_balance_with_notification'),
pytest.param(1, 0, True, id='new_balance_after_payment_with_notification'),
pytest.param(None, 1, True, id='very_new_balance_with_notification'),
pytest.param(1, 1, False, id='same_balance_without_notification'),
]
last_balance, last_transaction = handler.watch_miner(address='addr', last_transaction='trx1')
assert last_transaction == 'trx2'
notifier.notify_payment.assert_called_once()
def test_very_new_payment_with_notification(self, mocker):
"""No transaction saved, one transaction detected (trx1), should send notification"""
)
def test_balance(self, mocker, old_balance, new_balance, should_notify):
notifier = mocker.Mock()
notifier.notify_payment = mocker.Mock()
notifier.notify_balance = mocker.Mock()
handler = FlexpoolHandler(notifier=notifier)
mocker.patch('flexpoolapi.miner')
mocker.patch('companion.pools.flexpool.FlexpoolHandler._watch_miner_balance')
get_payements = mocker.patch('companion.pools.flexpool.Miner.get_payements')
get_payements.return_value = [
Transaction(txid='trx1', amount=1, time=datetime.now(), duration=timedelta(minutes=1))
miner = mocker.patch('flexpoolapi.miner')
miner().balance.return_value = new_balance
mocker.patch('companion.pools.flexpool.FlexpoolHandler._watch_miner_payments')
mocker.patch('companion.pools.flexpool.Miner.get_payements')
last_balance, last_transaction = handler.watch_miner(address='addr', last_balance=old_balance)
assert last_balance == new_balance
if should_notify:
notifier.notify_balance.assert_called_once()
else:
notifier.notify_balance.assert_not_called()
@staticmethod
def _create_transactions(names):
if names:
return [Transaction(txid=n, amount=1, time=datetime.now(), duration=timedelta(minutes=1)) for n in names]
@pytest.mark.parametrize(
'old_transaction,new_transactions,should_notify',
[
pytest.param('trx1', ['trx1', 'trx2'], True, id='new_payment_with_notification'),
pytest.param(None, ['trx1'], True, id='very_new_payment_with_notification'),
pytest.param('trx1', ['trx1'], False, id='same_payment_without_notification'),
pytest.param(None, None, False, id='zero_payment_without_notification'),
]
last_balance, last_transaction = handler.watch_miner(address='addr', last_transaction=None)
assert last_transaction == 'trx1'
notifier.notify_payment.assert_called_once()
def test_same_payment_without_notification(self, mocker):
"""One transaction saved (trx1), one transaction detected (trx1), should not send notification"""
)
def test_payments(self, mocker, old_transaction, new_transactions, should_notify):
notifier = mocker.Mock()
notifier.notify_payment = mocker.Mock()
handler = FlexpoolHandler(notifier=notifier)
mocker.patch('flexpoolapi.miner')
mocker.patch('companion.pools.flexpool.FlexpoolHandler._watch_miner_balance')
get_payements = mocker.patch('companion.pools.flexpool.Miner.get_payements')
get_payements.return_value = [
Transaction(txid='trx1', amount=1, time=datetime.now(), duration=timedelta(minutes=1))
]
last_balance, last_transaction = handler.watch_miner(address='addr', last_transaction='trx1')
assert last_transaction == 'trx1'
notifier.notify_payment.assert_not_called()
def test_zero_payment_without_notification(self, mocker):
"""Zero transaction saved, zero transaction detected, should not send notification"""
notifier = mocker.Mock()
notifier.notify_payment = mocker.Mock()
handler = FlexpoolHandler(notifier=notifier)
mocker.patch('flexpoolapi.miner')
mocker.patch('companion.pools.flexpool.FlexpoolHandler._watch_miner_balance')
get_payements = mocker.patch('companion.pools.flexpool.Miner.get_payements')
get_payements.return_value = []
last_balance, last_transaction = handler.watch_miner(address='addr', last_transaction=None)
assert last_transaction is None
notifier.notify_payment.assert_not_called()
get_payements.return_value = self._create_transactions(new_transactions)
last_balance, last_transaction = handler.watch_miner(address='addr', last_transaction=old_transaction)
if new_transactions:
assert last_transaction == new_transactions[-1]
else:
assert last_transaction is None
if should_notify:
notifier.notify_payment.assert_called_once()
else:
notifier.notify_payment.assert_not_called()