-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathcashLetterControl_test.go
186 lines (165 loc) · 5.93 KB
/
cashLetterControl_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
// 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"
"time"
"github.com/stretchr/testify/require"
)
// mockCashLetterControl creates a CashLetterControl
func mockCashLetterControl() *CashLetterControl {
clc := NewCashLetterControl()
clc.CashLetterBundleCount = 1
clc.CashLetterItemsCount = 7
clc.CashLetterTotalAmount = 100000 // 1000.00
clc.CashLetterImagesCount = 1
clc.ECEInstitutionName = "Wells Fargo"
clc.SettlementDate = time.Now()
clc.CreditTotalIndicator = 0
return clc
}
// TestMockCashLetterControl creates a CashLetterControl
func TestMockCashLetterControl(t *testing.T) {
clc := mockCashLetterControl()
require.NoError(t, clc.Validate())
require.Equal(t, "90", clc.recordType)
require.Equal(t, 1, clc.CashLetterBundleCount)
require.Equal(t, 7, clc.CashLetterItemsCount)
require.Equal(t, 100000, clc.CashLetterTotalAmount)
require.Equal(t, 1, clc.CashLetterImagesCount)
require.Equal(t, "Wells Fargo", clc.ECEInstitutionName)
require.Equal(t, 0, clc.CreditTotalIndicator)
}
// TestParseCashLetterControl parses a known CashLetterControl record string
func TestParseCashLetterControl(t *testing.T) {
var line = "900000010000000100000000100000000000000Wells Fargo 201809050 "
r := NewReader(strings.NewReader(line))
r.line = line
clh := mockCashLetterHeader()
r.addCurrentCashLetter(NewCashLetter(clh))
require.NoError(t, r.parseCashLetterControl())
record := r.currentCashLetter.CashLetterControl
require.Equal(t, "90", record.recordType)
require.Equal(t, "000001", record.CashLetterBundleCountField())
require.Equal(t, "00000001", record.CashLetterItemsCountField())
require.Equal(t, "00000000100000", record.CashLetterTotalAmountField())
require.Equal(t, "000000000", record.CashLetterImagesCountField())
require.Equal(t, "Wells Fargo ", record.ECEInstitutionNameField())
require.Equal(t, "20180905", record.SettlementDateField())
require.Equal(t, "0", record.CreditTotalIndicatorField())
require.Equal(t, " ", record.reservedField())
}
// testCLCString validates that a known parsed CashLetterControl can be return to a string of the same value
func testCLCString(t testing.TB) {
var line = "900000010000000100000000100000000000000Wells Fargo 201809050 "
r := NewReader(strings.NewReader(line))
r.line = line
clh := mockCashLetterHeader()
r.addCurrentCashLetter(NewCashLetter(clh))
require.NoError(t, r.parseCashLetterControl())
record := r.currentCashLetter.CashLetterControl
require.Equal(t, line, record.String())
}
// TestCLCString tests validating that a known parsed CashLetterControl can be return to a string of the same value
func TestCLCString(t *testing.T) {
testCLCString(t)
}
// BenchmarkCLCString benchmarks validating that a known parsed CashLetterControl can be return to a string of the same value
func BenchmarkCLCString(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
testCLCString(b)
}
}
// TestCLCRecordType validation
func TestCLCRecordType(t *testing.T) {
clc := mockCashLetterControl()
clc.recordType = "00"
err := clc.Validate()
var fieldErr *FieldError
require.ErrorAs(t, err, &fieldErr)
require.Equal(t, "recordType", fieldErr.FieldName)
}
// TestECEInstitutionName validation
func TestECEInstitutionName(t *testing.T) {
clc := mockCashLetterControl()
clc.ECEInstitutionName = "®©"
err := clc.Validate()
var fieldErr *FieldError
require.ErrorAs(t, err, &fieldErr)
require.Equal(t, "ECEInstitutionName", fieldErr.FieldName)
}
// TestCLCCreditTotalIndicator validation
func TestCLCCreditTotalIndicator(t *testing.T) {
clc := mockCashLetterControl()
clc.CreditTotalIndicator = 9
err := clc.Validate()
var fieldErr *FieldError
require.ErrorAs(t, err, &fieldErr)
require.Equal(t, "CreditTotalIndicator", fieldErr.FieldName)
}
// TestCLCFieldInclusionRecordType validates FieldInclusion
func TestCLCFieldInclusionRecordType(t *testing.T) {
clc := mockCashLetterControl()
clc.recordType = ""
err := clc.Validate()
var fieldErr *FieldError
require.ErrorAs(t, err, &fieldErr)
require.Equal(t, "recordType", fieldErr.FieldName)
}
// TestFieldInclusionCashLetterItemsCount validates FieldInclusion
func TestFieldInclusionCashLetterItemsCount(t *testing.T) {
clc := mockCashLetterControl()
clc.CashLetterItemsCount = 0
err := clc.Validate()
var fieldErr *FieldError
require.ErrorAs(t, err, &fieldErr)
require.Equal(t, "CashLetterItemsCount", fieldErr.FieldName)
}
// TestFieldInclusionCashLetterTotalAmount validates FieldInclusion
func TestFieldInclusionCashLetterTotalAmount(t *testing.T) {
clc := mockCashLetterControl()
clc.CashLetterTotalAmount = 0
err := clc.Validate()
var fieldErr *FieldError
require.ErrorAs(t, err, &fieldErr)
require.Equal(t, "CashLetterTotalAmount", fieldErr.FieldName)
}
// TestFieldInclusionSettlementDate validates FieldInclusion
func TestFieldInclusionRecordTypeSettlementDate(t *testing.T) {
clc := mockCashLetterControl()
// if present (non-zero), SettlementDate.Year() must be between 1993 and 9999
clc.SettlementDate = time.Date(40010, time.November, 9, 0, 0, 0, 0, time.UTC)
err := clc.Validate()
var fieldErr *FieldError
require.ErrorAs(t, err, &fieldErr)
require.Equal(t, "SettlementDate", fieldErr.FieldName)
}
// TestCashLetterControlRuneCountInString validates RuneCountInString
func TestCashLetterControlRuneCountInString(t *testing.T) {
clc := NewCashLetterControl()
var line = "90"
clc.Parse(line)
require.Equal(t, 0, clc.CashLetterBundleCount)
}
func TestCashLetterControl_isReturnCollectionType(t *testing.T) {
tests := []struct {
collectionType string
expected bool
}{
{"03", true},
{"04", true},
{"05", true},
{"06", true},
{"07", false},
{"01", false},
{"", false},
}
for _, tt := range tests {
t.Run(tt.collectionType, func(t *testing.T) {
require.Equal(t, tt.expected, isReturnCollectionType(tt.collectionType))
})
}
}