-
Notifications
You must be signed in to change notification settings - Fork 8
/
api_test.go
61 lines (50 loc) · 1.52 KB
/
api_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
package vfilter
import (
"context"
"encoding/json"
"testing"
"github.com/Velocidex/ordereddict"
"github.com/sebdah/goldie/v2"
"github.com/stretchr/testify/assert"
)
func marshal_indent(rows []Row) ([]byte, error) {
return json.MarshalIndent(rows, "", " ")
}
func TestAPIGetResponseChannel(t *testing.T) {
ctx := context.Background()
scope := makeTestScope()
golden := ordereddict.NewDict()
// test() returns 3 rows
vql, err := Parse("SELECT * FROM test()")
assert.NoError(t, err)
// GetResponseChannel streams result sets over a channel.
test_GetResponseChannel := func(name string, vql *VQL, max_rows int) {
payloads := []*VFilterJsonResult{}
serialized := []string{}
for result := range GetResponseChannel(
vql, ctx, scope, marshal_indent,
max_rows, 1000) {
serialized = append(serialized, string(result.Payload))
payloads = append(payloads, result)
}
golden.Set(name, payloads)
golden.Set(name+".Payloads", serialized)
}
// Send all rows in one packet.
test_GetResponseChannel("GetResponseChannel", vql, 1000)
// Send packets of max 1 row (i.e. will send 3 packets)
test_GetResponseChannel("GetResponseChannel_Small", vql, 1)
{
// OutputJSON dumps everything in one big json blob.
serialized, err := OutputJSON(vql, ctx, scope, marshal_indent)
assert.NoError(t, err)
golden.Set("OutputJSON", string(serialized))
}
g := goldie.New(
t,
goldie.WithFixtureDir("fixtures"),
goldie.WithNameSuffix(".golden"),
goldie.WithDiffEngine(goldie.ColoredDiff),
)
g.AssertJson(t, "api", golden)
}