-
Notifications
You must be signed in to change notification settings - Fork 23
/
ACHDictionary_test.go
531 lines (465 loc) · 16.2 KB
/
ACHDictionary_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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
// Copyright 2020 The Moov Authors
// Use of this source code is governed by an Apache License
// license that can be found in the LICENSE file.
package fed
import (
"fmt"
"os"
"path/filepath"
"strings"
"testing"
"github.com/moov-io/base"
"github.com/stretchr/testify/require"
)
func loadTestACHFiles(t *testing.T) (*ACHDictionary, *ACHDictionary) {
t.Helper()
open := func(path string) *ACHDictionary {
f, err := os.Open(path)
if err != nil {
t.Fatalf("%T: %s", err, err)
}
t.Cleanup(func() { f.Close() })
dict := NewACHDictionary()
if err := dict.Read(f); err != nil {
t.Fatalf("%T: %s", err, err)
}
return dict
}
jsonDict := open(filepath.Join("data", "fedachdir.json"))
plainDict := open(filepath.Join("data", "FedACHdir.txt"))
return jsonDict, plainDict
}
// Values within the tests can change if the FED ACH participants change (e.g. Number of participants, etc.)
func TestACHParseParticipant(t *testing.T) {
var line = "073905527O0710003011012908000000000LINCOLN SAVINGS BANK P O BOX E REINBECK IA506690159319788644111 "
f := NewACHDictionary()
f.Read(strings.NewReader(line))
if fi, ok := f.IndexACHRoutingNumber["073905527"]; ok {
if fi.RoutingNumber != "073905527" {
t.Errorf("CustomerName Expected '073905527' got: %v", fi.RoutingNumber)
}
if fi.OfficeCode != "O" {
t.Errorf("OfficeCode Expected 'O' got: %v", fi.OfficeCode)
}
if fi.ServicingFRBNumber != "071000301" {
t.Errorf("ServicingFrbNumber Expected '071000301' got: %v", fi.ServicingFRBNumber)
}
if fi.RecordTypeCode != "1" {
t.Errorf("RecordTypeCode Expected '1' got: %v", fi.RecordTypeCode)
}
if fi.Revised != "012908" {
t.Errorf("Revised Expected '012908' got: %v", fi.Revised)
}
if fi.NewRoutingNumber != "000000000" {
t.Errorf("NewRoutingNumber Expected '000000000' got: %v", fi.NewRoutingNumber)
}
if fi.CustomerName != "LINCOLN SAVINGS BANK" {
t.Errorf("CustomerName Expected 'LINCOLN SAVINGS BANK' got: %v", fi.CustomerName)
}
if fi.Address != "P O BOX E" {
t.Errorf("Address Expected 'P O BOX E' got: %v", fi.Address)
}
if fi.City != "REINBECK" {
t.Errorf("City Expected 'REINBECK' got: %v", fi.City)
}
if fi.State != "IA" {
t.Errorf("State Expected 'REINBECK' got: %v", fi.State)
}
if fi.PostalCode != "50669" {
t.Errorf("PostalCode Expected '50669' got: %v", fi.PostalCode)
}
if fi.PostalCodeExtension != "0159" {
t.Errorf("PostalCodeExtension Expected '0159' got: %v", fi.PostalCodeExtension)
}
if fi.PhoneNumber != "3197886441" {
t.Errorf("PhoneNumber Expected '3197886441' got: %v", fi.PhoneNumber)
}
if fi.StatusCode != "1" {
t.Errorf("StatusCode Expected '1' got: %v", fi.StatusCode)
}
if fi.ViewCode != "1" {
t.Errorf("ViewCode Expected '1' got: %v", fi.ViewCode)
}
} else {
t.Errorf("routing number `073905527` not found")
}
}
func TestACHDirectoryRead(t *testing.T) {
check := func(t *testing.T, kind string, dict *ACHDictionary) {
if fi, ok := dict.IndexACHRoutingNumber["073905527"]; ok {
if fi.CustomerName != "LINCOLN SAVINGS BANK" {
t.Errorf("%s: Expected `LINCOLN SAVINGS BANK` got : %v", kind, fi.CustomerName)
}
} else {
t.Errorf("%s: ach routing number `073905527` not found", kind)
}
}
jsonDict, plainDict := loadTestACHFiles(t)
if len(jsonDict.ACHParticipants) != 6 {
t.Errorf("got %d participants", len(jsonDict.ACHParticipants))
}
check(t, "json", jsonDict)
if len(plainDict.ACHParticipants) != 18198 {
t.Errorf("got %d participants", len(plainDict.ACHParticipants))
}
check(t, "plain", plainDict)
}
func TestACHInvalidRecordLength(t *testing.T) {
var line = "073905527O0710003011012908000000000LINCOLN SAVINGS BANK P O BOX E"
f := NewACHDictionary()
if err := f.Read(strings.NewReader(line)); err != nil {
if !base.Has(err, NewRecordWrongLengthErr(155, 80)) {
t.Errorf("%T: %s", err, err)
}
}
}
func TestACHParticipantLabel(t *testing.T) {
var line = "073905527O0710003011012908000000000LINCOLN SAVINGS BANK P O BOX E REINBECK IA506690159319788644111 "
f := NewACHDictionary()
f.Read(strings.NewReader(line))
if fi, ok := f.IndexACHRoutingNumber["073905527"]; ok {
if fi.CustomerNameLabel() != "Lincoln Savings Bank" {
t.Errorf("CustomerNameLabel Expected 'Lincoln Savings Bank' got: %v", fi.CustomerNameLabel())
}
} else {
t.Errorf("routing number `073905527` not found")
}
}
// TestACHRoutingNumberSearchSingle tests that a valid routing number defined in FedACHDir returns participant data
func TestACHRoutingNumberSearchSingle(t *testing.T) {
check := func(t *testing.T, kind string, dir *ACHDictionary) {
fi := dir.RoutingNumberSearchSingle("325183657")
if fi == nil {
t.Fatalf("%s: ach routing number `325183657` not found", kind)
}
if fi.CustomerName != "LOWER VALLEY CU" {
t.Errorf("%s: Expected `LOWER VALLEY CU` got : %v", kind, fi.CustomerName)
}
}
jsonDict, plainDict := loadTestACHFiles(t)
check(t, "json", jsonDict)
check(t, "plain", plainDict)
}
// TestInvalidRoutingNumberSearchSingle tests that an invalid routing number returns nil
func TestInvalidACHRoutingNumberSearchSingle(t *testing.T) {
jsonDict, plainDict := loadTestACHFiles(t)
fi := jsonDict.RoutingNumberSearchSingle("433")
if fi != nil {
t.Error("json: 433 should have returned nil")
}
fi = plainDict.RoutingNumberSearchSingle("433")
if fi != nil {
t.Error("plain: 433 should have returned nil")
}
}
// TestACHFinancialInstitutionSearchSingle tests that a Financial Institution defined in FedACHDir returns
// participant data
func TestACHFinancialInstitutionSearchSingle(t *testing.T) {
check := func(t *testing.T, kind string, dict *ACHDictionary) {
fi := dict.FinancialInstitutionSearchSingle("BANK OF AMERICA N.A")
if len(fi) == 0 {
t.Fatalf("%s: ach financial institution `BANK OF AMERICA N.A` not found", kind)
}
for _, f := range fi {
if f.CustomerName != "BANK OF AMERICA N.A" {
t.Errorf("%s: Expected `BANK OF AMERICA, N.A` got : %s", kind, f.CustomerName)
}
}
}
jsonDict, plainDict := loadTestACHFiles(t)
check(t, "json", jsonDict)
check(t, "plain", plainDict)
}
// TestInvalidACHFinancialInstitutionSearchSingle tests that a Financial Institution is not defined in FedACHDir
// returns nil
func TestInvalidACHFinancialInstitutionSearchSingle(t *testing.T) {
jsonDict, plainDict := loadTestACHFiles(t)
fi := jsonDict.FinancialInstitutionSearchSingle("XYZ")
if len(fi) != 0 {
t.Errorf("%s", "XYZ should have returned nil")
}
fi = plainDict.FinancialInstitutionSearchSingle("XYZ")
if len(fi) != 0 {
t.Errorf("%s", "XYZ should have returned nil")
}
}
// TestACHRoutingNumberSearch tests that routing number search returns nil or FEDACH participant data
func TestACHRoutingNumberSearch(t *testing.T) {
jsonDict, plainDict := loadTestACHFiles(t)
fi, err := jsonDict.RoutingNumberSearch("325", 10)
if err != nil {
t.Fatalf("%T: %s", err, err)
}
if len(fi) == 0 {
t.Errorf("%s", "325 should have returned values")
}
fi, err = plainDict.RoutingNumberSearch("325", 10)
if err != nil {
t.Fatalf("%T: %s", err, err)
}
if len(fi) == 0 {
t.Errorf("%s", "325 should have returned values")
}
}
// TestACHRoutingNumberSearch02 tests string `02` returns results
func TestACHRoutingNumberSearch02(t *testing.T) {
jsonDict, plainDict := loadTestACHFiles(t)
fi, err := jsonDict.RoutingNumberSearch("03", 10)
if err != nil {
t.Fatalf("%T: %s", err, err)
}
if len(fi) == 0 {
t.Fatalf("02 should have returned values")
}
fi, err = plainDict.RoutingNumberSearch("02", 10)
if err != nil {
t.Fatalf("%T: %s", err, err)
}
if len(fi) == 0 {
t.Fatalf("02 should have returned values")
}
}
// TestACHRoutingNumberSearchMinimumLength tests that routing number search returns a RecordWrongLengthErr if the
// length of the string passed in is less than 2.
func TestACHRoutingNumberSearchMinimumLength(t *testing.T) {
jsonDict, plainDict := loadTestACHFiles(t)
if _, err := jsonDict.RoutingNumberSearch("0", 10); err != nil {
if !base.Has(err, NewRecordWrongLengthErr(2, 1)) {
t.Errorf("%T: %s", err, err)
}
}
if _, err := plainDict.RoutingNumberSearch("0", 10); err != nil {
if !base.Has(err, NewRecordWrongLengthErr(2, 1)) {
t.Errorf("%T: %s", err, err)
}
}
}
// TestInvalidACHRoutingNumberSearch tests that routing number returns nil for an invalid RoutingNumber.
func TestInvalidACHRoutingNumberSearch(t *testing.T) {
check := func(t *testing.T, kind string, dict *ACHDictionary) {
fi, err := dict.RoutingNumberSearch("777777777", 10)
if err != nil {
t.Fatalf("%s: %T: %s", kind, err, err)
}
for i := range fi {
fmt.Printf("fi[%d]=%#v\n", i, fi[i])
}
if len(fi) != 0 {
t.Fatalf("%s: ach routing number search should have returned nil", kind)
}
}
jsonDict, plainDict := loadTestACHFiles(t)
check(t, "json", jsonDict)
check(t, "plain", plainDict)
}
// TestACHRoutingNumberMaximumLength tests that routing number search returns a RecordWrongLengthErr if the
// length of the string passed in is greater than 9.
func TestACHRoutingNumberSearchMaximumLength(t *testing.T) {
jsonDict, plainDict := loadTestACHFiles(t)
if _, err := jsonDict.RoutingNumberSearch("1234567890", 10); err != nil {
if !base.Has(err, NewRecordWrongLengthErr(9, 10)) {
t.Errorf("%T: %s", err, err)
}
}
if _, err := plainDict.RoutingNumberSearch("1234567890", 10); err != nil {
if !base.Has(err, NewRecordWrongLengthErr(9, 10)) {
t.Errorf("%T: %s", err, err)
}
}
}
// TestACHRoutingNumberNumeric tests that routing number search returns an ErrRoutingNumberNumeric if the
// string passed in is not numeric.
func TestACHRoutingNumberNumeric(t *testing.T) {
jsonDict, plainDict := loadTestACHFiles(t)
if _, err := jsonDict.RoutingNumberSearch("1 S5", 10); err != nil {
if !base.Has(err, ErrRoutingNumberNumeric) {
t.Errorf("%T: %s", err, err)
}
}
if _, err := plainDict.RoutingNumberSearch("1 S5", 10); err != nil {
if !base.Has(err, ErrRoutingNumberNumeric) {
t.Errorf("%T: %s", err, err)
}
}
}
func TestACHFinancialInstitutionSearch__Examples(t *testing.T) {
_, plainDict := loadTestACHFiles(t)
cases := []struct {
input string
expected *ACHParticipant
}{
{
input: "Chase",
expected: &ACHParticipant{
RoutingNumber: "021000021",
CustomerName: "JPMORGAN CHASE",
},
},
{
input: "Wells",
expected: &ACHParticipant{
RoutingNumber: "101205940",
CustomerName: "WELLS BANK",
},
},
{
input: "Fargo",
expected: &ACHParticipant{
RoutingNumber: "291378392",
CustomerName: "FARGO VA FEDERAL CU",
},
},
{
input: "Wells Fargo",
expected: &ACHParticipant{
RoutingNumber: "011100106",
CustomerName: "WELLS FARGO BANK",
},
},
}
for i := range cases {
// The plain dictionary has 18k records, so search is more realistic
results := plainDict.FinancialInstitutionSearch(cases[i].input, 1)
require.Len(t, results, 1)
require.Equal(t, cases[i].expected.RoutingNumber, results[0].RoutingNumber)
require.Equal(t, cases[i].expected.CustomerName, results[0].CustomerName)
}
}
// TestACHFinancialInstitutionSearch tests search string `First Bank`
func TestACHFinancialInstitutionSearch(t *testing.T) {
jsonDict, plainDict := loadTestACHFiles(t)
fi := jsonDict.FinancialInstitutionSearch("First Bank", 10)
if len(fi) == 0 {
t.Fatalf("json: No Financial Institutions matched your search query")
}
fi = plainDict.FinancialInstitutionSearch("First Bank", 10)
if len(fi) == 0 {
t.Fatalf("plain: No Financial Institutions matched your search query")
}
}
// TestACHFinancialInstitutionFarmers tests search string `FaRmerS`
func TestACHFinancialInstitutionFarmers(t *testing.T) {
jsonDict, plainDict := loadTestACHFiles(t)
fi := jsonDict.FinancialInstitutionSearch("FaRmerS", 10)
if len(fi) == 0 {
t.Fatalf("json: No Financial Institutions matched your search query")
}
fi = plainDict.FinancialInstitutionSearch("FaRmerS", 10)
if len(fi) == 0 {
t.Fatalf("plain: No Financial Institutions matched your search query")
}
}
// TestACHSearchStateFilter tests search string `Farmers State Bank` and filters by the state of Ohio, `OH`
func TestACHSearchStateFilter(t *testing.T) {
check := func(t *testing.T, kind string, dict *ACHDictionary) {
fi := dict.FinancialInstitutionSearch("Farmers State Bank", 100)
if len(fi) == 0 {
t.Fatalf("%s: No Financial Institutions matched your search query", kind)
}
filter := dict.ACHParticipantStateFilter(fi, "MO")
if len(filter) == 0 {
t.Fatalf("%s: No Financial Institutions matched your search query", kind)
}
for _, loc := range filter {
if loc.ACHLocation.State != "MO" {
t.Errorf("%s: Expected `MO` got : %s", kind, loc.ACHLocation.State)
}
}
}
jsonDict, plainDict := loadTestACHFiles(t)
check(t, "json", jsonDict)
check(t, "plain", plainDict)
}
// TestACHSearchCityFilter tests search string `Farmers State Bank` and filters by the city of `ARCHBOLD`
func TestACHSearchCityFilter(t *testing.T) {
check := func(t *testing.T, kind string, dict *ACHDictionary) {
fi := dict.FinancialInstitutionSearch("Farmers State Bank", 100)
if len(fi) == 0 {
t.Fatalf("%s: No Financial Institutions matched your search query", kind)
}
filter := dict.ACHParticipantCityFilter(fi, "CAMERON")
if len(filter) == 0 {
t.Fatalf("%s: No Financial Institutions matched your search query", kind)
}
for _, loc := range filter {
if loc.ACHLocation.City != "CAMERON" {
t.Errorf("%s: Expected `CAMERON` got : %s", kind, loc.ACHLocation.City)
}
}
}
jsonDict, plainDict := loadTestACHFiles(t)
check(t, "json", jsonDict)
check(t, "plain", plainDict)
}
// TestACHSearchPostalCodeFilter tests search string `Farmers State Bank` and filters by the postal code of
func TestACHSearchPostalCodeFilter(t *testing.T) {
check := func(t *testing.T, kind string, dict *ACHDictionary) {
fi := dict.FinancialInstitutionSearch("Farmers State Bank", 100)
if len(fi) == 0 {
t.Fatalf("%s: No Financial Institutions matched your search query", kind)
}
filter := dict.ACHParticipantPostalCodeFilter(fi, "64429")
if len(filter) == 0 {
t.Fatalf("%s: No Financial Institutions matched your search query", kind)
}
for _, loc := range filter {
if loc.ACHLocation.PostalCode != "64429" {
t.Errorf("%s: Expected `64429` got : %s", kind, loc.ACHLocation.PostalCode)
}
}
}
jsonDict, plainDict := loadTestACHFiles(t)
check(t, "json", jsonDict)
check(t, "plain", plainDict)
}
// TestACHDictionaryStateFilter tests filtering ACHDictionary.ACHParticipants by the state of `PA`
func TestACHDictionaryStateFilter(t *testing.T) {
check := func(t *testing.T, kind string, dict *ACHDictionary) {
filter := dict.StateFilter("nj")
if len(filter) == 0 {
t.Fatalf("%s: No Financial Institutions matched your search query", kind)
}
for _, loc := range filter {
if loc.ACHLocation.State != "NJ" {
t.Errorf("%s: Expected `NJ` got : %s", kind, loc.ACHLocation.State)
}
}
}
jsonDict, plainDict := loadTestACHFiles(t)
check(t, "json", jsonDict)
check(t, "plain", plainDict)
}
// TestACHDictionaryCityFilter tests filtering ACHDictionary.ACHParticipants by the city of `Reading`
func TestACHDictionaryCityFilter(t *testing.T) {
check := func(t *testing.T, kind string, dict *ACHDictionary) {
filter := dict.CityFilter("Hamilton")
if len(filter) == 0 {
t.Fatalf("%s: No Financial Institutions matched your search query", kind)
}
for _, loc := range filter {
if loc.ACHLocation.City != "HAMILTON" {
t.Errorf("%s: Expected `HAMILTON` got : %s", kind, loc.ACHLocation.City)
}
}
}
jsonDict, plainDict := loadTestACHFiles(t)
check(t, "json", jsonDict)
check(t, "plain", plainDict)
}
// TestACHDictionaryPostalCodeFilter tests filtering ACHDictionary.ACHParticipants by the postal code of `19468`
func TestACHDictionaryPostalCodeFilter(t *testing.T) {
check := func(t *testing.T, kind string, dict *ACHDictionary) {
filter := dict.PostalCodeFilter("08690")
if len(filter) == 0 {
t.Fatalf("%s: No Financial Institutions matched your search query", kind)
}
for _, loc := range filter {
if loc.ACHLocation.PostalCode != "08690" {
t.Errorf("%s: Expected `08690` got : %s", kind, loc.ACHLocation.PostalCode)
}
}
}
jsonDict, plainDict := loadTestACHFiles(t)
check(t, "json", jsonDict)
check(t, "plain", plainDict)
}