forked from moov-io/ach
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbatchControl_internal_test.go
207 lines (189 loc) · 6.13 KB
/
batchControl_internal_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
// Copyright 2017 The ACH 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"
)
func mockBatchControl() *BatchControl {
bc := NewBatchControl()
bc.ServiceClassCode = 220
bc.CompanyIdentification = "123456789"
bc.ODFIIdentification = 6200001
return bc
}
func TestMockBatchControl(t *testing.T) {
bc := mockBatchControl()
if err := bc.Validate(); err != nil {
t.Error("mockBatchControl does not validate and will break other tests")
}
if bc.ServiceClassCode != 220 {
t.Error("ServiceClassCode depedendent default value has changed")
}
if bc.CompanyIdentification != "123456789" {
t.Error("CompanyIdentification depedendent default value has changed")
}
if bc.ODFIIdentification != 6200001 {
t.Error("ODFIIdentification depedendent default value has changed")
}
}
// TestParseBatchControl parses a known Batch ControlRecord string.
func TestParseBatchControl(t *testing.T) {
var line = "82250000010005320001000000010500000000000000origid 076401250000001"
r := NewReader(strings.NewReader(line))
r.line = line
r.addCurrentBatch(NewBatchPPD())
bh := BatchHeader{BatchNumber: 1,
ServiceClassCode: 225,
CompanyIdentification: "origid",
ODFIIdentification: 7640125}
r.currentBatch.SetHeader(&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.recordType != "8" {
t.Errorf("RecordType Expected '8' got: %v", record.recordType)
}
if record.ServiceClassCode != 225 {
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.reserved != " " {
t.Errorf("Reserved Expected ' ' got: %v", record.reserved)
}
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())
}
}
// TestBCString validats that a known parsed file can be return to a string of the same value
func TestBCString(t *testing.T) {
var line = "82250000010005320001000000010500000000000000origid 076401250000001"
r := NewReader(strings.NewReader(line))
r.line = line
r.addCurrentBatch(NewBatchPPD())
bh := BatchHeader{BatchNumber: 1,
ServiceClassCode: 225,
CompanyIdentification: "origid",
ODFIIdentification: 7640125}
r.currentBatch.SetHeader(&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")
}
}
// TestValidateBCRecordType ensure error if recordType is not 8
func TestValidateBCRecordType(t *testing.T) {
bc := mockBatchControl()
bc.recordType = "2"
if err := bc.Validate(); err != nil {
if e, ok := err.(*FieldError); ok {
if e.FieldName != "recordType" {
t.Errorf("%T: %s", err, err)
}
}
}
}
func TestBCisServiceClassErr(t *testing.T) {
bc := mockBatchControl()
bc.ServiceClassCode = 123
if err := bc.Validate(); err != nil {
if e, ok := err.(*FieldError); ok {
if e.FieldName != "ServiceClassCode" {
t.Errorf("%T: %s", err, err)
}
}
}
}
func TestBCBatchNumber(t *testing.T) {
bc := mockBatchControl()
bc.BatchNumber = 0
if err := bc.Validate(); err != nil {
if e, ok := err.(*FieldError); ok {
if e.FieldName != "BatchNumber" {
t.Errorf("%T: %s", err, err)
}
}
}
}
func TestBCCompanyIdentificationAlphaNumeric(t *testing.T) {
bc := mockBatchControl()
bc.CompanyIdentification = "®"
if err := bc.Validate(); err != nil {
if e, ok := err.(*FieldError); ok {
if e.FieldName != "CompanyIdentification" {
t.Errorf("%T: %s", err, err)
}
}
}
}
func TestBCMessageAuthenticationCodeAlphaNumeric(t *testing.T) {
bc := mockBatchControl()
bc.MessageAuthenticationCode = "®"
if err := bc.Validate(); err != nil {
if e, ok := err.(*FieldError); ok {
if e.FieldName != "MessageAuthenticationCode" {
t.Errorf("%T: %s", err, err)
}
}
}
}
func TestBCFieldInclusionRecordType(t *testing.T) {
bc := mockBatchControl()
bc.recordType = ""
if err := bc.Validate(); err != nil {
if e, ok := err.(*FieldError); ok {
if e.Msg != msgFieldInclusion {
t.Errorf("%T: %s", err, err)
}
}
}
}
func TestBCFieldInclusionServiceClassCode(t *testing.T) {
bc := mockBatchControl()
bc.ServiceClassCode = 0
if err := bc.Validate(); err != nil {
if e, ok := err.(*FieldError); ok {
if e.Msg != msgFieldInclusion {
t.Errorf("%T: %s", err, err)
}
}
}
}
func TestBCFieldInclusionODFIIdentification(t *testing.T) {
bc := mockBatchControl()
bc.ODFIIdentification = 0
if err := bc.Validate(); err != nil {
if e, ok := err.(*FieldError); ok {
if e.Msg != msgFieldInclusion {
t.Errorf("%T: %s", err, err)
}
}
}
}