freqtrade_origin/tests/commands/test_build_config.py

122 lines
4.1 KiB
Python
Raw Normal View History

2020-01-29 20:30:29 +00:00
from pathlib import Path
from unittest.mock import MagicMock
import pytest
2020-02-01 13:22:40 +00:00
import rapidjson
2020-01-29 20:30:29 +00:00
2024-05-12 13:08:40 +00:00
from freqtrade.commands.build_config_commands import (
ask_user_config,
ask_user_overwrite,
start_new_config,
validate_is_float,
validate_is_int,
)
2020-02-01 13:12:21 +00:00
from freqtrade.exceptions import OperationalException
2020-01-29 20:30:29 +00:00
from tests.conftest import get_args, log_has_re
def test_validate_is_float():
2024-05-12 13:47:54 +00:00
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():
2024-05-12 13:47:54 +00:00
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")
2024-05-12 13:47:54 +00:00
@pytest.mark.parametrize("exchange", ["bybit", "binance", "kraken"])
2020-01-29 20:30:29 +00:00
def test_start_new_config(mocker, caplog, exchange):
wt_mock = mocker.patch.object(Path, "write_text", MagicMock())
2020-02-01 13:22:40 +00:00
mocker.patch.object(Path, "exists", MagicMock(return_value=True))
unlink_mock = mocker.patch.object(Path, "unlink", MagicMock())
2024-05-12 13:47:54 +00:00
mocker.patch("freqtrade.commands.build_config_commands.ask_user_overwrite", return_value=True)
2020-02-01 13:22:40 +00:00
2020-01-29 20:30:29 +00:00
sample_selections = {
2024-05-12 13:47:54 +00:00
"max_open_trades": 3,
"stake_currency": "USDT",
"stake_amount": 100,
"fiat_display_currency": "EUR",
"timeframe": "15m",
"dry_run": True,
"trading_mode": "spot",
"margin_mode": "",
"exchange_name": exchange,
"exchange_key": "sampleKey",
"exchange_secret": "Samplesecret",
"telegram": False,
"telegram_token": "asdf1244",
"telegram_chat_id": "1144444",
"api_server": False,
"api_server_listen_addr": "127.0.0.1",
"api_server_username": "freqtrader",
"api_server_password": "MoneyMachine",
2020-01-29 20:30:29 +00:00
}
2024-05-12 13:47:54 +00:00
mocker.patch(
"freqtrade.commands.build_config_commands.ask_user_config", return_value=sample_selections
)
args = ["new-config", "--config", "coolconfig.json"]
2020-01-29 20:30:29 +00:00
start_new_config(get_args(args))
assert log_has_re("Writing config to .*", caplog)
assert wt_mock.call_count == 1
2020-02-01 13:22:40 +00:00
assert unlink_mock.call_count == 1
2024-05-12 13:47:54 +00:00
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["timeframe"] == "15m"
2020-01-29 21:47:15 +00:00
2020-02-01 13:12:21 +00:00
def test_start_new_config_exists(mocker, caplog):
mocker.patch.object(Path, "exists", MagicMock(return_value=True))
2024-05-12 13:47:54 +00:00
mocker.patch("freqtrade.commands.build_config_commands.ask_user_overwrite", return_value=False)
args = ["new-config", "--config", "coolconfig.json"]
2020-02-01 13:12:21 +00:00
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.
"""
2024-05-12 13:47:54 +00:00
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()
2024-05-12 13:47:54 +00:00
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.
"""
2024-05-12 13:47:54 +00:00
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
2024-05-12 13:47:54 +00:00
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()