-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathparser_test.go
More file actions
213 lines (195 loc) · 4.61 KB
/
parser_test.go
File metadata and controls
213 lines (195 loc) · 4.61 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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package simpleSqlParser
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestSelect(t *testing.T) {
tests := []struct {
s string
expectedStmt SelectStatement
}{
{
s: `SELECT foo FROM k1.tbl1; `,
expectedStmt: SelectStatement{
Keyspace: "k1",
TableName: "tbl1",
Columns: []string{"foo"},
},
},
{
s: `Select * from k2.tbl;`,
expectedStmt: SelectStatement{
Keyspace: "k2",
TableName: "tbl",
AllColumns: true,
},
},
{
s: `Select (col1, col2 , col3 ) from k1.tbl;`,
expectedStmt: SelectStatement{
Keyspace: "k1",
TableName: "tbl",
Columns: []string{"col1", "col2", "col3"},
},
},
{
s: `Select * from k1.tbl where x=10;`,
expectedStmt: SelectStatement{
Keyspace: "k1",
TableName: "tbl",
AllColumns: true,
WhereColumns: []string{"x"},
WhereValues: []string{"10"},
Operators: []string{"="},
},
},
{
s: `Select * from k1.tbl where x>10 and y=abc;`,
expectedStmt: SelectStatement{
Keyspace: "k1",
TableName: "tbl",
AllColumns: true,
WhereColumns: []string{"x", "y"},
WhereValues: []string{"10", "abc"},
Operators: []string{">", "="},
},
},
{
s: `Select * from k1.tbl where x>=10 and y < abc and z= 3.122 limit 5 ;`,
expectedStmt: SelectStatement{
Keyspace: "k1",
TableName: "tbl",
AllColumns: true,
WhereColumns: []string{"x", "y", "z"},
WhereValues: []string{"10", "abc", "3.122"},
Operators: []string{">=", "<", "="},
Limit: int(5),
},
},
}
for i, eachTest := range tests {
got := &SQL{Buffer: eachTest.s}
got.Init()
if err := got.Parse(); assert.Nil(t, err, fmt.Sprintf("Test Case %d", i)) {
got.Execute()
assert.Equal(t, eachTest.expectedStmt, got.SelectStatement, fmt.Sprintf("Test Case %d ", i))
}
}
}
func TestInsert(t *testing.T) {
tests := []struct {
s string
expectedStmt InsertStatement
}{
{
s: `Insert into k2.instaTbl values ( 1, "a",1.123, "d" );`,
expectedStmt: InsertStatement{
Keyspace: "k2",
TableName: "instaTbl",
Values: []string{"1", "a", "1.123", "d"},
},
},
{
s: `INSERT INTO k1.insertTable(a,b,c)values("a",23434, 23.533536);`,
expectedStmt: InsertStatement{
Keyspace: "k1",
TableName: "insertTable",
Values: []string{"a", "23434", "23.533536"},
Columns: []string{"a", "b", "c"},
},
},
{
s: `INSERT INTO k1.kaboo(a,b,c) VALUES (1);`,
},
}
for i, eachTest := range tests {
got := &SQL{Buffer: eachTest.s}
got.Init()
if err := got.Parse(); assert.Nil(t, err) {
got.Execute()
assert.Equal(t, eachTest.expectedStmt, got.InsertStatement, fmt.Sprintf("Test Case %d ", i))
}
}
}
func TestCreate(t *testing.T) {
tests := []struct {
s string
expectedStmt CreateStatement
}{
{
s: ` Create Table keyspace.TableName( col1 , col2, col3 ); `,
expectedStmt: CreateStatement{
Keyspace: "keyspace",
TableName: "TableName",
Columns: []string{"col1", "col2", "col3"},
PartitioningKey: "col1",
},
},
}
for i, eachTest := range tests {
got := &SQL{Buffer: eachTest.s}
got.Init()
if err := got.Parse(); assert.Nil(t, err) {
got.Execute()
assert.Equal(t, got.CreateStatement, eachTest.expectedStmt, fmt.Sprint("Test Case %d ", i))
}
}
}
func TestDrop(t *testing.T) {
tests := []struct {
s string
expectedStmt DropStatement
}{
{
s: `Drop keyspace.Tbl;`,
expectedStmt: DropStatement{
Keyspace: "keyspace",
TableName: "Tbl",
},
},
}
for i, eachTest := range tests {
got := &SQL{Buffer: eachTest.s}
got.Init()
if err := got.Parse(); assert.Nil(t, err) {
got.Execute()
assert.Equal(t, got.DropStatement, eachTest.expectedStmt, fmt.Sprint("Test Case %d ", i))
}
}
}
func TestDelete(t *testing.T) {
tests := []struct {
s string
expectedStmt DeleteStatement
}{
{
s: `Delete from k1.tbl where x=10;`,
expectedStmt: DeleteStatement{
Keyspace: "k1",
TableName: "tbl",
WhereColumn: "x",
WhereValue: "10",
Operator: "=",
},
},
{
s: `Delete from k1.tbl where x>=10;`,
expectedStmt: DeleteStatement{
Keyspace: "k1",
TableName: "tbl",
WhereColumn: "x",
WhereValue: "10",
Operator: ">=",
},
},
}
for i, eachTest := range tests {
got := &SQL{Buffer: eachTest.s}
got.Init()
if err := got.Parse(); assert.Nil(t, err, fmt.Sprintf("Test Case %d", i)) {
got.Execute()
assert.Equal(t, eachTest.expectedStmt, got.DeleteStatement, fmt.Sprintf("Test Case %d ", i))
}
}
}