-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathdump_test.go
89 lines (87 loc) · 2.2 KB
/
dump_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
package main
import "testing"
func TestParseTableNameFromDDL(t *testing.T) {
tests := []struct {
name string
ddl string
want string
}{
{
name: "create table",
ddl: `CREATE TABLE table_name_1 (
column1 STRING(32) NOT NULL,
column2 TIMESTAMP NOT NULL OPTIONS (
allow_commit_timestamp = true
),
) PRIMARY KEY(column1);`,
want: "table_name_1",
},
{
name: "create table, table enclosed by backtick (`)",
ddl: `CREATE TABLE ` + "`table_name_1`" + ` (
column1 STRING(32) NOT NULL,
column2 TIMESTAMP NOT NULL OPTIONS (
allow_commit_timestamp = true
),
) PRIMARY KEY(column1);`,
want: "table_name_1",
},
{
name: "create table, include multiple spaces",
ddl: ` CREATE TABLE table_name_1 (
column1 STRING(32) NOT NULL,
column2 TIMESTAMP NOT NULL OPTIONS (
allow_commit_timestamp = true
),
) PRIMARY KEY(column1);`,
want: "table_name_1",
},
{
name: "create unique index",
ddl: `CREATE UNIQUE INDEX table_name_1_column2_a ON table_name_1(column2);`,
want: "table_name_1",
},
{
name: "create index",
ddl: `CREATE INDEX table_name_1_column2_a ON table_name_1(column2);`,
want: "table_name_1",
},
{
name: "create index, index name enclosed by backtick (`)",
ddl: "CREATE INDEX `order` ON TABLE(`by`)",
want: "TABLE",
},
{
name: "create index, table name enclosed by backtick (`)",
ddl: "CREATE INDEX `order` ON `TABLE`(`by`)",
want: "TABLE",
},
{
name: "create index, include multiple spaces",
ddl: " CREATE INDEX `order` ON TABLE(`by`)",
want: "TABLE",
},
{
name: "alter table",
ddl: "ALTER TABLE t5 ADD FOREIGN KEY(T6Id) REFERENCES t6(Id);",
want: "t5",
},
{
name: "alter table, table name enclosed by backtick (`)",
ddl: "ALTER TABLE `t5` ADD FOREIGN KEY(T6Id) REFERENCES t6(Id);",
want: "t5",
},
{
name: "alter table, include multiple spaces",
ddl: " ALTER TABLE \r\n `t5` ADD FOREIGN KEY(T6Id) REFERENCES t6(Id);",
want: "t5",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := parseTableNameFromDDL(tt.ddl); got != tt.want {
t.Errorf("parseTableNameFromDDL() = %v, want %v", got, tt.want)
}
})
}
}