test ticker caching

This commit is contained in:
xmatthias 2018-06-06 20:24:47 +02:00
parent e690003621
commit a901f21bcd
2 changed files with 11 additions and 0 deletions

View File

@ -275,6 +275,7 @@ def get_ticker(pair: str, refresh: Optional[bool] = True) -> dict:
try:
_CACHED_TICKER[pair] = {
'bid': float(data['bid']),
'ask': float(data['ask']),
}
except KeyError as e:
logger.debug("Could not cache ticker data for %s", pair)

View File

@ -310,9 +310,19 @@ def test_get_ticker(default_conf, mocker):
# if not fetching a new result we should get the cached ticker
ticker = get_ticker(pair='ETH/BTC')
assert api_mock.fetch_ticker.call_count == 1
assert ticker['bid'] == 0.5
assert ticker['ask'] == 1
assert 'ETH/BTC' in exchange._CACHED_TICKER
assert exchange._CACHED_TICKER['ETH/BTC']['bid'] == 0.5
assert exchange._CACHED_TICKER['ETH/BTC']['ask'] == 1
# Test caching
api_mock.fetch_ticker = MagicMock()
get_ticker(pair='ETH/BTC', refresh=False)
assert api_mock.fetch_ticker.call_count == 0
with pytest.raises(TemporaryError): # test retrier
api_mock.fetch_ticker = MagicMock(side_effect=ccxt.NetworkError)
mocker.patch('freqtrade.exchange._API', api_mock)