forked from btnguyen2k/godynamo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstmt_index_parsing_test.go
277 lines (268 loc) · 7.55 KB
/
stmt_index_parsing_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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package godynamo
import (
"fmt"
"reflect"
"testing"
"github.com/aws/aws-sdk-go-v2/aws"
)
func TestStmtDescribeLSI_parse(t *testing.T) {
testName := "TestStmtDescribeLSI_parse"
testData := []struct {
name string
sql string
expected *StmtDescribeLSI
mustError bool
}{
{
name: "basic",
sql: "DESCRIBE LSI idxname ON tblname",
expected: &StmtDescribeLSI{tableName: "tblname", indexName: "idxname"},
},
{
name: "no_table",
sql: "DESCRIBE LSI idxname",
mustError: true,
},
}
for _, testCase := range testData {
t.Run(testCase.name, func(t *testing.T) {
s, err := parseQuery(nil, testCase.sql)
if testCase.mustError && err == nil {
t.Fatalf("%s failed: parsing must fail", testName+"/"+testCase.name)
}
if testCase.mustError {
return
}
if err != nil {
t.Fatalf("%s failed: %s", testName+"/"+testCase.name, err)
}
stmt, ok := s.(*StmtDescribeLSI)
if !ok {
t.Fatalf("%s failed: expected StmtDescribeLSI but received %T", testName+"/"+testCase.name, stmt)
}
stmt.Stmt = nil
if !reflect.DeepEqual(stmt, testCase.expected) {
t.Fatalf("%s failed:\nexpected %#v\nreceived %#v", testName+"/"+testCase.name, testCase.expected, stmt)
}
})
}
}
func TestStmtCreateGSI_parse(t *testing.T) {
testName := "TestStmtCreateGSI_parse"
testData := []struct {
name string
sql string
expected *StmtCreateGSI
mustError bool
}{
{
name: "no_table",
sql: "CREATE GSI abc ON WITH pk=id:string",
mustError: true,
},
{
name: "no_index_name",
sql: "CREATE GSI ON table WITH pk=id:string",
mustError: true,
},
{
name: "no_pk",
sql: "CREATE GSI index ON table",
mustError: true,
},
{
name: "invalid_rcu",
sql: "CREATE GSI index ON table WITH pk=id:string WITH RCU=-1",
mustError: true,
},
{
name: "invalid_wcu",
sql: "CREATE GSI index ON table WITH pk=id:string WITH wcu=-1",
mustError: true,
},
{
name: "invalid_pk_type",
sql: "CREATE GSI index ON table WITH pk=id:int",
mustError: true,
},
{
name: "invalid_sk_type",
sql: "CREATE GSI index ON table WITH pk=id:string WITH sk=grade:int",
mustError: true,
},
{
name: "basic",
sql: "CREATE GSI index ON table WITH pk=id:string",
expected: &StmtCreateGSI{tableName: "table", indexName: "index", pkName: "id", pkType: "STRING"},
},
{
name: "with_sk",
sql: "CREATE GSI index ON table WITH pk=id:string with SK=grade:binary WITH projection=*",
expected: &StmtCreateGSI{tableName: "table", indexName: "index", pkName: "id", pkType: "STRING", skName: aws.String("grade"), skType: aws.String("BINARY"), projectedAttrs: "*"},
},
{
name: "with_rcu_wcu",
sql: "CREATE GSI IF NOT EXISTS index ON table WITH pk=id:number, with WCU=1 WITH rcu=0 WITH projection=a,b,c",
expected: &StmtCreateGSI{tableName: "table", indexName: "index", ifNotExists: true, pkName: "id", pkType: "NUMBER", wcu: aws.Int64(1), rcu: aws.Int64(0), projectedAttrs: "a,b,c"},
},
}
for _, testCase := range testData {
t.Run(testCase.name, func(t *testing.T) {
s, err := parseQuery(nil, testCase.sql)
if testCase.mustError && err == nil {
t.Fatalf("%s failed: parsing must fail", testName+"/"+testCase.name)
}
if testCase.mustError {
return
}
if err != nil {
fmt.Printf("[DEBUG] %s\n", testCase.sql)
t.Fatalf("%s failed: %s", testName+"/"+testCase.name, err)
}
stmt, ok := s.(*StmtCreateGSI)
if !ok {
t.Fatalf("%s failed: expected StmtCreateGSI but received %T", testName+"/"+testCase.name, s)
}
stmt.Stmt = nil
stmt.withOptsStr = ""
if !reflect.DeepEqual(stmt, testCase.expected) {
t.Fatalf("%s failed:\nexpected %#v\nreceived %#v", testName+"/"+testCase.name, testCase.expected, stmt)
}
})
}
}
func TestStmtDescribeGSI_parse(t *testing.T) {
testName := "TestStmtDescribeGSI_parse"
testData := []struct {
name string
sql string
expected *StmtDescribeGSI
mustError bool
}{
{
name: "basic",
sql: "DESCRIBE GSI idxname ON tblname",
expected: &StmtDescribeGSI{tableName: "tblname", indexName: "idxname"},
},
{
name: "no_table",
sql: "DESCRIBE LSI idxname",
mustError: true,
},
}
for _, testCase := range testData {
t.Run(testCase.name, func(t *testing.T) {
s, err := parseQuery(nil, testCase.sql)
if testCase.mustError && err == nil {
t.Fatalf("%s failed: parsing must fail", testName+"/"+testCase.name)
}
if testCase.mustError {
return
}
if err != nil {
t.Fatalf("%s failed: %s", testName+"/"+testCase.name, err)
}
stmt, ok := s.(*StmtDescribeGSI)
if !ok {
t.Fatalf("%s failed: expected StmtDescribeGSI but received %T", testName+"/"+testCase.name, stmt)
}
stmt.Stmt = nil
if !reflect.DeepEqual(stmt, testCase.expected) {
t.Fatalf("%s failed:\nexpected %#v\nreceived %#v", testName+"/"+testCase.name, testCase.expected, stmt)
}
})
}
}
func TestStmtAlterGSI_parse(t *testing.T) {
testName := "TestStmtAlterGSI_parse"
testData := []struct {
name string
sql string
expected *StmtAlterGSI
mustError bool
}{
{
name: "no_table",
sql: "ALTER GSI abc ON WITH wcu=1 WITH rcu=2",
mustError: true,
},
{
name: "no_index_name",
sql: "ALTER GSI ON table WITH wcu=1 WITH rcu=2",
mustError: true,
},
{
name: "invalid_rcu",
sql: "ALTER GSI index ON table WITH RCU=-1",
mustError: true,
},
{
name: "invalid_wcu",
sql: "ALTER GSI index ON table WITH wcu=-1",
mustError: true,
},
{
name: "basic",
sql: "ALTER GSI index ON table WITH wcu=1 WITH rcu=2",
expected: &StmtAlterGSI{tableName: "table", indexName: "index", wcu: aws.Int64(1), rcu: aws.Int64(2)},
},
}
for _, testCase := range testData {
t.Run(testCase.name, func(t *testing.T) {
s, err := parseQuery(nil, testCase.sql)
if testCase.mustError && err == nil {
t.Fatalf("%s failed: parsing must fail", testName+"/"+testCase.name)
}
if testCase.mustError {
return
}
if err != nil {
t.Fatalf("%s failed: %s", testName+"/"+testCase.name, err)
}
stmt, ok := s.(*StmtAlterGSI)
if !ok {
t.Fatalf("%s failed: expected StmtAlterGSI but received %T", testName+"/"+testCase.name, s)
}
stmt.Stmt = nil
stmt.withOptsStr = ""
if !reflect.DeepEqual(stmt, testCase.expected) {
t.Fatalf("%s failed:\nexpected %#v\nreceived %#v", testName+"/"+testCase.name, testCase.expected, stmt)
}
})
}
}
func TestStmtDropGSI_parse(t *testing.T) {
testName := "TestStmtDropGSI_parse"
testData := []struct {
name string
sql string
expected *StmtDropGSI
}{
{
name: "basic",
sql: "DROP GSI index ON table",
expected: &StmtDropGSI{tableName: "table", indexName: "index"},
},
{
name: "if_exists",
sql: "DROP GSI IF EXISTS index ON table",
expected: &StmtDropGSI{tableName: "table", indexName: "index", ifExists: true},
},
}
for _, testCase := range testData {
t.Run(testCase.name, func(t *testing.T) {
s, err := parseQuery(nil, testCase.sql)
if err != nil {
t.Fatalf("%s failed: %s", testName+"/"+testCase.name, err)
}
stmt, ok := s.(*StmtDropGSI)
if !ok {
t.Fatalf("%s failed: expected StmtDropGSI but received %T", testName+"/"+testCase.name, s)
}
stmt.Stmt = nil
if !reflect.DeepEqual(stmt, testCase.expected) {
t.Fatalf("%s failed:\nexpected %#v\nreceived %#v", testName+"/"+testCase.name, testCase.expected, stmt)
}
})
}
}