Passed
Push — master ( 684169...29f120 )
by Rafael S.
01:50
created

TestJSONCase0302   A

Complexity

Total Complexity 0

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 0
c 1
b 0
f 0
dl 0
loc 108
rs 10
1
"""Tests for the JSON interface with options and exercise operations."""
2
3
from __future__ import absolute_import
4
5
import unittest
6
import json
7
from trade_app.trade.occurrence import Occurrence as Operation
8
from trade_app.trade.subject import Subject as Asset
9
from trade_app.trade.context import (
10
    find_volume, fetch_daytrades, group_positions
11
)
12
from trade_app.trade.trade_json import TradeJSON
13
14
from trade_app.options import fetch_exercises, Option, Exercise
15
16
17
class TestJSON(unittest.TestCase):
18
    """Base class for the JSON tests."""
19
20
    json_input = None
21
    json_output = None
22
    maxDiff = None
23
24
    def setUp(self):
25
        types = {
26
            'Asset': Asset,
27
            'Operation': Operation,
28
        }
29
        self.interface = TradeJSON(
30
            [find_volume, fetch_daytrades, group_positions],
31
            types
32
        )
33
34
    def test_json_interface(self):
35
        """Test the json response."""
36
        if self.json_input:
37
            self.assertEqual(
38
                json.loads(self.interface.get_trade_results(self.json_input)),
39
                json.loads(self.json_output)
40
            )
41
42
43
class TestJSONWithOptionsBase(TestJSON):
44
    """Base class for the JSON tests."""
45
46
    json_input = None
47
    json_output = None
48
    maxDiff = None
49
50
    def setUp(self):
51
        """Include the fetch_exercises function and the new types."""
52
        types = {
53
            'Asset': Asset,
54
            'Operation': Operation,
55
            'Option': Option,
56
            'Exercise': Exercise,
57
        }
58
        self.interface = TradeJSON(
59
            [
60
                find_volume, fetch_daytrades, fetch_exercises, group_positions
61
            ],
62
            types)
63
64
65
66
class TestJSONWithOptions(TestJSONWithOptionsBase):
67
    """Option operations."""
68
69
    json_input = """{
70
        "subjects": {
71
            "ASSET": {
72
                "type": "Asset",
73
                "name": "Some Asset"
74
            },
75
            "OPTION": {
76
                "type": "Option",
77
                "name": "Some Option",
78
                "underlying_assets": {"ASSET":1},
79
                "expiration_date": "2015-12-23"
80
            }
81
        },
82
        "occurrences": [
83
            {
84
                "type": "Operation",
85
                "subject": "OPTION",
86
                "date": "2015-01-01",
87
                "quantity": 10,
88
                "price": 650.33
89
            },
90
            {
91
                "type": "Operation",
92
                "subject": "ASSET",
93
                "date": "2015-01-02",
94
                "quantity": 10,
95
                "price": 650.33
96
            },
97
            {
98
                "type": "Operation",
99
                "subject": "ASSET",
100
                "date": "2015-01-02",
101
                "quantity": -10,
102
                "price": 651.33
103
            }
104
        ],
105
        "initial state": {}
106
    }"""
107
108
    json_output = """{
109
        "totals": {
110
            "sales": {
111
                "volume": 6513.3,
112
                "operations": 1
113
            },
114
            "purchases": {
115
                "volume": 13006.6,
116
                "operations": 2
117
            },
118
            "operations": 3,
119
            "daytrades": 1,
120
            "results": {
121
                "daytrades": 10.0
122
            }
123
        },
124
        "assets": {
125
            "OPTION": {
126
                "totals": {
127
                    "sales": 0,
128
                    "purchases": 1,
129
                    "operations": 1,
130
                    "daytrades": 0,
131
                    "results": {}
132
                },
133
                "states": {
134
                    "2015-01-01": {
135
                        "quantity": 10,
136
                        "price": 650.33,
137
                        "results": {}
138
                    }
139
                }
140
            },
141
            "ASSET": {
142
                "totals": {
143
                    "sales": 1,
144
                    "purchases": 1,
145
                    "operations": 2,
146
                    "daytrades": 1,
147
                    "results": {
148
                        "daytrades": 10.0
149
                    }
150
                },
151
                "states": {
152
                    "2015-01-02": {
153
                        "quantity": 0,
154
                        "price": 0,
155
                        "results": {
156
                            "daytrades": 10.0
157
                        }
158
                    }
159
                }
160
            }
161
        }
162
    }"""
163
164
165
class TestJSONCase0301(TestJSONWithOptionsBase):
166
    """An exercise operation."""
