forked from scrtlabs/catalyst
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_testing.py
177 lines (150 loc) · 5.11 KB
/
test_testing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
"""
Tests for our testing utilities.
"""
from itertools import product
from unittest import TestCase
from numpy import array, empty
# from catalyst._protocol import BarData
# from catalyst.finance.asset_restrictions import NoRestrictions
# from catalyst.finance.order import Order
from catalyst.testing import (
check_arrays,
make_alternating_boolean_array,
make_cascading_boolean_array,
parameter_space,
)
# from catalyst.testing.fixtures import (
# WithConstantEquityMinuteBarData,
# WithDataPortal,
# CatalystTestCase,
# )
# from catalyst.testing.slippage import TestingSlippage
from catalyst.utils.numpy_utils import bool_dtype
class TestParameterSpace(TestCase):
x_args = [1, 2]
y_args = [3, 4]
@classmethod
def setUpClass(cls):
cls.xy_invocations = []
cls.yx_invocations = []
@classmethod
def tearDownClass(cls):
# This is the only actual test here.
assert cls.xy_invocations == list(product(cls.x_args, cls.y_args))
assert cls.yx_invocations == list(product(cls.y_args, cls.x_args))
@parameter_space(x=x_args, y=y_args)
def test_xy(self, x, y):
self.xy_invocations.append((x, y))
@parameter_space(x=x_args, y=y_args)
def test_yx(self, y, x):
# Ensure that product is called with args in the order that they appear
# in the function's parameter list.
self.yx_invocations.append((y, x))
def test_nothing(self):
# Ensure that there's at least one "real" test in the class, or else
# our {setUp,tearDown}Class won't be called if, for example,
# `parameter_space` returns None.
pass
class TestMakeBooleanArray(TestCase):
def test_make_alternating_boolean_array(self):
check_arrays(
make_alternating_boolean_array((3, 3)),
array(
[[True, False, True],
[False, True, False],
[True, False, True]]
),
)
check_arrays(
make_alternating_boolean_array((3, 3), first_value=False),
array(
[[False, True, False],
[True, False, True],
[False, True, False]]
),
)
check_arrays(
make_alternating_boolean_array((1, 3)),
array([[True, False, True]]),
)
check_arrays(
make_alternating_boolean_array((3, 1)),
array([[True], [False], [True]]),
)
check_arrays(
make_alternating_boolean_array((3, 0)),
empty((3, 0), dtype=bool_dtype),
)
def test_make_cascading_boolean_array(self):
check_arrays(
make_cascading_boolean_array((3, 3)),
array(
[[True, True, False],
[True, False, False],
[False, False, False]]
),
)
check_arrays(
make_cascading_boolean_array((3, 3), first_value=False),
array(
[[False, False, True],
[False, True, True],
[True, True, True]]
),
)
check_arrays(
make_cascading_boolean_array((1, 3)),
array([[True, True, False]]),
)
check_arrays(
make_cascading_boolean_array((3, 1)),
array([[False], [False], [False]]),
)
check_arrays(
make_cascading_boolean_array((3, 0)),
empty((3, 0), dtype=bool_dtype),
)
"""
class TestTestingSlippage(WithConstantEquityMinuteBarData,
WithDataPortal,
CatalystTestCase):
ASSET_FINDER_EQUITY_SYMBOLS = ('A',)
ASSET_FINDER_EQUITY_SIDS = (1,)
@classmethod
def init_class_fixtures(cls):
super(TestTestingSlippage, cls).init_class_fixtures()
cls.asset = cls.asset_finder.retrieve_asset(1)
cls.minute, _ = (
cls.trading_calendar.open_and_close_for_session(cls.START_DATE)
)
def init_instance_fixtures(self):
super(TestTestingSlippage, self).init_instance_fixtures()
self.bar_data = BarData(
self.data_portal,
lambda: self.minute,
"minute",
self.trading_calendar,
NoRestrictions()
)
def make_order(self, amount):
return Order(
self.minute,
self.asset,
amount,
)
def test_constant_filled_per_tick(self):
filled_per_tick = 1
model = TestingSlippage(filled_per_tick)
order = self.make_order(100)
price, volume = model.process_order(self.bar_data, order)
self.assertEqual(price, self.EQUITY_MINUTE_CONSTANT_CLOSE)
self.assertEqual(volume, filled_per_tick)
def test_fill_all(self):
filled_per_tick = TestingSlippage.ALL
order_amount = 100
model = TestingSlippage(filled_per_tick)
order = self.make_order(order_amount)
price, volume = model.process_order(self.bar_data, order)
self.assertEqual(price, self.EQUITY_MINUTE_CONSTANT_CLOSE)
self.assertEqual(volume, order_amount)
"""