Add test for no-exchange dataprovider

This commit is contained in:
Matthias 2021-05-05 20:08:31 +02:00 committed by Rokas Kupstys
parent d344194b36
commit 4b6cd69c81
3 changed files with 30 additions and 6 deletions

View File

@ -19,6 +19,8 @@ from freqtrade.state import RunMode
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
NO_EXCHANGE_EXCEPTION = 'Exchange is not available to DataProvider.'
class DataProvider: class DataProvider:
@ -126,7 +128,7 @@ class DataProvider:
Refresh data, called with each cycle Refresh data, called with each cycle
""" """
if self._exchange is None: if self._exchange is None:
raise OperationalException('Exchange is not available to DataProvider.') raise OperationalException(NO_EXCHANGE_EXCEPTION)
if helping_pairs: if helping_pairs:
self._exchange.refresh_latest_ohlcv(pairlist + helping_pairs) self._exchange.refresh_latest_ohlcv(pairlist + helping_pairs)
else: else:
@ -139,7 +141,7 @@ class DataProvider:
Should be whitelist + open trades. Should be whitelist + open trades.
""" """
if self._exchange is None: if self._exchange is None:
raise OperationalException('Exchange is not available to DataProvider.') raise OperationalException(NO_EXCHANGE_EXCEPTION)
return list(self._exchange._klines.keys()) return list(self._exchange._klines.keys())
def ohlcv(self, pair: str, timeframe: str = None, copy: bool = True) -> DataFrame: def ohlcv(self, pair: str, timeframe: str = None, copy: bool = True) -> DataFrame:
@ -151,6 +153,8 @@ class DataProvider:
:param copy: copy dataframe before returning if True. :param copy: copy dataframe before returning if True.
Use False only for read-only operations (where the dataframe is not modified) Use False only for read-only operations (where the dataframe is not modified)
""" """
if self._exchange is None:
raise OperationalException(NO_EXCHANGE_EXCEPTION)
if self.runmode in (RunMode.DRY_RUN, RunMode.LIVE): if self.runmode in (RunMode.DRY_RUN, RunMode.LIVE):
return self._exchange.klines((pair, timeframe or self._config['timeframe']), return self._exchange.klines((pair, timeframe or self._config['timeframe']),
copy=copy) copy=copy)
@ -164,7 +168,7 @@ class DataProvider:
:return: Market data dict from ccxt or None if market info is not available for the pair :return: Market data dict from ccxt or None if market info is not available for the pair
""" """
if self._exchange is None: if self._exchange is None:
raise OperationalException('Exchange is not available to DataProvider.') raise OperationalException(NO_EXCHANGE_EXCEPTION)
return self._exchange.markets.get(pair) return self._exchange.markets.get(pair)
def ticker(self, pair: str): def ticker(self, pair: str):
@ -174,7 +178,7 @@ class DataProvider:
:return: Ticker dict from exchange or empty dict if ticker is not available for the pair :return: Ticker dict from exchange or empty dict if ticker is not available for the pair
""" """
if self._exchange is None: if self._exchange is None:
raise OperationalException('Exchange is not available to DataProvider.') raise OperationalException(NO_EXCHANGE_EXCEPTION)
try: try:
return self._exchange.fetch_ticker(pair) return self._exchange.fetch_ticker(pair)
except ExchangeError: except ExchangeError:
@ -189,5 +193,5 @@ class DataProvider:
:return: dict including bids/asks with a total of `maximum` entries. :return: dict including bids/asks with a total of `maximum` entries.
""" """
if self._exchange is None: if self._exchange is None:
raise OperationalException('Exchange is not available to DataProvider.') raise OperationalException(NO_EXCHANGE_EXCEPTION)
return self._exchange.fetch_l2_order_book(pair, maximum) return self._exchange.fetch_l2_order_book(pair, maximum)

View File

@ -31,7 +31,6 @@ from freqtrade.optimize.hyperopt_loss_interface import IHyperOptLoss # noqa: F4
from freqtrade.optimize.hyperopt_tools import HyperoptTools from freqtrade.optimize.hyperopt_tools import HyperoptTools
from freqtrade.optimize.optimize_reports import generate_strategy_stats from freqtrade.optimize.optimize_reports import generate_strategy_stats
from freqtrade.resolvers.hyperopt_resolver import HyperOptLossResolver, HyperOptResolver from freqtrade.resolvers.hyperopt_resolver import HyperOptLossResolver, HyperOptResolver
from freqtrade.strategy import IStrategy
# Suppress scikit-learn FutureWarnings from skopt # Suppress scikit-learn FutureWarnings from skopt

View File

@ -246,3 +246,24 @@ def test_get_analyzed_dataframe(mocker, default_conf, ohlcv_history):
assert dataframe.empty assert dataframe.empty
assert isinstance(time, datetime) assert isinstance(time, datetime)
assert time == datetime(1970, 1, 1, tzinfo=timezone.utc) assert time == datetime(1970, 1, 1, tzinfo=timezone.utc)
def test_no_exchange_mode(default_conf):
dp = DataProvider(default_conf, None)
message = "Exchange is not available to DataProvider."
with pytest.raises(OperationalException, match=message):
dp.refresh([()])
with pytest.raises(OperationalException, match=message):
dp.ohlcv('XRP/USDT', '5m')
with pytest.raises(OperationalException, match=message):
dp.market('XRP/USDT')
with pytest.raises(OperationalException, match=message):
dp.ticker('XRP/USDT')
with pytest.raises(OperationalException, match=message):
dp.orderbook('XRP/USDT', 20)