-
Notifications
You must be signed in to change notification settings - Fork 76
/
parser_test.go
113 lines (98 loc) · 3.62 KB
/
parser_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
package parser
import (
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestParser(t *testing.T) {
var p *Parser
Convey("SQL type", t, func() {
p = &Parser{}
typ := p.GetSQLType("SELECT * FROM foo WHERE id < 3")
So(typ, ShouldEqual, SELECT)
p = &Parser{}
typ = p.GetSQLType("INSERT INTO foo VALUES (1,2,3)")
So(typ, ShouldEqual, INSERT)
p = &Parser{}
typ = p.GetSQLType("UPSERT INTO foo VALUES (1,2,3)")
So(typ, ShouldEqual, UNSUPPORTED)
})
}
func TestParserSelect(t *testing.T) {
var p *Parser
Convey("SELECT SQL", t, func() {
p = &Parser{}
ast, err := p.ParseSelect("SELECT ab,b, c FROM foo WHERE id < 3")
So(err, ShouldBeNil)
So(ast.Projects, ShouldResemble, []string{"ab", "b", "c"})
So(ast.Table, ShouldEqual, "foo")
p = &Parser{}
ast, err = p.ParseSelect("SELECT ab,b, c FROM foo LIMIT 3")
So(err, ShouldBeNil)
So(ast.Projects, ShouldResemble, []string{"ab", "b", "c"})
So(ast.Table, ShouldEqual, "foo")
So(ast.Limit, ShouldEqual, 3)
p = &Parser{}
ast, err = p.ParseSelect("SELECT ab,b,c FROM foo WHERE id < 3 AND ab > 10 LIMIT 11")
So(err, ShouldBeNil)
So(ast.Projects, ShouldResemble, []string{"ab", "b", "c"})
So(ast.Where, ShouldResemble, []string{"id", "<", "3", "AND", "ab", ">", "10"})
So(ast.Table, ShouldEqual, "foo")
So(ast.Limit, ShouldEqual, 11)
p = &Parser{}
ast, err = p.ParseSelect("SELECT 1")
So(err, ShouldBeNil)
So(ast.Projects, ShouldResemble, []string{"1"})
So(ast.Table, ShouldEqual, "")
})
}
func TestParserInsert(t *testing.T) {
var p *Parser
Convey("INSERT SQL with Column names", t, func() {
p = &Parser{}
ast, err := p.ParseInsert("INSERT INTO table_name(column1, column2) VALUES (value1, value2)")
So(err, ShouldBeNil)
So(ast.Table, ShouldEqual, "table_name")
So(ast.Columns, ShouldResemble, []string{"column1", "column2"})
So(ast.Values, ShouldResemble, [][]string{{"value1", "value2"}})
})
Convey("column count miss match", t, func() {
p = &Parser{}
_, err := p.ParseInsert("INSERT INTO table_name(column1, column2, column3) VALUES (value1, value2)")
So(err, ShouldNotBeNil)
So(err.Error(), ShouldContainSubstring, "expected column count")
})
Convey("INSERT SQL", t, func() {
p = &Parser{}
ast, err := p.ParseInsert("INSERT INTO table_name VALUES (value1, value2)")
So(err, ShouldBeNil)
So(ast.Table, ShouldEqual, "table_name")
So(ast.Columns, ShouldBeNil)
So(ast.Values, ShouldResemble, [][]string{{"value1", "value2"}})
})
Convey("INSERT multiple rows", t, func() {
p = &Parser{}
ast, err := p.ParseInsert("INSERT INTO table_name VALUES (\"value1\", value2), (\"value3\", value4)")
So(err, ShouldBeNil)
So(ast.Table, ShouldEqual, "table_name")
So(ast.Columns, ShouldBeNil)
So(ast.Values, ShouldResemble, [][]string{{"\"value1\"", "value2"}, {"\"value3\"", "value4"}})
})
Convey("INSERT multiple rows 2", t, func() {
p = &Parser{}
ast, err := p.ParseInsert("INSERT INTO table (id, username, email) VALUES " +
"(0, auxten, \"[email protected]\")," +
"(1, hahaha, \"[email protected]\")," +
"(2, jijiji, \"[email protected]\")")
So(err, ShouldBeNil)
So(ast.Table, ShouldEqual, "table")
So(ast.Columns, ShouldResemble, []string{"id", "username", "email"})
So(ast.Values, ShouldResemble, [][]string{{"0", "auxten", "\"[email protected]\""},
{"1", "hahaha", "\"[email protected]\""}, {"2", "jijiji", "\"[email protected]\""}})
})
Convey("column count miss match 2", t, func() {
p = &Parser{}
_, err := p.ParseInsert("INSERT INTO table_name VALUES (value1, value2), (value3, value4, value5)")
So(err, ShouldNotBeNil)
So(err.Error(), ShouldContainSubstring, "expected column count")
})
}