-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_jointSpendingCalculator.py
389 lines (352 loc) · 12.3 KB
/
test_jointSpendingCalculator.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
import pytest
from fixtures import *
from unittest.mock import patch
from jointSpendingCalculator import *
import os
import csv
class TestGetDetails:
def test_get_names(self):
list_of_names = [' John ', ' john ', 'john', 'John']
with patch('builtins.input', side_effect=list_of_names):
names = get_names()
for name in names:
assert name[0].isupper() == True
assert name[-1].isalpha() == True
def test_which_folder_are_the_statements_in(self, directory):
assert os.path.isdir(directory) is True
assert directory[-1] == '/'
def test_add_trailing_space(self, directory):
folder_name = add_trailing_slash_if_needed(directory)
assert folder_name[-1] == '/'
def test_folder_contains_correct_statements(self, statements):
statement_1 = 'coop_statement.csv'
statement_2 = 'monzo_statement.csv'
statement_3 = 'jans_coop_bank_statement.csv'
assert statement_1 in statements
assert statement_2 in statements
assert statement_3 not in statements
def test_create_totals_file(self, directory):
with patch("builtins.input", return_value="totals"):
spreadsheet = create_totals_file(directory)
assert os.path.isfile(directory + spreadsheet) is True
test_cases_bank_name = [
(
['Jan','abc', '123', '1','2','3',' co-operative'],
{
'person':'Jan',
'outgoings_column': ' Money Out'
}
)
,(
['Sophie', 'elephants', 'tomatoes', ' monzo'],
{
'person':'Sophie',
'outgoings_column': 'Amount'
}
),
(
['Julius', 'Co-operative'],
{
'person':'Julius',
'outgoings_column': ' Money Out'
}
),
(
['Julius', 'Monzo'],
{
'person':'Julius',
'outgoings_column': 'Amount'
}
)
]
@pytest.mark.parametrize('name_and_bank_names,expected', test_cases_bank_name)
def test_whose_statement_which_bank(self, statements, name_and_bank_names, expected):
with patch("builtins.input", side_effect=name_and_bank_names):
person, outgoings_column_name = whose_statement_and_which_bank(statements)
assert person == expected['person']
assert outgoings_column_name == expected['outgoings_column']
class TestReadStatement:
test_cases = [
# Read Padme's statement - Jan pays for everything
(
"Padme",
"monzo_statement.csv",
"Amount",
"Jan",
{
"owes":"Padme",
"Jan":90.0,
"Padme":0.0
}
),
# Read Reggie's statement - Jan and Sophie split everything
(
"Reggie",
"monzo_statement.csv",
"Amount",
"Jan Sophie",
{
"owes": "Reggie",
"Jan": 45.0,
"Sophie": 45.0,
"Reggie": 0.0
}
),
# Read Jan's statement - Sophie, Jane, Sven split everything between them
(
"Jan",
"monzo_statement.csv",
"Amount",
"Sophie Jane Sven",
{
"owes": "Jan",
"Sophie": 30.0,
"Jane": 30.0,
"Sven": 30.0,
"Jan": 0.0
}
),
# Read Sophie's statement - Sophie and Lou split everything between them
(
"Sophie",
"coop_statement.csv",
" Money Out",
"Sophie Lou Nai",
{
"owes": "Sophie",
"Sophie": 0.0,
"Lou": 136.67,
"Nai": 136.67
}
),
(
"Sophie",
"coop_statement.csv",
" Money Out",
"*SKIP*",
{
"owes": "Sophie",
"Sophie": 0.0
}
)
]
@pytest.mark.parametrize("statement_owner,statement,outgoings_column,return_value,expected", test_cases)
def test_read_statement(self, statement_owner, statement, outgoings_column, return_value, expected, directory):
with patch("builtins.input", return_value=return_value):
owed_from_statement, everyone_who_owes_from_this_statement = read_statement(statement, outgoings_column, statement_owner, directory)
people = return_value.split(' ')
assert expected == owed_from_statement
assert people.sort() == everyone_who_owes_from_this_statement.sort()
class TestReadThenMerge:
'''Merge the values owed from a statement with the values in a totals spreadsheet'''
test_cases = [
# Can add values to an empty totals spreadsheet
(
"Denise",
"monzo_statement.csv",
"Amount",
"Jan",
"totals.csv",
[
{
"owes": "Denise",
"Jan": 90.0,
"Denise": 0.0
}
]
),
# Can update values names already in the totals spreadsheet
(
"Sophie",
"monzo_statement.csv",
"Amount",
"Jan",
"../prefilled_totals.csv",
[
{
"owes": "Sophie",
"Jan": 190.0,
"Sophie": 0.0
},
{
"owes": "Jan",
"Jan": 0.0,
"Sophie":10.0
}
]
),
# Can add new statement owner to the totals spreadsheet
(
"Martin",
"coop_statement.csv",
" Money Out",
"Petr Daniela Sophie Jan",
"../prefilled_totals.csv",
[
{
"owes": "Sophie",
"Jan": 100.0,
"Sophie": 0.0,
"Petr": 0.0,
"Daniela": 0.0,
"Martin": 0.0
},
{
"owes": "Jan",
"Sophie": 10.0,
"Jan": 0.0,
"Petr": 0.0,
"Daniela": 0.0,
"Martin": 0.0
},
{
"owes": "Martin",
"Petr": 102.5,
"Daniela": 102.5,
"Jan": 102.5,
"Sophie": 102.5,
"Martin": 0.0
}
]
)
]
@pytest.mark.parametrize("statement_owner,statement,outgoings_column,return_value,totals_statement,expected", test_cases)
def test_can_merge_money_owed_with_values_in_totals_spreadsheet(self, statement_owner, statement, outgoings_column,return_value, expected, directory, totals_statement, totals_spreadsheet):
dir = directory
with patch("builtins.input", return_value=return_value):
owed_from_statement, everyone_who_owes_from_this_statement = read_statement(statement, outgoings_column, statement_owner, dir)
updated_totals, _ = merge_owed_from_statement_with_totals(dir, everyone_who_owes_from_this_statement, statement_owner, totals_statement, owed_from_statement)
assert expected == updated_totals
class TestWriteTotalsSpreadsheet:
test_cases = [
# Can read a statement, 1 person pays for everything
# Can update values for names already in pre-populated totals statement
# Can re-write spreadsheet with updated values
(
"Sophie",
"monzo_statement.csv",
"Amount",
"Jan",
"../prefilled_totals.csv",
[
{
'owes': 'owes',
'Jan': 'Jan',
'Sophie': 'Sophie'
},
{
"owes": "Sophie",
"Jan": "190.0",
"Sophie": "0.0"
},
{
"owes": "Jan",
"Jan": "0.0",
"Sophie":"10.0"
}
]
),
# Can read a spreadsheet, 3 people split cost
# Can merge with prefilled totals spreadsheet
# Can write to totals statement
(
"Sophie",
"monzo_statement.csv",
"Amount",
"Jan Finn Lou",
"../prefilled_totals.csv",
[
{
'owes': 'owes',
'Jan': 'Jan',
'Sophie': 'Sophie',
'Finn': 'Finn',
'Lou': 'Lou'
},
{
"owes": "Sophie",
"Jan": "130.0",
"Sophie": "0.0",
"Lou": "30.0",
"Finn": "30.0"
},
{
"owes": "Jan",
"Jan": "0.0",
"Sophie":"10.0",
"Finn":"0.0",
"Lou": "0.0"
}
]
),
# When statement owner pays for everything,
# then names already in the totals spreadsheet get value 0.0
(
"Lou",
"monzo_statement.csv",
"Amount",
"Lou",
"../prefilled_totals.csv",
[
{
'owes': 'owes',
'Jan': 'Jan',
'Sophie': 'Sophie',
'Lou': 'Lou'
},
{
"owes": "Sophie",
"Jan": "100.0",
"Sophie": "0.0",
"Lou": "0.0"
},
{
"owes": "Jan",
"Jan": "0.0",
"Sophie":"10.0",
"Lou": "0.0"
},
{
"owes": "Lou",
"Jan": "0.0",
"Sophie": "0.0",
"Lou": "0.0"
}
]
)
]
@pytest.mark.parametrize("statement_owner,statement,outgoings_column,return_value,totals_statement,expected", test_cases)
def test_can_write_to_totals_spreadsheet(self, statement_owner, statement, outgoings_column, return_value, totals_statement, expected, totals_spreadsheet, directory):
totals_sheet = totals_spreadsheet
with patch("builtins.input", return_value=return_value):
owed_from_statement, everyone_who_owes_from_this_statement = read_statement(statement, outgoings_column, statement_owner, directory)
updated_totals, header = merge_owed_from_statement_with_totals(directory, everyone_who_owes_from_this_statement,statement_owner, totals_statement, owed_from_statement)
write_to_totals_spreadsheet(directory, header, totals_sheet, updated_totals)
with open(directory + totals_sheet, "r") as t:
reader = csv.DictReader(t, fieldnames=header)
newly_written_totals_sheet = list(reader)
assert newly_written_totals_sheet == expected
def test_create_totals_html_file():
csv_file = 'prefilled_totals.csv'
directory = './'
html_file = create_table_in_html_file(directory, csv_file)
html_tables = pandas.read_html(html_file)
html_tables[0].to_csv('test_html2csv.csv')
html_2_csv = []
with open('test_html2csv.csv', 'r') as h:
header = ['','Unnamed: 0','owes','Jan','Sophie']
csv_reader = csv.DictReader(h, header)
html_2_csv = list(csv_reader)
for row in html_2_csv:
row.pop('')
row.pop('Unnamed: 0')
prefilled_totals = []
with open('prefilled_totals.csv', 'r') as p:
fieldnames = ['owes', 'Jan', 'Sophie']
csv_reader = csv.DictReader(p, fieldnames)
prefilled_totals = list(csv_reader)
assert prefilled_totals[1] in html_2_csv
assert html_file == 'prefilled_totals.html'
assert os.path.isfile(html_file)
os.remove(html_file)
os.remove('test_html2csv.csv')