Merge branch 'develop' into data_handler
This commit is contained in:
116
tests/commands/test_build_config.py
Normal file
116
tests/commands/test_build_config.py
Normal file
@@ -0,0 +1,116 @@
|
||||
from pathlib import Path
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
import pytest
|
||||
import rapidjson
|
||||
|
||||
from freqtrade.commands.build_config_commands import (ask_user_config,
|
||||
ask_user_overwrite,
|
||||
start_new_config,
|
||||
validate_is_float,
|
||||
validate_is_int)
|
||||
from freqtrade.exceptions import OperationalException
|
||||
from tests.conftest import get_args, log_has_re
|
||||
|
||||
|
||||
def test_validate_is_float():
|
||||
assert validate_is_float('2.0')
|
||||
assert validate_is_float('2.1')
|
||||
assert validate_is_float('0.1')
|
||||
assert validate_is_float('-0.5')
|
||||
assert not validate_is_float('-0.5e')
|
||||
|
||||
|
||||
def test_validate_is_int():
|
||||
assert validate_is_int('2')
|
||||
assert validate_is_int('6')
|
||||
assert validate_is_int('-1')
|
||||
assert validate_is_int('500')
|
||||
assert not validate_is_int('2.0')
|
||||
assert not validate_is_int('2.1')
|
||||
assert not validate_is_int('-2.1')
|
||||
assert not validate_is_int('-ee')
|
||||
|
||||
|
||||
@pytest.mark.parametrize('exchange', ['bittrex', 'binance', 'kraken', 'ftx'])
|
||||
def test_start_new_config(mocker, caplog, exchange):
|
||||
wt_mock = mocker.patch.object(Path, "write_text", MagicMock())
|
||||
mocker.patch.object(Path, "exists", MagicMock(return_value=True))
|
||||
unlink_mock = mocker.patch.object(Path, "unlink", MagicMock())
|
||||
mocker.patch('freqtrade.commands.build_config_commands.ask_user_overwrite', return_value=True)
|
||||
|
||||
sample_selections = {
|
||||
'max_open_trades': 3,
|
||||
'stake_currency': 'USDT',
|
||||
'stake_amount': 100,
|
||||
'fiat_display_currency': 'EUR',
|
||||
'ticker_interval': '15m',
|
||||
'dry_run': True,
|
||||
'exchange_name': exchange,
|
||||
'exchange_key': 'sampleKey',
|
||||
'exchange_secret': 'Samplesecret',
|
||||
'telegram': False,
|
||||
'telegram_token': 'asdf1244',
|
||||
'telegram_chat_id': '1144444',
|
||||
}
|
||||
mocker.patch('freqtrade.commands.build_config_commands.ask_user_config',
|
||||
return_value=sample_selections)
|
||||
args = [
|
||||
"new-config",
|
||||
"--config",
|
||||
"coolconfig.json"
|
||||
]
|
||||
start_new_config(get_args(args))
|
||||
|
||||
assert log_has_re("Writing config to .*", caplog)
|
||||
assert wt_mock.call_count == 1
|
||||
assert unlink_mock.call_count == 1
|
||||
result = rapidjson.loads(wt_mock.call_args_list[0][0][0],
|
||||
parse_mode=rapidjson.PM_COMMENTS | rapidjson.PM_TRAILING_COMMAS)
|
||||
assert result['exchange']['name'] == exchange
|
||||
assert result['ticker_interval'] == '15m'
|
||||
|
||||
|
||||
def test_start_new_config_exists(mocker, caplog):
|
||||
mocker.patch.object(Path, "exists", MagicMock(return_value=True))
|
||||
mocker.patch('freqtrade.commands.build_config_commands.ask_user_overwrite', return_value=False)
|
||||
args = [
|
||||
"new-config",
|
||||
"--config",
|
||||
"coolconfig.json"
|
||||
]
|
||||
with pytest.raises(OperationalException, match=r"Configuration .* already exists\."):
|
||||
start_new_config(get_args(args))
|
||||
|
||||
|
||||
def test_ask_user_overwrite(mocker):
|
||||
"""
|
||||
Once https://github.com/tmbo/questionary/issues/35 is implemented, improve this test.
|
||||
"""
|
||||
prompt_mock = mocker.patch('freqtrade.commands.build_config_commands.prompt',
|
||||
return_value={'overwrite': False})
|
||||
assert not ask_user_overwrite(Path('test.json'))
|
||||
assert prompt_mock.call_count == 1
|
||||
|
||||
prompt_mock.reset_mock()
|
||||
prompt_mock = mocker.patch('freqtrade.commands.build_config_commands.prompt',
|
||||
return_value={'overwrite': True})
|
||||
assert ask_user_overwrite(Path('test.json'))
|
||||
assert prompt_mock.call_count == 1
|
||||
|
||||
|
||||
def test_ask_user_config(mocker):
|
||||
"""
|
||||
Once https://github.com/tmbo/questionary/issues/35 is implemented, improve this test.
|
||||
"""
|
||||
prompt_mock = mocker.patch('freqtrade.commands.build_config_commands.prompt',
|
||||
return_value={'overwrite': False})
|
||||
answers = ask_user_config()
|
||||
assert isinstance(answers, dict)
|
||||
assert prompt_mock.call_count == 1
|
||||
|
||||
prompt_mock = mocker.patch('freqtrade.commands.build_config_commands.prompt',
|
||||
return_value={})
|
||||
|
||||
with pytest.raises(OperationalException, match=r"User interrupted interactive questions\."):
|
||||
ask_user_config()
|
@@ -778,6 +778,121 @@ def test_hyperopt_list(mocker, capsys, hyperopt_results):
|
||||
assert all(x not in captured.out
|
||||
for x in [" 1/12", " 3/12", " 4/12", " 5/12", " 6/12", " 7/12", " 8/12", " 9/12",
|
||||
" 11/12", " 12/12"])
|
||||
args = [
|
||||
"hyperopt-list",
|
||||
"--no-details",
|
||||
"--no-color",
|
||||
"--min-trades", "20"
|
||||
]
|
||||
pargs = get_args(args)
|
||||
pargs['config'] = None
|
||||
start_hyperopt_list(pargs)
|
||||
captured = capsys.readouterr()
|
||||
assert all(x in captured.out
|
||||
for x in [" 3/12", " 6/12", " 7/12", " 9/12", " 11/12"])
|
||||
assert all(x not in captured.out
|
||||
for x in [" 1/12", " 2/12", " 4/12", " 5/12", " 8/12", " 10/12", " 12/12"])
|
||||
args = [
|
||||
"hyperopt-list",
|
||||
"--profitable",
|
||||
"--no-details",
|
||||
"--max-trades", "20"
|
||||
]
|
||||
pargs = get_args(args)
|
||||
pargs['config'] = None
|
||||
start_hyperopt_list(pargs)
|
||||
captured = capsys.readouterr()
|
||||
assert all(x in captured.out
|
||||
for x in [" 2/12", " 10/12"])
|
||||
assert all(x not in captured.out
|
||||
for x in [" 1/12", " 3/12", " 4/12", " 5/12", " 6/12", " 7/12", " 8/12", " 9/12",
|
||||
" 11/12", " 12/12"])
|
||||
args = [
|
||||
"hyperopt-list",
|
||||
"--profitable",
|
||||
"--no-details",
|
||||
"--min-avg-profit", "0.11"
|
||||
]
|
||||
pargs = get_args(args)
|
||||
pargs['config'] = None
|
||||
start_hyperopt_list(pargs)
|
||||
captured = capsys.readouterr()
|
||||
assert all(x in captured.out
|
||||
for x in [" 2/12"])
|
||||
assert all(x not in captured.out
|
||||
for x in [" 1/12", " 3/12", " 4/12", " 5/12", " 6/12", " 7/12", " 8/12", " 9/12",
|
||||
" 10/12", " 11/12", " 12/12"])
|
||||
args = [
|
||||
"hyperopt-list",
|
||||
"--no-details",
|
||||
"--max-avg-profit", "0.10"
|
||||
]
|
||||
pargs = get_args(args)
|
||||
pargs['config'] = None
|
||||
start_hyperopt_list(pargs)
|
||||
captured = capsys.readouterr()
|
||||
assert all(x in captured.out
|
||||
for x in [" 1/12", " 3/12", " 5/12", " 6/12", " 7/12", " 8/12", " 9/12",
|
||||
" 11/12"])
|
||||
assert all(x not in captured.out
|
||||
for x in [" 2/12", " 4/12", " 10/12", " 12/12"])
|
||||
args = [
|
||||
"hyperopt-list",
|
||||
"--no-details",
|
||||
"--min-total-profit", "0.4"
|
||||
]
|
||||
pargs = get_args(args)
|
||||
pargs['config'] = None
|
||||
start_hyperopt_list(pargs)
|
||||
captured = capsys.readouterr()
|
||||
assert all(x in captured.out
|
||||
for x in [" 10/12"])
|
||||
assert all(x not in captured.out
|
||||
for x in [" 1/12", " 2/12", " 3/12", " 4/12", " 5/12", " 6/12", " 7/12", " 8/12",
|
||||
" 9/12", " 11/12", " 12/12"])
|
||||
args = [
|
||||
"hyperopt-list",
|
||||
"--no-details",
|
||||
"--max-total-profit", "0.4"
|
||||
]
|
||||
pargs = get_args(args)
|
||||
pargs['config'] = None
|
||||
start_hyperopt_list(pargs)
|
||||
captured = capsys.readouterr()
|
||||
assert all(x in captured.out
|
||||
for x in [" 1/12", " 2/12", " 3/12", " 5/12", " 6/12", " 7/12", " 8/12",
|
||||
" 9/12", " 11/12"])
|
||||
assert all(x not in captured.out
|
||||
for x in [" 4/12", " 10/12", " 12/12"])
|
||||
args = [
|
||||
"hyperopt-list",
|
||||
"--profitable",
|
||||
"--no-details",
|
||||
"--min-avg-time", "2000"
|
||||
]
|
||||
pargs = get_args(args)
|
||||
pargs['config'] = None
|
||||
start_hyperopt_list(pargs)
|
||||
captured = capsys.readouterr()
|
||||
assert all(x in captured.out
|
||||
for x in [" 10/12"])
|
||||
assert all(x not in captured.out
|
||||
for x in [" 1/12", " 2/12", " 3/12", " 4/12", " 5/12", " 6/12", " 7/12",
|
||||
" 8/12", " 9/12", " 11/12", " 12/12"])
|
||||
args = [
|
||||
"hyperopt-list",
|
||||
"--no-details",
|
||||
"--max-avg-time", "1500"
|
||||
]
|
||||
pargs = get_args(args)
|
||||
pargs['config'] = None
|
||||
start_hyperopt_list(pargs)
|
||||
captured = capsys.readouterr()
|
||||
assert all(x in captured.out
|
||||
for x in [" 2/12", " 6/12"])
|
||||
assert all(x not in captured.out
|
||||
for x in [" 1/12", " 3/12", " 4/12", " 5/12", " 7/12", " 8/12"
|
||||
" 9/12", " 10/12", " 11/12", " 12/12"])
|
||||
|
||||
|
||||
def test_hyperopt_show(mocker, capsys, hyperopt_results):
|
||||
|
Reference in New Issue
Block a user