mirror of
https://github.com/freqtrade/freqtrade.git
synced 2024-11-10 10:21:59 +00:00
Better test hyperopt writing and reading
This commit is contained in:
parent
3cbe40875d
commit
5f5597b93f
|
@ -31,23 +31,7 @@ from .hyperopts.default_hyperopt import DefaultHyperOpt
|
|||
|
||||
|
||||
# Functions for recurrent object patching
|
||||
def create_results(mocker, hyperopt, testdatadir) -> List[Dict]:
|
||||
"""
|
||||
When creating results, mock the hyperopt so that *by default*
|
||||
- we don't create any pickle'd files in the filesystem
|
||||
- we might have a pickle'd file so make sure that we return
|
||||
false when looking for it
|
||||
"""
|
||||
hyperopt.results_file = testdatadir / 'optimize/ut_results.pickle'
|
||||
|
||||
mocker.patch.object(Path, "is_file", MagicMock(return_value=False))
|
||||
stat_mock = MagicMock()
|
||||
stat_mock.st_size = 1
|
||||
mocker.patch.object(Path, "stat", MagicMock(return_value=stat_mock))
|
||||
|
||||
mocker.patch.object(Path, "unlink", MagicMock(return_value=True))
|
||||
mocker.patch('freqtrade.optimize.hyperopt.dump', return_value=None)
|
||||
mocker.patch('freqtrade.optimize.hyperopt.file_dump_json')
|
||||
def create_results() -> List[Dict]:
|
||||
|
||||
return [{'loss': 1, 'result': 'foo', 'params': {}, 'is_best': True}]
|
||||
|
||||
|
@ -321,54 +305,49 @@ def test_no_log_if_loss_does_not_improve(hyperopt, caplog) -> None:
|
|||
assert caplog.record_tuples == []
|
||||
|
||||
|
||||
def test_save_results_saves_epochs(mocker, hyperopt, testdatadir, caplog) -> None:
|
||||
epochs = create_results(mocker, hyperopt, testdatadir)
|
||||
mock_dump = mocker.patch('freqtrade.optimize.hyperopt.dump', return_value=None)
|
||||
mock_dump_json = mocker.patch('freqtrade.optimize.hyperopt.file_dump_json', return_value=None)
|
||||
results_file = testdatadir / 'optimize' / 'ut_results.pickle'
|
||||
def test_save_results_saves_epochs(mocker, hyperopt, tmpdir, caplog) -> None:
|
||||
# Test writing to temp dir and reading again
|
||||
epochs = create_results()
|
||||
hyperopt.results_file = Path(tmpdir / 'ut_results.fthypt')
|
||||
|
||||
caplog.set_level(logging.DEBUG)
|
||||
|
||||
hyperopt.epochs = epochs
|
||||
hyperopt._save_results()
|
||||
assert log_has(f"1 epoch saved to '{results_file}'.", caplog)
|
||||
mock_dump.assert_called_once()
|
||||
mock_dump_json.assert_called_once()
|
||||
for epoch in epochs:
|
||||
hyperopt._save_result(epoch)
|
||||
assert log_has(f"1 epoch saved to '{hyperopt.results_file}'.", caplog)
|
||||
|
||||
hyperopt.epochs = epochs + epochs
|
||||
hyperopt._save_results()
|
||||
assert log_has(f"2 epochs saved to '{results_file}'.", caplog)
|
||||
hyperopt._save_result(epochs[0])
|
||||
assert log_has(f"2 epochs saved to '{hyperopt.results_file}'.", caplog)
|
||||
|
||||
hyperopt_epochs = HyperoptTools.load_previous_results(hyperopt.results_file)
|
||||
assert len(hyperopt_epochs) == 2
|
||||
|
||||
|
||||
def test_read_results_returns_epochs(mocker, hyperopt, testdatadir, caplog) -> None:
|
||||
epochs = create_results(mocker, hyperopt, testdatadir)
|
||||
mock_load = mocker.patch('joblib.load', return_value=epochs)
|
||||
results_file = testdatadir / 'optimize' / 'ut_results.pickle'
|
||||
hyperopt_epochs = HyperoptTools._read_results(results_file)
|
||||
assert log_has(f"Reading epochs from '{results_file}'", caplog)
|
||||
assert hyperopt_epochs == epochs
|
||||
mock_load.assert_called_once()
|
||||
def test_load_previous_results(testdatadir, caplog) -> None:
|
||||
|
||||
|
||||
def test_load_previous_results(mocker, hyperopt, testdatadir, caplog) -> None:
|
||||
epochs = create_results(mocker, hyperopt, testdatadir)
|
||||
mock_load = mocker.patch('joblib.load', return_value=epochs)
|
||||
mocker.patch.object(Path, 'is_file', MagicMock(return_value=True))
|
||||
statmock = MagicMock()
|
||||
statmock.st_size = 5
|
||||
# mocker.patch.object(Path, 'stat', MagicMock(return_value=statmock))
|
||||
|
||||
results_file = testdatadir / 'optimize' / 'ut_results.pickle'
|
||||
results_file = testdatadir / 'hyperopt_results_SampleStrategy.pickle'
|
||||
|
||||
hyperopt_epochs = HyperoptTools.load_previous_results(results_file)
|
||||
|
||||
assert hyperopt_epochs == epochs
|
||||
mock_load.assert_called_once()
|
||||
assert len(hyperopt_epochs) == 5
|
||||
assert log_has_re(r"Reading pickled epochs from .*", caplog)
|
||||
|
||||
del epochs[0]['is_best']
|
||||
mock_load = mocker.patch('joblib.load', return_value=epochs)
|
||||
caplog.clear()
|
||||
|
||||
with pytest.raises(OperationalException):
|
||||
# Modern version
|
||||
results_file = testdatadir / 'strategy_SampleStrategy.fthypt'
|
||||
|
||||
hyperopt_epochs = HyperoptTools.load_previous_results(results_file)
|
||||
|
||||
assert len(hyperopt_epochs) == 5
|
||||
assert log_has_re(r"Reading epochs from .*", caplog)
|
||||
|
||||
|
||||
def test_load_previous_results2(mocker, testdatadir, caplog) -> None:
|
||||
mocker.patch('freqtrade.optimize.hyperopt_tools.HyperoptTools._read_results_pickle',
|
||||
return_value=[{'asdf': '222'}])
|
||||
results_file = testdatadir / 'hyperopt_results_SampleStrategy.pickle'
|
||||
with pytest.raises(OperationalException, match=r"The file .* incompatible.*"):
|
||||
HyperoptTools.load_previous_results(results_file)
|
||||
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import re
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from datetime import timedelta
|
||||
from pathlib import Path
|
||||
|
||||
import pandas as pd
|
||||
|
|
BIN
tests/testdata/hyperopt_results_SampleStrategy.pickle
vendored
Normal file
BIN
tests/testdata/hyperopt_results_SampleStrategy.pickle
vendored
Normal file
Binary file not shown.
5
tests/testdata/strategy_SampleStrategy.fthypt
vendored
Normal file
5
tests/testdata/strategy_SampleStrategy.fthypt
vendored
Normal file
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user