Skip to content

Commit

Permalink
Merge pull request freqtrade#1096 from freqtrade/cleaner-tests
Browse files Browse the repository at this point in the history
Cleaning unit tests, first set
  • Loading branch information
vertti authored Jul 31, 2018
2 parents 2d7ef30 + f85cc42 commit 1044d15
Show file tree
Hide file tree
Showing 12 changed files with 186 additions and 598 deletions.
3 changes: 0 additions & 3 deletions freqtrade/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@

import arrow
import pytest
from jsonschema import validate
from telegram import Chat, Message, Update

from freqtrade import constants
from freqtrade.exchange.exchange_helpers import parse_ticker_dataframe
from freqtrade.exchange import Exchange
from freqtrade.freqtradebot import FreqtradeBot
Expand Down Expand Up @@ -127,7 +125,6 @@ def default_conf():
"db_url": "sqlite://",
"loglevel": logging.DEBUG,
}
validate(configuration, constants.CONF_SCHEMA)
return configuration


Expand Down
13 changes: 4 additions & 9 deletions freqtrade/tests/exchange/test_exchange.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# pragma pylint: disable=missing-docstring, C0103, bad-continuation, global-statement
# pragma pylint: disable=protected-access
import logging
from copy import deepcopy
from datetime import datetime
from random import randint
from unittest.mock import MagicMock, PropertyMock
Expand All @@ -15,8 +14,6 @@


def ccxt_exceptionhandlers(mocker, default_conf, api_mock, fun, mock_ccxt_fun, **kwargs):
"""Function to test ccxt exception handling """

with pytest.raises(TemporaryError):
api_mock.__dict__[mock_ccxt_fun] = MagicMock(side_effect=ccxt.NetworkError)
exchange = get_patched_exchange(mocker, default_conf, api_mock)
Expand Down Expand Up @@ -167,12 +164,11 @@ def test_validate_pairs_not_compatible(default_conf, mocker):
api_mock.load_markets = MagicMock(return_value={
'ETH/BTC': '', 'TKN/BTC': '', 'TRST/BTC': '', 'SWT/BTC': '', 'BCC/BTC': ''
})
conf = deepcopy(default_conf)
conf['stake_currency'] = 'ETH'
default_conf['stake_currency'] = 'ETH'
mocker.patch('freqtrade.exchange.Exchange._init_ccxt', MagicMock(return_value=api_mock))
mocker.patch('freqtrade.exchange.Exchange.validate_timeframes', MagicMock())
with pytest.raises(OperationalException, match=r'not compatible'):
Exchange(conf)
Exchange(default_conf)


def test_validate_pairs_exception(default_conf, mocker, caplog):
Expand All @@ -197,8 +193,7 @@ def test_validate_pairs_exception(default_conf, mocker, caplog):

def test_validate_pairs_stake_exception(default_conf, mocker, caplog):
caplog.set_level(logging.INFO)
conf = deepcopy(default_conf)
conf['stake_currency'] = 'ETH'
default_conf['stake_currency'] = 'ETH'
api_mock = MagicMock()
api_mock.name = MagicMock(return_value='binance')
mocker.patch('freqtrade.exchange.Exchange._init_ccxt', api_mock)
Expand All @@ -208,7 +203,7 @@ def test_validate_pairs_stake_exception(default_conf, mocker, caplog):
OperationalException,
match=r'Pair ETH/BTC not compatible with stake_currency: ETH'
):
Exchange(conf)
Exchange(default_conf)


def test_validate_timeframes(default_conf, mocker):
Expand Down
86 changes: 18 additions & 68 deletions freqtrade/tests/optimize/test_backtesting.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import json
import math
import random
from copy import deepcopy
from typing import List
from unittest.mock import MagicMock

Expand Down Expand Up @@ -164,9 +163,6 @@ def _trend_alternate(dataframe=None, metadata=None):

