-
Notifications
You must be signed in to change notification settings - Fork 7
/
stmt_database_parsing_test.go
169 lines (161 loc) · 6.62 KB
/
stmt_database_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
package gocosmos
import (
"reflect"
"testing"
)
func TestStmtCreateDatabase_parse(t *testing.T) {
testName := "TestStmtCreateDatabase_parse"
testData := []struct {
name string
sql string
expected *StmtCreateDatabase
mustError bool
}{
{name: "error_no_table", sql: "CREATE DATABASE ", mustError: true},
{name: "error_if_not_exists_no_table", sql: "CREATE DATABASE IF NOT EXISTS ", mustError: true},
{name: "error_syntax", sql: "CREATE DATABASE db0 IF NOT EXISTS", mustError: true},
{name: "error_if_exists", sql: "CREATE DATABASE if exists db0", mustError: true},
{name: "error_if_not_exist", sql: "CREATE DATABASE IF NOT EXIST db0", mustError: true},
{name: "error_ru_and_maxru", sql: "CREATE DATABASE db0 with RU=400, WITH MAXru=4000", mustError: true},
{name: "error_invalid_with", sql: "CREATE DATABASE db0 with a", mustError: true},
{name: "error_invalid_with2", sql: "CREATE DATABASE db0 with a=1", mustError: true},
{name: "basic", sql: "CREATE DATABASE db1", expected: &StmtCreateDatabase{dbName: "db1"}},
{name: "with_ru", sql: "create\ndatabase\n db-2 \nWITH \n ru=100", expected: &StmtCreateDatabase{dbName: "db-2", ru: 100}},
{name: "with_max", sql: "CREATE\r\nDATABASE \n \r db_3 \r \n with\n\rmaxru=100", expected: &StmtCreateDatabase{dbName: "db_3", maxru: 100}},
{name: "if_not_exists", sql: "CREATE DATABASE\tIF\rNOT\nEXISTS db-4-0", expected: &StmtCreateDatabase{dbName: "db-4-0", ifNotExists: true}},
{name: "if_not_exists_with_ru", sql: "create\ndatabase IF NOT EXISTS db-5_0 with\nru=100", expected: &StmtCreateDatabase{dbName: "db-5_0", ifNotExists: true, ru: 100}},
{name: "if_not_exists_with_maxru", sql: "CREATE DATABASE if not exists db_6-0 WITH maxru=100", expected: &StmtCreateDatabase{dbName: "db_6-0", ifNotExists: true, maxru: 100}},
}
for _, testCase := range testData {
t.Run(testCase.name, func(t *testing.T) {
s, err := parseQueryWithDefaultDb(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.(*StmtCreateDatabase)
if !ok {
t.Fatalf("%s failed: expected StmtCreateDatabase 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)
}
})
}
}
func TestStmtAlterDatabase_parse(t *testing.T) {
testName := "TestStmtAlterDatabase_parse"
testData := []struct {
name string
sql string
expected *StmtAlterDatabase
mustError bool
}{
{name: "error_no_ru_maxru", sql: "ALTER database db0", mustError: true},
{name: "error_ru_and_maxru", sql: "ALTER database db0 WITH RU=400, WITH maxRU=4000", mustError: true},
{name: "error_invalid_with", sql: "ALTER database db0 WITH RU=400, WITH a", mustError: true},
{name: "error_invalid_with2", sql: "ALTER database db0 WITH RU=400 WITH a=1", mustError: true},
{name: "with_ru", sql: "ALTER\rdatabase\ndb1\tWITH ru=400", expected: &StmtAlterDatabase{dbName: "db1", ru: 400}},
{name: "with_maxru", sql: "alter DATABASE db-1 with maxru=4000", expected: &StmtAlterDatabase{dbName: "db-1", maxru: 4000}},
}
for _, testCase := range testData {
t.Run(testCase.name, func(t *testing.T) {
s, err := parseQueryWithDefaultDb(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.(*StmtAlterDatabase)
if !ok {
t.Fatalf("%s failed: expected StmtAlterDatabase 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)
}
})
}
}
func TestStmtDropDatabase_parse(t *testing.T) {
testName := "TestStmtDropDatabase_parse"
testData := []struct {
name string
sql string
expected *StmtDropDatabase
mustError bool
}{
{name: "error_if_exist", sql: "DROP DATABASE IF EXIST db1", mustError: true},
{name: "error_no_db", sql: "DROP DATABASE ", mustError: true},
{name: "basic", sql: "DROP DATABASE db1", expected: &StmtDropDatabase{dbName: "db1"}},
{name: "lfcr", sql: "DROP\ndatabase\rdb-2", expected: &StmtDropDatabase{dbName: "db-2"}},
{name: "if_exists", sql: "drop\rdatabase\nIF\nEXISTS db_3", expected: &StmtDropDatabase{dbName: "db_3", ifExists: true}},
{name: "if_exists_2", sql: "Drop Database \tIf\t Exists \t db-4_0", expected: &StmtDropDatabase{dbName: "db-4_0", ifExists: true}},
}
for _, testCase := range testData {
t.Run(testCase.name, func(t *testing.T) {
s, err := parseQueryWithDefaultDb(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.(*StmtDropDatabase)
if !ok {
t.Fatalf("%s failed: expected StmtDropDatabase 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)
}
})
}
}
func TestStmtListDatabases_parse(t *testing.T) {
testName := "TestStmtListDatabases_parse"
testData := []struct {
name string
sql string
expected *StmtListDatabases
mustError bool
}{
{name: "basic", sql: "LIST DATABASES", expected: &StmtListDatabases{}},
{name: "database", sql: " lisT \r\t\n Database ", expected: &StmtListDatabases{}},
}
for _, testCase := range testData {
t.Run(testCase.name, func(t *testing.T) {
s, err := parseQueryWithDefaultDb(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.(*StmtListDatabases)
if !ok {
t.Fatalf("%s failed: expected StmtListDatabases 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)
}
})
}
}