-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathcreditItem_test.go
217 lines (193 loc) · 6.41 KB
/
creditItem_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
// Copyright 2020 The Moov Authors
// Use of this source code is governed by an Apache License
// license that can be found in the LICENSE file.
package imagecashletter
import (
"strings"
"testing"
"github.com/stretchr/testify/require"
)
// mockCreditItem creates a CreditItem
func mockCreditItem() *CreditItem {
ci := NewCreditItem()
ci.AuxiliaryOnUs = "123456789"
ci.ExternalProcessingCode = ""
ci.PostingBankRoutingNumber = "031300012"
ci.OnUs = "5558881"
ci.ItemAmount = 100000 // 1000.00
ci.CreditItemSequenceNumber = "1 "
ci.DocumentationTypeIndicator = "G"
ci.AccountTypeCode = "1"
ci.SourceWorkCode = "01"
ci.UserField = " "
return ci
}
// TestMockCreditItem creates a CreditItem
func TestMockCreditItem(t *testing.T) {
ci := mockCreditItem()
require.NoError(t, ci.Validate())
require.Equal(t, "62", ci.recordType)
require.Equal(t, "123456789", ci.AuxiliaryOnUs)
require.Equal(t, "", ci.ExternalProcessingCode)
require.Equal(t, "031300012", ci.PostingBankRoutingNumber)
require.Equal(t, "5558881", ci.OnUs)
require.Equal(t, 100000, ci.ItemAmount)
require.Equal(t, "1 ", ci.CreditItemSequenceNumber)
require.Equal(t, "G", ci.DocumentationTypeIndicator)
require.Equal(t, "1", ci.AccountTypeCode)
require.Equal(t, "01", ci.SourceWorkCode)
require.Equal(t, " ", ci.UserField)
}
func TestCreditItemCrash(t *testing.T) {
ci := &CreditItem{}
ci.Parse(`100000000000400000000200001010000010100000IG000000000000000000000000000000000 00`)
require.Equal(t, "", ci.UserField)
}
// TestParseCreditItem validates parsing a CreditItem
func TestParseCreditItem(t *testing.T) {
var line = "62 123456789 031300012 5558881000000001000001 G101 "
r := NewReader(strings.NewReader(line))
r.line = line
clh := mockCashLetterHeader()
r.addCurrentCashLetter(NewCashLetter(clh))
ci := mockCreditItem()
r.currentCashLetter.AddCreditItem(ci)
require.NoError(t, r.parseCreditItem())
record := r.currentCashLetter.GetCreditItems()[0]
require.Equal(t, "62", record.recordType)
require.Equal(t, "123456789", record.AuxiliaryOnUs)
require.Equal(t, "", record.ExternalProcessingCode)
require.Equal(t, "031300012", record.PostingBankRoutingNumber)
require.Equal(t, "5558881", record.OnUs)
require.Equal(t, 100000, record.ItemAmount)
require.Equal(t, "1 ", record.CreditItemSequenceNumber)
require.Equal(t, "G", record.DocumentationTypeIndicator)
require.Equal(t, "1", record.AccountTypeCode)
require.Equal(t, "01", record.SourceWorkCode)
require.Equal(t, " ", record.UserField)
}
// testCIString validates parsing a CreditItem
func testCIString(t testing.TB) {
var line = "62 123456789 031300012 5558881000000001000001 G101 "
r := NewReader(strings.NewReader(line))
r.line = line
clh := mockCashLetterHeader()
r.addCurrentCashLetter(NewCashLetter(clh))
ci := mockCreditItem()
r.currentCashLetter.AddCreditItem(ci)
require.NoError(t, r.parseCreditItem())
record := r.currentCashLetter.GetCreditItems()[0]
require.Equal(t, line, record.String())
}
// TestCIString tests validating that a known parsed CheckDetail can return to a string of the same value
func TestCIString(t *testing.T) {
testCIString(t)
}
// BenchmarkCIString benchmarks validating that a known parsed CreditItem
// can return to a string of the same value
func BenchmarkCIString(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testCIString(b)
}
}
// TestCIRecordType validation
func TestCIRecordType(t *testing.T) {
ci := mockCreditItem()
ci.recordType = "00"
err := ci.Validate()
var e *FieldError
require.ErrorAs(t, err, &e)
require.Equal(t, "recordType", e.FieldName)
}
// TestCIDocumentationTypeIndicator validation
func TestCIDocumentationTypeIndicator(t *testing.T) {
ci := mockCreditItem()
ci.DocumentationTypeIndicator = "P"
err := ci.Validate()
var e *FieldError
require.ErrorAs(t, err, &e)
require.Equal(t, "DocumentationTypeIndicator", e.FieldName)
}
// TestCIDocumentationTypeIndicatorZ validation
func TestCIDocumentationTypeIndicatorZ(t *testing.T) {
ci := mockCreditItem()
ci.DocumentationTypeIndicator = "Z"
err := ci.Validate()
var e *FieldError
require.ErrorAs(t, err, &e)
require.Equal(t, "DocumentationTypeIndicator", e.FieldName)
}
// TestCIDocumentationTypeIndicatorM validation
func TestCIDocumentationTypeIndicatorM(t *testing.T) {
ci := mockCreditItem()
ci.DocumentationTypeIndicator = "M"
err := ci.Validate()
var e *FieldError
require.ErrorAs(t, err, &e)
require.Equal(t, "DocumentationTypeIndicator", e.FieldName)
}
// TestCIAccountTypeCode validation
func TestCIAccountTypeCode(t *testing.T) {
ci := mockCreditItem()
ci.AccountTypeCode = "Z"
err := ci.Validate()
var e *FieldError
require.ErrorAs(t, err, &e)
require.Equal(t, "AccountTypeCode", e.FieldName)
}
// TestCISourceWorkCode validation
func TestCISourceWorkCode(t *testing.T) {
ci := mockCreditItem()
ci.SourceWorkCode = "99"
err := ci.Validate()
var e *FieldError
require.ErrorAs(t, err, &e)
require.Equal(t, "SourceWorkCode", e.FieldName)
}
// TestCIUserField validation
func TestCIUserField(t *testing.T) {
ci := mockCreditItem()
ci.UserField = "®©"
err := ci.Validate()
var e *FieldError
require.ErrorAs(t, err, &e)
require.Equal(t, "UserField", e.FieldName)
}
// Field Inclusion
// TestCIFIRecordType validation
func TestCIFIRecordType(t *testing.T) {
ci := mockCreditItem()
ci.recordType = ""
err := ci.Validate()
var e *FieldError
require.ErrorAs(t, err, &e)
require.Equal(t, "recordType", e.FieldName)
}
// TestCIFIPostingBankRoutingNumber validation
func TestCIFIPostingBankRoutingNumber(t *testing.T) {
ci := mockCreditItem()
ci.PostingBankRoutingNumber = ""
err := ci.Validate()
var e *FieldError
require.ErrorAs(t, err, &e)
require.Equal(t, "PostingBankRoutingNumber", e.FieldName)
}
// TestCIFIPostingBankRoutingNumberZero validation
func TestCIFIPostingBankRoutingNumberZero(t *testing.T) {
ci := mockCreditItem()
ci.PostingBankRoutingNumber = "000000000"
err := ci.Validate()
var e *FieldError
require.ErrorAs(t, err, &e)
require.Equal(t, "PostingBankRoutingNumber", e.FieldName)
}
// TestCIFICreditItemSequenceNumber validation
func TestCIFICreditItemSequenceNumber(t *testing.T) {
ci := mockCreditItem()
ci.CreditItemSequenceNumber = ""
err := ci.Validate()
var e *FieldError
require.ErrorAs(t, err, &e)
require.Equal(t, "CreditItemSequenceNumber", e.FieldName)
}