forked from influxdata/flux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
result_test.go
232 lines (219 loc) · 5.76 KB
/
result_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
package flux_test
import (
"errors"
"fmt"
"io"
"strings"
"testing"
"github.com/andreyvit/diff"
"github.com/influxdata/flux"
"github.com/influxdata/flux/execute"
"github.com/influxdata/flux/execute/executetest"
"github.com/influxdata/flux/iocounter"
"github.com/influxdata/flux/mock"
"github.com/influxdata/flux/semantic"
)
// TestColumnType tests that the column type gets returned from a semantic type correctly.
func TestColumnType(t *testing.T) {
for _, tt := range []struct {
typ semantic.Type
want flux.ColType
}{
{typ: semantic.String, want: flux.TString},
{typ: semantic.Int, want: flux.TInt},
{typ: semantic.UInt, want: flux.TUInt},
{typ: semantic.Float, want: flux.TFloat},
{typ: semantic.Bool, want: flux.TBool},
{typ: semantic.Time, want: flux.TTime},
{typ: semantic.Duration, want: flux.TInvalid},
{typ: semantic.Regexp, want: flux.TInvalid},
{typ: semantic.NewArrayType(semantic.String), want: flux.TInvalid},
{typ: semantic.NewObjectType(map[string]semantic.Type{
"foo": semantic.String,
}), want: flux.TInvalid},
{typ: semantic.NewFunctionType(semantic.FunctionSignature{}), want: flux.TInvalid},
} {
t.Run(fmt.Sprint(tt.typ), func(t *testing.T) {
if want, got := tt.want, flux.ColumnType(tt.typ); want != got {
t.Fatalf("unexpected type -want/+got\n\t- %s\n\t+ %s", want, got)
}
})
}
}
// ResultLineEncoder is a simple line encoder to encode the results.
type ResultLineEncoder struct {
testing.TB
}
func (enc *ResultLineEncoder) Encode(w io.Writer, result flux.Result) (int64, error) {
wc := &iocounter.Writer{Writer: w}
err := result.Tables().Do(func(tbl flux.Table) error {
return tbl.Do(func(cr flux.ColReader) error {
for i, n := 0, cr.Len(); i < n; i++ {
values := make([]string, len(cr.Cols()))
for j, col := range cr.Cols() {
v := execute.ValueForRow(cr, i, j)
values[j] = fmt.Sprintf("%s=%v", col.Label, v)
}
_, _ = fmt.Fprintf(wc, "result(%s): %s\n", result.Name(), strings.Join(values, " "))
}
return nil
})
})
return wc.Count(), err
}
func (enc *ResultLineEncoder) EncodeError(w io.Writer, err error) error {
_, _ = fmt.Fprintf(w, "error: %s\n", err.Error())
return nil
}
func TestDelimitedMultiResultEncoder_Encode(t *testing.T) {
for _, tt := range []struct {
name string
results func() flux.ResultIterator
want string
wantErr string
}{
{
name: "SingleResult",
results: func() flux.ResultIterator {
return flux.NewSliceResultIterator(
[]flux.Result{
&executetest.Result{
Nm: "success",
Tbls: []*executetest.Table{
{
ColMeta: []flux.ColMeta{
{Label: "_time", Type: flux.TTime},
{Label: "_value", Type: flux.TFloat},
},
Data: [][]interface{}{
{execute.Time(0), 2.0},
},
},
},
},
},
)
},
want: `result(success): _time=1970-01-01T00:00:00.000000000Z _value=2
`,
},
{
name: "MultipleResults",
results: func() flux.ResultIterator {
return flux.NewSliceResultIterator(
[]flux.Result{
&executetest.Result{
Nm: "first",
Tbls: []*executetest.Table{
{
ColMeta: []flux.ColMeta{
{Label: "_time", Type: flux.TTime},
{Label: "_value", Type: flux.TFloat},
},
Data: [][]interface{}{
{execute.Time(0), 2.0},
},
},
},
},
&executetest.Result{
Nm: "second",
Tbls: []*executetest.Table{
{
ColMeta: []flux.ColMeta{
{Label: "_time", Type: flux.TTime},
{Label: "_value", Type: flux.TFloat},
},
Data: [][]interface{}{
{execute.Time(0), 3.0},
},
},
},
},
},
)
},
want: `result(first): _time=1970-01-01T00:00:00.000000000Z _value=2
result(second): _time=1970-01-01T00:00:00.000000000Z _value=3
`,
},
{
name: "QueryError",
results: func() flux.ResultIterator {
results := make(chan flux.Result)
close(results)
q := &mock.Query{
ResultsCh: results,
}
q.SetErr(errors.New("expected error"))
return flux.NewResultIteratorFromQuery(q)
},
wantErr: "expected error",
},
{
name: "ResultError",
results: func() flux.ResultIterator {
return flux.NewSliceResultIterator(
[]flux.Result{
&executetest.Result{
Nm: "test",
Err: errors.New("expected error"),
},
},
)
},
wantErr: "expected error",
},
{
name: "ResultErrorOnSecondResult",
results: func() flux.ResultIterator {
return flux.NewSliceResultIterator(
[]flux.Result{
&executetest.Result{
Nm: "success",
Tbls: []*executetest.Table{
{
ColMeta: []flux.ColMeta{
{Label: "_time", Type: flux.TTime},
{Label: "_value", Type: flux.TFloat},
},
Data: [][]interface{}{
{execute.Time(0), 2.0},
},
},
},
},
&executetest.Result{
Nm: "error",
Err: errors.New("expected error"),
},
},
)
},
want: `result(success): _time=1970-01-01T00:00:00.000000000Z _value=2
error: expected error
`,
},
} {
t.Run(tt.name, func(t *testing.T) {
results := tt.results()
enc := &flux.DelimitedMultiResultEncoder{
Delimiter: []byte("\n"),
Encoder: &ResultLineEncoder{TB: t},
}
var got strings.Builder
if _, err := enc.Encode(&got, results); err != nil {
if tt.wantErr != "" {
if got, want := err.Error(), tt.wantErr; got != want {
t.Fatalf("unexpected error -want/+got:\n\t- %v\n\t+ %v", got, want)
}
return
}
t.Fatalf("unexpected error: %v", err)
}
if got, want := got.String(), tt.want; got != want {
t.Fatalf("unexpected output -want/+got\n%s", diff.LineDiff(want, got))
}
})
}
}