-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfile_header_test.go
139 lines (133 loc) · 4.3 KB
/
file_header_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
package cadeft
import (
"strings"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestParseARecord(t *testing.T) {
type testCase struct {
input string
expected FileHeader
expectReadErr bool
validationErr bool
}
cases := map[string]testCase{
"Valid header record": {
input: "A0000000010000000610000102313861210hello CAD",
expected: FileHeader{
RecordHeader: RecordHeader{
RecordType: HeaderRecord,
recordCount: 1,
OriginatorID: "0000000610",
FileCreationNum: 1,
},
CreationDate: Ptr(time.Date(2023, time.May, 18, 0, 0, 0, 0, time.UTC)),
DestinationDataCenterNo: 61210,
DirectClearerCommunicationArea: "hello",
CurrencyCode: "CAD",
},
},
"Invalid header record length": {
input: "C123",
expectReadErr: true,
},
"Invalid header record type": {
input: "C0000000010000000610000002313861210 CAD",
validationErr: true,
},
"Missing originator id": {
input: "A000000001 000002313861210 CAD",
validationErr: true,
},
"Missing creation date": {
input: "A00000000100000006100001 61210hello CAD",
validationErr: true,
},
"Invalid file creation num": {
input: "A0000000010000000610000002313861210 CAD",
validationErr: true,
},
"Invalid currency code": {
input: "A00000000100000006100001023138612210 AAA",
validationErr: true,
},
}
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
r := require.New(t)
header := FileHeader{}
err := header.parse(tc.input)
if tc.expectReadErr {
r.Error(err)
return
}
err = header.Validate()
if tc.validationErr {
r.Error(err)
} else {
r.NoError(err)
r.Equal(tc.expected.RecordType, header.RecordType)
r.Equal(tc.expected.recordCount, header.recordCount)
r.Equal(tc.expected.OriginatorID, header.OriginatorID)
r.Equal(tc.expected.FileCreationNum, header.FileCreationNum)
r.Equal(tc.expected.CreationDate, header.CreationDate)
r.Equal(tc.expected.DestinationDataCenterNo, header.DestinationDataCenterNo)
r.Equal(tc.expected.DirectClearerCommunicationArea, header.DirectClearerCommunicationArea)
r.Equal(tc.expected.CurrencyCode, header.CurrencyCode)
}
})
}
}
func TestCreateHeaderRecord(t *testing.T) {
type testCase struct {
header *FileHeader
noError bool
expectedSerializedOutput string
debug bool
}
date := time.Date(2023, 8, 29, 0, 0, 0, 0, time.UTC)
cases := map[string]testCase{
"valid header": {
header: NewFileHeader("0000000610", 1, &date, 61210, "CAD"),
expectedSerializedOutput: "A0000000010000000610000102324161210 CAD" + strings.Repeat(" ", 1406),
noError: true,
},
"missing originator id": {
header: NewFileHeader("", 1, &date, 1, "CAD", WithDirectClearerCommunicationArea("Samplecomm")),
debug: true,
},
"invalid file creation num": {
header: NewFileHeader("0000000001", 0, &date, 1, "CAD", WithDirectClearerCommunicationArea("Samplecomm")),
},
"missing file creation date": {
header: NewFileHeader("0000000001", 1, nil, 1, "CAD", WithDirectClearerCommunicationArea("Samplecomm")),
},
"invalid currency code": {
header: NewFileHeader("0000000001", 1, &date, 1, "ABC", WithDirectClearerCommunicationArea("Samplecomm")),
},
"originator id not correct length": {
header: NewFileHeader("000", 1, &date, 1, "CAD", WithDirectClearerCommunicationArea("Samplecomm")),
},
"originator id not alphanumeric": {
header: NewFileHeader("000000000!", 1, &date, 1, "CAD", WithDirectClearerCommunicationArea("Samplecomm")),
},
"direct clearer communcation area too long": {
header: NewFileHeader("0000000001", 1, &date, 0, "CAD", WithDirectClearerCommunicationArea("aaaaaaaaaaaaaaaaaaaaaaaaaaa")),
},
}
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
r := require.New(t)
err := tc.header.Validate()
if tc.noError {
r.NoError(err)
output, buildErr := tc.header.buildHeader(0)
r.NoError(buildErr)
r.Equal(tc.expectedSerializedOutput, output)
} else {
r.Error(err)
}
})
}
}