forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_test.go
227 lines (215 loc) · 5.65 KB
/
update_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
// Copyright 2019 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package executor_test
import (
"flag"
"fmt"
. "github.com/pingcap/check"
"github.com/pingcap/parser"
"github.com/pingcap/tidb/domain"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/session"
"github.com/pingcap/tidb/store/mockstore"
"github.com/pingcap/tidb/store/mockstore/mocktikv"
"github.com/pingcap/tidb/util/mock"
"github.com/pingcap/tidb/util/testkit"
)
type testUpdateSuite struct {
cluster *mocktikv.Cluster
mvccStore mocktikv.MVCCStore
store kv.Storage
domain *domain.Domain
*parser.Parser
ctx *mock.Context
}
func (s *testUpdateSuite) SetUpSuite(c *C) {
s.Parser = parser.New()
flag.Lookup("mockTikv")
useMockTikv := *mockTikv
if useMockTikv {
s.cluster = mocktikv.NewCluster()
mocktikv.BootstrapWithSingleStore(s.cluster)
s.mvccStore = mocktikv.MustNewMVCCStore()
store, err := mockstore.NewMockTikvStore(
mockstore.WithCluster(s.cluster),
mockstore.WithMVCCStore(s.mvccStore),
)
c.Assert(err, IsNil)
s.store = store
session.SetSchemaLease(0)
session.DisableStats4Test()
}
d, err := session.BootstrapSession(s.store)
c.Assert(err, IsNil)
d.SetStatsUpdating(true)
s.domain = d
}
func (s *testUpdateSuite) TearDownSuite(c *C) {
s.domain.Close()
s.store.Close()
}
func (s *testUpdateSuite) TearDownTest(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
r := tk.MustQuery("show tables")
for _, tb := range r.Rows() {
tableName := tb[0]
tk.MustExec(fmt.Sprintf("drop table %v", tableName))
}
}
func (s *testUpdateSuite) TestUpdateGenColInTxn(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec(`create table t(a bigint, b bigint as (a+1));`)
tk.MustExec(`begin;`)
tk.MustExec(`insert into t(a) values(1);`)
err := tk.ExecToErr(`update t set b=6 where b=2;`)
c.Assert(err.Error(), Equals, "[planner:3105]The value specified for generated column 'b' in table 't' is not allowed.")
tk.MustExec(`commit;`)
tk.MustQuery(`select * from t;`).Check(testkit.Rows(
`1 2`))
}
func (s *testUpdateSuite) TestUpdateWithAutoidSchema(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec(`use test`)
tk.MustExec(`create table t1(id int primary key auto_increment, n int);`)
tk.MustExec(`create table t2(id int primary key, n float auto_increment, key I_n(n));`)
tk.MustExec(`create table t3(id int primary key, n double auto_increment, key I_n(n));`)
tests := []struct {
exec string
query string
result [][]interface{}
}{
{
`insert into t1 set n = 1`,
`select * from t1 where id = 1`,
testkit.Rows(`1 1`),
},
{
`update t1 set id = id+1`,
`select * from t1 where id = 2`,
testkit.Rows(`2 1`),
},
{
`insert into t1 set n = 2`,
`select * from t1 where id = 3`,
testkit.Rows(`3 2`),
},
{
`update t1 set id = id + '1.1' where id = 3`,
`select * from t1 where id = 4`,
testkit.Rows(`4 2`),
},
{
`insert into t1 set n = 3`,
`select * from t1 where id = 5`,
testkit.Rows(`5 3`),
},
{
`update t1 set id = id + '0.5' where id = 5`,
`select * from t1 where id = 6`,
testkit.Rows(`6 3`),
},
{
`insert into t1 set n = 4`,
`select * from t1 where id = 7`,
testkit.Rows(`7 4`),
},
{
`insert into t2 set id = 1`,
`select * from t2 where id = 1`,
testkit.Rows(`1 1`),
},
{
`update t2 set n = n+1`,
`select * from t2 where id = 1`,
testkit.Rows(`1 2`),
},
{
`insert into t2 set id = 2`,
`select * from t2 where id = 2`,
testkit.Rows(`2 3`),
},
{
`update t2 set n = n + '2.2'`,
`select * from t2 where id = 2`,
testkit.Rows(`2 5.2`),
},
{
`insert into t2 set id = 3`,
`select * from t2 where id = 3`,
testkit.Rows(`3 6`),
},
{
`update t2 set n = n + '0.5' where id = 3`,
`select * from t2 where id = 3`,
testkit.Rows(`3 6.5`),
},
{
`insert into t2 set id = 4`,
`select * from t2 where id = 4`,
testkit.Rows(`4 7`),
},
{
`insert into t3 set id = 1`,
`select * from t3 where id = 1`,
testkit.Rows(`1 1`),
},
{
`update t3 set n = n+1`,
`select * from t3 where id = 1`,
testkit.Rows(`1 2`),
},
{
`insert into t3 set id = 2`,
`select * from t3 where id = 2`,
testkit.Rows(`2 3`),
},
{
`update t3 set n = n + '3.3'`,
`select * from t3 where id = 2`,
testkit.Rows(`2 6.3`),
},
{
`insert into t3 set id = 3`,
`select * from t3 where id = 3`,
testkit.Rows(`3 7`),
},
{
`update t3 set n = n + '0.5' where id = 3`,
`select * from t3 where id = 3`,
testkit.Rows(`3 7.5`),
},
{
`insert into t3 set id = 4`,
`select * from t3 where id = 4`,
testkit.Rows(`4 8`),
},
}
for _, tt := range tests {
tk.MustExec(tt.exec)
tk.MustQuery(tt.query).Check(tt.result)
}
}
func (s *testUpdateSuite) TestUpdateSchemaChange(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec(`create table t(a bigint, b bigint as (a+1));`)
tk.MustExec(`begin;`)
tk.MustExec(`insert into t(a) values(1);`)
err := tk.ExecToErr(`update t set b=6 where b=2;`)
c.Assert(err.Error(), Equals, "[planner:3105]The value specified for generated column 'b' in table 't' is not allowed.")
tk.MustExec(`commit;`)
tk.MustQuery(`select * from t;`).Check(testkit.Rows(
`1 2`))
}