forked from moov-io/fed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWIREDictionary_test.go
462 lines (389 loc) · 13.5 KB
/
WIREDictionary_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
// 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"
)
// loadTestWireFiles returns two WIREDictionary, one from the JSON source file
// and other from the plaintext source file.
func loadTestWireFiles(t *testing.T) (*WIREDictionary, *WIREDictionary) {
t.Helper()
open := func(path string) *WIREDictionary {
f, err := os.Open(path)
if err != nil {
t.Fatalf("%T: %s", err, err)
}
t.Cleanup(func() { f.Close() })
dict := NewWIREDictionary()
if err := dict.Read(f); err != nil {
t.Fatalf("%T: %s", err, err)
}
return dict
}
jsonDict := open(filepath.Join("data", "fpddir.json"))
plainDict := open(filepath.Join("data", "fpddir.txt"))
return jsonDict, plainDict
}
func TestWIREParseParticipant(t *testing.T) {
var line = "325280039MAC FCU MAC FEDERAL CREDIT UNION AKFAIRBANKS Y Y20180629"
f := NewWIREDictionary()
f.Read(strings.NewReader(line))
if fi, ok := f.IndexWIRERoutingNumber["325280039"]; ok {
if fi.RoutingNumber != "325280039" {
t.Errorf("Expected `325280039` got : %s", fi.RoutingNumber)
}
if fi.TelegraphicName != "MAC FCU" {
t.Errorf("Expected `MAC FCU` got : %s", fi.TelegraphicName)
}
if fi.CustomerName != "MAC FEDERAL CREDIT UNION" {
t.Errorf("Expected `MAC FEDERAL CREDIT UNION` got : %s", fi.CustomerName)
}
if fi.WIRELocation.State != "AK" {
t.Errorf("Expected `AK` got ; %s", fi.State)
}
if fi.WIRELocation.City != "FAIRBANKS" {
t.Errorf("Expected `FAIRBANKS` got : %s", fi.City)
}
if fi.FundsTransferStatus != "Y" {
t.Errorf("Expected `Y` got : %s", fi.FundsTransferStatus)
}
if fi.FundsSettlementOnlyStatus != " " {
t.Errorf("Expected ` ` got : %s", fi.FundsSettlementOnlyStatus)
}
if fi.BookEntrySecuritiesTransferStatus != "Y" {
t.Errorf("Expected `Y` got : %s", fi.BookEntrySecuritiesTransferStatus)
}
if fi.Date != "20180629" {
t.Errorf("Expected `20180629` got : %s", fi.Date)
}
} else {
t.Errorf("routing number `325280039` not found")
}
}
func TestWIREDirectoryRead(t *testing.T) {
jsonDict, plainDict := loadTestWireFiles(t)
check := func(t *testing.T, kind string, dict *WIREDictionary) {
if fi, ok := dict.IndexWIRERoutingNumber["325280039"]; ok {
if fi.TelegraphicName != "MAC FCU" {
t.Errorf("Expected `MAC FCU` got : %s", fi.TelegraphicName)
}
} else {
t.Errorf("routing number `325280039` not found")
}
}
if len(jsonDict.WIREParticipants) != 10 {
t.Errorf("Expected '7693' got: %v", len(jsonDict.WIREParticipants))
}
check(t, "json", jsonDict)
if len(plainDict.WIREParticipants) != 7693 {
t.Errorf("Expected '7693' got: %v", len(plainDict.WIREParticipants))
}
check(t, "plain", plainDict)
}
func TestWIREInvalidRecordLength(t *testing.T) {
var line = "325280039MAC FCU MAC FEDERAL CREDIT UNION"
f := NewWIREDictionary()
if err := f.Read(strings.NewReader(line)); err != nil {
if !base.Has(err, NewRecordWrongLengthErr(101, 51)) {
t.Errorf("%T: %s", err, err)
}
}
}
// TestWIRERoutingNumberSearch tests that a valid routing number defined in FedWIREDir returns the participant data
func TestWIRERoutingNumberSearchSingle(t *testing.T) {
jsonDict, plainDict := loadTestWireFiles(t)
check := func(t *testing.T, kind string, dict *WIREDictionary) {
fi := dict.RoutingNumberSearchSingle("324172465")
if fi == nil {
t.Fatalf("wire routing number `324172465` not found")
}
if fi.CustomerName != "TRUGROCER FEDERAL CREDIT UNION" {
t.Errorf("Expected `TRUGROCER FEDERAL CREDIT UNION` got : %s", fi.CustomerName)
}
}
check(t, "json", jsonDict)
check(t, "plain", plainDict)
}
// TestInvalidWIRERoutingNumberSearch tests that an invalid routing number returns nil
func TestInvalidWIRERoutingNumberSearchSingle(t *testing.T) {
jsonDict, plainDict := loadTestWireFiles(t)
check := func(t *testing.T, kind string, dict *WIREDictionary) {
fi := dict.RoutingNumberSearchSingle("325183657")
if fi != nil {
t.Errorf("%s", "325183657 should have returned nil")
}
}
check(t, "json", jsonDict)
check(t, "plain", plainDict)
}
// TestWIREFinancialInstitutionSearch tests that a Financial Institution defined in FedWIREDir returns the participant
// data
func TestWIREFinancialInstitutionSearchSingle(t *testing.T) {
jsonDict, plainDict := loadTestWireFiles(t)
check := func(t *testing.T, kind string, dict *WIREDictionary) {
fi := dict.FinancialInstitutionSearchSingle("TRUGROCER FEDERAL CREDIT UNION")
if fi == nil {
t.Fatalf("wire financial institution `TRUGROCER FEDERAL CREDIT UNION` not found")
}
for _, f := range fi {
if f.CustomerName != "TRUGROCER FEDERAL CREDIT UNION" {
t.Errorf("TRUGROCER FEDERAL CREDIT UNION` got : %v", f.CustomerName)
}
}
}
check(t, "json", jsonDict)
check(t, "plain", plainDict)
}
// TestInvalidWIREFinancialInstitutionSearchSingle tests that a Financial Institution defined in FedWIREDir returns
// the participant data
func TestInvalidWIREFinancialInstitutionSearchSingle(t *testing.T) {
jsonDict, plainDict := loadTestWireFiles(t)
check := func(t *testing.T, kind string, dict *WIREDictionary) {
fi := dict.FinancialInstitutionSearchSingle("XYZ")
if fi != nil {
t.Errorf("%s", "XYZ should have returned nil")
}
}
check(t, "json", jsonDict)
check(t, "plain", plainDict)
}
// TestWIRERoutingNumberSearch tests that routing number search returns nil or FEDWIRE participant data
func TestWIRERoutingNumberSearch(t *testing.T) {
jsonDict, plainDict := loadTestWireFiles(t)
check := func(t *testing.T, kind string, dict *WIREDictionary) {
fi, err := dict.RoutingNumberSearch("325", 1)
if err != nil {
t.Fatalf("%T: %s", err, err)
}
if len(fi) == 0 {
t.Errorf("%s", "325 should have returned values")
}
}
check(t, "json", jsonDict)
check(t, "plain", plainDict)
}
// TestWIRERoutingNumberSearch02 tests string `02` returns results
func TestWIRERoutingNumberSearch02(t *testing.T) {
jsonDict, plainDict := loadTestWireFiles(t)
check := func(t *testing.T, kind string, dict *WIREDictionary) {
fi, err := dict.RoutingNumberSearch("02", 1)
if err != nil {
t.Fatalf("%T: %s", err, err)
}
if len(fi) == 0 {
t.Fatalf("02 should have returned values")
}
}
_ = jsonDict
// check(t, "json", jsonDict)
check(t, "plain", plainDict)
}
// TestWIRERoutingNumberSearchMinimumLength tests that routing number search returns a RecordWrongLengthErr if the
// length of the string passed in is less than 2.
func TestWIRERoutingNumberSearchMinimumLength(t *testing.T) {
jsonDict, plainDict := loadTestWireFiles(t)
check := func(t *testing.T, kind string, dict *WIREDictionary) {
if _, err := dict.RoutingNumberSearch("0", 1); err != nil {
if !base.Has(err, NewRecordWrongLengthErr(2, 1)) {
t.Errorf("%T: %s", err, err)
}
}
}
check(t, "json", jsonDict)
check(t, "plain", plainDict)
}
// TestInvalidWIRERoutingNumberSearch tests that routing number returns nil for an invalid RoutingNumber.
func TestInvalidWIRERoutingNumberSearch(t *testing.T) {
jsonDict, plainDict := loadTestWireFiles(t)
check := func(t *testing.T, kind string, dict *WIREDictionary) {
fi, err := dict.RoutingNumberSearch("777777777", 1)
if err != nil {
t.Fatalf("%T: %s", err, err)
}
if len(fi) != 0 {
t.Fatal("wire routing number search should have returned nil")
}
}
check(t, "json", jsonDict)
check(t, "plain", plainDict)
}
// TestWIRERoutingNumberMaximumLength tests that routing number search returns a RecordWrongLengthErr if the
// length of the string passed in is greater than 9.
func TestWIRERoutingNumberSearchMaximumLength(t *testing.T) {
jsonDict, plainDict := loadTestWireFiles(t)
check := func(t *testing.T, kind string, dict *WIREDictionary) {
if _, err := dict.RoutingNumberSearch("1234567890", 1); err != nil {
if !base.Has(err, NewRecordWrongLengthErr(9, 10)) {
t.Errorf("%T: %s", err, err)
}
}
}
check(t, "json", jsonDict)
check(t, "plain", plainDict)
}
// TestWIRERoutingNumberNumeric tests that routing number search returns an ErrRoutingNumberNumeric if the
// string passed in is not numeric.
func TestWIRERoutingNumberNumeric(t *testing.T) {
jsonDict, plainDict := loadTestWireFiles(t)
check := func(t *testing.T, kind string, dict *WIREDictionary) {
if _, err := dict.RoutingNumberSearch("1 S5", 1); err != nil {
if !base.Has(err, ErrRoutingNumberNumeric) {
t.Errorf("%T: %s", err, err)
}
}
}
check(t, "json", jsonDict)
check(t, "plain", plainDict)
}
func TestWIREParsingError(t *testing.T) {
var line = "011000536FHLB BOSTON FEDERAL HOME LOAN BANK MABOSTON © Y20170818"
f := NewWIREDictionary()
if err := f.Read(strings.NewReader(line)); err != nil {
if !base.Has(err, NewRecordWrongLengthErr(101, 51)) {
t.Errorf("%T: %s", err, err)
}
}
}
func TestWIREFinancialInstitutionSearch__Examples(t *testing.T) {
_, plainDict := loadTestWireFiles(t)
cases := []struct {
input string
expected *ACHParticipant
}{
{
input: "Chase",
expected: &ACHParticipant{
RoutingNumber: "021000021",
CustomerName: "JPMORGAN CHASE BANK, NA",
},
},
{
input: "Wells",
expected: &ACHParticipant{
RoutingNumber: "101205940",
CustomerName: "WELLS BANK",
},
},
{
input: "Wells Fargo",
expected: &ACHParticipant{
RoutingNumber: "021052943",
CustomerName: "WELLS FARGO GNMA-P&I",
},
},
}
for i := range cases {
// The plain dictionary has 18k records, so search is more realistic
results := plainDict.FinancialInstitutionSearch(cases[i].input, 1)
require.Equal(t, fmt.Sprintf("#%d = 1", i), fmt.Sprintf("#%d = %d", i, len(results)))
require.Equal(t, cases[i].expected.RoutingNumber, results[0].RoutingNumber)
require.Equal(t, cases[i].expected.CustomerName, results[0].CustomerName)
}
}
// TestWIREFinancialInstitutionSearch tests search string `First Bank`
func TestWIREFinancialInstitutionSearch(t *testing.T) {
jsonDict, plainDict := loadTestWireFiles(t)
check := func(t *testing.T, kind string, dict *WIREDictionary) {
fi := dict.FinancialInstitutionSearch("First Bank", 1)
if len(fi) == 0 {
t.Fatalf("No Financial Institutions matched your search query")
}
}
check(t, "json", jsonDict)
check(t, "plain", plainDict)
}
// TestWIREFinancialInstitutionFarmers tests search string `FaRmerS`
func TestWIREFinancialInstitutionFarmers(t *testing.T) {
jsonDict, plainDict := loadTestWireFiles(t)
check := func(t *testing.T, kind string, dict *WIREDictionary) {
fi := dict.FinancialInstitutionSearch("FaRmerS", 1)
if len(fi) == 0 {
t.Fatalf("No Financial Institutions matched your search query")
}
}
check(t, "json", jsonDict)
check(t, "plain", plainDict)
}
// TestWIRESearchStateFilter tests search string `Farmers State Bank` and filters by the state of North Carolina, `NC`
func TestWIRESearchStateFilter(t *testing.T) {
jsonDict, plainDict := loadTestWireFiles(t)
check := func(t *testing.T, kind string, dict *WIREDictionary) {
fi := dict.FinancialInstitutionSearch("Farmers State Bank", 100)
if len(fi) == 0 {
t.Fatalf("No Financial Institutions matched your search query")
}
filter := dict.WIREParticipantStateFilter(fi, "NC")
if len(filter) == 0 {
t.Fatalf("No Financial Institutions matched your search query")
}
for _, loc := range filter {
if loc.WIRELocation.State != "NC" {
t.Errorf("Expected `NC` got : %s", loc.WIRELocation.State)
}
}
}
check(t, "json", jsonDict)
check(t, "plain", plainDict)
}
// TestWIRESearchCityFilter tests search string `Farmers State Bank` and filters by the city of `SALISBURY`
func TestWIRESearchCityFilter(t *testing.T) {
jsonDict, plainDict := loadTestWireFiles(t)
check := func(t *testing.T, kind string, dict *WIREDictionary) {
fi := dict.FinancialInstitutionSearch("Farmers State Bank", 100)
if len(fi) == 0 {
t.Fatalf("No Financial Institutions matched your search query")
}
filter := dict.WIREParticipantCityFilter(fi, "SALISBURY")
if len(filter) == 0 {
t.Fatalf("No Financial Institutions matched your search query")
}
for _, loc := range filter {
if loc.WIRELocation.City != "SALISBURY" {
t.Errorf("Expected `SALISBURY` got : %s", loc.WIRELocation.City)
}
}
}
check(t, "json", jsonDict)
check(t, "plain", plainDict)
}
// TestWIREDictionaryStateFilter tests filtering WIREDictionary.WIREParticipants by the state of `PA`
func TestWIREDictionaryStateFilter(t *testing.T) {
jsonDict, plainDict := loadTestWireFiles(t)
check := func(t *testing.T, kind string, dict *WIREDictionary) {
filter := dict.StateFilter("pa")
if len(filter) == 0 {
t.Fatalf("No Financial Institutions matched your search query")
}
for _, loc := range filter {
if loc.WIRELocation.State != "PA" {
t.Errorf("Expected `PA` got : %s", loc.WIRELocation.State)
}
}
}
check(t, "json", jsonDict)
check(t, "plain", plainDict)
}
// TestWIREDictionaryCityFilter tests filtering WIREDictionary.WIREParticipants by the city of `Reading`
func TestWIREDictionaryCityFilter(t *testing.T) {
jsonDict, plainDict := loadTestWireFiles(t)
check := func(t *testing.T, kind string, dict *WIREDictionary) {
filter := dict.CityFilter("Reading")
if len(filter) == 0 {
t.Fatalf("No Financial Institutions matched your search query")
}
for _, loc := range filter {
if loc.WIRELocation.City != "READING" {
t.Errorf("Expected `READING` got : %s", loc.WIRELocation.City)
}
}
}
check(t, "json", jsonDict)
check(t, "plain", plainDict)
}