forked from moov-io/ach
-
Notifications
You must be signed in to change notification settings - Fork 1
/
addenda99_test.go
393 lines (345 loc) · 10.5 KB
/
addenda99_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
// 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 (
"path/filepath"
"strings"
"testing"
"github.com/moov-io/base"
)
func mockAddenda99() *Addenda99 {
addenda99 := NewAddenda99()
addenda99.ReturnCode = "R07"
addenda99.OriginalTrace = "99912340000015"
addenda99.AddendaInformation = "Authorization Revoked"
addenda99.OriginalDFI = "9101298"
return addenda99
}
func testAddenda99Parse(t testing.TB) {
addenda99 := NewAddenda99()
line := "799R07099912340000015 09101298Authorization revoked 091012980000066"
addenda99.Parse(line)
// walk the Addenda99 struct
if addenda99.recordType != "7" {
t.Errorf("expected %v got %v", "7", addenda99.recordType)
}
if addenda99.TypeCode != "99" {
t.Errorf("expected %v got %v", "99", addenda99.TypeCode)
}
if addenda99.ReturnCode != "R07" {
t.Errorf("expected %v got %v", "R07", addenda99.ReturnCode)
}
if addenda99.OriginalTrace != "099912340000015" {
t.Errorf("expected: %v got: %v", "099912340000015", addenda99.OriginalTrace)
}
if addenda99.DateOfDeathField() == "" {
t.Errorf("got: %v", addenda99.DateOfDeath)
}
if addenda99.OriginalDFI != "09101298" {
t.Errorf("expected: %s got: %s", "09101298", addenda99.OriginalDFI)
}
if addenda99.AddendaInformation != "Authorization revoked" {
t.Errorf("expected: %v got: %v", "Authorization revoked", addenda99.AddendaInformation)
}
if addenda99.TraceNumber != "091012980000066" {
t.Errorf("expected: %v got: %v", "091012980000066", addenda99.TraceNumber)
}
if code := addenda99.ReturnCodeField(); code != nil {
if code.Code != "R07" || code.Reason != "Authorization Revoked by Customer" {
t.Errorf("code.Code=%q code.Reason=%q", code.Code, code.Reason)
}
} else {
t.Errorf("got nil ReturnCode")
}
}
func TestAddenda99__LookupReturnCode(t *testing.T) {
if code := LookupReturnCode(""); code != nil {
t.Error("expected nil ReturnCode")
}
if code := LookupReturnCode("R02"); code == nil {
t.Error("expected ReturnCode")
} else {
if code.Code != "R02" {
t.Errorf("code.Code=%s", code.Code)
}
if code.Reason != "Account Closed" {
t.Errorf("code.Reason=%s", code.Reason)
}
}
if code := LookupReturnCode("R99"); code != nil {
t.Errorf("expected nil: %#v", code)
}
}
func TestAddenda99Parse(t *testing.T) {
testAddenda99Parse(t)
}
func BenchmarkAddenda99Parse(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testAddenda99Parse(b)
}
}
func testAddenda99String(t testing.TB) {
addenda99 := NewAddenda99()
line := "799R07099912340000015 09101298Authorization revoked 091012980000066"
addenda99.Parse(line)
if addenda99.String() != line {
t.Errorf("\n expected: %v\n got : %v", line, addenda99.String())
}
}
func TestAddenda99String(t *testing.T) {
testAddenda99String(t)
}
func BenchmarkAddenda99String(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testAddenda99String(b)
}
}
// This is not an exported function but utilized for validation
func testAddenda99MakeReturnCodeDict(t testing.TB) {
codes := makeReturnCodeDict()
// check if known code is present
_, prs := codes["R01"]
if !prs {
t.Error("Return Code R01 was not found in the ReturnCodeDict")
}
// check if invalid code is present
_, prs = codes["ABC"]
if prs {
t.Error("Valid return for an invalid return code key")
}
}
func TestAddenda99MakeReturnCodeDict(t *testing.T) {
testAddenda99MakeReturnCodeDict(t)
}
func BenchmarkAddenda99MakeReturnCodeDict(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testAddenda99MakeReturnCodeDict(b)
}
}
func testAddenda99ValidateTrue(t testing.TB) {
addenda99 := mockAddenda99()
addenda99.ReturnCode = "R13"
err := addenda99.Validate()
// no error expected
if !base.Match(err, nil) {
t.Errorf("%T: %s", err, err)
}
}
func TestAddenda99ValidateTrue(t *testing.T) {
testAddenda99ValidateTrue(t)
}
func BenchmarkAddenda99ValidateTrue(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testAddenda99ValidateTrue(b)
}
}
func testAddenda99ValidateReturnCodeFalse(t testing.TB) {
addenda99 := mockAddenda99()
addenda99.ReturnCode = ""
err := addenda99.Validate()
if !base.Match(err, ErrAddenda99ReturnCode) {
t.Errorf("%T: %s", err, err)
}
}
func TestAddenda99ValidateReturnCodeFalse(t *testing.T) {
testAddenda99ValidateReturnCodeFalse(t)
}
func BenchmarkAddenda99ValidateReturnCodeFalse(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testAddenda99ValidateReturnCodeFalse(b)
}
}
func testAddenda99OriginalTraceField(t testing.TB) {
addenda99 := mockAddenda99()
addenda99.OriginalTrace = "12345"
if addenda99.OriginalTraceField() != "000000000012345" {
t.Errorf("expected %v received %v", "000000000012345", addenda99.OriginalTraceField())
}
}
func TestAddenda99OriginalTraceField(t *testing.T) {
testAddenda99OriginalTraceField(t)
}
func BenchmarkAddenda99OriginalTraceField(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testAddenda99OriginalTraceField(b)
}
}
func testAddenda99DateOfDeathField(t testing.TB) {
addenda99 := mockAddenda99()
// Check for all zeros
if addenda99.DateOfDeathField() != " " {
t.Errorf("expected %v received %v", " ", addenda99.DateOfDeathField())
}
// Year: 1978 Month: October Day: 23
addenda99.DateOfDeath = "781023"
if addenda99.DateOfDeathField() != "781023" {
t.Errorf("expected %v received %v", "781023", addenda99.DateOfDeathField())
}
}
func TestAddenda99DateOfDeathField(t *testing.T) {
testAddenda99DateOfDeathField(t)
}
func BenchmarkAddenda99DateOfDeathField(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testAddenda99DateOfDeathField(b)
}
}
func testAddenda99OriginalDFIField(t testing.TB) {
addenda99 := mockAddenda99()
exp := "09101298"
if addenda99.OriginalDFIField() != exp {
t.Errorf("expected %v received %v", exp, addenda99.OriginalDFIField())
}
}
func TestAddenda99OriginalDFIField(t *testing.T) {
testAddenda99OriginalDFIField(t)
}
func BenchmarkAddenda99OriginalDFIField(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testAddenda99OriginalDFIField(b)
}
}
func testAddenda99AddendaInformationField(t testing.TB) {
addenda99 := mockAddenda99()
exp := "Authorization Revoked "
if addenda99.AddendaInformationField() != exp {
t.Errorf("expected %v received %v", exp, addenda99.AddendaInformationField())
}
}
func TestAddenda99AddendaInformationField(t *testing.T) {
testAddenda99AddendaInformationField(t)
}
func BenchmarkAddenda99AddendaInformationField(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testAddenda99AddendaInformationField(b)
}
}
func testAddenda99TraceNumberField(t testing.TB) {
addenda99 := mockAddenda99()
addenda99.TraceNumber = "91012980000066"
exp := "091012980000066"
if addenda99.TraceNumberField() != exp {
t.Errorf("expected %v received %v", exp, addenda99.TraceNumberField())
}
}
func TestAddenda99TraceNumberField(t *testing.T) {
testAddenda99TraceNumberField(t)
}
func BenchmarkAddenda99TraceNumberField(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testAddenda99TraceNumberField(b)
}
}
func testAddenda99ValidRecordType(t testing.TB) {
addenda99 := mockAddenda99()
addenda99.recordType = "63"
err := addenda99.Validate()
if !base.Match(err, NewErrRecordType(7)) {
t.Errorf("%T: %s", err, err)
}
}
func TestAddenda99ValidRecordType(t *testing.T) {
testAddenda99ValidRecordType(t)
}
func BenchmarkAddenda99ValidRecordType(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testAddenda99ValidRecordType(b)
}
}
// testAddenda99TypeCode99 TypeCode is 99
func testAddenda99TypeCode99(t testing.TB) {
addenda99 := mockAddenda99()
addenda99.TypeCode = "05"
err := addenda99.Validate()
if !base.Match(err, ErrAddendaTypeCode) {
t.Errorf("%T: %s", err, err)
}
}
// TestAddenda99TypeCode99 tests TypeCode is 99
func TestAddenda99TypeCode99(t *testing.T) {
testAddenda99TypeCode99(t)
}
// BenchmarkAddenda99TypeCode99 benchmarks TypeCode is 99
func BenchmarkAddenda99TypeCode99(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testAddenda99TypeCode99(b)
}
}
// testAddenda99TypeCodeNil validates TypeCode is ""
func testAddenda99TypeCodeNil(t testing.TB) {
addenda99 := mockAddenda99()
addenda99.TypeCode = ""
err := addenda99.Validate()
if !base.Match(err, ErrConstructor) {
t.Errorf("%T: %s", err, err)
}
}
// TestAddenda99TypeCodeES tests TypeCode is ""
func TestAddenda99TypeCodeNil(t *testing.T) {
testAddenda99TypeCodeNil(t)
}
// BenchmarkAddenda99TypeCodeNil benchmarks TypeCode is ""
func BenchmarkAddenda99TypeCodeNil(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testAddenda99TypeCodeNil(b)
}
}
// TestAddenda99RuneCountInString validates RuneCountInString
func TestAddenda99RuneCountInString(t *testing.T) {
addenda99 := NewAddenda99()
var line = "799"
addenda99.Parse(line)
if addenda99.DateOfDeath != "" {
t.Error("Parsed with an invalid RuneCountInString not equal to 94")
}
}
func TestAddenda99__MissingFileHeaderControl(t *testing.T) {
// We have a usecase where returned files don't contain the FileHeader
// or FileControl records. Our parser can handle this, but returns
// errors are they're expected per the NACHA spec.
//
// This test just checks we can parse the file and get the expected errors.
file, err := ReadFile(filepath.Join("test", "testdata", "return-no-file-header-control.ach"))
if err == nil {
t.Error("expected an error")
}
if !strings.Contains(err.Error(), ErrFileHeader.Error()) {
t.Errorf("unexpected error: %v", err)
}
if !strings.Contains(err.Error(), ErrFileControl.Error()) {
t.Errorf("unexpected error: %v", err)
}
if len(file.Batches) != 1 {
t.Errorf("got %d batches", len(file.Batches))
}
if entries := file.Batches[0].GetEntries(); len(entries) != 1 {
t.Errorf("got %d entries", len(entries))
}
}