forked from cloudspannerecosystem/spanner-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatement_test.go
More file actions
92 lines (85 loc) · 3.29 KB
/
Copy pathstatement_test.go
File metadata and controls
92 lines (85 loc) · 3.29 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
package main
import (
"reflect"
"strings"
"testing"
"time"
"github.com/google/go-cmp/cmp"
)
func TestBuildStatement(t *testing.T) {
validTests := []struct {
Input string
Expected Statement
}{
{"SELECT * FROM t1", &SelectStatement{Query: "SELECT * FROM t1"}},
{"SELECT\n*\nFROM t1", &SelectStatement{Query: "SELECT\n*\nFROM t1"}},
{"CREATE DATABASE d1", &CreateDatabaseStatement{CreateStatement: "CREATE DATABASE d1"}},
{"CREATE TABLE t1 (id INT64 NOT NULL) PRIMARY KEY (id)", &DdlStatement{Ddl: "CREATE TABLE t1 (id INT64 NOT NULL) PRIMARY KEY (id)"}},
{"ALTER TABLE t1 ADD COLUMN name STRING(16) NOT NULL", &DdlStatement{Ddl: "ALTER TABLE t1 ADD COLUMN name STRING(16) NOT NULL"}},
{"DROP TABLE t1", &DdlStatement{Ddl: "DROP TABLE t1"}},
{"CREATE INDEX idx_name ON t1 (name DESC)", &DdlStatement{Ddl: "CREATE INDEX idx_name ON t1 (name DESC)"}},
{"DROP INDEX idx_name", &DdlStatement{Ddl: "DROP INDEX idx_name"}},
{"INSERT INTO t1 (id, name) VALUES (1, 'yuki')", &DmlStatement{Dml: "INSERT INTO t1 (id, name) VALUES (1, 'yuki')"}},
{"UPDATE t1 SET name = hello WHERE id = 1", &DmlStatement{Dml: "UPDATE t1 SET name = hello WHERE id = 1"}},
{"DELETE FROM t1 WHERE id = 1", &DmlStatement{Dml: "DELETE FROM t1 WHERE id = 1"}},
{"BEGIN", &BeginRwStatement{}},
{"BEGIN RW", &BeginRwStatement{}},
{"BEGIN RO", &BeginRoStatement{}},
{"BEGIN RO 10", &BeginRoStatement{Staleness: time.Duration(10 * time.Second)}},
{"COMMIT", &CommitStatement{}},
{"ROLLBACK", &RollbackStatement{}},
{"CLOSE", &CloseStatement{}},
{"EXIT", &ExitStatement{}},
{"USE database2", &UseStatement{Database: "database2"}},
{"SHOW DATABASES", &ShowDatabasesStatement{}},
{"SHOW CREATE TABLE t1", &ShowCreateTableStatement{Table: "t1"}},
{"SHOW TABLES", &ShowTablesStatement{}},
{"SHOW INDEX FROM t1", &ShowIndexStatement{Table: "t1"}},
{"SHOW INDEXES FROM t1", &ShowIndexStatement{Table: "t1"}},
{"SHOW KEYS FROM t1", &ShowIndexStatement{Table: "t1"}},
{"SHOW COLUMNS FROM t1", &ShowColumnsStatement{Table: "t1"}},
{"EXPLAIN t1", &ShowColumnsStatement{Table: "t1"}},
{"DESCRIBE t1", &ShowColumnsStatement{Table: "t1"}},
{"DESC t1", &ShowColumnsStatement{Table: "t1"}},
{"EXPLAIN SELECT * FROM t1", &ExplainStatement{Explain: "SELECT * FROM t1"}},
{"DESCRIBE SELECT * FROM t1", &ExplainStatement{Explain: "SELECT * FROM t1"}},
{"DESC SELECT * FROM t1", &ExplainStatement{Explain: "SELECT * FROM t1"}},
}
for _, test := range validTests {
// try with case-sensitive input
input := test.Input
expected := test.Expected
got, err := BuildStatement(input)
if err != nil {
t.Fatal(err)
}
if !cmp.Equal(got, expected) {
t.Errorf("diff: %s", cmp.Diff(got, expected))
}
// try with case-insensitive input
input = strings.ToLower(input)
got, err = BuildStatement(input)
if err != nil {
t.Fatal(err)
}
// check only type
gotType := reflect.TypeOf(got)
expectedType := reflect.TypeOf(expected)
if gotType != expectedType {
t.Errorf("invalid statement type: expected = %s, but got = %s", expectedType, gotType)
}
}
invalidTests := []struct {
Input string
}{
{"FOO BAR"},
{"SELEC T FROM t1"},
{"SET @a = 1"},
}
for _, test := range invalidTests {
got, err := BuildStatement(test.Input)
if err == nil {
t.Errorf("expected error, but got = %#v", got)
}
}
}