Better test hyperopt writing and reading

This commit is contained in:
Matthias 2021-05-12 06:32:55 +02:00
parent 3cbe40875d
commit 5f5597b93f
4 changed files with 38 additions and 54 deletions

View File

@ -31,23 +31,7 @@ from .hyperopts.default_hyperopt import DefaultHyperOpt
# Functions for recurrent object patching # Functions for recurrent object patching
def create_results(mocker, hyperopt, testdatadir) -> List[Dict]: def create_results() -> 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')
return [{'loss': 1, 'result': 'foo', 'params': {}, 'is_best': True}] 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 == [] assert caplog.record_tuples == []
def test_save_results_saves_epochs(mocker, hyperopt, testdatadir, caplog) -> None: def test_save_results_saves_epochs(mocker, hyperopt, tmpdir, caplog) -> None:
epochs = create_results(mocker, hyperopt, testdatadir) # Test writing to temp dir and reading again
mock_dump = mocker.patch('freqtrade.optimize.hyperopt.dump', return_value=None) epochs = create_results()
mock_dump_json = mocker.patch('freqtrade.optimize.hyperopt.file_dump_json', return_value=None) hyperopt.results_file = Path(tmpdir / 'ut_results.fthypt')
results_file = testdatadir / 'optimize' / 'ut_results.pickle'
caplog.set_level(logging.DEBUG) caplog.set_level(logging.DEBUG)
hyperopt.epochs = epochs for epoch in epochs:
hyperopt._save_results() hyperopt._save_result(epoch)
assert log_has(f"1 epoch saved to '{results_file}'.", caplog) assert log_has(f"1 epoch saved to '{hyperopt.results_file}'.", caplog)
mock_dump.assert_called_once()
mock_dump_json.assert_called_once()
hyperopt.epochs = epochs + epochs hyperopt._save_result(epochs[0])
hyperopt._save_results() assert log_has(f"2 epochs saved to '{hyperopt.results_file}'.", caplog)
assert log_has(f"2 epochs saved to '{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: def test_load_previous_results(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()
results_file = testdatadir / 'hyperopt_results_SampleStrategy.pickle'
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'
hyperopt_epochs = HyperoptTools.load_previous_results(results_file) hyperopt_epochs = HyperoptTools.load_previous_results(results_file)
assert hyperopt_epochs == epochs assert len(hyperopt_epochs) == 5
mock_load.assert_called_once() assert log_has_re(r"Reading pickled epochs from .*", caplog)
del epochs[0]['is_best'] caplog.clear()
mock_load = mocker.patch('joblib.load', return_value=epochs)
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) HyperoptTools.load_previous_results(results_file)

View File

@ -1,5 +1,5 @@
import re import re
from datetime import datetime, timedelta, timezone from datetime import timedelta
from pathlib import Path from pathlib import Path
import pandas as pd import pandas as pd

Binary file not shown.

File diff suppressed because one or more lines are too long