|
1 | 1 | import inspect
|
| 2 | +import multiprocessing |
2 | 3 | import os
|
3 | 4 | import sys
|
4 | 5 | import time
|
|
10 | 11 | from runpy import run_path
|
11 | 12 | from tempfile import NamedTemporaryFile, gettempdir
|
12 | 13 | from unittest import TestCase
|
13 |
| -from unittest.mock import patch |
14 | 14 |
|
15 | 15 | import numpy as np
|
16 | 16 | import pandas as pd
|
17 | 17 | from pandas.testing import assert_frame_equal
|
18 | 18 |
|
19 | 19 | from backtesting import Backtest, Strategy
|
20 | 20 | from backtesting._stats import compute_drawdown_duration_peaks
|
21 |
| -from backtesting._util import _Array, _as_str, _Indicator, try_ |
| 21 | +from backtesting._util import _Array, _as_str, _Indicator, patch, try_ |
22 | 22 | from backtesting.lib import (
|
23 | 23 | FractionalBacktest, OHLCV_AGG,
|
24 | 24 | SignalStrategy,
|
@@ -626,7 +626,7 @@ def test_multiprocessing_windows_spawn(self):
|
626 | 626 | kw = {'fast': [10]}
|
627 | 627 |
|
628 | 628 | stats1 = Backtest(df, SmaCross).optimize(**kw)
|
629 |
| - with patch('multiprocessing.get_start_method', lambda **_: 'spawn'): |
| 629 | + with patch(multiprocessing, 'get_start_method', lambda **_: 'spawn'): |
630 | 630 | with self.assertWarns(UserWarning) as cm:
|
631 | 631 | stats2 = Backtest(df, SmaCross).optimize(**kw)
|
632 | 632 |
|
@@ -776,7 +776,7 @@ def init(self):
|
776 | 776 | bt.run()
|
777 | 777 | import backtesting._plotting
|
778 | 778 | with _tempfile() as f, \
|
779 |
| - patch.object(backtesting._plotting, '_MAX_CANDLES', 10), \ |
| 779 | + patch(backtesting._plotting, '_MAX_CANDLES', 10), \ |
780 | 780 | self.assertWarns(UserWarning):
|
781 | 781 | bt.plot(filename=f, resample=True)
|
782 | 782 | # Give browser time to open before tempfile is removed
|
@@ -976,6 +976,15 @@ def __call__(self):
|
976 | 976 | for s in ('Open', 'High', 'Low', 'Close', 'Volume'):
|
977 | 977 | self.assertEqual(_as_str(_Array([1], name=s)), s[0])
|
978 | 978 |
|
| 979 | + def test_patch(self): |
| 980 | + class Object: |
| 981 | + pass |
| 982 | + o = Object() |
| 983 | + o.attr = False |
| 984 | + with patch(o, 'attr', True): |
| 985 | + self.assertTrue(o.attr) |
| 986 | + self.assertFalse(o.attr) |
| 987 | + |
979 | 988 | def test_pandas_accessors(self):
|
980 | 989 | class S(Strategy):
|
981 | 990 | def init(self):
|
|
0 commit comments