167
168
    json_input = """{
169
        "subjects": {
170
            "ASSET": {
171
                "type": "Asset",
172
                "name": "Some Asset"
173
            },
174
            "OPTION": {
175
                "type": "Option",
176
                "name": "Some Option",
177
                "expiration_date": "2016-12-23",
178
                "underlying_assets": {"ASSET": 1}
179
            }
180
        },
181
        "occurrences": [
182
            {
183
                "type": "Operation",
184
                "subject": "OPTION",
185
                "date": "2015-01-01",
186
                "quantity": 10,
187
                "price": 1
188
            },
189
            {
190
                "type": "Exercise",
191
                "subject": "OPTION",
192
                "date": "2015-01-03",
193
                "quantity": 10,
194
                "price": 4
195
            }
196
        ],
197
        "initial state": {}
198
    }"""
199
200
201
    json_output = """{
202
        "totals": {
203
            "sales": {
204
                "volume": 0,
205
                "operations": 0
206
            },
207
            "purchases": {
208
                "volume": 50,
209
                "operations": 2
210
            },
211
            "operations": 2,
212
            "daytrades": 0,
213
            "results": {}
214
        },
215
        "assets": {
216
            "OPTION": {
217
                "totals": {
218
                    "sales": 0,
219
                    "purchases": 2,
220
                    "operations": 2,
221
                    "daytrades": 0,
222
                    "results": {}
223
                },
224
                "states": {
225
                    "2015-01-01": {
226
                        "quantity": 10,
227
                        "price": 1.0,
228
                        "results": {}
229
                    },
230
                    "2015-01-03": {
231
                        "quantity": 0,
232
                        "price": 0,
233
                        "results": {}
234
                    }
235
                }
236
            },
237
            "ASSET": {
238
                "totals": {
239
                    "sales": 0,
240
                    "purchases": 0,
241
                    "operations": 0,
242
                    "daytrades": 0,
243
                    "results": {}
244
                },
245
                "states": {
246
                    "2015-01-03": {
247
                        "quantity": 10,
248
                        "price": 5,
249
                        "results": {}
250
                    }
251
                }
252
            }
253
        }
254
    }"""
255
256
257
258
class TestJSONCase0302(TestJSONWithOptionsBase):
259
    """An exercise operation that generates results."""
260
261
    json_input = """{
262
        "subjects": {
263
            "ASSET": {
264
                "type": "Asset",
265
                "name": "Some Asset"
266
            },
267
            "OPTION": {
268
                "type": "Option",
269
                "name": "Some Option",
270
                "expiration_date": "2016-12-23",
271
                "underlying_assets": {"ASSET": 1}
272
            }
273
        },
274
        "occurrences": [
275
            {
276
                "type": "Operation",
277
                "subject": "OPTION",
278
                "date": "2015-01-01",
279
                "quantity": 10,
280
                "price": 1
281
            },
282
            {
283
                "type": "Operation",
284
                "subject": "ASSET",
285
                "date": "2015-01-02",
286
                "quantity": -10,
287
                "price": 7
288
            },
289
            {
290
                "type": "Exercise",
291
                "subject": "OPTION",
292
                "date": "2015-01-03",
293
                "quantity": 10,
294
                "price": 4
295
            }
296
        ],
297
        "initial state": {}
298
    }"""
299
300
301
    json_output = """{
302
        "totals": {
303
            "sales": {
304
                "volume": 70,
305
                "operations": 1
306
            },
307
            "purchases": {
308
                "volume": 50,
309
                "operations": 2
310
            },
311
            "operations": 3,
312
            "daytrades": 0,
313
            "results": {
314
                "trades": 20
315
            }
316
        },
317
        "assets": {
318
            "OPTION": {
319
                "totals": {
320
                    "sales": 0,
321
                    "purchases": 2,
322
                    "operations": 2,
323
                    "daytrades": 0,
324
                    "results": {}
325
                },
326
                "states": {
327
                    "2015-01-01": {
328
                        "quantity": 10,
329
                        "price": 1.0,
330
                        "results": {}
331
                    },
332
                    "2015-01-03": {
333
                        "quantity": 0,
334
                        "price": 0,
335
                        "results": {}
336
                    }
337
                }
338
            },
339
            "ASSET": {
340
                "totals": {
341
                    "sales": 1,
342
                    "purchases": 0,
343
                    "operations": 1,
344
                    "daytrades": 0,
345
                    "results": {
346
                        "trades": 20
347
                    }
348
                },
349
                "states": {
350
                    "2015-01-02": {
351
                        "quantity": -10,
352
                        "price": 7,
353
                        "results": {}
354
                    },
355
                    "2015-01-03": {
356
                        "quantity": 0,
357
                        "price": 0,
358
                        "results": {
359
                            "trades": 20
360
                        }
361
                    }
362
                }
363
            }
364
        }
365
    }"""
366