2024-03-29 18:09:23 +00:00
|
|
|
import re
|
2024-06-15 07:43:48 +00:00
|
|
|
from unittest.mock import ANY, MagicMock
|
2024-03-29 08:52:25 +00:00
|
|
|
|
|
|
|
import pytest
|
2024-05-12 13:20:36 +00:00
|
|
|
from requests.exceptions import ConnectionError
|
|
|
|
|
2024-03-29 08:52:25 +00:00
|
|
|
from freqtrade_client import FtRestClient
|
2024-03-29 12:12:01 +00:00
|
|
|
from freqtrade_client.ft_client import add_arguments, main_exec
|
2024-03-29 14:52:35 +00:00
|
|
|
|
|
|
|
|
2024-03-29 18:09:23 +00:00
|
|
|
def log_has_re(line, logs):
|
|
|
|
"""Check if line matches some caplog's message."""
|
|
|
|
return any(re.match(line, message) for message in logs.messages)
|
2024-03-29 08:52:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_rest_client():
|
2024-05-12 14:16:26 +00:00
|
|
|
client = FtRestClient("http://localhost:8080", "freqtrader", "password")
|
2024-03-29 08:52:25 +00:00
|
|
|
client._session = MagicMock()
|
|
|
|
request_mock = MagicMock()
|
|
|
|
client._session.request = request_mock
|
|
|
|
return client, request_mock
|
|
|
|
|
|
|
|
|
|
|
|
def test_FtRestClient_init():
|
2024-05-12 14:16:26 +00:00
|
|
|
client = FtRestClient("http://localhost:8080", "freqtrader", "password")
|
2024-03-29 08:52:25 +00:00
|
|
|
assert client is not None
|
2024-05-12 14:16:26 +00:00
|
|
|
assert client._serverurl == "http://localhost:8080"
|
2024-03-29 08:52:25 +00:00
|
|
|
assert client._session is not None
|
|
|
|
assert client._session.auth is not None
|
2024-05-12 14:16:26 +00:00
|
|
|
assert client._session.auth == ("freqtrader", "password")
|
2024-03-29 08:52:25 +00:00
|
|
|
|
|
|
|
|
2024-05-12 14:16:26 +00:00
|
|
|
@pytest.mark.parametrize("method", ["GET", "POST", "DELETE"])
|
2024-03-29 08:52:25 +00:00
|
|
|
def test_FtRestClient_call(method):
|
|
|
|
client, mock = get_rest_client()
|
2024-05-12 14:16:26 +00:00
|
|
|
client._call(method, "/dummytest")
|
2024-03-29 08:52:25 +00:00
|
|
|
assert mock.call_count == 1
|
|
|
|
|
2024-05-12 14:16:26 +00:00
|
|
|
getattr(client, f"_{method.lower()}")("/dummytest")
|
2024-03-29 08:52:25 +00:00
|
|
|
assert mock.call_count == 2
|
|
|
|
|
|
|
|
|
2024-03-29 14:52:35 +00:00
|
|
|
def test_FtRestClient_call_invalid(caplog):
|
|
|
|
client, _ = get_rest_client()
|
|
|
|
with pytest.raises(ValueError):
|
2024-05-12 14:16:26 +00:00
|
|
|
client._call("PUTTY", "/dummytest")
|
2024-03-29 14:52:35 +00:00
|
|
|
|
|
|
|
client._session.request = MagicMock(side_effect=ConnectionError())
|
2024-05-12 14:16:26 +00:00
|
|
|
client._call("GET", "/dummytest")
|
|
|
|
|
|
|
|
assert log_has_re("Connection error", caplog)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
2024-06-15 07:21:35 +00:00
|
|
|
"method,args,kwargs",
|
2024-05-12 14:16:26 +00:00
|
|
|
[
|
2024-06-15 07:21:35 +00:00
|
|
|
("start", [], {}),
|
|
|
|
("stop", [], {}),
|
|
|
|
("stopbuy", [], {}),
|
|
|
|
("reload_config", [], {}),
|
|
|
|
("balance", [], {}),
|
|
|
|
("count", [], {}),
|
|
|
|
("entries", [], {}),
|
|
|
|
("exits", [], {}),
|
|
|
|
("mix_tags", [], {}),
|
|
|
|
("locks", [], {}),
|
|
|
|
("lock_add", ["XRP/USDT", "2024-01-01 20:00:00Z", "*", "rand"], {}),
|
|
|
|
("delete_lock", [2], {}),
|
|
|
|
("daily", [], {}),
|
|
|
|
("daily", [15], {}),
|
|
|
|
("weekly", [], {}),
|
|
|
|
("weekly", [15], {}),
|
|
|
|
("monthly", [], {}),
|
|
|
|
("monthly", [12], {}),
|
|
|
|
("edge", [], {}),
|
|
|
|
("profit", [], {}),
|
|
|
|
("stats", [], {}),
|
|
|
|
("performance", [], {}),
|
|
|
|
("status", [], {}),
|
|
|
|
("version", [], {}),
|
|
|
|
("show_config", [], {}),
|
|
|
|
("ping", [], {}),
|
|
|
|
("logs", [], {}),
|
|
|
|
("logs", [55], {}),
|
|
|
|
("trades", [], {}),
|
|
|
|
("trades", [5], {}),
|
|
|
|
("trades", [5, 5], {}), # With offset
|
|
|
|
("trade", [1], {}),
|
|
|
|
("delete_trade", [1], {}),
|
|
|
|
("cancel_open_order", [1], {}),
|
|
|
|
("whitelist", [], {}),
|
|
|
|
("blacklist", [], {}),
|
|
|
|
("blacklist", ["XRP/USDT"], {}),
|
|
|
|
("blacklist", ["XRP/USDT", "BTC/USDT"], {}),
|
|
|
|
("forcebuy", ["XRP/USDT"], {}),
|
|
|
|
("forcebuy", ["XRP/USDT", 1.5], {}),
|
|
|
|
("forceenter", ["XRP/USDT", "short"], {}),
|
|
|
|
("forceenter", ["XRP/USDT", "short", 1.5], {}),
|
|
|
|
("forceenter", ["XRP/USDT", "short", 1.5], {"order_type": "market"}),
|
|
|
|
("forceenter", ["XRP/USDT", "short", 1.5], {"order_type": "market", "stake_amount": 100}),
|
|
|
|
(
|
|
|
|
"forceenter",
|
|
|
|
["XRP/USDT", "short", 1.5],
|
|
|
|
{"order_type": "market", "stake_amount": 100, "leverage": 10.0},
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"forceenter",
|
|
|
|
["XRP/USDT", "short", 1.5],
|
|
|
|
{
|
|
|
|
"order_type": "market",
|
|
|
|
"stake_amount": 100,
|
|
|
|
"leverage": 10.0,
|
|
|
|
"enter_tag": "test_force_enter",
|
|
|
|
},
|
|
|
|
),
|
|
|
|
("forceexit", [1], {}),
|
|
|
|
("forceexit", [1, "limit"], {}),
|
|
|
|
("forceexit", [1, "limit", 100], {}),
|
|
|
|
("strategies", [], {}),
|
|
|
|
("strategy", ["sampleStrategy"], {}),
|
|
|
|
("pairlists_available", [], {}),
|
|
|
|
("plot_config", [], {}),
|
|
|
|
("available_pairs", [], {}),
|
|
|
|
("available_pairs", ["5m"], {}),
|
|
|
|
("pair_candles", ["XRP/USDT", "5m"], {}),
|
|
|
|
("pair_candles", ["XRP/USDT", "5m", 500], {}),
|
2024-06-15 07:22:17 +00:00
|
|
|
("pair_candles", ["XRP/USDT", "5m", 500], {"columns": ["close_time,close"]}),
|
2024-06-15 07:21:35 +00:00
|
|
|
("pair_history", ["XRP/USDT", "5m", "SampleStrategy"], {}),
|
2024-06-15 07:22:17 +00:00
|
|
|
("pair_history", ["XRP/USDT", "5m"], {"strategy": "SampleStrategy"}),
|
2024-06-15 07:21:35 +00:00
|
|
|
("sysinfo", [], {}),
|
|
|
|
("health", [], {}),
|
2024-05-12 14:16:26 +00:00
|
|
|
],
|
|
|
|
)
|
2024-06-15 07:21:35 +00:00
|
|
|
def test_FtRestClient_call_explicit_methods(method, args, kwargs):
|
2024-03-29 08:52:25 +00:00
|
|
|
client, mock = get_rest_client()
|
2024-07-05 06:54:49 +00:00
|
|
|
executor = getattr(client, method)
|
|
|
|
executor(*args, **kwargs)
|
2024-03-29 08:52:25 +00:00
|
|
|
assert mock.call_count == 1
|
2024-03-29 12:12:01 +00:00
|
|
|
|
|
|
|
|
2024-03-29 14:52:35 +00:00
|
|
|
def test_ft_client(mocker, capsys, caplog):
|
2024-03-29 12:12:01 +00:00
|
|
|
with pytest.raises(SystemExit):
|
2024-05-12 14:16:26 +00:00
|
|
|
args = add_arguments(["-V"])
|
2024-03-29 12:12:01 +00:00
|
|
|
|
2024-05-12 14:16:26 +00:00
|
|
|
args = add_arguments(["--show"])
|
2024-03-29 12:12:01 +00:00
|
|
|
assert isinstance(args, dict)
|
2024-05-12 14:16:26 +00:00
|
|
|
assert args["show"] is True
|
2024-03-29 12:12:01 +00:00
|
|
|
with pytest.raises(SystemExit):
|
|
|
|
main_exec(args)
|
|
|
|
captured = capsys.readouterr()
|
2024-05-12 14:16:26 +00:00
|
|
|
assert "Possible commands" in captured.out
|
|
|
|
|
|
|
|
mock = mocker.patch("freqtrade_client.ft_client.FtRestClient._call")
|
|
|
|
args = add_arguments(["--config", "tests/testdata/testconfigs/main_test_config.json", "ping"])
|
2024-03-29 12:18:11 +00:00
|
|
|
main_exec(args)
|
|
|
|
captured = capsys.readouterr()
|
|
|
|
assert mock.call_count == 1
|
2024-03-29 14:52:35 +00:00
|
|
|
|
|
|
|
with pytest.raises(SystemExit):
|
2024-05-12 14:16:26 +00:00
|
|
|
args = add_arguments(["--config", "tests/testdata/testconfigs/nonexisting.json"])
|
2024-03-29 14:52:35 +00:00
|
|
|
main_exec(args)
|
|
|
|
|
2024-05-12 14:16:26 +00:00
|
|
|
assert log_has_re(r"Could not load config file .*nonexisting\.json\.", caplog)
|
2024-03-29 14:52:35 +00:00
|
|
|
|
2024-05-12 14:16:26 +00:00
|
|
|
args = add_arguments(
|
|
|
|
["--config", "tests/testdata/testconfigs/main_test_config.json", "whatever"]
|
|
|
|
)
|
2024-03-29 14:52:35 +00:00
|
|
|
main_exec(args)
|
2024-05-12 14:16:26 +00:00
|
|
|
assert log_has_re("Command whatever not defined", caplog)
|
2024-06-15 07:43:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"params, expected_args, expected_kwargs",
|
|
|
|
[
|
|
|
|
("forceenter BTC/USDT long", ["BTC/USDT", "long"], {}),
|
|
|
|
("forceenter BTC/USDT long limit", ["BTC/USDT", "long", "limit"], {}),
|
|
|
|
(
|
|
|
|
# Skip most parameters, only providing enter_tag
|
|
|
|
"forceenter BTC/USDT long enter_tag=deadBeef",
|
|
|
|
["BTC/USDT", "long"],
|
|
|
|
{"enter_tag": "deadBeef"},
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"forceenter BTC/USDT long invalid_key=123",
|
|
|
|
[],
|
|
|
|
SystemExit,
|
|
|
|
# {"invalid_key": "deadBeef"},
|
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_ft_client_argparsing(mocker, params, expected_args, expected_kwargs, caplog):
|
|
|
|
mocked_method = params.split(" ")[0]
|
2024-06-16 06:09:35 +00:00
|
|
|
mocker.patch("freqtrade_client.ft_client.load_config", return_value={}, autospec=True)
|
2024-06-15 07:43:48 +00:00
|
|
|
mm = mocker.patch(
|
|
|
|
f"freqtrade_client.ft_client.FtRestClient.{mocked_method}", return_value={}, autospec=True
|
|
|
|
)
|
|
|
|
args = add_arguments(params.split(" "))
|
|
|
|
if isinstance(expected_kwargs, dict):
|
|
|
|
main_exec(args)
|
|
|
|
mm.assert_called_once_with(ANY, *expected_args, **expected_kwargs)
|
|
|
|
else:
|
|
|
|
with pytest.raises(expected_kwargs):
|
|
|
|
main_exec(args)
|
|
|
|
|
|
|
|
assert log_has_re(f"Error executing command {mocked_method}: got an unexpected .*", caplog)
|
|
|
|
mm.assert_not_called()
|