diff --git a/tests/rpc/test_rpc_apiserver.py b/tests/rpc/test_rpc_apiserver.py index 1247d4c02..226abec1c 100644 --- a/tests/rpc/test_rpc_apiserver.py +++ b/tests/rpc/test_rpc_apiserver.py @@ -7,8 +7,10 @@ import logging import time from copy import deepcopy from datetime import UTC, datetime, timedelta +from io import BytesIO from pathlib import Path from unittest.mock import ANY, MagicMock, PropertyMock, patch +from zipfile import ZipFile import pandas as pd import pytest @@ -3275,7 +3277,7 @@ def test_api_patch_backtest_history_entry(botclient, tmp_path: Path): assert fileres[CURRENT_TEST_STRATEGY]["notes"] == "FooBar" -def test_api_patch_backtest_market_change(botclient, tmp_path: Path): +def test_api_backtest_market_change(botclient, tmp_path: Path): ftbot, client = botclient # Create a temporary directory and file @@ -3313,6 +3315,55 @@ def test_api_patch_backtest_market_change(botclient, tmp_path: Path): ] +def test_api_backtest_wallets(botclient, tmp_path: Path): + ftbot, client = botclient + + # Create a temporary directory and file + bt_results_base = tmp_path / "backtest_results" + bt_results_base.mkdir() + zip_file = bt_results_base / "backtest_15.zip" + with ZipFile(zip_file, "w") as zipf: + wallet_df = pd.DataFrame( + { + "date": [ + "2018-01-01T00:00:00Z", + "2018-01-01T00:00:00Z", + "2018-01-01T00:05:00Z", + "2018-01-01T00:05:00Z", + ], + "currency": ["ETH", "BTC", "ETH", "BTC"], + "price": [2000, 60_000, 2001, 60_001], + "balance": [0.5, 0.25, 0.5, 0.25], + } + ) + wallet_df["date"] = pd.to_datetime(wallet_df["date"]) + wallet_buf = BytesIO() + wallet_df.reset_index().to_feather(wallet_buf, compression_level=9, compression="lz4") + wallet_buf.seek(0) + zipf.writestr("backtest_15_SampleStrategy_wallet.feather", wallet_buf.read()) + + # Wrong basedirectory + rc = client_get(client, f"{BASE_URI}/backtest/history/randomFile.json/SampleStrategy/wallet") + assert_response(rc, 503) + + ftbot.config["user_data_dir"] = tmp_path + ftbot.config["runmode"] = RunMode.WEBSERVER + + # Nonexisting file + rc = client_get(client, f"{BASE_URI}/backtest/history/randomFile.json/SampleStrategy/wallet") + assert_response(rc, 404) + + rc = client_get(client, f"{BASE_URI}/backtest/history/backtest_15/SampleStrategy/wallet") + assert_response(rc, 200) + result = rc.json() + assert result["length"] == 2 + assert result["columns"] == ["date", "__date_ts", "total"] + assert result["data"] == [ + ["2018-01-01T00:00:00Z", 1514764800000, 16000.0], + ["2018-01-01T00:05:00Z", 1514765100000, 16000.75], + ] + + def test_health(botclient): _ftbot, client = botclient