-
Notifications
You must be signed in to change notification settings - Fork 151
/
addenda98_test.go
488 lines (421 loc) · 13.9 KB
/
addenda98_test.go
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
// Licensed to The Moov Authors under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. The Moov Authors licenses this file to you 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.
package ach
import (
"strings"
"testing"
"github.com/moov-io/base"
"github.com/stretchr/testify/require"
)
func mockAddenda98() *Addenda98 {
addenda98 := NewAddenda98()
addenda98.ChangeCode = "C01"
addenda98.OriginalTrace = "12345"
addenda98.OriginalDFI = "9101298"
addenda98.CorrectedData = "1918171614"
addenda98.TraceNumber = "91012980000088"
return addenda98
}
func TestAddenda98_LookupChangecode(t *testing.T) {
if code := LookupChangeCode(""); code != nil {
t.Error("expected nil ChangeCode")
}
if code := LookupChangeCode("C05"); code == nil {
t.Error("expected ChangeCode")
} else {
if code.Code != "C05" {
t.Errorf("code.Code=%s", code.Code)
}
if code.Reason != "Incorrect transaction code" {
t.Errorf("code.Reason=%s", code.Reason)
}
}
if code := LookupChangeCode("C64"); code == nil {
t.Error("expected ChangeCode")
}
if code := LookupChangeCode("C99"); code != nil {
t.Errorf("expected nil: %#v", code)
}
}
func testAddenda98Parse(t testing.TB) {
addenda98 := NewAddenda98()
line := "798C01099912340000015 091012981918171614 091012980000088"
addenda98.Parse(line)
// walk the Addenda98 struct
if addenda98.TypeCode != "98" {
t.Errorf("expected %v got %v", "98", addenda98.TypeCode)
}
if addenda98.ChangeCode != "C01" {
t.Errorf("expected %v got %v", "C01", addenda98.ChangeCode)
}
if addenda98.OriginalTrace != "099912340000015" {
t.Errorf("expected %v got %v", "099912340000015", addenda98.OriginalTrace)
}
if addenda98.OriginalDFI != "09101298" {
t.Errorf("expected %s got %s", "09101298", addenda98.OriginalDFI)
}
if addenda98.CorrectedData != "1918171614" {
t.Errorf("expected %v got %v", "1918171614", addenda98.CorrectedData)
}
if addenda98.TraceNumber != "091012980000088" {
t.Errorf("expected %v got %v", "091012980000088", addenda98.TraceNumber)
}
}
func TestAddenda98Parse(t *testing.T) {
testAddenda98Parse(t)
}
func BenchmarkAddenda98Parse(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testAddenda98Parse(b)
}
}
func testAddenda98String(t testing.TB) {
addenda98 := NewAddenda98()
line := "798C01099912340000015 091012981918171614 091012980000088"
addenda98.Parse(line)
if addenda98.String() != line {
t.Errorf("\n expected: %v\n got : %v", line, addenda98.String())
}
}
func TestAddenda98String(t *testing.T) {
testAddenda98String(t)
}
func BenchmarkAddenda98String(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testAddenda98String(b)
}
}
func testAddenda98ValidTypeCode(t testing.TB) {
addenda98 := mockAddenda98()
addenda98.TypeCode = "05"
err := addenda98.Validate()
if !base.Match(err, ErrAddendaTypeCode) {
t.Errorf("%T: %s", err, err)
}
}
func TestAddenda98ValidTypeCode(t *testing.T) {
testAddenda98ValidTypeCode(t)
}
func BenchmarkAddenda98ValidTypeCode(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testAddenda98ValidTypeCode(b)
}
}
func testAddenda98ValidCorrectedData(t testing.TB) {
addenda98 := mockAddenda98()
addenda98.CorrectedData = ""
err := addenda98.Validate()
if !base.Match(err, ErrAddenda98CorrectedData) {
t.Errorf("%T: %s", err, err)
}
}
func TestAddenda98ValidCorrectedData(t *testing.T) {
testAddenda98ValidCorrectedData(t)
}
func BenchmarkAddenda98ValidCorrectedData(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testAddenda98ValidCorrectedData(b)
}
}
func testAddenda98ValidateTrue(t testing.TB) {
addenda98 := mockAddenda98()
addenda98.ChangeCode = "C11"
err := addenda98.Validate()
// no error expected
if !base.Match(err, nil) {
t.Errorf("%T: %s", err, err)
}
}
func TestAddenda98ValidateTrue(t *testing.T) {
testAddenda98ValidateTrue(t)
}
func BenchmarkAddenda98ValidateTrue(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testAddenda98ValidateTrue(b)
}
}
func testAddenda98ValidateChangeCodeFalse(t testing.TB) {
addenda98 := mockAddenda98()
addenda98.ChangeCode = "C55"
err := addenda98.Validate()
if !base.Match(err, ErrAddenda98ChangeCode) {
t.Errorf("%T: %s", err, err)
}
}
func TestAddenda98ValidateChangeCodeFalse(t *testing.T) {
testAddenda98ValidateChangeCodeFalse(t)
}
func BenchmarkAddenda98ValidateChangeCodeFalse(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testAddenda98ValidateChangeCodeFalse(b)
}
}
func testAddenda98OriginalTraceField(t testing.TB) {
addenda98 := mockAddenda98()
exp := "000000000012345"
if addenda98.OriginalTraceField() != exp {
t.Errorf("expected %v received %v", exp, addenda98.OriginalTraceField())
}
}
func TestAddenda98OriginalTraceField(t *testing.T) {
testAddenda98OriginalTraceField(t)
}
func BenchmarkAddenda98OriginalTraceField(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testAddenda98OriginalTraceField(b)
}
}
func testAddenda98OriginalDFIField(t testing.TB) {
addenda98 := mockAddenda98()
exp := "09101298"
if addenda98.OriginalDFIField() != exp {
t.Errorf("expected %v received %v", exp, addenda98.OriginalDFIField())
}
}
func TestAddenda98OriginalDFIField(t *testing.T) {
testAddenda98OriginalDFIField(t)
}
func BenchmarkAddenda98OriginalDFIField(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testAddenda98OriginalDFIField(b)
}
}
func testAddenda98CorrectedDataField(t testing.TB) {
addenda98 := mockAddenda98()
exp := "1918171614 " // 29 char
if addenda98.CorrectedDataField() != exp {
t.Errorf("expected %v received %v", exp, addenda98.CorrectedDataField())
}
}
func TestAddenda98CorrectedDataField(t *testing.T) {
testAddenda98CorrectedDataField(t)
}
func BenchmarkAddenda98CorrectedDataField(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testAddenda98CorrectedDataField(b)
}
}
func testAddenda98TraceNumberField(t testing.TB) {
addenda98 := mockAddenda98()
exp := "091012980000088"
if addenda98.TraceNumberField() != exp {
t.Errorf("expected %v received %v", exp, addenda98.TraceNumberField())
}
}
func TestAddenda98TraceNumberField(t *testing.T) {
testAddenda98TraceNumberField(t)
}
func BenchmarkAddenda98TraceNumberField(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testAddenda98TraceNumberField(b)
}
}
func TestAddenda98__ChangeCodeField(t *testing.T) {
addenda98 := mockAddenda98()
if addenda98.ChangeCode != "C01" {
t.Errorf("addenda98.ChangeCode=%s", addenda98.ChangeCode)
}
if code := addenda98.ChangeCodeField(); code == nil {
t.Fatal("nil Addenda98.ChangeCodeField")
} else {
if code.Code != "C01" {
t.Errorf("code.Code=%s", code.Code)
}
if code.Reason != "Incorrect bank account number" {
t.Errorf("code.Reason=%s", code.Reason)
}
}
// verify another change code
addenda98.ChangeCode = "C07"
if code := addenda98.ChangeCodeField(); code == nil {
t.Fatal("nil Addenda98.ChangeCodeField")
} else {
if code.Code != "C07" {
t.Errorf("code.Code=%s", code.Code)
}
}
// invalid change code
addenda98.ChangeCode = "C99"
if code := addenda98.ChangeCodeField(); code != nil {
t.Errorf("unexpected change code: %v", code)
}
}
// testAddenda98TypeCodeNil validates TypeCode is ""
func testAddenda98TypeCodeNil(t testing.TB) {
addenda98 := mockAddenda98()
addenda98.TypeCode = ""
err := addenda98.Validate()
if !base.Match(err, ErrConstructor) {
t.Errorf("%T: %s", err, err)
}
}
// TestAddenda98TypeCodeES tests TypeCode is ""
func TestAddenda98TypeCodeNil(t *testing.T) {
testAddenda98TypeCodeNil(t)
}
// BenchmarkAddenda98TypeCodeNil benchmarks TypeCode is ""
func BenchmarkAddenda98TypeCodeNil(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testAddenda98TypeCodeNil(b)
}
}
// TestAddenda98RuneCountInString validates RuneCountInString
func TestAddenda98RuneCountInString(t *testing.T) {
addenda98 := NewAddenda98()
var line = "798"
addenda98.Parse(line)
if addenda98.CorrectedData != "" {
t.Error("Parsed with an invalid RuneCountInString not equal to 94")
}
}
func TestCorrectedData__first(t *testing.T) {
if v := first(2, ""); v != "" {
t.Errorf("got='%s'", v)
}
if v := first(2, " "); v != "" {
t.Errorf("got='%s'", v)
}
if v := first(3, "22"); v != "22" {
t.Errorf("got='%s'", v)
}
if v := first(17, " 123 "); v != "123" {
t.Errorf("got='%s'", v)
}
if v := first(17, "123456789 "); v != "123456789" {
t.Errorf("got='%s'", v)
}
}
func TestCorrectedData__ParseCorrectedData(t *testing.T) {
run := func(code, data string) *CorrectedData {
add := NewAddenda98()
add.ChangeCode = code
add.CorrectedData = data
return add.ParseCorrectedData()
}
if v := run("C01", "123456789 "); v.AccountNumber != "123456789" {
t.Errorf("%#v", v)
}
if v := run("C02", "987654320 "); v.RoutingNumber != "987654320" {
t.Errorf("%#v", v)
}
if v := run("C03", "987654320 123456"); v.AccountNumber != "123456" || v.RoutingNumber != "987654320" {
t.Errorf("%#v", v)
}
if v := run("C03", "987654320 123456"); v.AccountNumber != "123456" || v.RoutingNumber != "987654320" {
t.Errorf("%#v", v)
}
if v := run("C04", "Jane Doe"); v.Name != "Jane Doe" {
t.Errorf("%#v", v)
}
if v := run("C05", "22 other"); v.TransactionCode != 22 {
t.Errorf("%#v", v)
}
if v := run("C06", "123456789 22"); v.AccountNumber != "123456789" || v.TransactionCode != 22 {
t.Errorf("%#v", v)
}
if v := run("C07", "987654320 12345 22"); v.RoutingNumber != "987654320" || v.AccountNumber != "12345" || v.TransactionCode != 22 {
t.Errorf("%#v", v)
}
if v := run("C07", "9876543201242415 22"); v.RoutingNumber != "987654320" || v.AccountNumber != "1242415" || v.TransactionCode != 22 {
t.Errorf("%#v", v)
}
if v := run("C07", "9876543201242415 22"); v.RoutingNumber != "987654320" || v.AccountNumber != "1242415" || v.TransactionCode != 22 {
t.Errorf("%#v", v)
}
if v := run("C07", "1234"); v != nil {
t.Errorf("expected nil: %v", v)
}
if v := run("C07", "987654320 1234 1234 1234"); v != nil {
t.Errorf("expected nil: %v", v)
}
if v := run("C09", "21345678 "); v.Identification != "21345678" {
t.Errorf("%#v", v)
}
if v := run("C99", " "); v != nil {
t.Error("expected nil CorrectedData")
}
}
func TestCorrectedData__WriteCorrectionData(t *testing.T) {
data := &CorrectedData{AccountNumber: "12345"}
require.Equal(t, "12345 ", WriteCorrectionData("C01", data))
data = &CorrectedData{RoutingNumber: "987654320"}
require.Equal(t, "987654320 ", WriteCorrectionData("C02", data))
data = &CorrectedData{AccountNumber: "123", RoutingNumber: "987654320"}
require.Equal(t, "987654320 123", WriteCorrectionData("C03", data))
data = &CorrectedData{Name: "Jane Doe"}
require.Equal(t, "Jane Doe ", WriteCorrectionData("C04", data))
data = &CorrectedData{TransactionCode: 22}
require.Equal(t, "22 ", WriteCorrectionData("C05", data))
data = &CorrectedData{AccountNumber: "5421", TransactionCode: 27}
require.Equal(t, "5421 27", WriteCorrectionData("C06", data))
data = &CorrectedData{RoutingNumber: "987654320", AccountNumber: "5421", TransactionCode: 32}
require.Equal(t, "9876543205421 32", WriteCorrectionData("C07", data))
data = &CorrectedData{Identification: "FooBar"}
require.Equal(t, "FooBar ", WriteCorrectionData("C09", data))
}
func TestIATCorrectedData(t *testing.T) {
t.Run("C01", func(t *testing.T) {
addenda98 := NewAddenda98()
line := "798C01123456780000001 34567891227-13569 345678910963842"
addenda98.Parse(line)
require.Equal(t, "98", addenda98.TypeCode)
require.Equal(t, "C01", addenda98.ChangeCode)
require.Equal(t, "123456780000001", addenda98.OriginalTrace)
require.Equal(t, "34567891", addenda98.OriginalDFI)
correctedData := "227-13569"
require.Equal(t, correctedData, addenda98.CorrectedData)
require.Equal(t, "345678910963842", addenda98.TraceNumber)
//
expected := correctedData + strings.Repeat(" ", 29-len(correctedData))
require.Equal(t, expected, addenda98.CorrectedDataField())
require.Equal(t, line, addenda98.String())
//
expected = correctedData + strings.Repeat(" ", 35-len(correctedData))
require.Equal(t, expected, addenda98.IATCorrectedDataField())
require.Equal(t, line, addenda98.String())
})
t.Run("C07", func(t *testing.T) {
addenda98 := NewAddenda98()
line := "798C07123456780000001 34567891227-13569 345678910963842"
addenda98.Parse(line)
data := &CorrectedData{RoutingNumber: "987654320", AccountNumber: "5421", TransactionCode: 32}
addenda98.CorrectedData = WriteCorrectionData("C07", data)
require.Equal(t, "9876543205421 32", addenda98.CorrectedDataField())
require.Equal(t, "9876543205421 32 ", addenda98.IATCorrectedDataField())
parsed := addenda98.ParseCorrectedData()
require.NotNil(t, parsed)
require.Equal(t, *data, *parsed)
// Now with 32 at the end
line = "798C07123456780000001 345678919876543205421 32 345678910963842"
addenda98.Parse(line)
require.Equal(t, "9876543205421 32 ", addenda98.IATCorrectedDataField())
parsed = addenda98.ParseCorrectedData()
require.NotNil(t, parsed)
require.Equal(t, *data, *parsed)
})
}