2018-01-28 01:33:04 +00:00
|
|
|
# pragma pylint: disable=missing-docstring, too-many-arguments, too-many-ancestors,
|
|
|
|
# pragma pylint: disable=protected-access, C0103
|
2017-12-25 07:51:41 +00:00
|
|
|
|
|
|
|
import time
|
|
|
|
from unittest.mock import MagicMock
|
|
|
|
|
2018-01-10 07:51:36 +00:00
|
|
|
import pytest
|
2018-05-21 18:03:25 +00:00
|
|
|
from requests.exceptions import RequestException
|
|
|
|
|
2018-12-11 19:26:53 +00:00
|
|
|
from freqtrade.rpc.fiat_convert import CryptoFiat, CryptoToFiatConverter
|
2020-03-07 10:52:26 +00:00
|
|
|
from tests.conftest import log_has, log_has_re
|
2017-12-25 07:51:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_pair_convertion_object():
|
|
|
|
pair_convertion = CryptoFiat(
|
|
|
|
crypto_symbol='btc',
|
|
|
|
fiat_symbol='usd',
|
|
|
|
price=12345.0
|
|
|
|
)
|
|
|
|
|
|
|
|
# Check the cache duration is 6 hours
|
|
|
|
assert pair_convertion.CACHE_DURATION == 6 * 60 * 60
|
|
|
|
|
|
|
|
# Check a regular usage
|
2020-03-07 10:52:26 +00:00
|
|
|
assert pair_convertion.crypto_symbol == 'btc'
|
|
|
|
assert pair_convertion.fiat_symbol == 'usd'
|
2017-12-25 07:51:41 +00:00
|
|
|
assert pair_convertion.price == 12345.0
|
|
|
|
assert pair_convertion.is_expired() is False
|
|
|
|
|
|
|
|
# Update the expiration time (- 2 hours) and check the behavior
|
|
|
|
pair_convertion._expiration = time.time() - 2 * 60 * 60
|
|
|
|
assert pair_convertion.is_expired() is True
|
|
|
|
|
|
|
|
# Check set price behaviour
|
|
|
|
time_reference = time.time() + pair_convertion.CACHE_DURATION
|
|
|
|
pair_convertion.set_price(price=30000.123)
|
|
|
|
assert pair_convertion.is_expired() is False
|
|
|
|
assert pair_convertion._expiration >= time_reference
|
|
|
|
assert pair_convertion.price == 30000.123
|
|
|
|
|
|
|
|
|
2018-06-16 11:42:25 +00:00
|
|
|
def test_fiat_convert_is_supported(mocker):
|
2017-12-25 07:51:41 +00:00
|
|
|
fiat_convert = CryptoToFiatConverter()
|
|
|
|
assert fiat_convert._is_supported_fiat(fiat='USD') is True
|
|
|
|
assert fiat_convert._is_supported_fiat(fiat='usd') is True
|
|
|
|
assert fiat_convert._is_supported_fiat(fiat='abc') is False
|
|
|
|
assert fiat_convert._is_supported_fiat(fiat='ABC') is False
|
|
|
|
|
|
|
|
|
2018-06-16 11:42:25 +00:00
|
|
|
def test_fiat_convert_add_pair(mocker):
|
|
|
|
|
2017-12-25 07:51:41 +00:00
|
|
|
fiat_convert = CryptoToFiatConverter()
|
|
|
|
|
2018-01-28 01:33:04 +00:00
|
|
|
pair_len = len(fiat_convert._pairs)
|
|
|
|
assert pair_len == 0
|
2017-12-25 07:51:41 +00:00
|
|
|
|
|
|
|
fiat_convert._add_pair(crypto_symbol='btc', fiat_symbol='usd', price=12345.0)
|
2018-01-28 01:33:04 +00:00
|
|
|
pair_len = len(fiat_convert._pairs)
|
|
|
|
assert pair_len == 1
|
2020-03-07 10:52:26 +00:00
|
|
|
assert fiat_convert._pairs[0].crypto_symbol == 'btc'
|
|
|
|
assert fiat_convert._pairs[0].fiat_symbol == 'usd'
|
2017-12-25 07:51:41 +00:00
|
|
|
assert fiat_convert._pairs[0].price == 12345.0
|
|
|
|
|
|
|
|
fiat_convert._add_pair(crypto_symbol='btc', fiat_symbol='Eur', price=13000.2)
|
2018-01-28 01:33:04 +00:00
|
|
|
pair_len = len(fiat_convert._pairs)
|
|
|
|
assert pair_len == 2
|
2020-03-07 10:52:26 +00:00
|
|
|
assert fiat_convert._pairs[1].crypto_symbol == 'btc'
|
|
|
|
assert fiat_convert._pairs[1].fiat_symbol == 'eur'
|
2017-12-25 07:51:41 +00:00
|
|
|
assert fiat_convert._pairs[1].price == 13000.2
|
|
|
|
|
|
|
|
|
|
|
|
def test_fiat_convert_find_price(mocker):
|
|
|
|
fiat_convert = CryptoToFiatConverter()
|
|
|
|
|
|
|
|
with pytest.raises(ValueError, match=r'The fiat ABC is not supported.'):
|
|
|
|
fiat_convert._find_price(crypto_symbol='BTC', fiat_symbol='ABC')
|
|
|
|
|
2018-04-21 21:20:12 +00:00
|
|
|
assert fiat_convert.get_price(crypto_symbol='XRP', fiat_symbol='USD') == 0.0
|
2018-03-17 23:42:24 +00:00
|
|
|
|
2018-12-11 19:26:53 +00:00
|
|
|
mocker.patch('freqtrade.rpc.fiat_convert.CryptoToFiatConverter._find_price',
|
|
|
|
return_value=12345.0)
|
2017-12-25 07:51:41 +00:00
|
|
|
assert fiat_convert.get_price(crypto_symbol='BTC', fiat_symbol='USD') == 12345.0
|
|
|
|
assert fiat_convert.get_price(crypto_symbol='btc', fiat_symbol='usd') == 12345.0
|
2018-01-07 04:07:40 +00:00
|
|
|
|
2018-12-11 19:26:53 +00:00
|
|
|
mocker.patch('freqtrade.rpc.fiat_convert.CryptoToFiatConverter._find_price',
|
|
|
|
return_value=13000.2)
|
2017-12-25 07:51:41 +00:00
|
|
|
assert fiat_convert.get_price(crypto_symbol='BTC', fiat_symbol='EUR') == 13000.2
|
|
|
|
|
|
|
|
|
2018-06-02 22:29:29 +00:00
|
|
|
def test_fiat_convert_unsupported_crypto(mocker, caplog):
|
2018-12-11 19:26:53 +00:00
|
|
|
mocker.patch('freqtrade.rpc.fiat_convert.CryptoToFiatConverter._cryptomap', return_value=[])
|
2018-06-02 22:29:29 +00:00
|
|
|
fiat_convert = CryptoToFiatConverter()
|
|
|
|
assert fiat_convert._find_price(crypto_symbol='CRYPTO_123', fiat_symbol='EUR') == 0.0
|
2019-08-11 18:17:22 +00:00
|
|
|
assert log_has('unsupported crypto-symbol CRYPTO_123 - returning 0.0', caplog)
|
2018-06-02 22:29:29 +00:00
|
|
|
|
|
|
|
|
2017-12-25 07:51:41 +00:00
|
|
|
def test_fiat_convert_get_price(mocker):
|
2018-12-11 19:26:53 +00:00
|
|
|
mocker.patch('freqtrade.rpc.fiat_convert.CryptoToFiatConverter._find_price',
|
|
|
|
return_value=28000.0)
|
2017-12-25 07:51:41 +00:00
|
|
|
|
|
|
|
fiat_convert = CryptoToFiatConverter()
|
|
|
|
|
2020-03-07 10:52:26 +00:00
|
|
|
with pytest.raises(ValueError, match=r'The fiat us dollar is not supported.'):
|
|
|
|
fiat_convert.get_price(crypto_symbol='btc', fiat_symbol='US Dollar')
|
2017-12-25 07:51:41 +00:00
|
|
|
|
|
|
|
# Check the value return by the method
|
2018-01-28 01:33:04 +00:00
|
|
|
pair_len = len(fiat_convert._pairs)
|
|
|
|
assert pair_len == 0
|
2020-03-07 10:52:26 +00:00
|
|
|
assert fiat_convert.get_price(crypto_symbol='btc', fiat_symbol='usd') == 28000.0
|
|
|
|
assert fiat_convert._pairs[0].crypto_symbol == 'btc'
|
|
|
|
assert fiat_convert._pairs[0].fiat_symbol == 'usd'
|
2017-12-25 07:51:41 +00:00
|
|
|
assert fiat_convert._pairs[0].price == 28000.0
|
2019-01-31 05:51:03 +00:00
|
|
|
assert fiat_convert._pairs[0]._expiration != 0
|
2017-12-25 07:51:41 +00:00
|
|
|
assert len(fiat_convert._pairs) == 1
|
|
|
|
|
|
|
|
# Verify the cached is used
|
|
|
|
fiat_convert._pairs[0].price = 9867.543
|
|
|
|
expiration = fiat_convert._pairs[0]._expiration
|
2020-03-07 10:52:26 +00:00
|
|
|
assert fiat_convert.get_price(crypto_symbol='btc', fiat_symbol='usd') == 9867.543
|
2017-12-25 07:51:41 +00:00
|
|
|
assert fiat_convert._pairs[0]._expiration == expiration
|
|
|
|
|
|
|
|
# Verify the cache expiration
|
|
|
|
expiration = time.time() - 2 * 60 * 60
|
|
|
|
fiat_convert._pairs[0]._expiration = expiration
|
2020-03-07 10:52:26 +00:00
|
|
|
assert fiat_convert.get_price(crypto_symbol='btc', fiat_symbol='usd') == 28000.0
|
2017-12-25 07:51:41 +00:00
|
|
|
assert fiat_convert._pairs[0]._expiration is not expiration
|
2018-01-07 04:07:40 +00:00
|
|
|
|
|
|
|
|
2018-06-02 03:58:07 +00:00
|
|
|
def test_fiat_convert_same_currencies(mocker):
|
|
|
|
fiat_convert = CryptoToFiatConverter()
|
|
|
|
|
|
|
|
assert fiat_convert.get_price(crypto_symbol='USD', fiat_symbol='USD') == 1.0
|
|
|
|
|
|
|
|
|
2018-06-02 20:26:24 +00:00
|
|
|
def test_fiat_convert_two_FIAT(mocker):
|
|
|
|
fiat_convert = CryptoToFiatConverter()
|
|
|
|
|
|
|
|
assert fiat_convert.get_price(crypto_symbol='USD', fiat_symbol='EUR') == 0.0
|
|
|
|
|
|
|
|
|
2018-05-13 18:46:02 +00:00
|
|
|
def test_loadcryptomap(mocker):
|
|
|
|
|
|
|
|
fiat_convert = CryptoToFiatConverter()
|
|
|
|
assert len(fiat_convert._cryptomap) == 2
|
|
|
|
|
2020-03-07 10:52:26 +00:00
|
|
|
assert fiat_convert._cryptomap["btc"] == "bitcoin"
|
2018-05-13 18:46:02 +00:00
|
|
|
|
|
|
|
|
2018-05-21 18:03:25 +00:00
|
|
|
def test_fiat_init_network_exception(mocker):
|
|
|
|
# Because CryptoToFiatConverter is a Singleton we reset the listings
|
|
|
|
listmock = MagicMock(side_effect=RequestException)
|
|
|
|
mocker.patch.multiple(
|
2020-03-07 10:52:26 +00:00
|
|
|
'freqtrade.rpc.fiat_convert.CoinGeckoAPI',
|
|
|
|
get_coins_list=listmock,
|
2018-05-21 18:03:25 +00:00
|
|
|
)
|
|
|
|
# with pytest.raises(RequestEsxception):
|
|
|
|
fiat_convert = CryptoToFiatConverter()
|
|
|
|
fiat_convert._cryptomap = {}
|
|
|
|
fiat_convert._load_cryptomap()
|
|
|
|
|
2018-06-02 22:12:02 +00:00
|
|
|
length_cryptomap = len(fiat_convert._cryptomap)
|
|
|
|
assert length_cryptomap == 0
|
2018-05-21 18:03:25 +00:00
|
|
|
|
|
|
|
|
2018-06-16 11:42:25 +00:00
|
|
|
def test_fiat_convert_without_network(mocker):
|
2020-03-07 10:52:26 +00:00
|
|
|
# Because CryptoToFiatConverter is a Singleton we reset the value of _coingekko
|
2018-01-07 04:07:40 +00:00
|
|
|
|
|
|
|
fiat_convert = CryptoToFiatConverter()
|
2018-04-07 18:06:53 +00:00
|
|
|
|
2020-03-07 10:52:26 +00:00
|
|
|
cmc_temp = CryptoToFiatConverter._coingekko
|
|
|
|
CryptoToFiatConverter._coingekko = None
|
2018-04-07 18:06:53 +00:00
|
|
|
|
2020-03-07 10:52:26 +00:00
|
|
|
assert fiat_convert._coingekko is None
|
|
|
|
assert fiat_convert._find_price(crypto_symbol='btc', fiat_symbol='usd') == 0.0
|
|
|
|
CryptoToFiatConverter._coingekko = cmc_temp
|
2018-06-02 22:12:02 +00:00
|
|
|
|
|
|
|
|
2018-07-21 17:50:38 +00:00
|
|
|
def test_fiat_invalid_response(mocker, caplog):
|
|
|
|
# Because CryptoToFiatConverter is a Singleton we reset the listings
|
|
|
|
listmock = MagicMock(return_value="{'novalidjson':DEADBEEFf}")
|
|
|
|
mocker.patch.multiple(
|
2020-03-07 10:52:26 +00:00
|
|
|
'freqtrade.rpc.fiat_convert.CoinGeckoAPI',
|
|
|
|
get_coins_list=listmock,
|
2018-07-21 17:50:38 +00:00
|
|
|
)
|
|
|
|
# with pytest.raises(RequestEsxception):
|
|
|
|
fiat_convert = CryptoToFiatConverter()
|
|
|
|
fiat_convert._cryptomap = {}
|
|
|
|
fiat_convert._load_cryptomap()
|
|
|
|
|
|
|
|
length_cryptomap = len(fiat_convert._cryptomap)
|
|
|
|
assert length_cryptomap == 0
|
2020-03-07 10:52:26 +00:00
|
|
|
assert log_has_re('Could not load FIAT Cryptocurrency map for the following problem: .*',
|
|
|
|
caplog)
|
2018-07-21 17:50:38 +00:00
|
|
|
|
|
|
|
|
2018-06-02 22:12:02 +00:00
|
|
|
def test_convert_amount(mocker):
|
2018-12-11 19:26:53 +00:00
|
|
|
mocker.patch('freqtrade.rpc.fiat_convert.CryptoToFiatConverter.get_price', return_value=12345.0)
|
2018-06-02 22:12:02 +00:00
|
|
|
|
|
|
|
fiat_convert = CryptoToFiatConverter()
|
|
|
|
result = fiat_convert.convert_amount(
|
|
|
|
crypto_amount=1.23,
|
|
|
|
crypto_symbol="BTC",
|
|
|
|
fiat_symbol="USD"
|
|
|
|
)
|
|
|
|
assert result == 15184.35
|
|
|
|
|
|
|
|
result = fiat_convert.convert_amount(
|
|
|
|
crypto_amount=1.23,
|
|
|
|
crypto_symbol="BTC",
|
|
|
|
fiat_symbol="BTC"
|
|
|
|
)
|
|
|
|
assert result == 1.23
|
2019-09-09 17:59:41 +00:00
|
|
|
|
|
|
|
result = fiat_convert.convert_amount(
|
|
|
|
crypto_amount="1.23",
|
|
|
|
crypto_symbol="BTC",
|
|
|
|
fiat_symbol="BTC"
|
|
|
|
)
|
|
|
|
assert result == 1.23
|