forked from moov-io/ach
-
Notifications
You must be signed in to change notification settings - Fork 0
/
batchWEB_test.go
307 lines (274 loc) · 9.74 KB
/
batchWEB_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
// 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 (
"testing"
"github.com/moov-io/base"
)
// mockBatchWEBHeader creates a WEB batch header
func mockBatchWEBHeader() *BatchHeader {
bh := NewBatchHeader()
bh.ServiceClassCode = CreditsOnly
bh.StandardEntryClassCode = WEB
bh.CompanyName = "Your Company, inc"
bh.CompanyIdentification = "121042882"
bh.CompanyEntryDescription = "Online Order"
bh.ODFIIdentification = "12104288"
return bh
}
// mockWEBEntryDetail creates a WEB entry detail
func mockWEBEntryDetail() *EntryDetail {
entry := NewEntryDetail()
entry.TransactionCode = CheckingCredit
entry.SetRDFI("231380104")
entry.DFIAccountNumber = "123456789"
entry.Amount = 100000000
entry.IndividualName = "Wade Arnold"
entry.SetTraceNumber(mockBatchWEBHeader().ODFIIdentification, 1)
entry.SetPaymentType("S")
return entry
}
// mockBatchWEB creates a WEB batch
func mockBatchWEB() *BatchWEB {
mockBatch := NewBatchWEB(mockBatchWEBHeader())
mockBatch.AddEntry(mockWEBEntryDetail())
mockBatch.GetEntries()[0].AddendaRecordIndicator = 1
mockBatch.GetEntries()[0].AddAddenda05(mockAddenda05())
if err := mockBatch.Create(); err != nil {
panic(err)
}
return mockBatch
}
// testBatchWebAddenda validates No more than 1 batch per entry detail record can exist
// and no more than 1 addenda record per entry detail record can exist
func testBatchWebAddenda(t testing.TB) {
mockBatch := mockBatchWEB()
// mock batch already has one addenda. Creating two addenda should error
mockBatch.GetEntries()[0].AddAddenda05(mockAddenda05())
err := mockBatch.Create()
if !base.Match(err, NewErrBatchAddendaCount(2, 1)) {
t.Errorf("%T: %s", err, err)
}
}
// TestBatchWebAddenda tests validating no more than 1 batch per entry detail
// record can exist and no more than 1 addenda record per entry detail record can exist
func TestBatchWebAddenda(t *testing.T) {
testBatchWebAddenda(t)
}
// BenchmarkBatchWebAddenda benchmarks validating no more than 1 batch per entry detail
// record can exist and no more than 1 addenda record per entry detail record can exist
func BenchmarkBatchWebAddenda(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testBatchWebAddenda(b)
}
}
// testBatchWebIndividualNameRequired validates Individual name is a mandatory field
func testBatchWebIndividualNameRequired(t testing.TB) {
mockBatch := mockBatchWEB()
// mock batch already has one addenda. Creating two addenda should error
mockBatch.GetEntries()[0].IndividualName = ""
err := mockBatch.Validate()
if !base.Match(err, ErrConstructor) {
t.Errorf("%T: %s", err, err)
}
}
// TestBatchWebIndividualNameRequired tests validating Individual name is a mandatory field
func TestBatchWebIndividualNameRequired(t *testing.T) {
testBatchWebIndividualNameRequired(t)
}
// BenchmarkBatchWebIndividualNameRequired benchmarks validating Individual name is a mandatory field
func BenchmarkBatchWebIndividualNameRequired(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testBatchWebIndividualNameRequired(b)
}
}
// TestBatchWEBAddendum98 validates Addenda98 returns an error
func TestBatchWEBAddendum98(t *testing.T) {
mockBatch := NewBatchWEB(mockBatchWEBHeader())
mockBatch.AddEntry(mockWEBEntryDetail())
mockAddenda98 := mockAddenda98()
mockAddenda98.TypeCode = "05"
mockBatch.GetEntries()[0].Category = CategoryNOC
mockBatch.GetEntries()[0].Addenda98 = mockAddenda98
err := mockBatch.Create()
if !base.Match(err, ErrAddendaTypeCode) {
t.Errorf("%T: %s", err, err)
}
}
// TestBatchWEBAddendum99 validates Addenda99 returns an error
func TestBatchWEBAddendum99(t *testing.T) {
mockBatch := NewBatchWEB(mockBatchWEBHeader())
mockBatch.AddEntry(mockWEBEntryDetail())
mockAddenda99 := mockAddenda99()
mockAddenda99.TypeCode = "05"
mockBatch.GetEntries()[0].Category = CategoryReturn
mockBatch.GetEntries()[0].Addenda99 = mockAddenda99
err := mockBatch.Create()
if !base.Match(err, ErrAddendaTypeCode) {
t.Errorf("%T: %s", err, err)
}
}
// testBatchWEBAddendaTypeCode validates addenda type code is valid
func testBatchWEBAddendaTypeCode(t testing.TB) {
mockBatch := mockBatchWEB()
mockBatch.GetEntries()[0].AddAddenda05(mockAddenda05())
mockBatch.GetEntries()[0].Addenda05[0].TypeCode = "02"
err := mockBatch.Validate()
if !base.Match(err, ErrAddendaTypeCode) {
t.Errorf("%T: %s", err, err)
}
}
// TestBatchWEBAddendaTypeCode tests validating addenda type code is valid
func TestBatchWEBAddendaTypeCode(t *testing.T) {
testBatchWEBAddendaTypeCode(t)
}
// BenchmarkBatchWEBAddendaTypeCode benchmarks validating addenda type code is valid
func BenchmarkBatchWEBAddendaTypeCode(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testBatchWEBAddendaTypeCode(b)
}
}
// testBatchWebSEC validates that the standard entry class code is WEB for batch Web
func testBatchWebSEC(t testing.TB) {
mockBatch := mockBatchWEB()
mockBatch.Header.StandardEntryClassCode = RCK
err := mockBatch.Validate()
if !base.Match(err, ErrBatchSECType) {
t.Errorf("%T: %s", err, err)
}
}
// TestBatchWebSEC tests validating that the standard entry class code is WEB for batch WEB
func TestBatchWebSEC(t *testing.T) {
testBatchWebSEC(t)
}
// BenchmarkBatchWebSEC benchmarks validating that the standard entry class code is WEB for batch WEB
func BenchmarkBatchWebSEC(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testBatchWebSEC(b)
}
}
// testBatchWebPaymentType validates that the entry detail
// payment type / discretionary data is either single or reoccurring
func testBatchWebPaymentType(t testing.TB) {
mockBatch := mockBatchWEB()
mockBatch.GetEntries()[0].DiscretionaryData = "AA"
err := mockBatch.Validate()
// TODO: are we expecting there to be no errors here?
if !base.Match(err, nil) {
t.Errorf("%T: %s", err, err)
}
}
// TestBatchWebPaymentType tests validating that the entry detail
// payment type / discretionary data is either single or reoccurring
func TestBatchWebPaymentType(t *testing.T) {
testBatchWebPaymentType(t)
}
// BenchmarkBatchWebPaymentType benchmarks validating that the entry detail
// payment type / discretionary data is either single or reoccurring
func BenchmarkBatchWebPaymentType(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testBatchWebPaymentType(b)
}
}
// testBatchWebCreate creates a WEB batch
func testBatchWebCreate(t testing.TB) {
mockBatch := mockBatchWEB()
// Must have valid batch header to create a batch
mockBatch.GetHeader().ServiceClassCode = 63
err := mockBatch.Create()
if !base.Match(err, ErrServiceClass) {
t.Errorf("%T: %s", err, err)
}
}
// TestBatchWebCreate tests creating a WEB batch
func TestBatchWebCreate(t *testing.T) {
testBatchWebCreate(t)
}
// BenchmarkBatchWebCreate benchmarks creating a WEB batch
func BenchmarkBatchWebCreate(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testBatchWebCreate(b)
}
}
// testBatchWebPaymentTypeR validates that the entry detail
// payment type / discretionary data is reoccurring
func testBatchWebPaymentTypeR(t testing.TB) {
mockBatch := mockBatchWEB()
mockBatch.GetEntries()[0].DiscretionaryData = "R"
err := mockBatch.Validate()
// TODO: are we expecting there to be no errors here?
if !base.Match(err, nil) {
t.Errorf("%T: %s", err, err)
}
if mockBatch.GetEntries()[0].PaymentTypeField() != "R" {
t.Errorf("PaymentTypeField %v was expecting R", mockBatch.GetEntries()[0].PaymentTypeField())
}
}
// TestBatchWebPaymentTypeR tests validating that the entry detail
// payment type / discretionary data is reoccurring
func TestBatchWebPaymentTypeR(t *testing.T) {
testBatchWebPaymentTypeR(t)
}
// BenchmarkBatchWebPaymentTypeR benchmarks validating that the entry detail
// payment type / discretionary data is reoccurring
func BenchmarkBatchWebPaymentTypeR(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testBatchWebPaymentTypeR(b)
}
}
// TestBatchWEBAddendum99Category validates Addenda99 returns an error
func TestBatchWEBAddendum99Category(t *testing.T) {
mockBatch := NewBatchWEB(mockBatchWEBHeader())
mockBatch.AddEntry(mockWEBEntryDetail())
mockAddenda99 := mockAddenda99()
mockBatch.Entries[0].AddendaRecordIndicator = 1
mockBatch.Entries[0].Category = CategoryForward
mockBatch.Entries[0].Addenda99 = mockAddenda99
err := mockBatch.Create()
if !base.Match(err, ErrBatchAddendaCategory) {
t.Errorf("%T: %s", err, err)
}
}
// TestBatchWEBCategoryReturn validates CategoryReturn returns an error
func TestBatchWEBCategoryReturn(t *testing.T) {
mockBatch := NewBatchWEB(mockBatchWEBHeader())
mockBatch.AddEntry(mockWEBEntryDetail())
mockAddenda05 := mockAddenda05()
mockBatch.GetEntries()[0].Category = CategoryReturn
mockBatch.GetEntries()[0].AddendaRecordIndicator = 1
mockBatch.GetEntries()[0].AddAddenda05(mockAddenda05)
err := mockBatch.Create()
if !base.Match(err, ErrBatchAddendaCategory) {
t.Errorf("%T: %s", err, err)
}
}
// TestBatchWEBCategoryReturnAddenda98 validates CategoryReturn returns an error
func TestBatchWEBCategoryReturnAddenda98(t *testing.T) {
mockBatch := NewBatchWEB(mockBatchWEBHeader())
mockBatch.AddEntry(mockWEBEntryDetail())
mockAddenda98 := mockAddenda98()
mockBatch.GetEntries()[0].Category = CategoryReturn
mockBatch.GetEntries()[0].AddendaRecordIndicator = 1
mockBatch.GetEntries()[0].Addenda98 = mockAddenda98
err := mockBatch.Create()
if !base.Match(err, ErrBatchAddendaCategory) {
t.Errorf("%T: %s", err, err)
}
}
// TestBatchWEBValidTranCodeForServiceClassCode validates a transactionCode based on ServiceClassCode
func TestBatchWEBValidTranCodeForServiceClassCode(t *testing.T) {
mockBatch := mockBatchWEB()
mockBatch.GetHeader().ServiceClassCode = DebitsOnly
err := mockBatch.Create()
if !base.Match(err, NewErrBatchServiceClassTranCode(DebitsOnly, 22)) {
t.Errorf("%T: %s", err, err)
}
}