-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers_test.go
301 lines (275 loc) · 7.25 KB
/
helpers_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
package edx12
import (
"context"
"os"
"path/filepath"
"strings"
"testing"
)
// failOnErr is a helper function that takes the result of a function that
// only has 1 return value (error), and fails the test if the error is not nil.
// It's intended to reduce boilerplate code in tests.
func failOnErr(t *testing.T, err error) {
t.Helper()
if err != nil {
t.Errorf("%v", err)
}
}
// replaceNewlines replaces `\r` and `\n` in the given text, so test assets
// can remain somewhat human-readable (one segment per line) without having
// the actual segment terminator set as a newline
func replaceNewlines(t *testing.T, text []byte) string {
t.Helper()
var replacer = strings.NewReplacer(
"\r\n", "",
"\r", "",
"\n", "",
)
withoutNewlines := replacer.Replace(string(text))
return withoutNewlines
}
func assertEqual[V comparable](t *testing.T, val V, expected V) {
t.Helper()
if val != expected {
t.Errorf(
"expected:\n%#v\n\ngot:\n%#v",
expected,
val,
)
}
}
func assertSlicesEqual[V comparable](t *testing.T, value []V, expected []V) {
t.Helper()
if len(value) != len(expected) {
t.Fatalf("expected %d elements, got %d", len(expected), len(value))
}
for i, v := range value {
if v != expected[i] {
t.Errorf(
"index %d: expected:\n%#v\n\ngot:\n%#v",
i,
expected,
value,
)
}
}
}
// x835Message test fixture data is from:
// https://x12.org/examples/005010x221/example-02-multiple-claims-single-check
// (with slight modification, as the example had an error in a DTM segment)
func x835Message(t *testing.T) []byte {
t.Helper()
file, err := os.ReadFile("testdata/835.txt")
assertNoError(t, err)
return file
}
// x271Message test fixture data is from:
// https://x12.org/examples/005010x279/example-1b-response-generic-request-clinic-patients-subscriber-eligibility
func x271Message(t *testing.T) []byte {
t.Helper()
file, err := os.ReadFile("testdata/271.txt")
assertNoError(t, err)
return file
}
// newSegment creates a new segment from a string, without a spec attached.
// It will split the string using the given elementSeparator, and further
// split any elements that contain the repetitionSeparator into
// RepeatElementNode nodes, and CompositeNode for elements
// containing the componentElementSeparator. All others will be
// added as ElementNode nodes.
func newSegment(
t *testing.T,
s string,
elementSeparator string,
repetitionSeparator string,
componentElementSeparator string,
) *X12Node {
t.Helper()
elements := strings.Split(s, elementSeparator)
segmentId := elements[0]
seg, e := NewNode(SegmentNode, segmentId)
assertNoError(t, e)
for _, v := range elements[1:] {
if repetitionSeparator != "" && strings.Contains(
v,
repetitionSeparator,
) {
subElems := strings.Split(v, repetitionSeparator)
repNode, e := NewNode(RepeatElementNode, "", subElems...)
if e != nil {
t.Fatalf("%v", e)
}
e = seg.append(repNode)
if e != nil {
t.Fatalf("%v", e)
}
} else if componentElementSeparator != "" && strings.Contains(
v,
componentElementSeparator,
) {
subElems := strings.Split(v, componentElementSeparator)
cmpNode, e := NewNode(CompositeNode, "")
if e != nil {
t.Fatalf("%v", e)
}
for elemInd := 0; elemInd < len(subElems); elemInd++ {
subNode, err := NewNode(ElementNode, "", subElems[elemInd])
if err != nil {
t.Fatalf("%v", err)
}
err = cmpNode.append(subNode)
if err != nil {
t.Fatalf("append error: %v", err)
}
}
e = seg.append(cmpNode)
if e != nil {
t.Fatalf("append error: %v", e)
}
} else {
elemNode, e := NewNode(ElementNode, "", v)
if e != nil {
t.Fatalf("%v", e)
}
e = seg.append(elemNode)
if e != nil {
t.Fatalf("%v", e)
}
}
}
return seg
}
func x271Spec(t *testing.T) *X12TransactionSetSpec {
t.Helper()
return X271v005010X279A1
}
// x271MessageUnmatchedSegment is the same as x271Message, but is missing
// the required NM1 (subscriber) name segment from loop 2000C
func x271MessageUnmatchedSegment(t *testing.T) []byte {
t.Helper()
file, err := os.ReadFile("testdata/271_unmatched_segment.txt")
if err != nil {
t.Fatalf(
"unable to open file %s",
"testdata/271_unmatched_segment.txt",
)
}
return file
}
func fileContent(t *testing.T, filename string) []byte {
t.Helper()
filePath := filepath.Join("testdata", filename)
file, err := os.ReadFile(filePath)
if err != nil {
t.Fatalf(
"unable to open file %s",
filePath,
)
}
return file
}
// x271Missing2100C is the same as x271Message, but is missing
// the required 2100C loop
func x271Missing2100C(t *testing.T) []byte {
t.Helper()
file, err := os.ReadFile("testdata/271_missing_2100C.txt")
if err != nil {
t.Fatalf(
"unable to open file %s",
"testdata/271_missing_2100C.txt",
)
}
return file
}
// x271Broken is the same as x271Message, but is missing
// the required 2100C loop
func x271Broken(t *testing.T) []byte {
t.Helper()
file, err := os.ReadFile("testdata/271_broken.txt")
if err != nil {
t.Fatalf(
"unable to open file %s",
"testdata/271_broken.txt",
)
}
return file
}
// x270MissingReceiverName is the same as x270Message, but is missing
// the receiver name (NM103 in 2100B)
func x270MissingReceiverName(t *testing.T) []byte {
t.Helper()
file, err := os.ReadFile("testdata/270_missing_receiver_name.txt")
if err != nil {
t.Fatalf(
"unable to open file %s",
"testdata/270_missing_receiver_name.txt",
)
}
return file
}
// x270MessageMismatchedControlNumbers is the same as x270Message, but
// with all mismatched envelope control numbers, including:
// - ST02/SE02
// - GS05/GE02
// - ISA13/IEA02
func x271MessageMismatchedControlNumbers(t *testing.T) []byte {
t.Helper()
filename := "testdata/271_mismatched_control_numbers.txt"
file, err := os.ReadFile(filename)
if err != nil {
t.Fatalf("unable to open file %s", filename)
}
return file
}
func assertErrorNotNil(t *testing.T, err error) {
t.Helper()
if err == nil {
t.Fatalf("expected error, got nil")
}
}
func assertNotNil(t *testing.T, val interface{}) {
t.Helper()
if val == nil {
t.Fatalf("expected non-nil value, got nil")
}
}
func assertSliceContains[V comparable](t *testing.T, row []V, expected V) {
t.Helper()
if !sliceContains(row, expected) {
t.Errorf("expected %v to be in slice %v", expected, row)
}
}
func assertNoError(t *testing.T, err error) {
t.Helper()
if err != nil {
t.Errorf("expected no error, got: %v", err)
}
}
// unmarshalText calls UnmarshalText and fails the test if there's an error,
// to reduce boilerplate.
func unmarshalText(t *testing.T, messageText []byte) (msg *Message) {
t.Helper()
rawMessage, err := Read(messageText)
assertNoError(t, err)
msg, err = rawMessage.Message(context.Background())
assertNoError(t, err)
return msg
}
// x270Message fixture contents are from:
// https://x12.org/examples/005010x279/example-1a-generic-request-clinic-patients-subscriber-eligibility
func x270Message(t *testing.T) []byte {
t.Helper()
file, err := os.ReadFile("testdata/270.txt")
assertNoError(t, err)
return file
}
func x270Spec(t *testing.T) *X12TransactionSetSpec {
t.Helper()
return X270v005010X279A1
}
func assertStringContains(t *testing.T, s string, contains string) {
t.Helper()
if !strings.Contains(s, contains) {
t.Errorf("expected: %q\nto contain: %q", s, contains)
}
}