Skip to content

Commit

Permalink
Add tests for build_config
Browse files Browse the repository at this point in the history
  • Loading branch information
xmatthias committed Feb 1, 2020
1 parent cfa6a3e commit d1a3a2d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
23 changes: 20 additions & 3 deletions freqtrade/commands/build_config_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@ def validate_is_float(val):
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]:
"""
Ask user a few questions to build the configuration.
Expand Down Expand Up @@ -169,8 +182,12 @@ def start_new_config(args: Dict[str, Any]) -> None:

config_path = Path(args['config'][0])
if config_path.exists():
raise OperationalException(
f"Configuration `{config_path}` already exists. "
"Please use another configuration name or delete the existing configuration.")
overwrite = ask_user_overwrite(config_path)
if overwrite:
config_path.unlink()
else:
raise OperationalException(
f"Configuration `{config_path}` already exists. "
"Please use another configuration name or delete the existing configuration.")
selections = ask_user_config()
deploy_new_config(config_path, selections)
12 changes: 9 additions & 3 deletions tests/commands/test_build_config.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import json
from pathlib import Path
from unittest.mock import MagicMock

import pytest
import rapidjson

from freqtrade.commands.build_config_commands import (ask_user_config,
start_new_config)
Expand All @@ -13,7 +13,10 @@
@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=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 = {
'max_open_trades': 3,
'stake_currency': 'USDT',
Expand All @@ -39,13 +42,16 @@ def test_start_new_config(mocker, caplog, exchange):

assert log_has_re("Writing config to .*", caplog)
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['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",
Expand Down

0 comments on commit d1a3a2d

Please sign in to comment.