/
tests.py
508 lines (403 loc) · 19.6 KB
/
tests.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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
import logging
from decimal import Decimal
import six
import tablib
from django.core.files.uploadedfile import SimpleUploadedFile
from django.test import Client
from django.test import TestCase
from django.urls import reverse
from django.utils.datetime_safe import date
from hordak.models import Account, StatementLine, StatementImport
from swiftwind.transactions.forms import TransactionImportForm
from swiftwind.transactions.models import TransactionImportColumn
from swiftwind.transactions.resources import StatementLineResource
from swiftwind.utilities.testing import DataProvider
from .models import TransactionImport
class CreateImportViewTestCase(DataProvider, TestCase):
def setUp(self):
self.view_url = reverse('transactions:import_create')
def test_load(self):
c = Client()
response = c.post(self.view_url)
self.assertEqual(response.status_code, 200)
def test_success_url(self):
from swiftwind.transactions.views import CreateImportView
view = CreateImportView()
view.object = TransactionImport.objects.create(hordak_import=self.statement_import())
self.assertIn(str(view.object.uuid), view.get_success_url())
class SetupImportViewTestCase(DataProvider, TestCase):
def setUp(self):
self.transaction_import = TransactionImport.objects.create(hordak_import=self.statement_import())
self.view_url = reverse('transactions:import_setup', args=[self.transaction_import.uuid])
def test_load(self):
c = Client()
response = c.get(self.view_url)
self.assertEqual(response.status_code, 200)
def test_submit(self):
c = Client()
column1 = TransactionImportColumn.objects.create(
transaction_import=self.transaction_import,
column_number=1,
column_heading='Transaction Date',
example='1/1/1'
)
column2 = TransactionImportColumn.objects.create(
transaction_import=self.transaction_import,
column_number=2,
column_heading='Transaction Amount',
example='123.45'
)
response = c.post(self.view_url, data={
'date_format': '%d-%m-%Y',
'columns-INITIAL_FORMS': '2',
'columns-TOTAL_FORMS': '2',
'columns-0-id': column1.pk,
'columns-0-to_field': 'date',
'columns-1-id': column2.pk,
'columns-1-to_field': 'amount',
})
if response.context:
# If we have a context then it is going to be because the form has errors
self.assertFalse(response.context['form'].errors)
self.transaction_import.refresh_from_db()
column1.refresh_from_db()
column2.refresh_from_db()
self.assertEqual(self.transaction_import.date_format, '%d-%m-%Y')
self.assertEqual(column1.to_field, 'date')
self.assertEqual(column2.to_field, 'amount')
class TransactionImportTestCase(DataProvider, TestCase):
def test_create_columns_ok(self):
f = SimpleUploadedFile('data.csv',
six.binary_type(
b'Number,Date,Account,Amount,Subcategory,Memo\n'
b'1,1/1/1,123456789,123,OTH,Some random notes')
)
inst = TransactionImport.objects.create(has_headings=True, file=f, hordak_import=self.statement_import())
inst.create_columns()
columns = inst.columns.all()
self.assertEqual(columns[0].column_number, 1)
self.assertEqual(columns[0].column_heading, 'Number')
self.assertEqual(columns[0].to_field, None)
self.assertEqual(columns[0].example, '1')
self.assertEqual(columns[1].column_number, 2)
self.assertEqual(columns[1].column_heading, 'Date')
self.assertEqual(columns[1].to_field, 'date')
self.assertEqual(columns[1].example, '1/1/1')
self.assertEqual(columns[2].column_number, 3)
self.assertEqual(columns[2].column_heading, 'Account')
self.assertEqual(columns[2].to_field, None)
self.assertEqual(columns[2].example, '123456789')
self.assertEqual(columns[3].column_number, 4)
self.assertEqual(columns[3].column_heading, 'Amount')
self.assertEqual(columns[3].to_field, 'amount')
self.assertEqual(columns[3].example, '123')
self.assertEqual(columns[4].column_number, 5)
self.assertEqual(columns[4].column_heading, 'Subcategory')
self.assertEqual(columns[4].to_field, None)
self.assertEqual(columns[4].example, 'OTH')
self.assertEqual(columns[5].column_number, 6)
self.assertEqual(columns[5].column_heading, 'Memo')
self.assertEqual(columns[5].to_field, 'description')
self.assertEqual(columns[5].example, 'Some random notes')
class StatementLineResourceTestCase(DataProvider, TestCase):
"""Test the resource definition in resources.py"""
def setUp(self):
self.account = self.account(is_bank_account=True, type=Account.TYPES.asset)
logging.disable(logging.CRITICAL)
def tearDown(self):
logging.disable(logging.INFO)
def makeResource(self):
statement_import = StatementImport.objects.create(bank_account=self.account)
return StatementLineResource('%d/%m/%Y', statement_import)
def test_import_one(self):
dataset = tablib.Dataset(
['15/6/2016', '5.10', 'Example payment'],
headers=['date', 'amount', 'description']
)
self.makeResource().import_data(dataset)
self.assertEqual(StatementLine.objects.count(), 1)
obj = StatementLine.objects.get()
self.assertEqual(obj.date, date(2016, 6, 15))
self.assertEqual(obj.amount, Decimal('5.10'))
self.assertEqual(obj.description, 'Example payment')
def test_import_skip_duplicates(self):
dataset = tablib.Dataset(
['15/6/2016', '5.10', 'Example payment'],
headers=['date', 'amount', 'description']
)
self.makeResource().import_data(dataset)
# Now do the import again
self.makeResource().import_data(dataset)
# The record in the second should have been ignored
self.assertEqual(StatementLine.objects.count(), 1)
def test_import_two_identical(self):
"""Ensure they both get imported and that one doesnt get skipped as a duplicate
After all, if there are two imported rows that look identical, it is probably because
there are two identical transactions.
"""
dataset = tablib.Dataset(
['15/6/2016', '5.10', 'Example payment'],
['15/6/2016', '5.10', 'Example payment'],
headers=['date', 'amount', 'description']
)
self.makeResource().import_data(dataset)
self.assertEqual(StatementLine.objects.count(), 2)
def test_import_a_few(self):
dataset = tablib.Dataset(
['15/6/2016', '5.10', 'Example payment'],
['16/6/2016', '10.91', 'Another payment'],
['17/6/2016', '-1.23', 'Paying someone'],
headers=['date', 'amount', 'description']
)
self.makeResource().import_data(dataset)
self.assertEqual(StatementLine.objects.count(), 3)
objs = StatementLine.objects.all().order_by('pk')
self.assertEqual(objs[0].date, date(2016, 6, 15))
self.assertEqual(objs[0].amount, Decimal('5.10'))
self.assertEqual(objs[0].description, 'Example payment')
self.assertEqual(objs[1].date, date(2016, 6, 16))
self.assertEqual(objs[1].amount, Decimal('10.91'))
self.assertEqual(objs[1].description, 'Another payment')
self.assertEqual(objs[2].date, date(2016, 6, 17))
self.assertEqual(objs[2].amount, Decimal('-1.23'))
self.assertEqual(objs[2].description, 'Paying someone')
def test_import_a_few_with_identical_transactions(self):
dataset = tablib.Dataset(
['15/6/2016', '5.10', 'Example payment'],
['16/6/2016', '10.91', 'Another payment'],
['16/6/2016', '10.91', 'Another payment'],
['17/6/2016', '-1.23', 'Paying someone'],
headers=['date', 'amount', 'description']
)
self.makeResource().import_data(dataset)
self.assertEqual(StatementLine.objects.count(), 4)
objs = StatementLine.objects.all().order_by('pk')
self.assertEqual(objs[0].date, date(2016, 6, 15))
self.assertEqual(objs[0].amount, Decimal('5.10'))
self.assertEqual(objs[0].description, 'Example payment')
self.assertEqual(objs[1].date, date(2016, 6, 16))
self.assertEqual(objs[1].amount, Decimal('10.91'))
self.assertEqual(objs[1].description, 'Another payment')
self.assertEqual(objs[2].date, date(2016, 6, 16))
self.assertEqual(objs[2].amount, Decimal('10.91'))
self.assertEqual(objs[2].description, 'Another payment')
self.assertEqual(objs[3].date, date(2016, 6, 17))
self.assertEqual(objs[3].amount, Decimal('-1.23'))
self.assertEqual(objs[3].description, 'Paying someone')
def test_split_amounts(self):
dataset = tablib.Dataset(
['15/6/2016', '', '100.56', 'Example payment'],
['16/6/2016', '60.31', '', 'Example income'],
headers=['date', 'amount_in', 'amount_out', 'description']
)
self.makeResource().import_data(dataset)
self.assertEqual(StatementLine.objects.count(), 2)
obj = StatementLine.objects.all().order_by('date')
self.assertEqual(obj[0].date, date(2016, 6, 15))
self.assertEqual(obj[0].amount, Decimal('-100.56'))
self.assertEqual(obj[0].description, 'Example payment')
self.assertEqual(obj[1].date, date(2016, 6, 16))
self.assertEqual(obj[1].amount, Decimal('60.31'))
self.assertEqual(obj[1].description, 'Example income')
def test_error_no_date(self):
dataset = tablib.Dataset(
['5.10', 'Example payment'],
headers=['amount', 'description']
)
result = self.makeResource().import_data(dataset)
self.assertEqual(len(result.row_errors()), 1)
self.assertIn('No date', str(result.row_errors()[0][1][0].error))
def test_error_empty_date(self):
dataset = tablib.Dataset(
['', '5.10', 'Example payment'],
headers=['date', 'amount', 'description']
)
result = self.makeResource().import_data(dataset)
self.assertEqual(len(result.row_errors()), 1)
self.assertIn('Expected dd/mm/yyyy', str(result.row_errors()[0][1][0].error))
def test_error_empty_amounts(self):
dataset = tablib.Dataset(
['15/6/2016', '', '', 'Example payment'],
headers=['date', 'amount_in', 'amount_out', 'description']
)
result = self.makeResource().import_data(dataset)
self.assertEqual(len(result.row_errors()), 1)
self.assertIn('Value required', str(result.row_errors()[0][1][0].error))
def test_error_empty_amount(self):
dataset = tablib.Dataset(
['15/6/2016', '', 'Example payment'],
headers=['date', 'amount', 'description']
)
result = self.makeResource().import_data(dataset)
self.assertEqual(len(result.row_errors()), 1)
self.assertIn('No value found', str(result.row_errors()[0][1][0].error))
def test_error_both_amounts(self):
dataset = tablib.Dataset(
['15/6/2016', '5.10', '1.20', 'Example payment'],
headers=['date', 'amount_in', 'amount_out', 'description']
)
result = self.makeResource().import_data(dataset)
self.assertEqual(len(result.row_errors()), 1)
self.assertIn('Values found for both', str(result.row_errors()[0][1][0].error))
def test_error_neither_amount(self):
dataset = tablib.Dataset(
['15/6/2016', '', '', 'Example payment'],
headers=['date', 'amount_in', 'amount_out', 'description']
)
result = self.makeResource().import_data(dataset)
self.assertEqual(len(result.row_errors()), 1)
self.assertIn('either', str(result.row_errors()[0][1][0].error))
def test_error_invalid_in_amount(self):
dataset = tablib.Dataset(
['15/6/2016', 'a', '', 'Example payment'],
headers=['date', 'amount_in', 'amount_out', 'description']
)
result = self.makeResource().import_data(dataset)
self.assertEqual(len(result.row_errors()), 1)
self.assertIn('Invalid', str(result.row_errors()[0][1][0].error))
def test_error_invalid_out_amount(self):
dataset = tablib.Dataset(
['15/6/2016', '', 'a', 'Example payment'],
headers=['date', 'amount_in', 'amount_out', 'description']
)
result = self.makeResource().import_data(dataset)
self.assertEqual(len(result.row_errors()), 1)
self.assertIn('Invalid', str(result.row_errors()[0][1][0].error))
def test_error_invalid_amount(self):
dataset = tablib.Dataset(
['15/6/2016', 'a', 'Example payment'],
headers=['date', 'amount', 'description']
)
result = self.makeResource().import_data(dataset)
self.assertEqual(len(result.row_errors()), 1)
self.assertIn('Invalid', str(result.row_errors()[0][1][0].error))
def test_error_no_amount(self):
dataset = tablib.Dataset(
['15/6/2016', 'Example payment'],
headers=['date', 'description']
)
result = self.makeResource().import_data(dataset)
self.assertEqual(len(result.row_errors()), 1)
self.assertIn('No amount', str(result.row_errors()[0][1][0].error))
def test_error_zero_amount(self):
dataset = tablib.Dataset(
['15/6/2016', '0', 'Example payment'],
headers=['date', 'amount', 'description']
)
result = self.makeResource().import_data(dataset)
self.assertEqual(len(result.row_errors()), 1)
self.assertIn('zero not allowed', str(result.row_errors()[0][1][0].error))
class DryRunViewTestCase(DataProvider, TestCase):
def setUp(self):
logging.disable(logging.CRITICAL)
def tearDown(self):
logging.disable(logging.INFO)
def create_import(self, year=b'2000'):
f = SimpleUploadedFile('data.csv',
six.binary_type(
b'Number,Date,Account,Amount,Subcategory,Memo\n'
b'1,1/1/' + year + b',123456789,123,OTH,Some random notes')
)
self.transaction_import = TransactionImport.objects.create(
has_headings=True,
file=f,
date_format='%d/%m/%Y',
hordak_import=self.statement_import(),
)
self.view_url = reverse('transactions:import_dry_run', args=[self.transaction_import.uuid])
self.transaction_import.create_columns()
self.transaction_import.columns.filter(column_number=2).update(to_field='date')
self.transaction_import.columns.filter(column_number=4).update(to_field='amount')
self.transaction_import.columns.filter(column_number=6).update(to_field='description')
def test_get(self):
self.create_import()
response = self.client.get(self.view_url)
self.assertEqual(response.status_code, 200)
self.assertEqual(StatementLine.objects.count(), 0)
def test_post(self):
self.create_import()
response = self.client.post(self.view_url)
result = response.context['result']
self.assertEqual(len(result.failed_dataset), 0, result.failed_dataset.dict)
self.assertEqual(result.base_errors, [])
self.assertEqual(result.totals['new'], 1)
self.assertEqual(result.totals['update'], 0)
self.assertEqual(result.totals['delete'], 0)
self.assertEqual(result.totals['skip'], 0)
self.assertEqual(result.totals['error'], 0)
self.assertEqual(StatementLine.objects.count(), 0)
def test_date_error(self):
self.create_import(b'1')
response = self.client.post(self.view_url)
result = response.context['result']
self.assertEqual(len(result.failed_dataset), 1, result.failed_dataset.dict)
self.assertEqual(len(result.row_errors()), 1)
self.assertEqual(StatementLine.objects.count(), 0)
class ExecuteViewTestCase(DataProvider, TestCase):
def setUp(self):
logging.disable(logging.CRITICAL)
def tearDown(self):
logging.disable(logging.INFO)
def create_import(self, year=b'2000'):
f = SimpleUploadedFile('data.csv',
six.binary_type(
b'Number,Date,Account,Amount,Subcategory,Memo\n'
b'1,1/1/' + year + b',123456789,123,OTH,Some random notes')
)
self.transaction_import = TransactionImport.objects.create(
has_headings=True,
file=f,
date_format='%d/%m/%Y',
hordak_import=self.statement_import()
)
self.view_url = reverse('transactions:import_execute', args=[self.transaction_import.uuid])
self.transaction_import.create_columns()
self.transaction_import.columns.filter(column_number=2).update(to_field='date')
self.transaction_import.columns.filter(column_number=4).update(to_field='amount')
self.transaction_import.columns.filter(column_number=6).update(to_field='description')
def test_get(self):
self.create_import()
response = self.client.get(self.view_url)
self.assertEqual(response.status_code, 200)
self.assertEqual(StatementLine.objects.count(), 0)
def test_post(self):
self.create_import()
response = self.client.post(self.view_url)
result = response.context['result']
self.assertEqual(len(result.failed_dataset), 0, result.failed_dataset.dict)
self.assertEqual(result.base_errors, [])
self.assertEqual(result.totals['new'], 1)
self.assertEqual(result.totals['update'], 0)
self.assertEqual(result.totals['delete'], 0)
self.assertEqual(result.totals['skip'], 0)
self.assertEqual(result.totals['error'], 0)
self.assertEqual(StatementLine.objects.count(), 1)
def test_date_error(self):
self.create_import(b'1')
response = self.client.post(self.view_url)
result = response.context['result']
self.assertEqual(len(result.failed_dataset), 1, result.failed_dataset.dict)
self.assertEqual(len(result.row_errors()), 1)
self.assertEqual(StatementLine.objects.count(), 0)
class TransactionImportFormTestCase(DataProvider, TestCase):
def setUp(self):
self.account = self.account(is_bank_account=True, type=Account.TYPES.asset)
self.f = SimpleUploadedFile('data.csv',
six.binary_type(b'Number,Date,Account,Amount,Subcategory,Memo'))
def test_create(self):
form = TransactionImportForm(data=dict(bank_account=self.account.pk), files=dict(file=self.f))
self.assertTrue(form.is_valid(), form.errors)
form.save()
obj = TransactionImport.objects.get()
self.assertEqual(obj.columns.count(), 6)
self.assertEqual(obj.hordak_import.bank_account, self.account)
def test_edit(self):
obj = TransactionImport.objects.create(
hordak_import=self.statement_import(bank_account=self.account),
has_headings=True,
file=self.f
)
form = TransactionImportForm(data=dict(bank_account=self.account.pk), files=dict(file=self.f), instance=obj)
self.assertTrue(form.is_valid(), form.errors)
form.save()
self.assertEqual(obj.columns.count(), 0)