forked from azoner/gox12
-
Notifications
You must be signed in to change notification settings - Fork 0
/
x12path_test.go
362 lines (333 loc) · 12.5 KB
/
x12path_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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package gox12
import (
//"fmt"
"testing"
)
func BenchmarkSegmentParseFormatIdentity(b *testing.B) {
for i := 0; i < b.N; i++ {
fpath, _ := ParseX12Path("/2000A/2000B/2300/2400/SV2[421]01")
_ = fpath.String()
}
}
func BenchmarkRefDes(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = ParseX12Path("N1[372]02-5")
}
}
func BenchmarkRelativePath(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = ParseX12Path("1000A/1000B/TST[AA]02")
}
}
func TestSegmentParseFormatIdentity(t *testing.T) {
paths := [...]string{
"/2000A/2000B/2300/2400/SV2",
"/2000A/2000B/2300/2400/SV201",
"/2000A/2000B/2300/2400/SV2[421]01",
}
for _, p := range paths {
//fmt.Println(p)
fpath, err := ParseX12Path(p)
if err != nil {
t.Errorf("Didn't get a value for [%s]", p)
}
//fmt.Println(fpath)
actual := fpath.String()
if actual != p {
t.Errorf("Didn't get expected result [%s], instead got [%s]", p, actual)
}
}
}
func TestRefDes(t *testing.T) {
var tests = []struct {
spath string
seg_id string
qual string
eleidx int
subeleidx int
}{
{"TST", "TST", "", 0, 0},
{"TST02", "TST", "", 2, 0},
{"TST03-2", "TST", "", 3, 2},
{"TST[AA]02", "TST", "AA", 2, 0},
{"TST[1B5]03-1", "TST", "1B5", 3, 1},
{"03", "", "", 3, 0},
{"03-2", "", "", 3, 2},
{"N102", "N1", "", 2, 0},
{"N102-5", "N1", "", 2, 5},
{"N1[AZR]02", "N1", "AZR", 2, 0},
{"N1[372]02-5", "N1", "372", 2, 5},
}
for _, tt := range tests {
actual, err := ParseX12Path(tt.spath)
if err != nil {
t.Errorf("Didn't get a value for [%s]", tt.spath)
}
if actual.SegmentId != tt.seg_id {
t.Errorf("Didn't get expected result [%s], instead got [%s]", tt.seg_id, actual.SegmentId)
}
if actual.IdValue != tt.qual {
t.Errorf("Didn't get expected result [%s], instead got [%s]", tt.qual, actual.IdValue)
}
if actual.ElementIdx != tt.eleidx {
t.Errorf("Didn't get expected result [%s], instead got [%s]", tt.eleidx, actual.ElementIdx)
}
if actual.SubelementIdx != tt.subeleidx {
t.Errorf("Didn't get expected result [%s], instead got [%s]", tt.subeleidx, actual.SubelementIdx)
}
if len(actual.Path) != 0 {
t.Errorf("Path is not empty")
}
path := actual.String()
if path != tt.spath {
t.Errorf("Didn't get expected result [%s], instead got [%s]", tt.spath, path)
}
}
}
func stringSliceEquals(a, b []string) bool {
if len(a) != len(b) {
return false
}
for i, v := range a {
if v != b[i] {
return false
}
}
return true
}
func TestRefDesMatchingNone(t *testing.T) {
tpath := "/ISA_LOOP/GS_LOOP/ST_LOOP/DETAIL/2000A/2000B/2300/2400"
actual, err := ParseX12Path(tpath)
if err != nil {
t.Errorf("Didn't get a value for [%s], error is [%s]", tpath, err.Error())
}
apath := actual.String()
if apath != tpath {
t.Errorf("Didn't get expected result [%s], instead got [%s]", tpath, apath)
}
if actual.SegmentId != "" {
t.Errorf("Didn't get expected result [%s], instead got [%s]", "", actual.SegmentId)
}
}
func TestRefDesMatchingOk(t *testing.T) {
tpath := "/ISA_LOOP/GS_LOOP/ST_LOOP/DETAIL/2000A/2000B/2300/2400/SV2"
actual, err := ParseX12Path(tpath)
if err != nil {
t.Errorf("Didn't get a value for [%s]", tpath)
}
apath := actual.String()
if apath != tpath {
t.Errorf("Didn't get expected result [%s], instead got [%s]", tpath, apath)
}
if actual.SegmentId != "SV2" {
t.Errorf("Didn't get expected result [%s], instead got [%s]", "SV2", actual.SegmentId)
}
}
func TestBadPaths(t *testing.T) {
var tests = []struct {
spath string
}{
{"AAA/03"},
{"BB/CC/03-2"},
{"/AAA/03"},
{"/BB/CC/03-2"},
}
for _, tt := range tests {
actual, err := ParseX12Path(tt.spath)
if err == nil {
t.Errorf("Path should be invalid Didn't get a value for [%s]", actual)
}
}
}
func TestX12PathGeneralNoSegment(t *testing.T) {
var tests = []struct {
spath string
x12Path X12Path
}{
{"ISA_LOOP/GS_LOOP", X12Path{Path: "ISA_LOOP/GS_LOOP"}},
{"GS_LOOP", X12Path{Path: "GS_LOOP"}},
{"ST_LOOP/DETAIL/2000", X12Path{Path: "ST_LOOP/DETAIL/2000"}},
{"GS_LOOP/ST_LOOP/DETAIL/2000A", X12Path{Path: "GS_LOOP/ST_LOOP/DETAIL/2000A"}},
{"DETAIL/2000A/2000B", X12Path{Path: "DETAIL/2000A/2000B"}},
{"2000A/2000B/2300", X12Path{Path: "2000A/2000B/2300"}},
{"2000B/2300/2400", X12Path{Path: "2000B/2300/2400"}},
{"ST_LOOP/HEADER", X12Path{Path: "ST_LOOP/HEADER"}},
{"ISA_LOOP/GS_LOOP/ST_LOOP/HEADER/1000A", X12Path{Path: "ISA_LOOP/GS_LOOP/ST_LOOP/HEADER/1000A"}},
{"GS_LOOP/ST_LOOP/HEADER/1000B", X12Path{Path: "GS_LOOP/ST_LOOP/HEADER/1000B"}},
{"/ISA_LOOP/GS_LOOP", X12Path{Path: "/ISA_LOOP/GS_LOOP"}},
{"/ISA_LOOP/GS_LOOP/ST_LOOP/DETAIL/2000", X12Path{Path: "/ISA_LOOP/GS_LOOP/ST_LOOP/DETAIL/2000"}},
{"/ISA_LOOP/GS_LOOP/ST_LOOP/DETAIL/2000A", X12Path{Path: "/ISA_LOOP/GS_LOOP/ST_LOOP/DETAIL/2000A"}},
{"/ISA_LOOP/GS_LOOP/ST_LOOP/DETAIL/2000A/2000B", X12Path{Path: "/ISA_LOOP/GS_LOOP/ST_LOOP/DETAIL/2000A/2000B"}},
{"/ISA_LOOP/GS_LOOP/ST_LOOP/DETAIL/2000A/2000B/2300", X12Path{Path: "/ISA_LOOP/GS_LOOP/ST_LOOP/DETAIL/2000A/2000B/2300"}},
{"/ISA_LOOP/GS_LOOP/ST_LOOP/DETAIL/2000A/2000B/2300/2400", X12Path{Path: "/ISA_LOOP/GS_LOOP/ST_LOOP/DETAIL/2000A/2000B/2300/2400"}},
{"/ISA_LOOP/GS_LOOP/ST_LOOP/HEADER", X12Path{Path: "/ISA_LOOP/GS_LOOP/ST_LOOP/HEADER"}},
{"/ISA_LOOP/GS_LOOP/ST_LOOP/HEADER/1000A", X12Path{Path: "/ISA_LOOP/GS_LOOP/ST_LOOP/HEADER/1000A"}},
{"/ISA_LOOP/GS_LOOP/ST_LOOP/HEADER/1000B", X12Path{Path: "/ISA_LOOP/GS_LOOP/ST_LOOP/HEADER/1000B"}},
}
for _, tt := range tests {
actual, err := ParseX12Path(tt.spath)
if err != nil {
t.Errorf("Didn't get a value for [%s]", tt.spath)
}
if actual.IsAbs() != tt.x12Path.IsAbs() {
t.Errorf("[%s] was not relative", tt.spath)
}
if actual.SegmentId != "" {
t.Errorf("Didn't get expected result [%s], instead got [%s]", "", actual.SegmentId)
}
if actual.IdValue != "" {
t.Errorf("Didn't get expected result [%s], instead got [%s]", "", actual.IdValue)
}
if actual.ElementIdx != 0 {
t.Errorf("Didn't get expected result [%s], instead got [%s]", 0, actual.ElementIdx)
}
if actual.SubelementIdx != 0 {
t.Errorf("Didn't get expected result [%s], instead got [%s]", 0, actual.SubelementIdx)
}
if actual.Path != tt.x12Path.Path {
t.Errorf("Path: Didn't get expected result [%s], instead got [%s]", tt.x12Path.Path, actual.Path)
}
path := actual.String()
if path != tt.x12Path.String() {
t.Errorf("String Didn't get expected result [%s], instead got [%s]", tt.x12Path.String(), path)
}
}
}
func TestX12PathGeneral(t *testing.T) {
var tests = []struct {
spath string
x12Path X12Path
}{
{"/AAA/TST", X12Path{"/AAA", "TST", "", 0, 0}},
{"/B1000/TST02", X12Path{"/B1000", "TST", "", 2, 0}},
{"/1000B/TST03-2", X12Path{"/1000B", "TST", "", 3, 2}},
{"/1000A/1000B/TST[AA]02", X12Path{"/1000A/1000B", "TST", "AA", 2, 0}},
{"/AA/BB/CC/TST[1B5]03-1", X12Path{"/AA/BB/CC", "TST", "1B5", 3, 1}},
{"/DDD/E1000/N102", X12Path{"/DDD/E1000", "N1", "", 2, 0}},
{"/E1000/D322/N102-5", X12Path{"/E1000/D322", "N1", "", 2, 5}},
{"/BB/CC/N1[AZR]02", X12Path{"/BB/CC", "N1", "AZR", 2, 0}},
{"/BB/CC/N1[372]02-5", X12Path{"/BB/CC", "N1", "372", 2, 5}},
{"AAA/TST", X12Path{"AAA", "TST", "", 0, 0}},
{"B1000/TST02", X12Path{"B1000", "TST", "", 2, 0}},
{"1000B/TST03-2", X12Path{"1000B", "TST", "", 3, 2}},
{"1000A/1000B/TST[AA]02", X12Path{"1000A/1000B", "TST", "AA", 2, 0}},
{"AA/BB/CC/TST[1B5]03-1", X12Path{"AA/BB/CC", "TST", "1B5", 3, 1}},
{"DDD/E1000/N102", X12Path{"DDD/E1000", "N1", "", 2, 0}},
{"E1000/D322/N102-5", X12Path{"E1000/D322", "N1", "", 2, 5}},
{"BB/CC/N1[AZR]02", X12Path{"BB/CC", "N1", "AZR", 2, 0}},
{"BB/CC/N1[372]02-5", X12Path{"BB/CC", "N1", "372", 2, 5}},
}
for _, tt := range tests {
actual, err := ParseX12Path(tt.spath)
if err != nil {
t.Errorf("Didn't get a value for [%s]", tt.spath)
}
if actual.IsAbs() != tt.x12Path.IsAbs() {
t.Errorf("[%s] was not relative", tt.spath)
}
if actual.SegmentId != tt.x12Path.SegmentId {
t.Errorf("Didn't get expected result [%s], instead got [%s]", tt.x12Path.SegmentId, actual.SegmentId)
}
if actual.IdValue != tt.x12Path.IdValue {
t.Errorf("Didn't get expected result [%s], instead got [%s]", tt.x12Path.IdValue, actual.IdValue)
}
if actual.ElementIdx != tt.x12Path.ElementIdx {
t.Errorf("Didn't get expected result [%s], instead got [%s]", tt.x12Path.ElementIdx, actual.ElementIdx)
}
if actual.SubelementIdx != tt.x12Path.SubelementIdx {
t.Errorf("Didn't get expected result [%s], instead got [%s]", tt.x12Path.SubelementIdx, actual.SubelementIdx)
}
if actual.Path != tt.x12Path.Path {
t.Errorf("Path: Didn't get expected result [%s], instead got [%s]", tt.x12Path.Path, actual.Path)
}
path := actual.String()
if path != tt.x12Path.String() {
t.Errorf("String Didn't get expected result [%s], instead got [%s]", tt.x12Path.String(), path)
}
}
}
/*
class Equality(unittest.TestCase):
def test_equal1(self):
p1 = pyx12.path.X12Path("/ISA_LOOP/GS_LOOP/ST_LOOP/DETAIL/2000A")
p2 = pyx12.path.X12Path("/ISA_LOOP/GS_LOOP/ST_LOOP/DETAIL/2000A")
self.assertEqual(p1, p2)
self.assertEqual(p1.format(), p2.format())
self.assertEqual(p1.__hash__(), p2.__hash__())
def test_equal2(self):
p1 = pyx12.path.X12Path("/ISA_LOOP/GS_LOOP/ST_LOOP/DETAIL/2000A")
p2 = pyx12.path.X12Path("/ISA_LOOP/GS_LOOP/ST_LOOP/DETAIL/")
p2.loop_list.append("2000A")
self.assertEqual(p1, p2)
self.assertEqual(p1.format(), p2.format())
self.assertEqual(p1.__hash__(), p2.__hash__())
def test_equal3(self):
p1 = pyx12.path.X12Path("/AA/BB/CC/TST[1B5]03-1")
p2 = pyx12.path.X12Path("/AA/BB/CC/AAA[1B5]03-1")
p2.seg_id = "TST"
self.assertEqual(p1, p2)
self.assertEqual(p1.format(), p2.format())
self.assertEqual(p1.__hash__(), p2.__hash__())
def test_equal4(self):
p1 = pyx12.path.X12Path("1000B/TST03-2")
p2 = pyx12.path.X12Path("1000B/TST04-2")
p2.ele_idx = 3
self.assertEqual(p1, p2)
self.assertEqual(p1.format(), p2.format())
self.assertEqual(p1.__hash__(), p2.__hash__())
class NonEquality(unittest.TestCase):
def test_nequal1(self):
p1 = pyx12.path.X12Path("/ISA_LOOP/GS_LOOP/ST_LOOP/DETAIL/2000A")
p2 = pyx12.path.X12Path("ISA_LOOP/GS_LOOP/ST_LOOP/DETAIL/2000A")
self.assertNotEqual(p1, p2)
self.assertNotEqual(p1.format(), p2.format())
self.assertNotEqual(p1.__hash__(), p2.__hash__())
def test_nequal2(self):
p1 = pyx12.path.X12Path("/ISA_LOOP/GS_LOOP/ST_LOOP/DETAIL/2000A")
p2 = pyx12.path.X12Path("/ISA_LOOP/GS_LOOP/ST_LOOP/DETAIL/")
self.assertNotEqual(p1, p2)
self.assertNotEqual(p1.format(), p2.format())
self.assertNotEqual(p1.__hash__(), p2.__hash__())
def test_nequal3(self):
p1 = pyx12.path.X12Path("/AA/BB/CC/TST[1B5]03-1")
p2 = pyx12.path.X12Path("/AA/BB/CC/AAA[1B5]03-1")
self.assertNotEqual(p1, p2)
self.assertNotEqual(p1.format(), p2.format())
self.assertNotEqual(p1.__hash__(), p2.__hash__())
def test_nequal4(self):
p1 = pyx12.path.X12Path("1000B/TST03-2")
p2 = pyx12.path.X12Path("1000B/TST04-2")
self.assertNotEqual(p1, p2)
self.assertNotEqual(p1.format(), p2.format())
self.assertNotEqual(p1.__hash__(), p2.__hash__())
class Empty(unittest.TestCase):
def test_not_empty_1(self):
p1 = "1000B/TST03-2"
self.assertFalse(pyx12.path.X12Path(
p1).empty(), "Path "%s" is not empty" % (p1))
def test_not_empty_2(self):
p1 = "/AA/BB/CC/AAA[1B5]03"
self.assertFalse(pyx12.path.X12Path(
p1).empty(), "Path "%s" is not empty" % (p1))
def test_not_empty_3(self):
p1 = "GS_LOOP/ST_LOOP/DETAIL/2000A"
self.assertFalse(pyx12.path.X12Path(
p1).empty(), "Path "%s" is not empty" % (p1))
def test_not_empty_4(self):
p1 = "/ISA_LOOP/GS_LOOP/ST_LOOP/DETAIL/2000A"
self.assertFalse(pyx12.path.X12Path(
p1).empty(), "Path "%s" is not empty" % (p1))
def test_not_empty_5(self):
p1 = "/"
self.assertFalse(pyx12.path.X12Path(
p1).empty(), "Path "%s" is not empty" % (p1))
def test_not_empty_6(self):
p1 = "/ISA_LOOP/GS_LOOP/ST_LOOP/DETAIL/2000A/"
self.assertFalse(pyx12.path.X12Path(
p1).empty(), "Path "%s" is not empty" % (p1))
def test_empty_1(self):
p1 = ""
a = pyx12.path.X12Path(p1)
self.assertTrue(pyx12.path.X12Path(
p1).empty(), "Path "%s" is empty" % (p1))
*/