-
Notifications
You must be signed in to change notification settings - Fork 14
/
parse_test.go
113 lines (103 loc) · 2.37 KB
/
parse_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
// Copyright 2012, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package sqlparser
import (
"bufio"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"testing"
)
func TestGen(t *testing.T) {
_, err := Parse("select :a from a where a in (:b)")
if err != nil {
t.Error(err)
}
}
func TestParse(t *testing.T) {
for tcase := range iterateFiles("sqlparser_test/*.sql") {
if tcase.output == "" {
tcase.output = tcase.input
}
tree, err := Parse(tcase.input)
var out string
if err != nil {
out = err.Error()
} else {
out = String(tree)
}
if out != tcase.output {
t.Error(fmt.Sprintf("File:%s Line:%v\n%q\n%q", tcase.file, tcase.lineno, tcase.output, out))
}
}
}
func BenchmarkParse1(b *testing.B) {
sql := "select 'abcd', 20, 30.0, eid from a where 1=eid and name='3'"
for i := 0; i < b.N; i++ {
_, err := Parse(sql)
if err != nil {
b.Fatal(err)
}
}
}
func BenchmarkParse2(b *testing.B) {
sql := "select aaaa, bbb, ccc, ddd, eeee, ffff, gggg, hhhh, iiii from tttt, ttt1, ttt3 where aaaa = bbbb and bbbb = cccc and dddd+1 = eeee group by fff, gggg having hhhh = iiii and iiii = jjjj order by kkkk, llll limit 3, 4"
for i := 0; i < b.N; i++ {
_, err := Parse(sql)
if err != nil {
b.Fatal(err)
}
}
}
type testCase struct {
file string
lineno int
input string
output string
}
func iterateFiles(pattern string) (testCaseIterator chan testCase) {
names := glob(pattern)
testCaseIterator = make(chan testCase)
go func() {
defer close(testCaseIterator)
for _, name := range names {
fd, err := os.OpenFile(name, os.O_RDONLY, 0)
if err != nil {
panic(fmt.Sprintf("Could not open file %s", name))
}
r := bufio.NewReader(fd)
lineno := 0
for {
line, err := r.ReadString('\n')
lines := strings.Split(strings.TrimRight(line, "\n"), "#")
lineno++
if err != nil {
if err != io.EOF {
panic(fmt.Sprintf("Error reading file %s: %s", name, err.Error()))
}
break
}
input := lines[0]
output := ""
if len(lines) > 1 {
output = lines[1]
}
if input == "" {
continue
}
testCaseIterator <- testCase{name, lineno, input, output}
}
}
}()
return testCaseIterator
}
func glob(pattern string) []string {
out, err := filepath.Glob(pattern)
if err != nil {
panic(err)
}
return out
}