diff --git a/freqtrade/rpc/api_server/api_download_data.py b/freqtrade/rpc/api_server/api_download_data.py index b5e89dd60..0505c65ae 100644 --- a/freqtrade/rpc/api_server/api_download_data.py +++ b/freqtrade/rpc/api_server/api_download_data.py @@ -53,7 +53,7 @@ def pairlists_evaluate( payload: DownloadDataPayload, background_tasks: BackgroundTasks, config=Depends(get_config) ): if ApiBG.download_data_running: - raise HTTPException(status_code=400, detail="Pairlist evaluation is already running.") + raise HTTPException(status_code=400, detail="Data Download is already running.") config_loc = deepcopy(config) config_loc["stake_currency"] = "" config_loc["pairs"] = payload.pairs diff --git a/tests/rpc/test_rpc_apiserver.py b/tests/rpc/test_rpc_apiserver.py index 730a6b05e..ee28bcc6c 100644 --- a/tests/rpc/test_rpc_apiserver.py +++ b/tests/rpc/test_rpc_apiserver.py @@ -691,20 +691,22 @@ def test_api_show_config(botclient): def test_api_daily(botclient, mocker, ticker, fee, markets): ftbot, client = botclient - patch_get_signal(ftbot) - mocker.patch.multiple( - EXMS, - get_balances=MagicMock(return_value=ticker), - fetch_ticker=ticker, - get_fee=fee, - markets=PropertyMock(return_value=markets), - ) + + ftbot.config["dry_run"] = False + mocker.patch(f"{EXMS}.get_balances", return_value=ticker) + mocker.patch(f"{EXMS}.get_tickers", ticker) + mocker.patch(f"{EXMS}.get_fee", fee) + mocker.patch(f"{EXMS}.markets", PropertyMock(return_value=markets)) + ftbot.wallets.update() + rc = client_get(client, f"{BASE_URI}/daily") assert_response(rc) - assert len(rc.json()["data"]) == 7 - assert rc.json()["stake_currency"] == "BTC" - assert rc.json()["fiat_display_currency"] == "USD" - assert rc.json()["data"][0]["date"] == str(datetime.now(timezone.utc).date()) + response = rc.json() + assert "data" in response + assert len(response["data"]) == 7 + assert response["stake_currency"] == "BTC" + assert response["fiat_display_currency"] == "USD" + assert response["data"][0]["date"] == str(datetime.now(timezone.utc).date()) def test_api_weekly(botclient, mocker, ticker, fee, markets, time_machine): @@ -2861,3 +2863,74 @@ def test_api_ws_send_msg(default_conf, mocker, caplog): finally: ApiServer.shutdown() ApiServer.shutdown() + + +def test_api_download_data(botclient, mocker, tmp_path, caplog): + ftbot, client = botclient + + rc = client_post(client, f"{BASE_URI}/download_data", data={}) + assert_response(rc, 503) + assert rc.json()["detail"] == "Bot is not in the correct state." + + ftbot.config["runmode"] = RunMode.WEBSERVER + ftbot.config["user_data_dir"] = tmp_path + + body = { + "pairs": ["ETH/BTC", "XRP/BTC"], + "timeframes": ["5m"], + } + + # Fail, already running + ApiBG.download_data_running = True + rc = client_post(client, f"{BASE_URI}/download_data", body) + assert_response(rc, 400) + assert rc.json()["detail"] == "Data Download is already running." + + # Reset running state + ApiBG.download_data_running = False + + # Test successful download + mocker.patch( + "freqtrade.data.history.history_utils.download_data", + return_value=None, + ) + + rc = client_post(client, f"{BASE_URI}/download_data", body) + assert_response(rc) + assert rc.json()["status"] == "Data Download started in background." + job_id = rc.json()["job_id"] + + rc = client_get(client, f"{BASE_URI}/background/{job_id}") + assert_response(rc) + response = rc.json() + assert response["job_id"] == job_id + assert response["job_category"] == "download_data" + # Job finishes immediately due to mock. + assert response["status"] == "success" + + # Background list contains the job + rc = client_get(client, f"{BASE_URI}/background") + assert_response(rc) + response = rc.json() + assert isinstance(response, list) + assert len(response) == 1 + assert response[0]["job_id"] == job_id + + # Test error case + ApiBG.download_data_running = False + mocker.patch( + "freqtrade.data.history.history_utils.download_data", + side_effect=OperationalException("Download error"), + ) + rc = client_post(client, f"{BASE_URI}/download_data", body) + assert_response(rc) + assert rc.json()["status"] == "Data Download started in background." + job_id = rc.json()["job_id"] + + rc = client_get(client, f"{BASE_URI}/background/{job_id}") + assert_response(rc) + response = rc.json() + assert response["job_id"] == job_id + assert response["job_category"] == "download_data" + assert response["status"] == "failed" + assert response["error"] == "Download error"