-
Notifications
You must be signed in to change notification settings - Fork 151
/
advFileControl_test.go
254 lines (222 loc) · 7.89 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
// 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 (
"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(t)
r.File.AddBatch(batchADV)
err := r.parseFileControl()
if err != nil {
t.Errorf("%T: %s", err, err)
}
record := r.File.ADVControl
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())
}
}
// 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(t)
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)
}
}
// 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)
}
}
// 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(t)
r.File.AddBatch(batchADV)
err := r.parseFileControl()
if !base.Match(err, ErrConstructor) {
t.Errorf("%T: %s", err, err)
}
}