# Unit tests
def test_setup_configuration_without_arguments(mocker, default_conf, caplog) -> None:
"""
Test setup_configuration() function
"""
mocker.patch('freqtrade.configuration.open', mocker.mock_open(
read_data=json.dumps(default_conf)
))
Expand Down Expand Up @@ -205,9 +201,6 @@ def test_setup_configuration_without_arguments(mocker, default_conf, caplog) ->


def test_setup_configuration_with_arguments(mocker, default_conf, caplog) -> None:
"""
Test setup_configuration() function
"""
mocker.patch('freqtrade.configuration.open', mocker.mock_open(
read_data=json.dumps(default_conf)
))
Expand Down Expand Up @@ -276,15 +269,10 @@ def test_setup_configuration_with_arguments(mocker, default_conf, caplog) -> Non


def test_setup_configuration_unlimited_stake_amount(mocker, default_conf, caplog) -> None:
"""
Test setup_configuration() function
"""

conf = deepcopy(default_conf)
conf['stake_amount'] = constants.UNLIMITED_STAKE_AMOUNT
default_conf['stake_amount'] = constants.UNLIMITED_STAKE_AMOUNT

mocker.patch('freqtrade.configuration.open', mocker.mock_open(
read_data=json.dumps(conf)
read_data=json.dumps(default_conf)
))

