-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.py
233 lines (199 loc) · 10.3 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import uuid
import unittest
from datetime import datetime
from unittest import TestCase
from unittest.mock import patch
from decimal import Decimal
from app import AccountStatement, AccountException
class Activity:
def __init__(self, symbol, type, quantity, price, currency='USD',
request_id=uuid.uuid4(), id=0, settle_date=datetime.now()):
self.symbol = symbol
self.type = type
self.quantity = quantity
self.price = price
self.currency = currency
self.request_id = request_id
self.id = id
self.settle_date = settle_date
def __repr__(self):
return f"{self.symbol} {self.type} {self.price}"
class Response:
status_code = 200
@staticmethod
def json():
return {'rates': [{'mid': 1.0}]}
class TestApp(TestCase):
@patch('requests.get', return_value=Response)
def test_calculate_profit_return_positive_profit(self, mock_get):
buy = Activity('CCC', 'BUY', Decimal(20), Decimal('0.2'))
sell = Activity('CCC', 'SELL', Decimal(20), Decimal('0.3'))
activities = [buy, sell]
income, cost, _ = AccountStatement.calculate_profit(activities)
self.assertEqual(Decimal(2.0), income - cost)
@patch('requests.get', return_value=Response)
def test_calculate_profit_return_negative_profit(self, mock_get):
buy = Activity('CCC', 'BUY', Decimal(20), Decimal('0.4'))
sell = Activity('CCC', 'SELL', Decimal(20), Decimal('0.3'))
activities = [buy, sell]
income, cost, _ = AccountStatement.calculate_profit(activities)
self.assertEqual(Decimal(-2.0), income - cost)
@patch('requests.get', return_value=Response)
def test_calculate_profit_return_profit_only_for_sold_quantity(self, mock_get):
buy = Activity('CCC', 'BUY', Decimal(40), Decimal('0.4'))
sell = Activity('CCC', 'SELL', Decimal(20), Decimal('0.3'))
activities = [buy, sell]
income, cost, _ = AccountStatement.calculate_profit(activities)
self.assertEqual(Decimal(-2.0), income - cost)
@patch('requests.get', return_value=Response)
def test_calculate_profit_return_profit_from_fifo_buy_price(self, mock_get):
buy_q, buy2_q, sell_q = 40, 20, 60
buy = Activity('CCC', 'BUY', Decimal(buy_q), Decimal('0.4'))
buy2 = Activity('CCC', 'BUY', Decimal(buy2_q), Decimal('0.2'))
sell = Activity('CCC', 'SELL', Decimal(sell_q), Decimal('0.3'))
activities = [buy, buy2, sell]
income, cost, _ = AccountStatement.calculate_profit(activities)
expected_profit = sell_q * 0.3 - (buy_q * 0.4 + buy2_q * 0.2)
self.assertAlmostEqual(expected_profit, income - cost, places=6)
@patch('requests.get', return_value=Response)
def test_calculate_profit_return_exception_when_too_much_sell_quantity(self, mock_get):
buy = Activity('CCC', 'BUY', Decimal(40), Decimal('0.4'))
buy2 = Activity('CCC', 'BUY', Decimal(20), Decimal('0.2'))
sell = Activity('CCC', 'SELL', Decimal(61), Decimal('0.3'))
activities = [buy, buy2, sell]
with self.assertRaises(AccountException):
AccountStatement.calculate_profit(activities)
@patch('requests.get', return_value=Response)
def test_calculate_profit_return_profit_only_for_last_year(self, mock_get):
buy2_q = 20
buy2 = Activity('CCC', 'BUY', buy2_q, Decimal('0.2'),
settle_date=datetime.strptime("2018/12/22", "%Y/%m/%d"))
sell_q = 10
sell = Activity('CCC', 'SELL', sell_q, Decimal('0.3'),
settle_date=datetime.strptime("2019/12/22", "%Y/%m/%d"))
buy_q = 40
buy = Activity('CCC', 'BUY', buy_q, Decimal('0.4'),
settle_date=datetime.strptime("2020/12/21", "%Y/%m/%d"))
sell2_q = 30
sell2 = Activity('CCC', 'SELL', sell2_q, Decimal('0.3'),
settle_date=datetime.strptime("2020/12/22", "%Y/%m/%d"))
# activities are sorted by date in db
activities = sorted([buy, buy2, sell2, sell],
key=lambda x: x.settle_date)
expected_profit = sell2_q * 0.3 - ((buy2_q - sell_q) * 0.2 + (buy_q - 20) * 0.4)
income, cost, _ = AccountStatement.calculate_profit(activities)
self.assertAlmostEqual(Decimal(expected_profit), income - cost)
@patch('requests.get', return_value=Response)
def test_calculate_profit_include_buy_before_sell_only(self, mock_get):
buy_q = 40
buy = Activity('CCC', 'BUY', buy_q, Decimal('0.4'),
settle_date=datetime.strptime("2020/12/21", "%Y/%m/%d"))
buy2_q = 20
buy2 = Activity('CCC', 'BUY', buy2_q, Decimal('0.2'),
settle_date=datetime.strptime("2018/12/22", "%Y/%m/%d"))
sell_q = 10
sell = Activity('CCC', 'SELL', sell_q, Decimal('0.3'),
settle_date=datetime.strptime("2020/11/22", "%Y/%m/%d"))
# activities are sorted by date in db
activities = sorted([buy, buy2, sell], key=lambda x: x.settle_date)
income, cost, _ = AccountStatement.calculate_profit(activities)
self.assertAlmostEqual(
(Decimal('0.3') - Decimal('0.2')) * sell_q, income - cost)
@patch('requests.get', return_value=Response)
def test_calculate_profit_multiple_action_symbol(self, mock_get):
buy_q = 40
buy = Activity('CCC', 'BUY', buy_q, Decimal('0.4'))
buy2_q = 20
buy2 = Activity('INT', 'BUY', buy2_q, Decimal('0.2'))
sell_q = 20
sell = Activity('CCC', 'SELL', sell_q, Decimal('0.4'))
sell2_q = 10
sell2 = Activity('INT', 'SELL', sell2_q, Decimal('0.3'))
activities = [buy, buy2, sell2, sell]
income, cost, _ = AccountStatement.calculate_profit(activities)
self.assertAlmostEqual((Decimal('0.4') - Decimal('0.4')) * sell_q +
(Decimal('0.3') - Decimal('0.2')) * sell2_q, income - cost)
@patch('requests.get', return_value=Response)
def test_calculate_profit_only_past_activity(self, mock_get):
buy = Activity('CCC', 'BUY', 40, Decimal('0.4'),
settle_date=datetime.strptime("2019/12/21", "%Y/%m/%d"))
buy2 = Activity('CCC', 'BUY', 20, Decimal('0.2'),
settle_date=datetime.strptime("2019/12/22", "%Y/%m/%d"))
sell = Activity('CCC', 'SELL', 20, Decimal('0.3'),
settle_date=datetime.strptime("2019/12/23", "%Y/%m/%d"))
sell2 = Activity('CCC', 'SELL', 40, Decimal('0.3'),
settle_date=datetime.strptime("2019/12/24", "%Y/%m/%d"))
activities = [buy, buy2, sell2, sell]
expected_profit = 0
income, cost, _ = AccountStatement.calculate_profit(activities, year=2020)
self.assertAlmostEqual(expected_profit, income - cost)
@patch('requests.get', return_value=Response)
def test_calculate_profit_fifo_multile_buys(self, mock_get):
buy_q = 10
buy = Activity('CCC', 'BUY', buy_q, Decimal('0.4'))
sell_q = 5
sell = Activity('CCC', 'SELL', sell_q, Decimal('0.4'))
buy2_q = 20
buy2 = Activity('CCC', 'BUY', buy2_q, Decimal('0.2'))
sell2_q = 15
sell2 = Activity('CCC', 'SELL', sell2_q, Decimal('0.3'))
activities = [buy, buy2, sell2, sell]
expected_profit = sell_q * 0.4 - sell_q * 0.4 + sell2_q * 0.3 - (5 * 0.4 + 10 * 0.2)
income, cost, _ = AccountStatement.calculate_profit(activities)
self.assertAlmostEqual(expected_profit, income - cost)
class TestAccountStatement(TestCase):
def test_create_from_pdf_multiple_pages(self):
statement = AccountStatement()
statement.create_from_pdf_file('test_data/april.pdf')
self.assertEqual(Decimal(108), len(statement._activity_data))
def test_create_from_pdf_with_strange_ending(self):
statement = AccountStatement()
statement.create_from_pdf_file('test_data/sie.pdf')
activities = statement.parse_activities()
activities = [e for e in activities if e.type == 'SELL']
self.assertEqual(12, len(activities))
def test_create_from_pdf(self):
months = [
# Należy podać nazwy raportów w folderze test_data
# Niestety posiadają one dane personalne i publiczne repozytorium nie jest dobrym miejscem dla nich
]
statement = AccountStatement()
for month in months:
statement.create_from_pdf_file(f'test_data/{month}')
self.assertEqual(106, len(statement._activity_data))
def test_calculate_income(self):
months = [
# Należy podać nazwy raportów w folderze test_data
# Niestety posiadają one dane personalne i publiczne repozytorium nie jest dobrym miejscem dla nich
]
statement = AccountStatement()
for month in months:
statement.create_from_pdf_file(f'test_data/{month}')
activites = sorted(statement.parse_activities(),
key=lambda x: x.settle_date)
activites = [e for e in activites if e.symbol == 'RLGY']
# quantity, type, price
#'71', 'BUY', '5.5899',
#'0.55813953', 'BUY', '5.59',
#'60', 'SELL', '7.40',
#'11', 'SELL', '7.40',
#'0.55813953', 'SELL', '7.40'
income, cost, _ = statement.calculate_profit(activites, test_only=True)
expected_profit = (60 * 7.40 - 60 * 5.5899 + 11
* 7.40 - (11 * 5.5899) + 0.55813953 * 7.40 - 0.55813953 * 5.59)
self.assertAlmostEqual(Decimal(expected_profit), income - cost)
def test_calculate_income_error_data(self):
months = [
# Należy podać nazwy raportów w folderze test_data
# Niestety posiadają one dane personalne i publiczne repozytorium nie jest dobrym miejscem dla nich
]
statement = AccountStatement()
for month in months:
statement.create_from_pdf_file(f'test_data/{month}')
activites = sorted(statement.parse_activities(),
key=lambda x: x.settle_date)
self.assertEqual(46, len(
[row for row in statement._activity_data if row[3] == 'BUY' or row[3] == 'SELL']))
statement.calculate_profit(activites, test_only=True)
if __name__ == '__main__':
unittest.main()