Add tests for build_config

This commit is contained in:
Matthias 2020-02-01 14:22:40 +01:00
parent cfa6a3e3d3
commit d1a3a2d000
2 changed files with 29 additions and 6 deletions

View File

@ -27,6 +27,19 @@ def validate_is_float(val):
return False return False
def ask_user_overwrite(config_path: Path) -> bool:
questions = [
{
"type": "confirm",
"name": "overwrite",
"message": f"File {config_path} already exists. Overwrite?",
"default": False,
},
]
answers = prompt(questions)
return answers['overwrite']
def ask_user_config() -> Dict[str, Any]: def ask_user_config() -> Dict[str, Any]:
""" """
Ask user a few questions to build the configuration. Ask user a few questions to build the configuration.
@ -169,6 +182,10 @@ def start_new_config(args: Dict[str, Any]) -> None:
config_path = Path(args['config'][0]) config_path = Path(args['config'][0])
if config_path.exists(): if config_path.exists():
overwrite = ask_user_overwrite(config_path)
if overwrite:
config_path.unlink()
else:
raise OperationalException( raise OperationalException(
f"Configuration `{config_path}` already exists. " f"Configuration `{config_path}` already exists. "
"Please use another configuration name or delete the existing configuration.") "Please use another configuration name or delete the existing configuration.")

View File

@ -1,8 +1,8 @@
import json
from pathlib import Path from pathlib import Path
from unittest.mock import MagicMock from unittest.mock import MagicMock
import pytest import pytest
import rapidjson
from freqtrade.commands.build_config_commands import (ask_user_config, from freqtrade.commands.build_config_commands import (ask_user_config,
start_new_config) start_new_config)
@ -13,7 +13,10 @@ from tests.conftest import get_args, log_has_re
@pytest.mark.parametrize('exchange', ['bittrex', 'binance', 'kraken', 'ftx']) @pytest.mark.parametrize('exchange', ['bittrex', 'binance', 'kraken', 'ftx'])
def test_start_new_config(mocker, caplog, exchange): def test_start_new_config(mocker, caplog, exchange):
wt_mock = mocker.patch.object(Path, "write_text", MagicMock()) wt_mock = mocker.patch.object(Path, "write_text", MagicMock())
mocker.patch.object(Path, "exists", MagicMock(return_value=False)) 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 = { sample_selections = {
'max_open_trades': 3, 'max_open_trades': 3,
'stake_currency': 'USDT', 'stake_currency': 'USDT',
@ -39,13 +42,16 @@ def test_start_new_config(mocker, caplog, exchange):
assert log_has_re("Writing config to .*", caplog) assert log_has_re("Writing config to .*", caplog)
assert wt_mock.call_count == 1 assert wt_mock.call_count == 1
result = json.loads(wt_mock.call_args_list[0][0][0]) 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['exchange']['name'] == exchange
assert result['ticker_interval'] == '15m' assert result['ticker_interval'] == '15m'
def test_start_new_config_exists(mocker, caplog): def test_start_new_config_exists(mocker, caplog):
mocker.patch.object(Path, "exists", MagicMock(return_value=True)) mocker.patch.object(Path, "exists", MagicMock(return_value=True))
mocker.patch('freqtrade.commands.build_config_commands.ask_user_overwrite', return_value=False)
args = [ args = [
"new-config", "new-config",
"--config", "--config",