forked from moov-io/ach
-
Notifications
You must be signed in to change notification settings - Fork 0
/
advFileControl_test.go
294 lines (255 loc) · 8.85 KB
/
advFileControl_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
// Copyright 2018 The Moov Authors
// Use of this source code is governed by an Apache License
// license that can be found in the LICENSE file.
package ach
import (
"strings"
"testing"
"github.com/moov-io/base"
)
// mockADVFileControl create a file control
func mockADVFileControl() ADVFileControl {
fc := NewADVFileControl()
fc.BatchCount = 1
fc.BlockCount = 1
fc.EntryAddendaCount = 1
fc.EntryHash = 5320001
return fc
}
// testMockADVFileControl validates a file control record
func testMockADVFileControl(t testing.TB) {
fc := mockADVFileControl()
if err := fc.Validate(); err != nil {
t.Error("mockADVFileControl does not validate and will break other tests")
}
if fc.BatchCount != 1 {
t.Error("BatchCount dependent default value has changed")
}
if fc.BlockCount != 1 {
t.Error("BlockCount dependent default value has changed")
}
if fc.EntryAddendaCount != 1 {
t.Error("EntryAddendaCount dependent default value has changed")
}
if fc.EntryHash != 5320001 {
t.Error("EntryHash dependent default value has changed")
}
}
// TestMockADVFileControl tests validating a file control record
func TestMockADVFileControl(t *testing.T) {
testMockADVFileControl(t)
}
// BenchmarkMockADVFileControl benchmarks validating a file control record
func BenchmarkMockADVFileControl(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testMockADVFileControl(b)
}
}
// testParseADVFileControl parses a known file control record string
func testParseADVFileControl(t testing.TB) {
var line = "90000010000010000000100053200010000000000000001050000000000000000000000 "
r := NewReader(strings.NewReader(line))
r.line = line
batchADV := mockBatchADV()
r.File.AddBatch(batchADV)
err := r.parseFileControl()
if err != nil {
t.Errorf("%T: %s", err, err)
}
record := r.File.ADVControl
if record.recordType != "9" {
t.Errorf("RecordType Expected '9' got: %v", record.recordType)
}
if record.BatchCountField() != "000001" {
t.Errorf("BatchCount Expected '000001' got: %v", record.BatchCountField())
}
if record.BlockCountField() != "000001" {
t.Errorf("BlockCount Expected '000001' got: %v", record.BlockCountField())
}
if record.EntryAddendaCountField() != "00000001" {
t.Errorf("EntryAddendaCount Expected '00000001' got: %v", record.EntryAddendaCountField())
}
if record.EntryHashField() != "0005320001" {
t.Errorf("EntryHash Expected '0005320001' got: %v", record.EntryHashField())
}
if record.TotalDebitEntryDollarAmountInFileField() != "00000000000000010500" {
t.Errorf("TotalDebitEntryDollarAmountInFile Expected '0000000000000010500' got: %v", record.TotalDebitEntryDollarAmountInFileField())
}
if record.TotalCreditEntryDollarAmountInFileField() != "00000000000000000000" {
t.Errorf("TotalCreditEntryDollarAmountInFile Expected '00000000000000000000' got: %v", record.TotalCreditEntryDollarAmountInFileField())
}
if record.reserved != " " {
t.Errorf("Reserved Expected ' ' got: %v", record.reserved)
}
}
// TestParseADVFileControl tests parsing a known file control record string
func TestParseADVFileControl(t *testing.T) {
testParseADVFileControl(t)
}
// BenchmarkParseADVFileControl benchmarks parsing a known file control record string
func BenchmarkParseADVFileControl(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testParseADVFileControl(b)
}
}
// testADVFCString validates that a known parsed file can be return to a string of the same value
func testADVFCString(t testing.TB) {
var line = "90000010000010000000100053200010000000000000001050000000000000000000000 "
r := NewReader(strings.NewReader(line))
r.line = line
batchADV := mockBatchADV()
r.File.AddBatch(batchADV)
err := r.parseFileControl()
if err != nil {
t.Errorf("%T: %s", err, err)
}
record := r.File.ADVControl
if record.String() != line {
t.Errorf("\nStrings do not match %s\n %s", line, record.String())
}
}
// TestADVFCString tests validating that a known parsed file can be return to a string of the same value
func TestADVFCString(t *testing.T) {
testADVFCString(t)
}
// BenchmarkADVFCString benchmarks validating that a known parsed file can be return to a string of the same value
func BenchmarkADVFCString(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testADVFCString(b)
}
}
// testValidateADVFCRecordType validates error if recordType is not 9
func testValidateADVFCRecordType(t testing.TB) {
fc := mockADVFileControl()
fc.recordType = "2"
err := fc.Validate()
if !base.Match(err, NewErrRecordType(9)) {
t.Errorf("%T: %s", err, err)
}
}
// TestValidateADVFCRecordType tests validating error if recordType is not 9
func TestValidateADVFCRecordType(t *testing.T) {
testValidateADVFCRecordType(t)
}
// BenchmarkValidateADVFCRecordType benchmarks validating error if recordType is not 9
func BenchmarkValidateADVFCRecordType(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testValidateADVFCRecordType(b)
}
}
// testADVFCFieldInclusion validates file control field inclusion
func testADVFCFieldInclusion(t testing.TB) {
fc := mockADVFileControl()
fc.BatchCount = 0
err := fc.Validate()
if !base.Match(err, ErrConstructor) {
t.Errorf("%T: %s", err, err)
}
}
// TestADVFCFieldInclusion tests validating file control field inclusion
func TestADVFCFieldInclusion(t *testing.T) {
testADVFCFieldInclusion(t)
}
// BenchmarkADVFCFieldInclusion benchmarks validating file control field inclusion
func BenchmarkADVFCFieldInclusion(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testADVFCFieldInclusion(b)
}
}
// testADVFCFieldInclusionRecordType validates file control record type field inclusion
func testADVFCFieldInclusionRecordType(t testing.TB) {
fc := mockADVFileControl()
fc.recordType = ""
err := fc.Validate()
if !base.Match(err, ErrConstructor) {
t.Errorf("%T: %s", err, err)
}
}
// TestADVFCFieldInclusionRecordType tests validating file control record type field inclusion
func TestADVFCFieldInclusionRecordType(t *testing.T) {
testADVFCFieldInclusionRecordType(t)
}
// BenchmarkADVFCFieldInclusionRecordType benchmarks tests validating file control record type field inclusion
func BenchmarkADVFCFieldInclusionRecordType(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testADVFCFieldInclusionRecordType(b)
}
}
// testADVFCFieldInclusionBlockCount validates file control block count field inclusion
func testADVFCFieldInclusionBlockCount(t testing.TB) {
fc := mockADVFileControl()
fc.BlockCount = 0
err := fc.Validate()
if !base.Match(err, ErrConstructor) {
t.Errorf("%T: %s", err, err)
}
}
// TestADVFCFieldInclusionBlockCount tests validating file control block count field inclusion
func TestADVFCFieldInclusionBlockCount(t *testing.T) {
testADVFCFieldInclusionBlockCount(t)
}
// BenchmarkADVFCFieldInclusionBlockCount benchmarks validating file control block count field inclusion
func BenchmarkADVFCFieldInclusionBlockCount(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testADVFCFieldInclusionBlockCount(b)
}
}
// testADVFCFieldInclusionEntryAddendaCount validates file control addenda count field inclusion
func testADVFCFieldInclusionEntryAddendaCount(t testing.TB) {
fc := mockADVFileControl()
fc.EntryAddendaCount = 0
err := fc.Validate()
if !base.Match(err, ErrConstructor) {
t.Errorf("%T: %s", err, err)
}
}
// TestADVFCFieldInclusionEntryAddendaCount tests validating file control addenda count field inclusion
func TestADVFCFieldInclusionEntryAddendaCount(t *testing.T) {
testADVFCFieldInclusionEntryAddendaCount(t)
}
// BenchmarkADVFCFieldInclusionEntryAddendaCount benchmarks validating file control addenda count field inclusion
func BenchmarkADVFCFieldInclusionEntryAddendaCount(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testADVFCFieldInclusionEntryAddendaCount(b)
}
}
// testADVFCFieldInclusionEntryHash validates file control entry hash field inclusion
func testADVFCFieldInclusionEntryHash(t testing.TB) {
fc := mockADVFileControl()
fc.EntryHash = 0
err := fc.Validate()
if !base.Match(err, ErrConstructor) {
t.Errorf("%T: %s", err, err)
}
}
// TestADVFCFieldInclusionEntryHash tests validating file control entry hash field inclusion
func TestADVFCFieldInclusionEntryHash(t *testing.T) {
testADVFCFieldInclusionEntryHash(t)
}
// BenchmarkADVFCFieldInclusionEntryHash benchmarks validating file control entry hash field inclusion
func BenchmarkADVFCFieldInclusionEntryHash(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testADVFCFieldInclusionEntryHash(b)
}
}
// TestInvalidADVFCParse returns an error when parsing an ADV File Control
func TestInvalidADVFCParse(t *testing.T) {
var line = "9000001000001000000010005320001000000000000000105"
r := NewReader(strings.NewReader(line))
r.line = line
batchADV := mockBatchADV()
r.File.AddBatch(batchADV)
err := r.parseFileControl()
if !base.Match(err, ErrConstructor) {
t.Errorf("%T: %s", err, err)
}
}