forked from rqlite/sql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwalk_test.go
More file actions
122 lines (99 loc) · 2.48 KB
/
walk_test.go
File metadata and controls
122 lines (99 loc) · 2.48 KB
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
package sql_test
import (
"reflect"
"testing"
"github.com/TcMits/sql"
)
func Test_Walk(t *testing.T) {
s := `WITH derived AS (
SELECT MAX(a) AS max_a,
COUNT(b) AS b_num,
user_id
FROM table_name
GROUP BY user_id
)
SELECT * FROM table_name
LEFT JOIN derived USING (user_id)`
stmt, err := sql.ParseStmtString(s)
if err != nil {
t.Fatal(err)
}
expected := [...]reflect.Type{
reflect.TypeOf(&sql.SelectStatement{}),
// WITH derived
reflect.TypeOf(&sql.WithClause{}),
reflect.TypeOf(&sql.CTE{}),
reflect.TypeOf(&sql.Ident{}),
reflect.TypeOf(&sql.SelectStatement{}),
// max(a) as max_a
reflect.TypeOf(&sql.ResultColumn{}),
reflect.TypeOf(&sql.Call{}),
reflect.TypeOf(&sql.QualifiedName{}),
reflect.TypeOf(&sql.Ident{}),
reflect.TypeOf(&sql.FunctionArg{}),
reflect.TypeOf(&sql.Ident{}),
reflect.TypeOf(&sql.Ident{}),
// count(b) AS b_num
reflect.TypeOf(&sql.ResultColumn{}),
reflect.TypeOf(&sql.Call{}),
reflect.TypeOf(&sql.QualifiedName{}),
reflect.TypeOf(&sql.Ident{}),
reflect.TypeOf(&sql.FunctionArg{}),
reflect.TypeOf(&sql.Ident{}),
reflect.TypeOf(&sql.Ident{}),
// user_id
reflect.TypeOf(&sql.ResultColumn{}),
reflect.TypeOf(&sql.Ident{}),
// FROM table_name
reflect.TypeOf(&sql.QualifiedName{}),
reflect.TypeOf(&sql.Ident{}),
// GROUP BY user_id
reflect.TypeOf(&sql.Ident{}),
// SELECT *
reflect.TypeOf(&sql.ResultColumn{}),
reflect.TypeOf(&sql.JoinClause{}),
// FROM table_name
reflect.TypeOf(&sql.QualifiedName{}),
reflect.TypeOf(&sql.Ident{}),
// LEFT JOIN
reflect.TypeOf(&sql.JoinOperator{}),
// derived
reflect.TypeOf(&sql.QualifiedName{}),
reflect.TypeOf(&sql.Ident{}),
// USING (user_id)
reflect.TypeOf(&sql.UsingConstraint{}),
reflect.TypeOf(&sql.Ident{}),
}
i := 0
yield := func(n sql.Node) bool {
v := reflect.ValueOf(n)
if v.Type() != expected[i] {
t.Fatalf("expected type %s, got %s %v %v at index %d", expected[i], v.Type(), v.IsZero(), v.IsNil(), i)
}
i++
return true
}
sql.Walk(stmt, yield)
if i != len(expected) {
t.Errorf("expected %d nodes, got %d", len(expected), i)
}
}
func Benchmark_Walk(b *testing.B) {
s := `WITH derived AS (
SELECT MAX(a) AS max_a,
COUNT(b) AS b_num,
user_id
FROM table_name
GROUP BY user_id
)
SELECT * FROM table_name
LEFT JOIN derived USING (user_id)`
stmt, err := sql.ParseStmtString(s)
if err != nil {
b.Fatal(err)
}
yield := func(n sql.Node) bool { return true }
for b.Loop() {
sql.Walk(stmt, yield)
}
}