@@ -9,26 +9,41 @@ import (
9
9
"github.com/stretchr/testify/assert"
10
10
11
11
"github.com/jf-tech/omniparser/errs"
12
+ "github.com/jf-tech/omniparser/schemahandler"
12
13
)
13
14
14
15
type testReadCall struct {
15
- record []byte
16
+ result []byte
16
17
err error
17
18
}
18
19
20
+ func (trc testReadCall ) Checksum () string {
21
+ if trc .err != nil {
22
+ panic ("Checksum() called when err != nil" )
23
+ }
24
+ return fmt .Sprintf ("checksum of raw record of '%s'" , string (trc .result ))
25
+ }
26
+
27
+ func (trc testReadCall ) Raw () interface {} {
28
+ if trc .err != nil {
29
+ panic ("Raw() called when err != nil" )
30
+ }
31
+ return fmt .Sprintf ("raw record of '%s'" , string (trc .result ))
32
+ }
33
+
19
34
type testIngester struct {
20
35
readCalled int
21
36
readCalls []testReadCall
22
37
continuableErrs map [error ]bool
23
38
}
24
39
25
- func (g * testIngester ) Read () (interface {} , []byte , error ) {
40
+ func (g * testIngester ) Read () (schemahandler. RawRecord , []byte , error ) {
26
41
if g .readCalled >= len (g .readCalls ) {
27
42
panic (fmt .Sprintf ("Read() called %d time(s), but not enough mock entries setup" , g .readCalled ))
28
43
}
29
44
r := g .readCalls [g .readCalled ]
30
45
g .readCalled ++
31
- return fmt . Sprintf ( "raw record %d" , g . readCalled - 1 ), r . record , r .err
46
+ return r , r . result , r .err
32
47
}
33
48
34
49
func (g * testIngester ) IsContinuableError (err error ) bool {
@@ -45,9 +60,9 @@ func TestTransform_Read_EndWithEOF(t *testing.T) {
45
60
tfm := & transform {
46
61
ingester : & testIngester {
47
62
readCalls : []testReadCall {
48
- {record : []byte ("1st good read" )},
63
+ {result : []byte ("1st good read" )},
49
64
{err : continuableErr1 },
50
- {record : []byte ("2nd good read" )},
65
+ {result : []byte ("2nd good read" )},
51
66
{err : io .EOF },
52
67
},
53
68
continuableErrs : map [error ]bool {continuableErr1 : true },
@@ -56,32 +71,34 @@ func TestTransform_Read_EndWithEOF(t *testing.T) {
56
71
record , err := tfm .Read ()
57
72
assert .NoError (t , err )
58
73
assert .Equal (t , "1st good read" , string (record ))
59
- raw , err := tfm .CurrentRawRecord ()
74
+ raw , err := tfm .RawRecord ()
60
75
assert .NoError (t , err )
61
- assert .Equal (t , "raw record 0" , raw .(string ))
76
+ assert .Equal (t , "raw record of '1st good read'" , raw .Raw ())
77
+ assert .Equal (t , "checksum of raw record of '1st good read'" , raw .Checksum ())
62
78
63
79
record , err = tfm .Read ()
64
80
assert .Error (t , err )
65
81
assert .True (t , errs .IsErrTransformFailed (err ))
66
82
assert .Equal (t , continuableErr1 .Error (), err .Error ())
67
83
assert .Nil (t , record )
68
- raw , err = tfm .CurrentRawRecord ()
84
+ raw , err = tfm .RawRecord ()
69
85
assert .Error (t , err )
70
86
assert .True (t , errs .IsErrTransformFailed (err ))
71
87
assert .Nil (t , raw )
72
88
73
89
record , err = tfm .Read ()
74
90
assert .NoError (t , err )
75
91
assert .Equal (t , "2nd good read" , string (record ))
76
- raw , err = tfm .CurrentRawRecord ()
92
+ raw , err = tfm .RawRecord ()
77
93
assert .NoError (t , err )
78
- assert .Equal (t , "raw record 2" , raw .(string ))
94
+ assert .Equal (t , "raw record of '2nd good read'" , raw .Raw ())
95
+ assert .Equal (t , "checksum of raw record of '2nd good read'" , raw .Checksum ())
79
96
80
97
record , err = tfm .Read ()
81
98
assert .Error (t , err )
82
99
assert .Equal (t , io .EOF , err )
83
100
assert .Nil (t , record )
84
- raw , err = tfm .CurrentRawRecord ()
101
+ raw , err = tfm .RawRecord ()
85
102
assert .Error (t , err )
86
103
assert .Equal (t , io .EOF , err )
87
104
assert .Nil (t , raw )
@@ -91,7 +108,7 @@ func TestTransform_Read_EndWithEOF(t *testing.T) {
91
108
assert .Error (t , err )
92
109
assert .Equal (t , io .EOF , err )
93
110
assert .Nil (t , record )
94
- raw , err = tfm .CurrentRawRecord ()
111
+ raw , err = tfm .RawRecord ()
95
112
assert .Error (t , err )
96
113
assert .Equal (t , io .EOF , err )
97
114
assert .Nil (t , raw )
@@ -101,24 +118,25 @@ func TestTransform_Read_EndWithNonContinuableError(t *testing.T) {
101
118
tfm := & transform {
102
119
ingester : & testIngester {
103
120
readCalls : []testReadCall {
104
- {record : []byte ("1st good read" )},
121
+ {result : []byte ("1st good read" )},
105
122
{err : errors .New ("fatal error" )},
106
123
},
107
124
},
108
125
}
109
126
record , err := tfm .Read ()
110
127
assert .NoError (t , err )
111
128
assert .Equal (t , "1st good read" , string (record ))
112
- raw , err := tfm .CurrentRawRecord ()
129
+ raw , err := tfm .RawRecord ()
113
130
assert .NoError (t , err )
114
- assert .Equal (t , "raw record 0" , raw .(string ))
131
+ assert .Equal (t , "raw record of '1st good read'" , raw .Raw ())
132
+ assert .Equal (t , "checksum of raw record of '1st good read'" , raw .Checksum ())
115
133
116
134
record , err = tfm .Read ()
117
135
assert .Error (t , err )
118
136
assert .False (t , errs .IsErrTransformFailed (err ))
119
137
assert .Equal (t , "fatal error" , err .Error ())
120
138
assert .Nil (t , record )
121
- raw , err = tfm .CurrentRawRecord ()
139
+ raw , err = tfm .RawRecord ()
122
140
assert .Error (t , err )
123
141
assert .False (t , errs .IsErrTransformFailed (err ))
124
142
assert .Equal (t , "fatal error" , err .Error ())
@@ -129,15 +147,15 @@ func TestTransform_Read_EndWithNonContinuableError(t *testing.T) {
129
147
assert .Error (t , err )
130
148
assert .Equal (t , "fatal error" , err .Error ())
131
149
assert .Nil (t , record )
132
- raw , err = tfm .CurrentRawRecord ()
150
+ raw , err = tfm .RawRecord ()
133
151
assert .Error (t , err )
134
152
assert .Equal (t , "fatal error" , err .Error ())
135
153
assert .Nil (t , raw )
136
154
}
137
155
138
- func TestTransform_CurrentRawRecord_CalledBeforeRead (t * testing.T ) {
156
+ func TestTransform_RawRecord_CalledBeforeRead (t * testing.T ) {
139
157
tfm := & transform {ingester : & testIngester {readCalls : []testReadCall {}}}
140
- raw , err := tfm .CurrentRawRecord ()
158
+ raw , err := tfm .RawRecord ()
141
159
assert .Error (t , err )
142
160
assert .Equal (t , "must call Read first" , err .Error ())
143
161
assert .Nil (t , raw )
0 commit comments