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

TestFetchPositions.test_daytrades_buy_state()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
1
"""Test the trade.plugins.fetch_exercises task from the Option plugin."""
2
3
from __future__ import absolute_import
4
5
import unittest
6
import copy
7
from trade_app.trade.context import Context
8
from trade_app.trade.context import (
9
    find_volume, fetch_daytrades, prorate_commissions, group_positions
10
)
11
12
from fixtures.operations import (
13
    EXERCISE_OPERATION2, EXERCISE_OPERATION3
14
)
15
from fixtures.assets import OPTION1
16
17
18
class TestFetchPositions(unittest.TestCase):
19
    """Create the default operations for the test cases."""
20
21
    commissions = {}
22
    operations = []
23
    positions = {}
24
    daytrades = {}
25
    exercises = {}
26
    volume = False
27
28
    def setUp(self):
29
30
        self.container = Context(
31
            tasks=[
32
                find_volume,
33
                fetch_daytrades,
34
                group_positions,
35
            ],
36
            operations=copy.deepcopy(self.operations)
37
        )
38
        self.container.commissions = self.commissions
39
        self.container.fetch_positions()
40
        prorate_commissions(self.container)
41
42
        self.state = {
43
            'operations': self.positions,
44
            'exercises': self.exercises,
45
            'daytrades': self.daytrades
46
        }
47
48
    def test_container_volume(self):
49
        """Check the volume of the OperationContainer."""
50
        self.assertEqual(self.container.context['volume'], self.volume)
51
52
    def len_check(self, len_type):
53
        """Check the len of a type of position in the container."""
54
        if len_type in self.container.context['positions']:
55
            self.assertEqual(
56
                len(self.container.context['positions'][len_type].keys()),
57
                len(self.state[len_type].keys())
58
            )
59
60
    def state_check(self, position_type):
61
        """Check the state of a type of position in the container."""
62
        if 'positions' in self.container.context:
63
            if position_type in self.container.context['positions']:
64
                for position in self.container.context['positions'][position_type].values():
65
                    position_details = position.__dict__
66
                    for key in position_details:
67
                        if key in \
68
                            self.state[position_type][position.subject.symbol]:
69
                            self.assertEqual(
70
                                position_details[key],
71
                                self.state\
72
                                    [position_type][position.subject.symbol][key]
73
                            )
74
75
    def test_operations_len(self):
76
        """Check the len of the common operations positions."""
77
        self.len_check('operations')
78
79
    def test_operations_states(self):
80
        """Check the state of the common operations positions."""
81
        self.state_check('operations')
82
83
    def test_exercises_len(self):
84
        """Check the len of the exercise positions."""
85
        self.len_check('exercises')
86
87
    def test_exercise_states(self):
88
        """Check the state of the exercise positions."""
89
        self.state_check('exercises')
90
91
    def test_daytrades_len(self):
92
        """Check the len of the daytrade positions."""
93
        self.len_check('daytrades')
94
95
    def test_daytrades_states(self):
96
        """Check the state of the daytrade positions."""
97
        self.state_check('daytrades')
98
99
    def test_daytrades_buy_state(self):
100
        """Check the state of the daytrade positions purchases."""
101
        self.check_daytrade_suboperation(0, 'buy')
102
103
    def test_daytrades_sale_state(self):
104
        """Check the state of the daytrade positions sales."""
105
        self.check_daytrade_suboperation(1, 'sale')
106
107
    def check_daytrade_suboperation(self, operation_index, operation_type):
108
        """Check the state of the daytrade suboperations."""
109
        for asset in self.daytrades.keys():
110
            self.assertEqual(
111
                (
112
                    self.container.context['positions']['daytrades'][asset]\
113
                        .operations[operation_index].price,
114
                    self.container.context['positions']['daytrades'][asset]\
115
                        .operations[operation_index].quantity,
116
                    self.container.context['positions']['daytrades'][asset]\
117
                        .operations[operation_index].commissions,
118
                ),
119
                (
120
                    self.daytrades[asset][operation_type + ' price'],
121
                    self.daytrades[asset][operation_type + ' quantity'],
122
                    self.daytrades[asset][operation_type + ' commissions']
123
                )
124
            )
125
126
127
class TestFetchExercisesCase00(TestFetchPositions):
128
    """Test the fetch_exercises() task of the Accumulator."""
129
130
    volume = 100
131
    operations = [EXERCISE_OPERATION2]
132
    exercises = {
133
        OPTION1.symbol: {
134
            'quantity': 100,
135
            'price': 1,
136
            'volume': 0,
137
        }
138
    }
139
140
141
class TestFetchExercisesCase01(TestFetchPositions):
142
    """Test the fetch_exercises() task of the Accumulator."""
143
144
    volume = 400
145
    operations = [EXERCISE_OPERATION2, EXERCISE_OPERATION3]
146
    exercises = {
147
        OPTION1.symbol: {
148
            'quantity': 200,
149
            'price': 2,
150
            'volume': 0,
151
        }
152
    }
153