Submitting with unit test for the working scenario.

Strongly recommend core team check the unit test is even targetting the
correct code in exchange/__init__.py

I have a real knowledge gap on mocker, in so far as how tests map to
what they're targeting.
This commit is contained in:
creslinux 2018-07-28 20:32:10 +00:00
parent 8648ac9da2
commit 0a059662b3
2 changed files with 39 additions and 3 deletions

View File

@ -95,9 +95,7 @@ class Exchange(object):
except (KeyError, AttributeError):
raise OperationalException(f'Exchange {name} is not supported')
# check if config requests sanbox, if so use ['test'] from url
if (exchange_config.get('sandbox')):
api.urls['api'] = api.urls['test']
self.set_sandbox(api, exchange_config, name)
return api
@ -111,6 +109,14 @@ class Exchange(object):
"""exchange ccxt id"""
return self._api.id
def set_sandbox(self, api, exchange_config: dict, name: str):
if exchange_config.get('sandbox'):
if api.urls.get('test'):
api.urls['api'] = api.urls['test']
else:
logger.warning(self, "No Sandbox URL in CCXT, exiting. Please check your config.json")
raise OperationalException(f'Exchange {name} does not provide a sandbox api')
def validate_pairs(self, pairs: List[str]) -> None:
"""
Checks if all given pairs are tradable on the current exchange.

View File

@ -51,6 +51,36 @@ def test_init_exception(default_conf, mocker):
mocker.patch("ccxt.binance", MagicMock(side_effect=AttributeError))
Exchange(default_conf)
def test_set_sandbox(default_conf, mocker):
"""
Test working scenario
"""
api_mock = MagicMock()
api_mock.load_markets = MagicMock(return_value={
'ETH/BTC': '', 'LTC/BTC': '', 'XRP/BTC': '', 'NEO/BTC': ''
})
url_mock = PropertyMock(return_value={'test': "api-public.sandbox.gdax.com", 'api': 'https://api.gdax.com'})
type(api_mock).urls = url_mock
mocker.patch('freqtrade.exchange.Exchange._init_ccxt', MagicMock(return_value=api_mock))
mocker.patch('freqtrade.exchange.Exchange.validate_timeframes', MagicMock())
Exchange(default_conf)
# def test_set_sandbox_exception(default_conf, mocker):
# """
# Test Fail scenario
# """
# api_mock = MagicMock()
# api_mock.load_markets = MagicMock(return_value={
# 'ETH/BTC': '', 'LTC/BTC': '', 'XRP/BTC': '', 'NEO/BTC': ''
# })
# url_mock = PropertyMock(return_value={'api': 'https://api.gdax.com'})
# type(api_mock).urls = url_mock
#
# mocker.patch('freqtrade.exchange.Exchange._init_ccxt', MagicMock(return_value=api_mock))
# mocker.patch('freqtrade.exchange.Exchange.validate_timeframes', MagicMock())
#
# with pytest.raises(OperationalException, match=r'does not provide a sandbox api'):
# Exchange(default_conf)
def test_validate_pairs(default_conf, mocker):
api_mock = MagicMock()