-
Notifications
You must be signed in to change notification settings - Fork 155
/
batchControl_test.go
353 lines (308 loc) · 11.2 KB
/
batchControl_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
// 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 (
"math"
"strings"
"testing"
"github.com/moov-io/base"
"github.com/stretchr/testify/require"
)
func mockBatchControl() *BatchControl {
bc := NewBatchControl()
bc.ServiceClassCode = CreditsOnly
bc.CompanyIdentification = "121042882"
bc.ODFIIdentification = "12104288"
return bc
}
// testMockBatchControl tests mock batch control
func testMockBatchControl(t testing.TB) {
bc := mockBatchControl()
if err := bc.Validate(); err != nil {
t.Error("mockBatchControl does not validate and will break other tests")
}
if bc.ServiceClassCode != CreditsOnly {
t.Error("ServiceClassCode depedendent default value has changed")
}
if bc.CompanyIdentification != "121042882" {
t.Error("CompanyIdentification depedendent default value has changed")
}
if bc.ODFIIdentification != "12104288" {
t.Error("ODFIIdentification depedendent default value has changed")
}
}
// TestMockBatchControl test mock batch control
func TestMockBatchControl(t *testing.T) {
testMockBatchControl(t)
}
// BenchmarkMockBatchControl benchmarks mock batch control
func BenchmarkMockBatchControl(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testMockBatchControl(b)
}
}
// TestParseBatchControl parses a known Batch ControlRecord string.
func testParseBatchControl(t testing.TB) {
var line = "82250000010005320001000000010500000000000000origid 076401250000001"
r := NewReader(strings.NewReader(line))
r.line = line
bh := BatchHeader{BatchNumber: 1,
ServiceClassCode: DebitsOnly,
CompanyIdentification: "origid",
ODFIIdentification: "7640125"}
r.addCurrentBatch(NewBatchPPD(&bh))
r.currentBatch.AddEntry(&EntryDetail{TransactionCode: 27, Amount: 10500, RDFIIdentification: "5320001", TraceNumber: "76401255655291"})
if err := r.parseBatchControl(); err != nil {
t.Errorf("%T: %s", err, err)
}
record := r.currentBatch.GetControl()
if record.ServiceClassCode != DebitsOnly {
t.Errorf("ServiceClassCode Expected '225' got: %v", record.ServiceClassCode)
}
if record.EntryAddendaCountField() != "000001" {
t.Errorf("EntryAddendaCount Expected '000001' got: %v", record.EntryAddendaCountField())
}
if record.EntryHashField() != "0005320001" {
t.Errorf("EntryHash Expected '0005320001' got: %v", record.EntryHashField())
}
if record.TotalDebitEntryDollarAmountField() != "000000010500" {
t.Errorf("TotalDebitEntryDollarAmount Expected '000000010500' got: %v", record.TotalDebitEntryDollarAmountField())
}
if record.TotalCreditEntryDollarAmountField() != "000000000000" {
t.Errorf("TotalCreditEntryDollarAmount Expected '000000000000' got: %v", record.TotalCreditEntryDollarAmountField())
}
if record.CompanyIdentificationField() != "origid " {
t.Errorf("CompanyIdentification Expected 'origid ' got: %v", record.CompanyIdentificationField())
}
if record.MessageAuthenticationCodeField() != " " {
t.Errorf("MessageAuthenticationCode Expected ' ' got: %v", record.MessageAuthenticationCodeField())
}
if record.ODFIIdentificationField() != "07640125" {
t.Errorf("OdfiIdentification Expected '07640125' got: %v", record.ODFIIdentificationField())
}
if record.BatchNumberField() != "0000001" {
t.Errorf("BatchNumber Expected '0000001' got: %v", record.BatchNumberField())
}
}
// TestParseBatchControl tests parsing a known Batch ControlRecord string.
func TestParseBatchControl(t *testing.T) {
testParseBatchControl(t)
}
// BenchmarkParseBatchControl benchmarks parsing a known Batch ControlRecord string.
func BenchmarkParseBatchControl(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testParseBatchControl(b)
}
}
// testBCString validates that a known parsed file can be return to a string of the same value
func testBCString(t testing.TB) {
var line = "82250000010005320001000000010500000000000000origid 076401250000001"
r := NewReader(strings.NewReader(line))
r.line = line
bh := BatchHeader{BatchNumber: 1,
ServiceClassCode: DebitsOnly,
CompanyIdentification: "origid",
ODFIIdentification: "7640125"}
r.addCurrentBatch(NewBatchPPD(&bh))
r.currentBatch.AddEntry(&EntryDetail{TransactionCode: 27, Amount: 10500, RDFIIdentification: "5320001", TraceNumber: "76401255655291"})
if err := r.parseBatchControl(); err != nil {
t.Errorf("%T: %s", err, err)
}
record := r.currentBatch.GetControl()
if record.String() != line {
t.Errorf("Strings do not match")
}
}
// TestBCString tests validating that a known parsed file can be return to a string of the same value
func TestBCString(t *testing.T) {
testBCString(t)
}
// BenchmarkBCString benchmarks validating that a known parsed file can be return to a string of the same value
func BenchmarkBCString(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testBCString(b)
}
}
// testBCisServiceClassErr verifies service class code
func testBCisServiceClassErr(t testing.TB) {
bc := mockBatchControl()
bc.ServiceClassCode = 123
err := bc.Validate()
if !base.Match(err, ErrServiceClass) {
t.Errorf("%T: %s", err, err)
}
}
// TestBCisServiceClassErr tests verifying service class code
func TestBCisServiceClassErr(t *testing.T) {
testBCisServiceClassErr(t)
}
// BenchmarkBCisServiceClassErr benchmarks verifying service class code
func BenchmarkBCisServiceClassErr(b *testing.B) {
for i := 0; i < b.N; i++ {
testBCisServiceClassErr(b)
}
}
// testBCBatchNumber verifies batch number
func testBCBatchNumber(t testing.TB) {
bc := mockBatchControl()
bc.BatchNumber = 0
err := bc.Validate()
// TODO: are we expecting there to be no errors here?
if !base.Match(err, nil) {
t.Errorf("%T: %s", err, err)
}
}
// TestBCBatchNumber tests verifying batch number
func TestBCBatchNumber(t *testing.T) {
testBCBatchNumber(t)
}
// BenchmarkBCBatchNumber benchmarks verifying batch number
func BenchmarkBCBatchNumber(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testBCBatchNumber(b)
}
}
// testBCCompanyIdentificationAlphaNumeric verifies Company Identification is AlphaNumeric
func testBCCompanyIdentificationAlphaNumeric(t testing.TB) {
bc := mockBatchControl()
bc.CompanyIdentification = "®"
err := bc.Validate()
if !base.Match(err, ErrNonAlphanumeric) {
t.Errorf("%T: %s", err, err)
}
}
// TestBCCompanyIdentificationAlphaNumeric tests verifying Company Identification is AlphaNumeric
func TestBCCompanyIdentificationAlphaNumeric(t *testing.T) {
testBCCompanyIdentificationAlphaNumeric(t)
}
// BenchmarkBCCompanyIdentificationAlphaNumeric benchmarks verifying Company Identification is AlphaNumeric
func BenchmarkBCCompanyIdentificationAlphaNumeric(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testBCCompanyIdentificationAlphaNumeric(b)
}
}
// testBCMessageAuthenticationCodeAlphaNumeric verifies AuthenticationCode is AlphaNumeric
func testBCMessageAuthenticationCodeAlphaNumeric(t testing.TB) {
bc := mockBatchControl()
bc.MessageAuthenticationCode = "®"
err := bc.Validate()
if !base.Match(err, ErrNonAlphanumeric) {
t.Errorf("%T: %s", err, err)
}
}
// TestBCMessageAuthenticationCodeAlphaNumeric tests verifying AuthenticationCode is AlphaNumeric
func TestBCMessageAuthenticationCodeAlphaNumeric(t *testing.T) {
testBCMessageAuthenticationCodeAlphaNumeric(t)
}
// BenchmarkBCMessageAuthenticationCodeAlphaNumeric benchmarks verifying AuthenticationCode is AlphaNumeric
func BenchmarkBCMessageAuthenticationCodeAlphaNumeric(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testBCMessageAuthenticationCodeAlphaNumeric(b)
}
}
// testBCFieldInclusionServiceClassCode verifies Service Class Code is included
func testBCFieldInclusionServiceClassCode(t testing.TB) {
bc := mockBatchControl()
bc.ServiceClassCode = 0
err := bc.Validate()
if !base.Match(err, ErrConstructor) {
t.Errorf("%T: %s", err, err)
}
}
// TestBCFieldInclusionServiceClassCode tests verifying Service Class Code is included
func TestBCFieldInclusionServiceClassCode(t *testing.T) {
testBCFieldInclusionServiceClassCode(t)
}
// BenchmarkBCFieldInclusionServiceClassCod benchmarks verifying Service Class Code is included
func BenchmarkBCFieldInclusionServiceClassCode(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testBCFieldInclusionServiceClassCode(b)
}
}
// testBCFieldInclusionODFIIdentification verifies batch control ODFIIdentification
func testBCFieldInclusionODFIIdentification(t testing.TB) {
bc := mockBatchControl()
bc.ODFIIdentification = "000000000"
err := bc.Validate()
if !base.Match(err, ErrConstructor) {
t.Errorf("%T: %s", err, err)
}
}
// TestBCFieldInclusionODFIIdentification tests verifying batch control ODFIIdentification
func TestBCFieldInclusionODFIIdentification(t *testing.T) {
testBCFieldInclusionODFIIdentification(t)
}
// BenchmarkBCFieldInclusionODFIIdentification benchmarks verifying batch control ODFIIdentification
func BenchmarkBCFieldInclusionODFIIdentification(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testBCFieldInclusionODFIIdentification(b)
}
}
// testBatchControlLength verifies batch control length
func testBatchControlLength(t testing.TB) {
bc := NewBatchControl()
recordLength := len(bc.String())
if recordLength != 94 {
t.Errorf("Instantiated length of Batch Control string is not 94 but %v", recordLength)
}
}
// TestBatchControlLength tests verifying batch control length
func TestBatchControlLength(t *testing.T) {
testBatchControlLength(t)
}
// BenchmarkBatchControlLength benchmarks verifying batch control length
func BenchmarkBatchControlLength(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testBatchControlLength(b)
}
}
func TestBatchControl__SetValidation(t *testing.T) {
bh := NewBatchControl()
bh.SetValidation(&ValidateOpts{})
// nil out
bh = nil
bh.SetValidation(&ValidateOpts{})
}
func TestBatchControl__IsValidWithPreserveSpacesOpt(t *testing.T) {
var line = "82200000010023138010000000000000000100000000231380104 121042880000001"
bc := NewBatchControl()
bc.SetValidation(&ValidateOpts{
PreserveSpaces: true,
})
bc.Parse(line)
if err := bc.Validate(); err != nil {
t.Error(err)
}
}
func TestBatchControl_Overflow(t *testing.T) {
bc := mockBatchControl()
require.NoError(t, bc.totalDebitsOverflowsField())
require.NoError(t, bc.totalCreditsOverflowsField())
bc.TotalDebitEntryDollarAmount = math.MaxInt64
require.ErrorContains(t, bc.totalDebitsOverflowsField(), "does not match formatted value 036854775807")
require.NoError(t, bc.totalCreditsOverflowsField())
require.ErrorContains(t, bc.Validate(), "does not match formatted value 036854775807")
}