-
-
Notifications
You must be signed in to change notification settings - Fork 74
/
edi_test.go
173 lines (153 loc) · 3.78 KB
/
edi_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
package edi
import (
"bytes"
"encoding/json"
"io"
"io/ioutil"
"os"
"testing"
"github.com/bradleyjkemp/cupaloy"
"github.com/jf-tech/go-corelib/jsons"
"github.com/stretchr/testify/assert"
"github.com/jf-tech/omniparser"
"github.com/jf-tech/omniparser/extensions/omniv21/fileformat/edi"
"github.com/jf-tech/omniparser/extensions/omniv21/samples"
"github.com/jf-tech/omniparser/transformctx"
)
type testCase struct {
schemaFile string
inputFile string
schema omniparser.Schema
input []byte
}
const (
test1_CanadaPost_EDI_214 = iota
test2_UPS_EDI_210
test3_X12_834
)
var tests = []testCase{
{
// test1_CanadaPost_EDI_214
schemaFile: "./1_canadapost_edi_214.schema.json",
inputFile: "./1_canadapost_edi_214.input.txt",
},
{
// test2_UPS_EDI_210
schemaFile: "./2_ups_edi_210.schema.json",
inputFile: "./2_ups_edi_210.input.txt",
},
{
// test3_X12_834
schemaFile: "./3_x12_834.schema.json",
inputFile: "./3_x12_834.input.txt",
},
}
func init() {
for i := range tests {
schema, err := ioutil.ReadFile(tests[i].schemaFile)
if err != nil {
panic(err)
}
tests[i].schema, err = omniparser.NewSchema("bench", bytes.NewReader(schema))
if err != nil {
panic(err)
}
tests[i].input, err = ioutil.ReadFile(tests[i].inputFile)
if err != nil {
panic(err)
}
}
}
func (tst testCase) doTest(t *testing.T) {
cupaloy.SnapshotT(t, jsons.BPJ(samples.SampleTestCommon(t, tst.schemaFile, tst.inputFile)))
}
func (tst testCase) doBenchmark(b *testing.B) {
for i := 0; i < b.N; i++ {
transform, err := tst.schema.NewTransform(
"bench", bytes.NewReader(tst.input), &transformctx.Ctx{})
if err != nil {
b.FailNow()
}
for {
_, err = transform.Read()
if err == io.EOF {
break
}
if err != nil {
b.FailNow()
}
}
}
}
func Test1_CanadaPost_EDI_214(t *testing.T) {
tests[test1_CanadaPost_EDI_214].doTest(t)
}
func Test2_UPS_EDI_210(t *testing.T) {
tests[test2_UPS_EDI_210].doTest(t)
}
func Test3_X12_834(t *testing.T) {
tests[test3_X12_834].doTest(t)
}
func Test3_NonValidatingReader(t *testing.T) {
schemaFileReader, err := os.Open("./2_ups_edi_210.schema.json")
assert.NoError(t, err)
defer schemaFileReader.Close()
inputFileReader, err := os.Open("./2_ups_edi_210.input.txt")
assert.NoError(t, err)
defer inputFileReader.Close()
schemaContent, err := ioutil.ReadAll(schemaFileReader)
assert.NoError(t, err)
type ediSchema struct {
FileDecl *edi.FileDecl `json:"file_declaration"`
}
var schema ediSchema
err = json.Unmarshal(schemaContent, &schema)
assert.NoError(t, err)
type rawElem struct {
ElemIndex int
CompIndex int
Data string
}
type rawSeg struct {
Name string
Raw string
Elems []rawElem
}
r := edi.NewNonValidatingReader(inputFileReader, schema.FileDecl)
var rawSegs []rawSeg
for {
seg, err := r.Read()
if err == io.EOF {
break
}
assert.NoError(t, err)
rawSegs = append(rawSegs, rawSeg{
Name: seg.Name,
Raw: string(seg.Raw),
Elems: func() []rawElem {
var elems []rawElem
for _, e := range seg.Elems {
elems = append(elems, rawElem{
ElemIndex: e.ElemIndex,
CompIndex: e.CompIndex,
Data: string(e.Data),
})
}
return elems
}(),
})
}
cupaloy.SnapshotT(t, jsons.BPM(rawSegs))
}
// Benchmark1_CanadaPost_EDI_214-8 1627 752143 ns/op 398667 B/op 9401 allocs/op
func Benchmark1_CanadaPost_EDI_214(b *testing.B) {
tests[test1_CanadaPost_EDI_214].doBenchmark(b)
}
// Benchmark2_UPS_EDI_210-8 201 6012683 ns/op 3213400 B/op 79062 allocs/op
func Benchmark2_UPS_EDI_210(b *testing.B) {
tests[test2_UPS_EDI_210].doBenchmark(b)
}
// Benchmark3_X12_834-8 4438 259565 ns/op 77049 B/op 2179 allocs/op
func Benchmark3_X12_834(b *testing.B) {
tests[test3_X12_834].doBenchmark(b)
}