-
Notifications
You must be signed in to change notification settings - Fork 44
/
test_input_parser.py
346 lines (319 loc) · 16.9 KB
/
test_input_parser.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# Copyright 2021 eprbell
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import unittest
from typing import Dict, List, NamedTuple, Optional, Type
from dateutil.parser import parse
from rp2.configuration import Configuration
from rp2.entry_types import TransactionType
from rp2.in_transaction import InTransaction
from rp2.input_data import InputData
from rp2.intra_transaction import IntraTransaction
from rp2.ods_parser import open_ods, parse_ods
from rp2.out_transaction import OutTransaction
from rp2.plugin.country.us import US
from rp2.rp2_decimal import RP2Decimal
from rp2.rp2_error import RP2Error, RP2RuntimeError, RP2TypeError, RP2ValueError
from rp2.transaction_set import TransactionSet
class ErrorAndMessage(NamedTuple):
error_class: Type[RP2Error]
message: str
class TestInputParser(unittest.TestCase):
_good_input_configuration: Configuration
_bad_input_configuration: Configuration
@classmethod
def setUpClass(cls) -> None:
TestInputParser._good_input_configuration = Configuration("./config/test_data.ini", US())
TestInputParser._bad_input_configuration = Configuration("./config/test_bad_data.ini", US())
def setUp(self) -> None:
self.maxDiff = None # pylint: disable=invalid-name
def test_good_input(self) -> None:
self._verify_good_sheet("B1", out_empty=True, intra_empty=True)
self._verify_good_sheet("B2", out_empty=False, intra_empty=True)
self._verify_good_sheet("B3", out_empty=True, intra_empty=False)
self._verify_good_sheet("B4", out_empty=False, intra_empty=False)
def _verify_good_sheet(self, sheet_name: str, out_empty: bool, intra_empty: bool) -> None:
asset = sheet_name
input_file_handle: object = open_ods(configuration=self._good_input_configuration, input_file_path="./input/test_data.ods")
input_data: InputData = parse_ods(self._good_input_configuration, asset, input_file_handle)
# In table is always present
self._verify_non_empty_in_table(input_data.unfiltered_in_transaction_set, asset)
if out_empty:
self._verify_empty_table(input_data.unfiltered_out_transaction_set)
else:
self._verify_non_empty_out_table(input_data.unfiltered_out_transaction_set, asset)
if intra_empty:
self._verify_empty_table(input_data.unfiltered_intra_transaction_set)
else:
self._verify_non_empty_intra_table(input_data.unfiltered_intra_transaction_set, asset)
def _verify_empty_table(self, transaction_set: TransactionSet) -> None:
self.assertTrue(transaction_set.is_empty())
def _verify_non_empty_in_table(self, in_transaction_set: TransactionSet, asset: str) -> None:
internal_ids: List[str] = ["4", "3", "7", "6", "5"]
timestamps: List[str] = [
"2020-01-01T08:41Z",
"2020-02-01T11:18Z",
"2020-03-01T09:45Z",
"2020-04-01T09:45Z",
"2020-05-01T14:03Z",
]
transaction_types: List[TransactionType] = [
TransactionType.BUY,
TransactionType.INTEREST,
TransactionType.INTEREST,
TransactionType.BUY,
TransactionType.BUY,
]
spot_prices: List[RP2Decimal] = [RP2Decimal(s) for s in ["11000", "12000", "13000", "14000", "15000"]]
fiat_taxable_amounts: List[RP2Decimal] = [RP2Decimal(s) for s in ["0", "24000", "39000", "0", "0"]]
crypto_balance_changes: List[RP2Decimal] = [RP2Decimal(s) for s in ["1", "2", "3", "4", "5"]]
fiat_balance_changes: List[RP2Decimal] = [RP2Decimal(s) for s in ["11100", "24000", "39000", "56400", "75500"]]
is_taxable_values: List[bool] = [False, True, True, False, False]
count: int = 0
row: int
internal_id: str
timestamp: str
transaction_type: TransactionType
spot_price: RP2Decimal
fiat_taxable_amount: RP2Decimal
crypto_balance_change: RP2Decimal
fiat_balance_change: RP2Decimal
is_taxable: bool
transaction: Optional[InTransaction] = None
previous_transaction: Optional[InTransaction] = None
for ( # type: ignore
transaction,
internal_id,
timestamp,
transaction_type,
spot_price,
fiat_taxable_amount,
crypto_balance_change,
fiat_balance_change,
is_taxable,
) in zip( # type: ignore
in_transaction_set,
internal_ids,
timestamps,
transaction_types,
spot_prices,
fiat_taxable_amounts,
crypto_balance_changes,
fiat_balance_changes,
is_taxable_values,
):
row = int(internal_id)
if not in_transaction_set or not transaction: # Unwrap the Optional types to keep mypy happy
raise RP2RuntimeError("Internal error: in_transaction_set or transaction are None")
self.assertEqual(in_transaction_set.get_parent(transaction), previous_transaction)
self.assertEqual(transaction.row, row)
self.assertEqual(transaction.internal_id, internal_id)
self.assertEqual(transaction.timestamp, parse(timestamp))
self.assertEqual(transaction.transaction_type, transaction_type)
self.assertEqual(transaction.spot_price, spot_price)
self.assertEqual(transaction.asset, asset)
self.assertEqual(transaction.fiat_taxable_amount, fiat_taxable_amount)
self.assertEqual(transaction.crypto_balance_change, crypto_balance_change)
self.assertEqual(transaction.fiat_balance_change, fiat_balance_change)
self.assertEqual(transaction.is_taxable(), is_taxable)
previous_transaction = transaction
count += 1
self.assertEqual(count, 5)
def _verify_non_empty_out_table(self, out_transaction_set: TransactionSet, asset: str) -> None:
internal_ids: List[str] = ["16", "15", "17", "13", "14"]
timestamps: List[str] = [
"2020-01-11T11:15Z",
"2020-02-11T19:58Z",
"2020-04-11T07:10Z",
"2020-04-12T17:50Z",
"2021-06-11T05:31Z",
]
transaction_types: List[TransactionType] = [
TransactionType.SELL,
TransactionType.SELL,
TransactionType.GIFT,
TransactionType.DONATE,
TransactionType.SELL,
]
spot_prices: List[RP2Decimal] = [RP2Decimal(d) for d in ["11200", "12200", "14200", "14300", "20200"]]
fiat_taxable_amounts: List[RP2Decimal] = [RP2Decimal(d) for d in ["2240", "12200", "71000", "54197", "40400.0"]]
crypto_balance_changes: List[RP2Decimal] = [RP2Decimal(d) for d in ["0.2", "1", "5", "3.79", "2.01"]]
fiat_balance_changes: List[RP2Decimal] = [RP2Decimal(d) for d in ["2240", "12200", "71000", "54197", "40602.0"]]
is_taxable_values: List[bool] = [True, True, True, True, True]
count: int = 0
row: int
internal_id: str
timestamp: str
transaction_type: TransactionType
spot_price: RP2Decimal
fiat_taxable_amount: RP2Decimal
crypto_balance_change: RP2Decimal
fiat_balance_change: RP2Decimal
is_taxable: bool
transaction: Optional[OutTransaction] = None
previous_transaction: Optional[OutTransaction] = None
for ( # type: ignore
transaction,
internal_id,
timestamp,
transaction_type,
spot_price,
fiat_taxable_amount,
crypto_balance_change,
fiat_balance_change,
is_taxable,
) in zip( # type: ignore
out_transaction_set,
internal_ids,
timestamps,
transaction_types,
spot_prices,
fiat_taxable_amounts,
crypto_balance_changes,
fiat_balance_changes,
is_taxable_values,
):
row = int(internal_id)
if not out_transaction_set or not transaction: # Unwrap the Optional types to keep mypy happy
raise RP2RuntimeError("Internal error: in_transaction_set or transaction are None")
self.assertEqual(out_transaction_set.get_parent(transaction), previous_transaction)
self.assertEqual(transaction.row, row)
self.assertEqual(transaction.internal_id, internal_id)
self.assertEqual(transaction.timestamp, parse(timestamp))
self.assertEqual(transaction.transaction_type, transaction_type)
self.assertEqual(transaction.spot_price, spot_price)
self.assertEqual(transaction.asset, asset)
self.assertEqual(transaction.fiat_taxable_amount, fiat_taxable_amount)
self.assertEqual(transaction.crypto_balance_change, crypto_balance_change)
self.assertEqual(transaction.fiat_balance_change, fiat_balance_change)
self.assertEqual(transaction.is_taxable(), is_taxable)
previous_transaction = transaction
count += 1
self.assertEqual(count, 5)
def _verify_non_empty_intra_table(self, intra_transaction_set: TransactionSet, asset: str) -> None:
internal_ids: List[str] = ["26", "24", "25", "23"]
timestamps: List[str] = [
"2020-01-21T18:33:14.342Z",
"2020-02-21T20:23:31Z",
"2020-05-21T12:58:10Z",
"2021-07-21T10:02:02Z",
]
transaction_types: List[TransactionType] = [
TransactionType.MOVE,
TransactionType.MOVE,
TransactionType.MOVE,
TransactionType.MOVE,
]
spot_prices: List[RP2Decimal] = [RP2Decimal(d) for d in ["11400", "0", "14400", "21400"]]
fiat_taxable_amounts: List[RP2Decimal] = [RP2Decimal(d) for d in ["114", "0", "288", "856"]]
crypto_balance_changes: List[RP2Decimal] = [RP2Decimal(d) for d in ["0.01", "0", "0.02", "0.04"]]
fiat_balance_changes: List[RP2Decimal] = [RP2Decimal(d) for d in ["114", "0", "288", "856"]]
is_taxable_values: List[bool] = [True, False, True, True]
count: int = 0
row: int
internal_id: str
timestamp: str
transaction_type: TransactionType
spot_price: RP2Decimal
fiat_taxable_amount: RP2Decimal
crypto_balance_change: RP2Decimal
fiat_balance_change: RP2Decimal
is_taxable: bool
transaction: Optional[IntraTransaction] = None
previous_transaction: Optional[IntraTransaction] = None
for ( # type: ignore
transaction,
internal_id,
timestamp,
transaction_type,
spot_price,
fiat_taxable_amount,
crypto_balance_change,
fiat_balance_change,
is_taxable,
) in zip( # type: ignore
intra_transaction_set,
internal_ids,
timestamps,
transaction_types,
spot_prices,
fiat_taxable_amounts,
crypto_balance_changes,
fiat_balance_changes,
is_taxable_values,
):
row = int(internal_id)
if not intra_transaction_set or not transaction: # Unwrap the Optional types to keep mypy happy
raise RP2RuntimeError("Internal error: intra_transaction_set or transaction are None")
self.assertEqual(intra_transaction_set.get_parent(transaction), previous_transaction)
self.assertEqual(transaction.row, row)
self.assertEqual(transaction.internal_id, internal_id)
self.assertEqual(transaction.timestamp, parse(timestamp))
self.assertEqual(transaction.transaction_type, transaction_type)
self.assertEqual(transaction.spot_price, spot_price)
self.assertEqual(transaction.asset, asset)
self.assertEqual(transaction.fiat_taxable_amount, fiat_taxable_amount)
self.assertEqual(transaction.crypto_balance_change, crypto_balance_change)
self.assertEqual(transaction.fiat_balance_change, fiat_balance_change)
self.assertEqual(transaction.is_taxable(), is_taxable)
previous_transaction = transaction
count += 1
self.assertEqual(count, 4)
def test_bad_input(self) -> None:
sheets_to_expected_messages: Dict[str, ErrorAndMessage] = {
"B1": ErrorAndMessage(RP2ValueError, "IN table not found"),
"B2": ErrorAndMessage(RP2ValueError, 'Found an invalid cell "foo" while looking for a table-begin token'),
"B3": ErrorAndMessage(RP2ValueError, 'Found an invalid cell "bar" while looking for a table-begin token'),
"B4": ErrorAndMessage(RP2ValueError, "Found end-table keyword without having found a table-begin keyword first"),
"B5": ErrorAndMessage(RP2ValueError, "Found end-table keyword without having found a table-begin keyword first"),
"B6": ErrorAndMessage(RP2ValueError, "TABLE END not found for EntrySetType.IN table"),
"B7": ErrorAndMessage(RP2ValueError, "TABLE END not found for EntrySetType.OUT table"),
"B8": ErrorAndMessage(RP2ValueError, "TABLE END not found for EntrySetType.INTRA table"),
"B9": ErrorAndMessage(RP2ValueError, 'Found "IN" keyword while parsing table EntrySetType.IN'),
"B10": ErrorAndMessage(RP2ValueError, 'Found "OUT" keyword while parsing table EntrySetType.OUT'),
"B11": ErrorAndMessage(RP2ValueError, 'Found "INTRA" keyword while parsing table EntrySetType.INTRA'),
"B12": ErrorAndMessage(RP2ValueError, 'Found "OUT" keyword while parsing table EntrySetType.IN'),
"B13": ErrorAndMessage(RP2ValueError, 'Found "OUT" keyword while parsing table EntrySetType.INTRA'),
"B14": ErrorAndMessage(RP2ValueError, "IN table not found or empty"),
"B15": ErrorAndMessage(RP2ValueError, "TABLE END not found for EntrySetType.IN table"),
"B16": ErrorAndMessage(RP2ValueError, "Parameter 'data' has length .*, but required minimum from in-table headers in .* is .*"),
"B17": ErrorAndMessage(RP2ValueError, "Parameter 'data' has length .*, but required minimum from out-table headers in .* is .*"),
"B18": ErrorAndMessage(RP2TypeError, "Parameter 'asset' has non-string value .*"),
"B19": ErrorAndMessage(RP2ValueError, "Found an empty cell while parsing table EntrySetType.OUT"),
"B20": ErrorAndMessage(RP2ValueError, "IN table not found"),
"B21": ErrorAndMessage(RP2ValueError, "IN table not found or empty"),
"B22": ErrorAndMessage(RP2ValueError, "Found data with no header"),
"B23": ErrorAndMessage(RP2ValueError, "Parameter 'timestamp' value has no timezone info: .*"),
"B24": ErrorAndMessage(RP2ValueError, "Parameter 'exchange' value is not known: .*"),
"B25": ErrorAndMessage(RP2ValueError, "Parameter 'holder' value is not known: .*"),
"B26": ErrorAndMessage(RP2ValueError, "Parameter 'transaction_type' has invalid transaction type value: .*"),
"B27": ErrorAndMessage(RP2ValueError, "Parameter 'asset' value is not known: .*"),
"B28": ErrorAndMessage(RP2ValueError, "Parameter 'crypto_in' has non-positive value .*"),
"B29": ErrorAndMessage(RP2ValueError, "Parameter 'spot_price' has non-positive value .*"),
"B30": ErrorAndMessage(RP2ValueError, "Parameter 'fiat_fee' has non-positive value .*"),
"B31": ErrorAndMessage(RP2ValueError, "Found an empty cell while parsing table EntrySetType.IN"),
"B32": ErrorAndMessage(RP2ValueError, "TABLE END not found for EntrySetType.OUT table"),
"B33": ErrorAndMessage(RP2ValueError, "Found more than one IN symbol"),
"B34": ErrorAndMessage(RP2ValueError, "IN table not found or empty"),
"B35": ErrorAndMessage(RP2ValueError, "IN table not found or empty"),
"B36": ErrorAndMessage(RP2ValueError, "IN table not found or empty"),
}
sheet: str
message: str
for sheet, (error_class, message) in sheets_to_expected_messages.items():
with self.assertRaisesRegex(error_class, message):
asset: str = sheet
input_file_handle: object = open_ods(configuration=self._bad_input_configuration, input_file_path="./input/test_bad_data.ods")
parse_ods(self._bad_input_configuration, asset, input_file_handle)
if __name__ == "__main__":
unittest.main()