args = [
Expand All @@ -298,9 +286,6 @@ def test_setup_configuration_unlimited_stake_amount(mocker, default_conf, caplog


def test_start(mocker, fee, default_conf, caplog) -> None:
"""
Test start() function
"""
start_mock = MagicMock()
mocker.patch('freqtrade.exchange.Exchange.get_fee', fee)
patch_exchange(mocker)
Expand All @@ -323,9 +308,6 @@ def test_start(mocker, fee, default_conf, caplog) -> None:


def test_backtesting_init(mocker, default_conf) -> None:
"""
Test Backtesting._init() method
"""
patch_exchange(mocker)
get_fee = mocker.patch('freqtrade.exchange.Exchange.get_fee', MagicMock(return_value=0.5))
backtesting = Backtesting(default_conf)
Expand All @@ -339,9 +321,6 @@ def test_backtesting_init(mocker, default_conf) -> None:


def test_tickerdata_to_dataframe(default_conf, mocker) -> None:
"""
Test Backtesting.tickerdata_to_dataframe() method
"""
patch_exchange(mocker)
timerange = TimeRange(None, 'line', 0, -100)
tick = optimize.load_tickerdata_file(None, 'UNITTEST/BTC', '1m', timerange=timerange)
Expand All @@ -358,9 +337,6 @@ def test_tickerdata_to_dataframe(default_conf, mocker) -> None:


def test_get_timeframe(default_conf, mocker) -> None:
"""
Test Backtesting.get_timeframe() method
"""
patch_exchange(mocker)
backtesting = Backtesting(default_conf)

Expand All @@ -377,9 +353,6 @@ def test_get_timeframe(default_conf, mocker) -> None:


def test_generate_text_table(default_conf, mocker):
"""
Test Backtesting.generate_text_table() method
"""
patch_exchange(mocker)
backtesting = Backtesting(default_conf)

Expand Down Expand Up @@ -408,9 +381,6 @@ def test_generate_text_table(default_conf, mocker):


def test_generate_text_table_sell_reason(default_conf, mocker):
"""
Test Backtesting.generate_text_table_sell_reason() method
"""
patch_exchange(mocker)
backtesting = Backtesting(default_conf)

Expand All @@ -437,10 +407,6 @@ def test_generate_text_table_sell_reason(default_conf, mocker):


def test_backtesting_start(default_conf, mocker, caplog) -> None:
"""
Test Backtesting.start() method
"""

def get_timeframe(input1, input2):
return Arrow(2017, 11, 14, 21, 17), Arrow(2017, 11, 14, 22, 59)

Expand All @@ -454,15 +420,14 @@ def get_timeframe(input1, input2):
get_timeframe=get_timeframe,
)

conf = deepcopy(default_conf)
conf['exchange']['pair_whitelist'] = ['UNITTEST/BTC']
conf['ticker_interval'] = 1
conf['live'] = False
conf['datadir'] = None
conf['export'] = None
conf['timerange'] = '-100'
default_conf['exchange']['pair_whitelist'] = ['UNITTEST/BTC']
default_conf['ticker_interval'] = 1
default_conf['live'] = False
default_conf['datadir'] = None
default_conf['export'] = None
default_conf['timerange'] = '-100'

backtesting = Backtesting(conf)
backtesting = Backtesting(default_conf)
backtesting.start()
# check the logs, that will contain the backtest result
exists = [
Expand All @@ -477,10 +442,6 @@ def get_timeframe(input1, input2):


def test_backtesting_start_no_data(default_conf, mocker, caplog) -> None:
"""
Test Backtesting.start() method if no data is found
"""

def get_timeframe(input1, input2):
return Arrow(2017, 11, 14, 21, 17), Arrow(2017, 11, 14, 22, 59)

Expand All @@ -494,25 +455,21 @@ def get_timeframe(input1, input2):
get_timeframe=get_timeframe,
)

conf = deepcopy(default_conf)
conf['exchange']['pair_whitelist'] = ['UNITTEST/BTC']
conf['ticker_interval'] = "1m"
conf['live'] = False
conf['datadir'] = None
conf['export'] = None
conf['timerange'] = '20180101-20180102'
default_conf['exchange']['pair_whitelist'] = ['UNITTEST/BTC']
default_conf['ticker_interval'] = "1m"
default_conf['live'] = False
default_conf['datadir'] = None
default_conf['export'] = None
default_conf['timerange'] = '20180101-20180102'

backtesting = Backtesting(conf)
backtesting = Backtesting(default_conf)
backtesting.start()
# check the logs, that will contain the backtest result

assert log_has('No data found. Terminating.', caplog.record_tuples)


def test_backtest(default_conf, fee, mocker) -> None:
"""
Test Backtesting.backtest() method
"""
mocker.patch('freqtrade.exchange.Exchange.get_fee', fee)
patch_exchange(mocker)
backtesting = Backtesting(default_conf)
Expand Down Expand Up @@ -560,9 +517,6 @@ def test_backtest(default_conf, fee, mocker) -> None:


def test_backtest_1min_ticker_interval(default_conf, fee, mocker) -> None:
"""
Test Backtesting.backtest() method with 1 min ticker
"""
mocker.patch('freqtrade.exchange.Exchange.get_fee', fee)
patch_exchange(mocker)
backtesting = Backtesting(default_conf)
Expand All @@ -583,9 +537,6 @@ def test_backtest_1min_ticker_interval(default_conf, fee, mocker) -> None:


def test_processed(default_conf, mocker) -> None:
"""
Test Backtesting.backtest() method with offline data
"""
patch_exchange(mocker)
backtesting = Backtesting(default_conf)

Expand Down Expand Up @@ -725,15 +676,14 @@ def test_backtest_record(default_conf, fee, mocker):


def test_backtest_start_live(default_conf, mocker, caplog):
conf = deepcopy(default_conf)
conf['exchange']['pair_whitelist'] = ['UNITTEST/BTC']
default_conf['exchange']['pair_whitelist'] = ['UNITTEST/BTC']
mocker.patch('freqtrade.exchange.Exchange.get_ticker_history',
new=lambda s, n, i: _load_pair_as_ticks(n, i))
patch_exchange(mocker)
mocker.patch('freqtrade.optimize.backtesting.Backtesting.backtest', MagicMock())
mocker.patch('freqtrade.optimize.backtesting.Backtesting._generate_text_table', MagicMock())
mocker.patch('freqtrade.configuration.open', mocker.mock_open(
read_data=json.dumps(conf)
read_data=json.dumps(default_conf)
))

args = MagicMock()
Expand Down
Loading

0 comments on commit 1044d15

Please sign in to comment.