1414# You should have received a copy of the GNU General Public
1515# License along with OctoBot-Script. If not, see <https://www.gnu.org/licenses/>.
1616
17+ import mock
1718import pytest
18- import os
19+ import shutil
1920import tulipy
2021
2122import octobot_script as obs
23+ import octobot_script .model .backtest_plot as backtest_plot
2224from octobot_script .api .ploting import generate_and_show_report
2325from tests .functionnal import one_day_btc_usdt_data
2426
2729pytestmark = pytest .mark .asyncio
2830
2931
30- async def test_precomputed_vs_iteration_rsi (one_day_btc_usdt_data ):
32+ @pytest .fixture
33+ def mock_backtest_report_server_serve ():
34+ with mock .patch .object (
35+ backtest_plot .BacktestReportServer , "serve" , mock .Mock ()
36+ ) as serve_mock :
37+ yield serve_mock
38+
39+
40+ async def test_precomputed_vs_iteration_rsi (
41+ one_day_btc_usdt_data , mock_backtest_report_server_serve
42+ ):
3143 # 1. pre-compute entries at first iteration only
3244 async def _pre_compute_update (ctx ):
3345 if run_data ["entries" ] is None :
@@ -79,7 +91,9 @@ async def _pre_compute_update(ctx):
7991 assert res .duration < 10
8092 assert res .candles_count == 1947
8193 await _check_report (res )
94+ mock_backtest_report_server_serve .assert_called_once ()
8295
96+ await _reload_backtest_data (one_day_btc_usdt_data )
8397 # ensure second run gives the same result
8498 run_data = {
8599 "entries" : None ,
@@ -101,6 +115,7 @@ async def _pre_compute_update(ctx):
101115 != res_2 .report ["bot_report" ]["starting_portfolio" ]["binance" ]
102116 )
103117
118+ await _reload_backtest_data (one_day_btc_usdt_data )
104119 # try with different config
105120 run_data = {
106121 "entries" : None ,
@@ -146,6 +161,7 @@ async def _iterations_update(ctx):
146161 take_profit_offset = "25%" ,
147162 )
148163
164+ await _reload_backtest_data (one_day_btc_usdt_data )
149165 res_iteration = await obs .run (
150166 one_day_btc_usdt_data ,
151167 config ,
@@ -160,23 +176,20 @@ async def _iterations_update(ctx):
160176 )
161177
162178
179+ async def _reload_backtest_data (backtesting_data ):
180+ """Recreate importers after a run; OctoBot closes DB handles when the run ends."""
181+ await backtesting_data .initialize ()
182+ backtesting_data .preloaded_candle_managers = {}
183+
184+
163185async def _check_report (res ):
164186 description = res .describe ()
165187 assert str (res .strategy_config ) in description
166- report = "report.html"
167188 import os
168189 import octobot_script .resources as resources
169190
170191 dist_index = resources .get_report_resource_path ("dist/index.html" )
171- if not os .path .exists (dist_index ):
172- return
173- await generate_and_show_report (res )
174- with open (report ) as rep :
175- report_content = rep .read ()
176- for key , val in res .strategy_config .items ():
177- assert str (key ) in report_content
178- assert str (val ) in report_content
179- assert "BTC/USDT" in report_content
180- assert "1d" in report_content
181- assert "Binance" in report_content
182- os .remove (report )
192+ assert os .path .isfile (dist_index )
193+ report_result = await generate_and_show_report (res )
194+ assert os .path .isfile (report_result .report_file )
195+ shutil .rmtree (os .path .dirname (report_result .report_file ))
0 commit comments