tests: add test for download_data api method

This commit is contained in:
Matthias
2024-11-17 15:28:49 +01:00
parent d8a2c59b04
commit 19f23106e7
2 changed files with 86 additions and 13 deletions
@@ -53,7 +53,7 @@ def pairlists_evaluate(
payload: DownloadDataPayload, background_tasks: BackgroundTasks, config=Depends(get_config) payload: DownloadDataPayload, background_tasks: BackgroundTasks, config=Depends(get_config)
): ):
if ApiBG.download_data_running: 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 = deepcopy(config)
config_loc["stake_currency"] = "" config_loc["stake_currency"] = ""
config_loc["pairs"] = payload.pairs config_loc["pairs"] = payload.pairs
+85 -12
View File
@@ -691,20 +691,22 @@ def test_api_show_config(botclient):
def test_api_daily(botclient, mocker, ticker, fee, markets): def test_api_daily(botclient, mocker, ticker, fee, markets):
ftbot, client = botclient ftbot, client = botclient
patch_get_signal(ftbot)
mocker.patch.multiple( ftbot.config["dry_run"] = False
EXMS, mocker.patch(f"{EXMS}.get_balances", return_value=ticker)
get_balances=MagicMock(return_value=ticker), mocker.patch(f"{EXMS}.get_tickers", ticker)
fetch_ticker=ticker, mocker.patch(f"{EXMS}.get_fee", fee)
get_fee=fee, mocker.patch(f"{EXMS}.markets", PropertyMock(return_value=markets))
markets=PropertyMock(return_value=markets), ftbot.wallets.update()
)
rc = client_get(client, f"{BASE_URI}/daily") rc = client_get(client, f"{BASE_URI}/daily")
assert_response(rc) assert_response(rc)
assert len(rc.json()["data"]) == 7 response = rc.json()
assert rc.json()["stake_currency"] == "BTC" assert "data" in response
assert rc.json()["fiat_display_currency"] == "USD" assert len(response["data"]) == 7
assert rc.json()["data"][0]["date"] == str(datetime.now(timezone.utc).date()) 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): 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: finally:
ApiServer.shutdown() ApiServer.shutdown()
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"