diff --git a/freqtrade/misc.py b/freqtrade/misc.py index afc4334e8..00d431723 100644 --- a/freqtrade/misc.py +++ b/freqtrade/misc.py @@ -14,6 +14,11 @@ from freqtrade import __version__ logger = logging.getLogger(__name__) +def file_dump_json(filename, data): + with open(filename, 'w') as fp: + json.dump(data, fp) + + class State(enum.Enum): RUNNING = 0 STOPPED = 1 diff --git a/freqtrade/optimize/__init__.py b/freqtrade/optimize/__init__.py index 2d73c3215..613bb7b17 100644 --- a/freqtrade/optimize/__init__.py +++ b/freqtrade/optimize/__init__.py @@ -8,6 +8,7 @@ from pandas import DataFrame from freqtrade.exchange import get_ticker_history from freqtrade.optimize.hyperopt_conf import hyperopt_optimize_conf from freqtrade.analyze import populate_indicators, parse_ticker_dataframe +from freqtrade import misc logger = logging.getLogger(__name__) @@ -127,7 +128,6 @@ def download_backtesting_testdata(datadir: str, pair: str, interval: int = 5) -> logger.debug("New End: {}".format(data[-1:][0]['T'])) data = sorted(data, key=lambda data: data['T']) - with open(filename, "wt") as fp: - json.dump(data, fp) + misc.file_dump_json(filename, data) return True diff --git a/freqtrade/tests/test_misc.py b/freqtrade/tests/test_misc.py index 0b85000cb..e0cd70709 100644 --- a/freqtrade/tests/test_misc.py +++ b/freqtrade/tests/test_misc.py @@ -5,10 +5,11 @@ import argparse from copy import deepcopy import pytest +from unittest.mock import MagicMock from jsonschema import ValidationError from freqtrade.misc import throttle, parse_args, load_config,\ - parse_args_common + parse_args_common, file_dump_json def test_throttle(): @@ -135,6 +136,14 @@ def test_parse_args_hyperopt_custom(mocker): assert call_args.func is not None +def test_file_dump_json(default_conf, mocker): + file_open = mocker.patch('freqtrade.misc.open', MagicMock()) + json_dump = mocker.patch('json.dump', MagicMock()) + file_dump_json('somefile', [1, 2, 3]) + assert file_open.call_count == 1 + assert json_dump.call_count == 1 + + def test_load_config(default_conf, mocker): file_mock = mocker.patch('freqtrade.misc.open', mocker.mock_open( read_data=json.dumps(default_conf) diff --git a/freqtrade/tests/testdata/download_backtest_data.py b/freqtrade/tests/testdata/download_backtest_data.py index 37cd4c95f..32c36073b 100755 --- a/freqtrade/tests/testdata/download_backtest_data.py +++ b/freqtrade/tests/testdata/download_backtest_data.py @@ -1,11 +1,11 @@ #!/usr/bin/env python3 """This script generate json data from bittrex""" -import json from os import path from freqtrade import exchange from freqtrade.exchange import Bittrex +from freqtrade import misc PAIRS = [ 'BTC_BCC', 'BTC_ETH', 'BTC_MER', 'BTC_POWR', 'BTC_ETC', @@ -25,5 +25,4 @@ for pair in PAIRS: pair, TICKER_INTERVAL, )) - with open(filename, 'w') as fp: - json.dump(data, fp) + misc.file_dump_json(filename, data)