forked from jf-tech/omniparser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transform_test.go
162 lines (143 loc) · 4.38 KB
/
transform_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
package omniparser
import (
"errors"
"fmt"
"io"
"testing"
"github.com/stretchr/testify/assert"
"github.com/jf-tech/omniparser/errs"
"github.com/jf-tech/omniparser/schemahandler"
)
type testReadCall struct {
result []byte
err error
}
func (trc testReadCall) Checksum() string {
if trc.err != nil {
panic("Checksum() called when err != nil")
}
return fmt.Sprintf("checksum of raw record of '%s'", string(trc.result))
}
func (trc testReadCall) Raw() interface{} {
if trc.err != nil {
panic("Raw() called when err != nil")
}
return fmt.Sprintf("raw record of '%s'", string(trc.result))
}
type testIngester struct {
readCalled int
readCalls []testReadCall
continuableErrs map[error]bool
}
func (g *testIngester) Read() (schemahandler.RawRecord, []byte, error) {
if g.readCalled >= len(g.readCalls) {
panic(fmt.Sprintf("Read() called %d time(s), but not enough mock entries setup", g.readCalled))
}
r := g.readCalls[g.readCalled]
g.readCalled++
return r, r.result, r.err
}
func (g *testIngester) IsContinuableError(err error) bool {
_, found := g.continuableErrs[err]
return found
}
func (g *testIngester) FmtErr(format string, args ...interface{}) error {
return errors.New("ctx formatted: " + fmt.Sprintf(format, args...))
}
func TestTransform_Read_EndWithEOF(t *testing.T) {
continuableErr1 := errors.New("continuable error 1")
tfm := &transform{
ingester: &testIngester{
readCalls: []testReadCall{
{result: []byte("1st good read")},
{err: continuableErr1},
{result: []byte("2nd good read")},
{err: io.EOF},
},
continuableErrs: map[error]bool{continuableErr1: true},
},
}
record, err := tfm.Read()
assert.NoError(t, err)
assert.Equal(t, "1st good read", string(record))
raw, err := tfm.RawRecord()
assert.NoError(t, err)
assert.Equal(t, "raw record of '1st good read'", raw.Raw())
assert.Equal(t, "checksum of raw record of '1st good read'", raw.Checksum())
record, err = tfm.Read()
assert.Error(t, err)
assert.True(t, errs.IsErrTransformFailed(err))
assert.Equal(t, continuableErr1.Error(), err.Error())
assert.Nil(t, record)
raw, err = tfm.RawRecord()
assert.Error(t, err)
assert.True(t, errs.IsErrTransformFailed(err))
assert.Nil(t, raw)
record, err = tfm.Read()
assert.NoError(t, err)
assert.Equal(t, "2nd good read", string(record))
raw, err = tfm.RawRecord()
assert.NoError(t, err)
assert.Equal(t, "raw record of '2nd good read'", raw.Raw())
assert.Equal(t, "checksum of raw record of '2nd good read'", raw.Checksum())
record, err = tfm.Read()
assert.Error(t, err)
assert.Equal(t, io.EOF, err)
assert.Nil(t, record)
raw, err = tfm.RawRecord()
assert.Error(t, err)
assert.Equal(t, io.EOF, err)
assert.Nil(t, raw)
// Verifying when EOF is reached, repeatedly calling Next will still get you EOF.
record, err = tfm.Read()
assert.Error(t, err)
assert.Equal(t, io.EOF, err)
assert.Nil(t, record)
raw, err = tfm.RawRecord()
assert.Error(t, err)
assert.Equal(t, io.EOF, err)
assert.Nil(t, raw)
}
func TestTransform_Read_EndWithNonContinuableError(t *testing.T) {
tfm := &transform{
ingester: &testIngester{
readCalls: []testReadCall{
{result: []byte("1st good read")},
{err: errors.New("fatal error")},
},
},
}
record, err := tfm.Read()
assert.NoError(t, err)
assert.Equal(t, "1st good read", string(record))
raw, err := tfm.RawRecord()
assert.NoError(t, err)
assert.Equal(t, "raw record of '1st good read'", raw.Raw())
assert.Equal(t, "checksum of raw record of '1st good read'", raw.Checksum())
record, err = tfm.Read()
assert.Error(t, err)
assert.False(t, errs.IsErrTransformFailed(err))
assert.Equal(t, "fatal error", err.Error())
assert.Nil(t, record)
raw, err = tfm.RawRecord()
assert.Error(t, err)
assert.False(t, errs.IsErrTransformFailed(err))
assert.Equal(t, "fatal error", err.Error())
assert.Nil(t, raw)
// Verifying when fatal error occurred, repeatedly calling Next/Read will still get you the same err
record, err = tfm.Read()
assert.Error(t, err)
assert.Equal(t, "fatal error", err.Error())
assert.Nil(t, record)
raw, err = tfm.RawRecord()
assert.Error(t, err)
assert.Equal(t, "fatal error", err.Error())
assert.Nil(t, raw)
}
func TestTransform_RawRecord_CalledBeforeRead(t *testing.T) {
tfm := &transform{ingester: &testIngester{readCalls: []testReadCall{}}}
raw, err := tfm.RawRecord()
assert.Error(t, err)
assert.Equal(t, "must call Read first", err.Error())
assert.Nil(t, raw)
}