|
1
|
|
|
# |
|
2
|
|
|
# Copyright 2015 Quantopian, Inc. |
|
3
|
|
|
# |
|
4
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); |
|
5
|
|
|
# you may not use this file except in compliance with the License. |
|
6
|
|
|
# You may obtain a copy of the License at |
|
7
|
|
|
# |
|
8
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0 |
|
9
|
|
|
# |
|
10
|
|
|
# Unless required by applicable law or agreed to in writing, software |
|
11
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, |
|
12
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13
|
|
|
# See the License for the specific language governing permissions and |
|
14
|
|
|
# limitations under the License. |
|
15
|
|
|
|
|
16
|
|
|
import os |
|
17
|
|
|
import pickle |
|
18
|
|
|
|
|
19
|
|
|
from nose_parameterized import parameterized |
|
20
|
|
|
from unittest import TestCase, skip |
|
21
|
|
|
|
|
22
|
|
|
from zipline.finance.blotter import Order |
|
23
|
|
|
|
|
24
|
|
|
from .serialization_cases import ( |
|
25
|
|
|
object_serialization_cases, |
|
26
|
|
|
assert_dict_equal |
|
27
|
|
|
) |
|
28
|
|
|
|
|
29
|
|
|
base_state_dir = 'tests/resources/saved_state_archive' |
|
30
|
|
|
|
|
31
|
|
|
BASE_STATE_DIR = os.path.join( |
|
32
|
|
|
os.path.dirname(__file__), |
|
33
|
|
|
'resources', |
|
34
|
|
|
'saved_state_archive') |
|
35
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
class VersioningTestCase(TestCase): |
|
38
|
|
|
|
|
39
|
|
|
def load_state_from_disk(self, cls): |
|
40
|
|
|
state_dir = cls.__module__ + '.' + cls.__name__ |
|
41
|
|
|
|
|
42
|
|
|
full_dir = BASE_STATE_DIR + '/' + state_dir |
|
43
|
|
|
|
|
44
|
|
|
state_files = \ |
|
45
|
|
|
[f for f in os.listdir(full_dir) if 'State_Version_' in f] |
|
46
|
|
|
|
|
47
|
|
|
for f_name in state_files: |
|
48
|
|
|
f = open(full_dir + '/' + f_name, 'r') |
|
49
|
|
|
yield pickle.load(f) |
|
50
|
|
|
|
|
51
|
|
|
# Only test versioning in minutely mode right now |
|
52
|
|
|
@parameterized.expand(object_serialization_cases(skip_daily=True)) |
|
53
|
|
|
@skip |
|
54
|
|
|
def test_object_serialization(self, |
|
55
|
|
|
_, |
|
56
|
|
|
cls, |
|
57
|
|
|
initargs, |
|
58
|
|
|
di_vars, |
|
59
|
|
|
comparison_method='dict'): |
|
60
|
|
|
|
|
61
|
|
|
# Make reference object |
|
62
|
|
|
obj = cls(*initargs) |
|
63
|
|
|
for k, v in di_vars.items(): |
|
64
|
|
|
setattr(obj, k, v) |
|
65
|
|
|
|
|
66
|
|
|
# Fetch state |
|
67
|
|
|
state_versions = self.load_state_from_disk(cls) |
|
68
|
|
|
|
|
69
|
|
|
for version in state_versions: |
|
70
|
|
|
|
|
71
|
|
|
# For each version inflate a new object and ensure that it |
|
72
|
|
|
# matches the original. |
|
73
|
|
|
|
|
74
|
|
|
newargs = version['newargs'] |
|
75
|
|
|
initargs = version['initargs'] |
|
76
|
|
|
state = version['obj_state'] |
|
77
|
|
|
|
|
78
|
|
|
if newargs is not None: |
|
79
|
|
|
obj2 = cls.__new__(cls, *newargs) |
|
80
|
|
|
else: |
|
81
|
|
|
obj2 = cls.__new__(cls) |
|
82
|
|
|
if initargs is not None: |
|
83
|
|
|
obj2.__init__(*initargs) |
|
84
|
|
|
|
|
85
|
|
|
obj2.__setstate__(state) |
|
86
|
|
|
for k, v in di_vars.items(): |
|
87
|
|
|
setattr(obj2, k, v) |
|
88
|
|
|
|
|
89
|
|
|
# The ObjectId generated on instantiation of Order will |
|
90
|
|
|
# not be the same as the one loaded from saved state. |
|
91
|
|
|
if cls == Order: |
|
92
|
|
|
obj.__dict__['id'] = obj2.__dict__['id'] |
|
93
|
|
|
|
|
94
|
|
|
if comparison_method == 'repr': |
|
95
|
|
|
self.assertEqual(obj.__repr__(), obj2.__repr__()) |
|
96
|
|
|
elif comparison_method == 'to_dict': |
|
97
|
|
|
assert_dict_equal(obj.to_dict(), obj2.to_dict()) |
|
98
|
|
|
else: |
|
99
|
|
|
assert_dict_equal(obj.__dict__, obj2.__dict__) |
|
100
|
|
|
|