-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathplugins_test.go
97 lines (82 loc) · 1.91 KB
/
plugins_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
package vfilter
import (
"context"
"testing"
"github.com/Velocidex/ordereddict"
"www.velocidex.com/golang/vfilter/types"
"www.velocidex.com/golang/vfilter/utils"
)
type execPluginTest struct {
query string
result []Row
}
var execPluginTests = []execPluginTest{
execPluginTest{
query: "select * from test_plugin() where foo.bar < 2",
result: []Row{
ordereddict.NewDict().
Set("foo", ordereddict.NewDict().Set("bar", 1)).
Set("foo_2", 2).
Set("foo_3", 3),
},
},
execPluginTest{
query: ("select foo.bar as column1, foo.bar from " +
"test_plugin() where foo.bar = 2"),
result: []Row{
ordereddict.NewDict().
Set("column1", 2).
Set("foo.bar", 2),
},
},
}
// Implement some test plugins for testing.
type TestGeneratorPlugin struct{}
func (self TestGeneratorPlugin) Call(
ctx context.Context,
scope types.Scope,
args *ordereddict.Dict) <-chan Row {
output_chan := make(chan Row)
go func() {
defer close(output_chan)
for i := 1; i < 10; i++ {
row := ordereddict.NewDict().
Set("foo", ordereddict.NewDict().Set("bar", i)).
Set("foo_2", i*2).
Set("foo_3", i*3)
output_chan <- row
}
}()
return output_chan
}
func (self TestGeneratorPlugin) Info(scope types.Scope, type_map *TypeMap) *PluginInfo {
return &PluginInfo{
Name: "test_plugin",
}
}
func TestPlugins(t *testing.T) {
scope := NewScope().AppendPlugins(TestGeneratorPlugin{})
for _, test := range execPluginTests {
sql, err := Parse(test.query)
if err != nil {
t.Fatalf("Failed to parse %v: %v", test.query, err)
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
input := sql.Eval(ctx, scope)
var result []Row
for {
row, ok := <-input
if !ok {
break
}
result = append(result, row)
}
if !scope.Eq(result, test.result) {
utils.Debug(scope)
utils.Debug(result)
utils.Debug(test.result)
t.Fatalf("Query %v Failed.", test.query)
}
}
}