|
1
|
|
|
"""Tests for the utils functions.""" |
|
2
|
|
|
|
|
3
|
|
|
from __future__ import absolute_import |
|
4
|
|
|
import unittest |
|
5
|
|
|
|
|
6
|
|
|
from trade import utils |
|
7
|
|
|
from tests.fixtures.operations import ( |
|
8
|
|
|
OPERATION46, OPERATION55, OPERATION56, OPERATION57, OPERATION58, |
|
9
|
|
|
OPERATION59, OPERATION43 |
|
10
|
|
|
) |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
class TestSameSign(unittest.TestCase): |
|
14
|
|
|
"""Test the same_sign() function. |
|
15
|
|
|
|
|
16
|
|
|
This function should return True if the two values |
|
17
|
|
|
have opposite signs, and False otherwise. |
|
18
|
|
|
""" |
|
19
|
|
|
|
|
20
|
|
|
def test_same_signs(self): |
|
21
|
|
|
self.assertTrue(utils.same_sign(-1, -4)) |
|
22
|
|
|
|
|
23
|
|
|
def test_opposite_signs(self): |
|
24
|
|
|
self.assertFalse(utils.same_sign(-1, 4)) |
|
25
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
class TestAveragePrice(unittest.TestCase): |
|
28
|
|
|
"""Test the function average_price. |
|
29
|
|
|
|
|
30
|
|
|
This function receives two quantity values, both with a |
|
31
|
|
|
price associeated to it, and returns the average price. |
|
32
|
|
|
""" |
|
33
|
|
|
|
|
34
|
|
|
def test_case_00(self): |
|
35
|
|
|
price = utils.average_price(10, 2, 10, 4) |
|
36
|
|
|
self.assertEqual(price, 3) |
|
37
|
|
|
|
|
38
|
|
|
def test_case_01(self): |
|
39
|
|
|
price = utils.average_price(10, 1, 10, 2) |
|
40
|
|
|
self.assertEqual(price, 1.5) |
|
41
|
|
|
|
|
42
|
|
|
def test_case_02(self): |
|
43
|
|
|
price = utils.average_price(10, 1, 10, 3) |
|
44
|
|
|
self.assertEqual(price, 2) |
|
45
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
class TestDaytradeCondition(unittest.TestCase): |
|
48
|
|
|
"""Tests the function daytrade_condition(). |
|
49
|
|
|
|
|
50
|
|
|
The daytrade_condition function receives two operations and |
|
51
|
|
|
shoul return True if the two operations configure a daytrade, |
|
52
|
|
|
False otherwise. |
|
53
|
|
|
""" |
|
54
|
|
|
|
|
55
|
|
|
def test_case_00(self): |
|
56
|
|
|
self.assertTrue( |
|
57
|
|
|
utils.daytrade_condition(OPERATION55, OPERATION46) |
|
58
|
|
|
) |
|
59
|
|
|
|
|
60
|
|
|
def test_case_01(self): |
|
61
|
|
|
self.assertTrue( |
|
62
|
|
|
utils.daytrade_condition(OPERATION46, OPERATION55) |
|
63
|
|
|
) |
|
64
|
|
|
|
|
65
|
|
|
def test_case_02(self): |
|
66
|
|
|
self.assertFalse( |
|
67
|
|
|
utils.daytrade_condition(OPERATION57, OPERATION57) |
|
68
|
|
|
) |
|
69
|
|
|
|
|
70
|
|
|
def test_case_04(self): |
|
71
|
|
|
self.assertFalse( |
|
72
|
|
|
utils.daytrade_condition(OPERATION55, OPERATION59) |
|
73
|
|
|
) |
|
74
|
|
|
|
|
75
|
|
|
def test_case_06(self): |
|
76
|
|
|
self.assertFalse( |
|
77
|
|
|
utils.daytrade_condition(OPERATION57, OPERATION46) |
|
78
|
|
|
) |
|
79
|
|
|
|
|
80
|
|
|
def test_case_09(self): |
|
81
|
|
|
self.assertFalse( |
|
82
|
|
|
utils.daytrade_condition(OPERATION46, OPERATION46) |
|
83
|
|
|
) |
|
84
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
class TestFindPurchaseAndSale(unittest.TestCase): |
|
87
|
|
|
"""Test the find_purchase_and_sale() function. |
|
88
|
|
|
|
|
89
|
|
|
This function receives two operations an is expected to |
|
90
|
|
|
return a tuple with the two operations: the purchase operation |
|
91
|
|
|
first as the first element and the sale operation as the second |
|
92
|
|
|
element. |
|
93
|
|
|
""" |
|
94
|
|
|
|
|
95
|
|
|
def test_case_00(self): |
|
96
|
|
|
self.assertEqual( |
|
97
|
|
|
utils.find_purchase_and_sale(OPERATION46, OPERATION55), |
|
98
|
|
|
(OPERATION46, OPERATION55) |
|
99
|
|
|
) |
|
100
|
|
|
|
|
101
|
|
|
def test_case_01(self): |
|
102
|
|
|
self.assertEqual( |
|
103
|
|
|
utils.find_purchase_and_sale(OPERATION55, OPERATION46), |
|
104
|
|
|
(OPERATION46, OPERATION55) |
|
105
|
|
|
) |
|
106
|
|
|
|
|
107
|
|
|
def test_case_05(self): |
|
108
|
|
|
self.assertEqual( |
|
109
|
|
|
utils.find_purchase_and_sale(OPERATION56, OPERATION43), |
|
110
|
|
|
(OPERATION56, OPERATION43) |
|
111
|
|
|
) |
|
112
|
|
|
|
|
113
|
|
|
def test_case_07(self): |
|
114
|
|
|
self.assertEqual( |
|
115
|
|
|
utils.find_purchase_and_sale(OPERATION58, OPERATION57), |
|
116
|
|
|
(OPERATION57, OPERATION58) |
|
117
|
|
|
) |
|
118
|
|
|
|