apiserver: implement tests for start() and stop()

This commit is contained in:
gcarq 2018-06-23 12:52:29 +02:00
parent d0b729e78e
commit a0c9a9585d
3 changed files with 64 additions and 2 deletions

View File

@ -1,3 +1,4 @@
import json
import threading import threading
import logging import logging
# import json # import json
@ -138,7 +139,7 @@ class ApiServer(RPC):
Starts TradeThread in bot if stopped. Starts TradeThread in bot if stopped.
""" """
msg = self._rpc_start() msg = self._rpc_start()
return jsonify(msg) return json.dumps(msg)
def stop(self): def stop(self):
""" """
@ -147,4 +148,4 @@ class ApiServer(RPC):
Stops TradeThread in bot if running Stops TradeThread in bot if running
""" """
msg = self._rpc_stop() msg = self._rpc_stop()
return jsonify(msg) return json.dumps(msg)

View File

@ -83,6 +83,15 @@ def patch_coinmarketcap(mocker, value: Optional[Dict[str, float]] = None) -> Non
) )
def patch_apiserver(mocker) -> None:
mocker.patch.multiple(
'freqtrade.rpc.api_server.ApiServer',
run=MagicMock(),
register_rest_other=MagicMock(),
register_rest_rpc_urls=MagicMock(),
)
@pytest.fixture(scope="function") @pytest.fixture(scope="function")
def default_conf(): def default_conf():
""" Returns validated configuration suitable for most tests """ """ Returns validated configuration suitable for most tests """

View File

@ -0,0 +1,52 @@
"""
Unit test file for rpc/api_server.py
"""
from unittest.mock import MagicMock
from freqtrade.rpc.api_server import ApiServer
from freqtrade.state import State
from freqtrade.tests.conftest import get_patched_freqtradebot, patch_apiserver
def test__init__(default_conf, mocker):
"""
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
def test_start_endpoint(default_conf, mocker):
"""Test /start endpoint"""
patch_apiserver(mocker)
bot = get_patched_freqtradebot(mocker, default_conf)
apiserver = ApiServer(bot)
bot.state = State.STOPPED
assert bot.state == State.STOPPED
result = apiserver.start()
assert result == '{"status": "starting trader ..."}'
assert bot.state == State.RUNNING
result = apiserver.start()
assert result == '{"status": "already running"}'
def test_stop_endpoint(default_conf, mocker):
"""Test /stop endpoint"""
patch_apiserver(mocker)
bot = get_patched_freqtradebot(mocker, default_conf)
apiserver = ApiServer(bot)
bot.state = State.RUNNING
assert bot.state == State.RUNNING
result = apiserver.stop()
assert result == '{"status": "stopping trader ..."}'
assert bot.state == State.STOPPED
result = apiserver.stop()
assert result == '{"status": "already stopped"}'