stable/freqtrade/tests/rpc/test_rpc_apiserver.py

80 lines
2.2 KiB
Python
Raw Normal View History

2019-04-04 05:12:58 +00:00
"""
Unit test file for rpc/api_server.py
"""
from unittest.mock import MagicMock
2019-05-10 05:07:38 +00:00
import pytest
2019-04-04 05:12:58 +00:00
from freqtrade.rpc.api_server import ApiServer
from freqtrade.state import State
from freqtrade.tests.conftest import get_patched_freqtradebot, patch_apiserver
2019-05-10 05:07:38 +00:00
@pytest.fixture
2019-05-11 06:55:21 +00:00
def botclient(default_conf, mocker):
ftbot = get_patched_freqtradebot(mocker, default_conf)
apiserver = ApiServer(ftbot)
yield ftbot, apiserver.app.test_client()
2019-05-10 05:07:38 +00:00
# Cleanup ... ?
def response_success_assert(response):
assert response.status_code == 200
assert response.content_type == "application/json"
2019-05-11 06:55:21 +00:00
def test_api_stop_workflow(botclient):
ftbot, client = botclient
assert ftbot.state == State.RUNNING
rc = client.post("/stop")
2019-05-10 05:07:38 +00:00
response_success_assert(rc)
2019-05-11 06:55:21 +00:00
assert rc.json == {'status': 'stopping trader ...'}
assert ftbot.state == State.STOPPED
2019-05-10 05:07:38 +00:00
2019-05-11 06:55:21 +00:00
# Stop bot again
2019-05-10 05:07:38 +00:00
rc = client.post("/stop")
response_success_assert(rc)
2019-05-11 06:55:21 +00:00
assert rc.json == {'status': 'already stopped'}
# Start bot
rc = client.post("/start")
response_success_assert(rc)
assert rc.json == {'status': 'starting trader ...'}
assert ftbot.state == State.RUNNING
# Call start again
rc = client.post("/start")
response_success_assert(rc)
assert rc.json == {'status': 'already running'}
2019-05-10 05:07:38 +00:00
2019-05-11 06:55:21 +00:00
def test_api__init__(default_conf, mocker):
2019-04-04 05:12:58 +00:00
"""
Test __init__() method
"""
mocker.patch('freqtrade.rpc.telegram.Updater', MagicMock())
mocker.patch('freqtrade.rpc.api_server.ApiServer.run', MagicMock())
apiserver = ApiServer(get_patched_freqtradebot(mocker, default_conf))
assert apiserver._config == default_conf
2019-05-11 06:55:21 +00:00
def test_api_reloadconf(botclient):
ftbot, client = botclient
2019-04-04 05:12:58 +00:00
2019-05-11 06:55:21 +00:00
rc = client.post("/reload_conf")
response_success_assert(rc)
assert rc.json == {'status': 'reloading config ...'}
assert ftbot.state == State.RELOAD_CONF
2019-04-04 05:12:58 +00:00
2019-05-11 06:55:21 +00:00
def test_api_stopbuy(botclient):
ftbot, client = botclient
assert ftbot.config['max_open_trades'] != 0
2019-04-04 05:12:58 +00:00
2019-05-11 06:55:21 +00:00
rc = client.post("/stopbuy")
response_success_assert(rc)
assert rc.json == {'status': 'No more buy will occur from now. Run /reload_conf to reset.'}
assert ftbot.config['max_open_trades'] == 0