forked from zhenjl/sqlparser
-
Notifications
You must be signed in to change notification settings - Fork 1
/
parse_test.go
170 lines (158 loc) · 4.45 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
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
// 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"
"io/ioutil"
"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))
}
}
}
// TestFullFileParse will test to make sure we can handle files with multiple sql statments, and comments.
func TestFullFilePassParse(t *testing.T) {
// the test case are the files in this directory. The expected file is located in the expected
// directory.
for _, name := range glob("sqlparser_test/full_file/pass/*.sql") {
sql, err := ioutil.ReadFile(name)
if err != nil {
t.Errorf("Skipping test %v, got error trying to load it: %v", name, err)
continue
}
expectedName := "sqlparser_test/full_file/pass/expected/" + filepath.Base(name)
esql, err := ioutil.ReadFile(expectedName)
if err != nil {
t.Errorf("Skipping test %v, got error trying to load expected file(%v): %v", name, expectedName, err)
continue
}
tree, err := Parse(string(sql))
if err != nil {
t.Errorf("Failed test %v: Got error: %v", err)
}
out := String(tree)
if out != string(esql) {
sesql := string(esql)
t.Errorf("Failed test %v:\nexpected(%v):\n[%v]\ngot(%v):\n[%v]\n", name, len(sesql), sesql, len(out), out)
fmt.Printf("Failed test %v:\nexpected(%v):\n[%v]\ngot(%v):\n[%v]\n", name, len(sesql), sesql, len(out), out)
}
}
}
// TestFullFileParse will test to make sure we can handle files with multiple sql statments, and comments.
func TestFullFileFailParse(t *testing.T) {
// the test case are the files in this directory. The expected file is located in the expected
// directory.
for _, name := range glob("sqlparser_test/full_file/fail/*.sql") {
sql, err := ioutil.ReadFile(name)
if err != nil {
t.Errorf("Skipping test %v, got error trying to load it: %v", name, err)
continue
}
/*
expectedName := "sqlparser_test/full_file/fail/expected/" + filepath.Base(name)
esql, err := ioutil.ReadFile(expectedName)
if err != nil {
t.Errorf("Skipping test %v, got error trying to load expected file(%v): %v", name, expectedName, err)
continue
}
*/
tree, err := Parse(string(sql))
if err == nil {
out := String(tree)
t.Errorf("Failed test %v , expected error Got(%v)\n%v", name, len(out), out)
}
t.Logf("Got expected error (%v)", err)
}
}
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
}