Repository navigation
Expand file tree
/
Copy pathtest_pdf2ynab.py
More file actions
364 lines (342 loc) · 11.2 KB
/
Copy pathtest_pdf2ynab.py
File metadata and controls
364 lines (342 loc) · 11.2 KB
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
"""Unit test suite for PDF2YNAB
"""
import unittest
import numpy as np
import pandas as pd
import pdf2ynab as p2y
import globals as gl
import bank_formats as bf
class TestFixDuplicatedHeaders(unittest.TestCase):
def test_removes_duplicated_header(self):
test_input = np.array([[
"DATE",
"TIME",
"TRANSACTION",
"CHANNEL",
"DESCRIPTION",
"CHQ NO.",
"WITHDRAWAL",
"DEPOSIT",
"BALANCE"
], [
"24/03/2017",
"19:19",
"X2",
"POS",
"BLAH",
"",
"-796.00",
"",
"+233,070.25"
], [
"24/03/2017",
"20:29",
"X2",
"ENET",
"BLEH",
"",
"-100.00",
"",
"+232,970.25"
], [
"DATE",
"TIME",
"TRANSACTION",
"CHANNEL",
"DESCRIPTION",
"CHQ NO.",
"WITHDRAWAL",
"DEPOSIT",
"BALANCE"
], [
"24/03/2017",
"20:31",
"X2",
"ENET",
"FOO",
"",
"-35,000.00",
"",
"+197,970.25"
], [
"26/03/2017",
"20:04",
"X2",
"POS",
"BAR",
"",
"-825.00",
"",
"+197,145.25"
]])
test_dataframe = pd.DataFrame(
data=test_input[1:, 0:],
index=range(test_input.shape[0] - 1),
columns=test_input[0, 0:]
)
result_dataframe = p2y.fix_duplicated_headers(test_dataframe)
header = result_dataframe.columns.values.tolist()
for row in result_dataframe.iterrows():
data_series = row[1]
data = data_series.tolist()
self.assertNotEqual(data, header)
class TestFixColumnsMethod(unittest.TestCase):
"""Tests for fix_columns()
Method should rename and edit columns to match YNAB format
"""
def setUp(self):
test_input_1 = np.array([[
"DATE",
"TIME",
"TRANSACTION",
"CHANNEL",
"DESCRIPTION",
"CHQ NO.",
"WITHDRAWAL",
"DEPOSIT",
"BALANCE"
], [
"01/02/2017",
"10:11",
"X2",
"ENET",
"FOO",
"",
"-500.00",
"",
"+483,346.08"
], [
"28/02/2017",
"16:17",
"X1",
"BCMS",
"NERTZ",
"",
"",
"+142,442.50",
"+514,138.80"
]
])
test_dataframe_1 = pd.DataFrame(
data=test_input_1[1:, 0:],
index=range(test_input_1.shape[0] - 1),
columns=test_input_1[0, 0:]
)
bank = bf.BANK_FORMATS["SCB"]
self.column_options = bank[gl.BF_COL_FORMAT]
self.result_dataframe_1 = p2y.fix_columns(
test_dataframe_1, self.column_options
)
def test_renamed_using_column_options(self):
"""Tests that method properly uses column_options
"""
for column in self.column_options.keys():
self.assertIn(
column,
self.result_dataframe_1.columns.values.tolist()
)
def test_date_column_has_date_string(self):
"""Tests that renamed "Date" column has date-type data
Checks that string splits into 2 char, 2 char and 2/4 char strings
"""
col_list_date = self.result_dataframe_1[gl.COL_NAME_DATE].tolist()
for the_date in col_list_date:
self.assertIsInstance(the_date, str)
the_date_list = the_date.split("/")
the_date_string_month = the_date_list[0]
the_date_string_day = the_date_list[1]
the_date_string_year = the_date_list[2]
the_date_string_length_year = len(the_date_string_year)
self.assertEqual(len(the_date_string_month), 2)
self.assertEqual(len(the_date_string_day), 2)
self.assertTrue(
the_date_string_length_year == 4 or
the_date_string_length_year == 2
)
def test_payee_column_is_full_of_strings(self):
"""Tests that renamed "Payee" column has a string in every entry
"""
col_list_payee = self.result_dataframe_1[gl.COL_NAME_PAYEE].tolist()
for the_payee in col_list_payee:
self.assertIsInstance(the_payee, str)
def test_money_columns_have_money_strings(self):
"""Tests that renamed "Outflow"/"Inflow" strings contain $ values
String should contain digits
Can have leading non-numeric characters
Can have thousands separators
Must have decimal separator and 2 decimal digits
"""
regex = r"^(\+|-)\d+(,\d{3})*.\d{2}$"
col_list_oflow = self.result_dataframe_1[gl.COL_NAME_OUTFLOW].tolist()
col_list_iflow = self.result_dataframe_1[gl.COL_NAME_INFLOW].tolist()
for outflow in col_list_oflow:
if not len(outflow) == 0:
self.assertRegex(outflow, regex)
for inflow in col_list_iflow:
if not len(inflow) == 0:
self.assertRegex(inflow, regex)
def test_correct_columns_in_correct_order(self):
"""Tests that fixed DataFrame has all correct columns
"""
actual_columns = self.result_dataframe_1.columns.values.tolist()
expected_columns = gl.COL_NAMES_LIST_ALL
self.assertEqual(actual_columns, expected_columns)
def test_expected_empty_columns_are_empty(self):
"""Tests that columns not in column_options only have empty strings
"""
expected_empty_columns = []
given_columns = self.column_options.keys()
for expected_column in gl.COL_NAMES_LIST_ALL:
if expected_column not in given_columns:
expected_empty_columns.append(expected_column)
for column in expected_empty_columns:
column_list = self.result_dataframe_1[column].tolist()
for entry in column_list:
self.assertTrue(
(entry is None) or
(len(entry) == 0)
)
class TestFixMoneyColumns(unittest.TestCase):
"""Tests for fix_money_columns()
Method should fix values in "Inflow" and "Outflow" columns
"""
def test_removes_nonnumerics_except_decimal(self):
"""Tests that method strips all non-numerics except decimal separator
"""
test_input_1 = np.array([[
"DATE",
"TIME",
"TRANSACTION",
"CHANNEL",
"DESCRIPTION",
"CHQ NO.",
"Outflow",
"Inflow",
"BALANCE"
], [
"01/02/2017",
"10:11",
"X2",
"ENET",
"FOO",
"",
"-500.00",
"",
"+483,346.08"
], [
"28/02/2017",
"16:17",
"X1",
"BCMS",
"NERTZ",
"",
"",
"+142,442.50",
"+514,138.80"
]
])
test_dataframe_1 = pd.DataFrame(
data=test_input_1[1:, 0:],
index=range(test_input_1.shape[0] - 1),
columns=test_input_1[0, 0:]
)
separator = gl.DECIMAL_SEPARATOR.PERIOD
self.result_dataframe_1 = p2y.fix_money_columns(
test_dataframe_1, separator
)
regex = r"\d+\.\d{2}"
col_list_oflow = self.result_dataframe_1[gl.COL_NAME_OUTFLOW].tolist()
col_list_iflow = self.result_dataframe_1[gl.COL_NAME_INFLOW].tolist()
for outflow in col_list_oflow:
if len(outflow) != 0:
self.assertRegex(outflow, regex)
for inflow in col_list_iflow:
if len(inflow) != 0:
self.assertRegex(inflow, regex)
def test_strip_nondecimal_numerics_strips_correctly(self):
"""Tests that stripper function works properly
"""
separator = gl.DECIMAL_SEPARATOR.PERIOD
test_string_1 = "-500.00"
test_string_2 = "+500.00"
expected_string_1 = "500.00"
test_string_3 = "5,000.00"
expected_string_2 = "5000.00"
result_1 = p2y.fix_money_value(test_string_1, separator)
result_2 = p2y.fix_money_value(test_string_2, separator)
result_3 = p2y.fix_money_value(test_string_3, separator)
result_4 = p2y.fix_money_value(expected_string_1, separator)
self.assertEqual(result_1, expected_string_1)
self.assertEqual(result_2, expected_string_1)
self.assertEqual(result_3, expected_string_2)
self.assertEqual(result_4, expected_string_1)
class TestFixDateColumn(unittest.TestCase):
"""Tests for fix_date_column
Method should fix strings in "Date" column to be easier to import to YNAB
"""
def test_reformats_dates_correctly(self):
"""Tests that reformatted dates use MM/DD/YYYY format
Month should be between 01 and 12
Day should be between 01 and 31
"""
test_input_1 = np.array([[
"Date",
"TIME",
"TRANSACTION",
"CHANNEL",
"DESCRIPTION",
"CHQ NO.",
"WITHDRAWAL",
"DEPOSIT",
"BALANCE"
], [
"01/02/2017",
"10:11",
"X2",
"ENET",
"FOO",
"",
"-500.00",
"",
"+483,346.08"
], [
"28/02/2017",
"16:17",
"X1",
"BCMS",
"NERTZ",
"",
"",
"+142,442.50",
"+514,138.80"
]
])
test_dataframe_1 = pd.DataFrame(
data=test_input_1[1:, 0:],
index=range(test_input_1.shape[0] - 1),
columns=test_input_1[0, 0:]
)
bank = bf.BANK_FORMATS["SCB"]
date_regex = bank[gl.BF_DATE_REGEX]
date_repl = bank[gl.BF_DATE_REPLACE]
result_dataframe_1 = p2y.fix_date_column(
test_dataframe_1, date_regex, date_repl
)
col_list_date = result_dataframe_1[gl.COL_NAME_DATE].tolist()
for the_date in col_list_date:
the_date_list = the_date.split("/")
the_date_string_month = the_date_list[0]
the_date_string_day = the_date_list[1]
the_date_string_year = the_date_list[2]
the_date_string_length_year = len(the_date_string_year)
self.assertEqual(len(the_date_string_month), 2)
self.assertEqual(len(the_date_string_day), 2)
self.assertTrue(the_date_string_length_year == 4)
the_date_month = int(the_date_string_month)
the_date_day = int(the_date_string_day)
self.assertGreater(the_date_month, 0)
self.assertGreater(the_date_day, 0)
self.assertLess(the_date_month, 13)
self.assertLess(the_date_day, 32)
if __name__ == '__main__':
unittest.main()