Passed
Branch master (bc72f6)
by Rafael S.
01:23
created

TestFetchPositions.state_check()   B

Complexity

Conditions 5

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
rs 8.5454
cc 5
1
"""Base class for container tests."""
2
3
from __future__ import absolute_import
4
import unittest
5
import copy
6
7
from trade import trade
8
from trade.container_tasks import (
9
    prorate_commissions, fetch_daytrades, group_positions, find_volume
10
)
11
12
13
class TestFetchPositions(unittest.TestCase):
14
    """Create the default operations for the test cases."""
15
16
    commissions = {}
17
    operations = []
18
    positions = {}
19
    daytrades = {}
20
    exercises = {}
21
    volume = False
22
23
    def setUp(self):
24
        tasks = [
25
            find_volume,
26
            fetch_daytrades,
27
            group_positions,
28
            prorate_commissions,
29
        ]
30
        self.container = trade.OperationContainer(
31
            operations=copy.deepcopy(self.operations),
32
            tasks=tasks
33
            )
34
        self.container.commissions = self.commissions
35
        if not self.volume:
36
            self.volume = sum(
37
                operation.volume for operation in self.container.operations
38
            )
39
        self.container.fetch_positions()
40
        self.state = {
41
            'operations': self.positions,
42
            'exercises': self.exercises,
43
            'daytrades': self.daytrades
44
        }
45
46
    def test_container_volume(self):
47
        """Check the volume of the OperationContainer."""
48
        self.assertEqual(self.container.context['volume'], self.volume)
49
50
    def len_check(self, len_type):
51
        """Check the len of a type of position in the container."""
52
        if 'positions' in self.container.context:
53
            if len_type in self.container.context['positions']:
54
                self.assertEqual(
55
                    len(self.container.context['positions'][len_type].keys()),
56
                    len(self.state[len_type].keys())
57
                )
58
59
    def state_check(self, position_type):
60
        """Check the state of a type of position in the container."""
61
        if position_type in self.container.context['positions']:
62
            for position in self.container.context['positions']\
63
            [position_type].values():
64
                position_details = position.__dict__
65
                for key in position_details:
66
                    if key in \
67
                        self.state[position_type][position.subject.symbol]:
68
                        self.assertEqual(
69
                            position_details[key],
70
                            self.state[position_type]\
71
                                [position.subject.symbol][key]
72
                        )
73
74
    def test_operations_len(self):
75
        """Check the len of the common operations positions."""
76
        self.len_check('operations')
77
78
    def test_operations_states(self):
79
        """Check the state of the common operations positions."""
80
        self.state_check('operations')
81
82
    def test_exercises_len(self):
83
        """Check the len of the exercise positions."""
84
        self.len_check('exercises')
85
86
    def test_exercise_states(self):
87
        """Check the state of the exercise positions."""
88
        self.state_check('exercises')
89
90
    def test_daytrades_len(self):
91
        """Check the len of the daytrade positions."""
92
        self.len_check('daytrades')
93
94
    def test_daytrades_states(self):
95
        """Check the state of the daytrade positions."""
96
        self.state_check('daytrades')
97
98
    def test_daytrades_buy_state(self):
99
        """Check the state of the daytrade positions purchases."""
100
        self.check_daytrade_suboperation(0, 'buy')
101
102
    def test_daytrades_sale_state(self):
103
        """Check the state of the daytrade positions sales."""
104
        self.check_daytrade_suboperation(1, 'sale')
105
106
    def check_daytrade_suboperation(self, operation_index, operation_type):
107
        """Check the state of the daytrade suboperations."""
108
        if 'daytrades' in self.container.context['positions']:
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