-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathalter_test.go
175 lines (168 loc) · 5.25 KB
/
alter_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
package sqlbuilder
import (
"testing"
)
func TestAlterTable(t *testing.T) {
table1 := NewTable(
"TABLE_A",
&TableOption{},
IntColumn("id", &ColumnOption{
PrimaryKey: true,
}),
IntColumn("test1", nil),
IntColumn("test2", nil),
)
table2 := NewTable(
"TABLE_B",
&TableOption{},
IntColumn("id", &ColumnOption{
PrimaryKey: true,
}),
)
tableJoined := table1.InnerJoin(table2, table1.C("test1").Eq(table2.C("id")))
var cases = []statementTestCase{{
stmt: AlterTable(table1).
RenameTo("TABLE_AAA").
AddColumn(IntColumn("test3", nil)).
AddColumn(IntColumn("test4", nil)).
ChangeColumn(table1.C("test1"), IntColumn("test1a", nil)).
DropColumn(table1.C("test1")),
query: `ALTER TABLE "TABLE_A" ADD COLUMN "test3" INTEGER, ADD COLUMN "test4" INTEGER, CHANGE COLUMN "test1" "test1a" INTEGER, DROP COLUMN "test1", RENAME TO "TABLE_AAA";`,
args: []interface{}{},
errmsg: "",
}, {
stmt: AlterTable(table1).RenameTo("TABLE_AAA"),
query: `ALTER TABLE "TABLE_A" RENAME TO "TABLE_AAA";`,
args: []interface{}{},
errmsg: "",
}, {
stmt: AlterTable(table1).AddColumn(IntColumn("test3", &ColumnOption{
Unique: true,
})),
query: `ALTER TABLE "TABLE_A" ADD COLUMN "test3" INTEGER UNIQUE;`,
args: []interface{}{},
errmsg: "",
}, {
stmt: AlterTable(table1).AddColumnAfter(IntColumn("test0", nil), table1.C("id")),
query: `ALTER TABLE "TABLE_A" ADD COLUMN "test0" INTEGER AFTER "id";`,
args: []interface{}{},
errmsg: "",
}, {
stmt: AlterTable(table1).AddColumnFirst(IntColumn("test0", nil)),
query: `ALTER TABLE "TABLE_A" ADD COLUMN "test0" INTEGER FIRST;`,
args: []interface{}{},
errmsg: "",
}, {
stmt: AlterTable(table1).ChangeColumn(table1.C("test1"), IntColumn("test1a", &ColumnOption{
Unique: true,
})),
query: `ALTER TABLE "TABLE_A" CHANGE COLUMN "test1" "test1a" INTEGER UNIQUE;`,
args: []interface{}{},
errmsg: "",
}, {
stmt: AlterTable(table1).ChangeColumnFirst(table1.C("test1"), IntColumn("test1a", nil)),
query: `ALTER TABLE "TABLE_A" CHANGE COLUMN "test1" "test1a" INTEGER FIRST;`,
args: []interface{}{},
errmsg: "",
}, {
stmt: AlterTable(table1).ChangeColumnAfter(table1.C("test1"), IntColumn("test1a", nil), table1.C("test2")),
query: `ALTER TABLE "TABLE_A" CHANGE COLUMN "test1" "test1a" INTEGER AFTER "test2";`,
args: []interface{}{},
errmsg: "",
}, {
stmt: AlterTable(table1).DropColumn(table1.C("test1")),
query: `ALTER TABLE "TABLE_A" DROP COLUMN "test1";`,
args: []interface{}{},
errmsg: "",
}, {
stmt: AlterTable(table1).DropColumn(table1.C("invalid")),
query: ``,
args: []interface{}{},
errmsg: "sqlbuilder: column TABLE_A.invalid was not found.",
}, {
stmt: AlterTable(table1).ChangeColumnAfter(table1.C("invalid"), IntColumn("test1a", nil), table1.C("test2")),
query: ``,
args: []interface{}{},
errmsg: "sqlbuilder: column TABLE_A.invalid was not found.",
}, {
stmt: AlterTable(table1).ChangeColumnAfter(table1.C("test1"), IntColumn("test1a", nil), table1.C("invalid")),
query: ``,
args: []interface{}{},
errmsg: "sqlbuilder: column TABLE_A.invalid was not found.",
}, {
stmt: AlterTable(table1).AddColumnAfter(IntColumn("test0", nil), table1.C("invalid")),
query: ``,
args: []interface{}{},
errmsg: "sqlbuilder: column TABLE_A.invalid was not found.",
}, {
stmt: AlterTable(nil).DropColumn(table1.C("invalid")),
query: ``,
args: []interface{}{},
errmsg: "sqlbuilder: table is nil.",
}, {
stmt: AlterTable(tableJoined).DropColumn(table1.C("id")),
query: ``,
args: []interface{}{},
errmsg: "sqlbuilder: AlterTable can use only natural table.",
}}
for num, c := range cases {
mes, args, ok := c.Run()
if !ok {
t.Errorf(mes+" (case no.%d)", append(args, num)...)
}
}
}
func TestAlterTableApplyToTable(t *testing.T) {
var cases = []struct {
stmt func(Table) *AlterTableStatement
expect_columns []string
expect_name string
}{{
stmt: func(t Table) *AlterTableStatement {
return AlterTable(t).
RenameTo("TABLE_AAA").
AddColumn(IntColumn("test3", nil)).
AddColumnFirst(IntColumn("test4", nil)).
AddColumnAfter(IntColumn("test5", nil), t.C("id")).
ChangeColumn(t.C("test1"), IntColumn("test1a", nil)).
ChangeColumnFirst(t.C("test2"), IntColumn("test2a", nil)).
DropColumn(t.C("id"))
},
expect_columns: []string{"test2a", "test4", "test5", "test1a", "test3"},
expect_name: "TABLE_AAA",
}, {
stmt: func(t Table) *AlterTableStatement {
return AlterTable(t).
ChangeColumnAfter(t.C("test1"), IntColumn("test1a", nil), t.C("test2"))
},
expect_columns: []string{"id", "test2", "test1a"},
expect_name: "TABLE_A",
}}
for num, c := range cases {
table1 := NewTable(
"TABLE_A",
&TableOption{},
IntColumn("id", &ColumnOption{
PrimaryKey: true,
}),
IntColumn("test1", nil),
IntColumn("test2", nil),
)
err := c.stmt(table1).ApplyToTable()
if err != nil {
t.Errorf("failed on %d", num)
}
if len(table1.Columns()) != len(c.expect_columns) {
t.Errorf("failed on %d", num)
}
for i, col := range table1.Columns() {
if c.expect_columns[i] != col.column_name() {
t.Errorf("failed on %d", num)
break
}
}
if table1.Name() != c.expect_name {
t.Errorf("failed on %d", num)
}
}
}