From c16f231beee110496650a5997ca2b720d53b7ae3 Mon Sep 17 00:00:00 2001 From: Sam Germain Date: Thu, 2 Sep 2021 00:53:20 -0600 Subject: [PATCH] Added check method, fails because of magic mock i think --- freqtrade/exchange/exchange.py | 12 +++++++++++- tests/exchange/test_exchange.py | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/freqtrade/exchange/exchange.py b/freqtrade/exchange/exchange.py index ecf3302d8..4e01393b8 100644 --- a/freqtrade/exchange/exchange.py +++ b/freqtrade/exchange/exchange.py @@ -228,6 +228,16 @@ class Exchange: """exchange ccxt precisionMode""" return self._api.precisionMode + def throw_exception_if_method_not_on_exchange(self, method_name: str): + """ + Throws exception if method is implemented by the current exchange on ccxt + :param method_name: The method that may/may not exist for this exchange class + """ + # api_method = hasattr(self._api, method_name, None) + if not self._api.describe()['has'][method_name]: + raise OperationalException( + f"{method_name}() has not been implemented on ccxt.{self.name}") + def _log_exchange_response(self, endpoint, response) -> None: """ Log exchange responses """ if self.log_responses: @@ -361,7 +371,7 @@ class Exchange: raise OperationalException( 'Could not load markets, therefore cannot start. ' 'Please investigate the above error for more details.' - ) + ) quote_currencies = self.get_quote_currencies() if stake_currency not in quote_currencies: raise OperationalException( diff --git a/tests/exchange/test_exchange.py b/tests/exchange/test_exchange.py index 42da5dddc..8cd28ebb5 100644 --- a/tests/exchange/test_exchange.py +++ b/tests/exchange/test_exchange.py @@ -2926,3 +2926,36 @@ def test_calculate_fee_rate(mocker, default_conf, order, expected) -> None: ]) def test_calculate_backoff(retrycount, max_retries, expected): assert calculate_backoff(retrycount, max_retries) == expected + + +@pytest.mark.parametrize("exchange_name,method_name,is_callable", [ + ("binance", "set_margin_mode", True), + ("kraken", "set_margin_mode", False), + ("ftx", "set_margin_mode", False), + ("bittrex", "set_margin_mode", False), + ("binance", "not_a_real_method", False), + ("kraken", "not_a_real_method", False), + ("ftx", "not_a_real_method", False), + ("bittrex", "not_a_real_method", False), + ("binance", "create_order", True), + ("kraken", "create_order", True), + ("ftx", "create_order", True), + ("bittrex", "create_order", True), +]) +def test_throw_exception_if_method_not_on_exchange( + mocker, + default_conf, + caplog, + exchange_name, + method_name, + is_callable +): + exchange = get_patched_exchange(mocker, default_conf, id=exchange_name) + warning = f"{method_name}() has not been implemented on ccxt.{exchange_name.capitalize()}" + if not is_callable: + with pytest.raises(OperationalException) as e: + exchange.throw_exception_if_method_not_on_exchange(method_name) + assert e.value.args[0] == warning + else: + # Running the method will fail the test if an exception is thrown + exchange.throw_exception_if_method_not_on_exchange(method_name)