From cfb9f30245eddbdb16a39925908828fa61c5ad70 Mon Sep 17 00:00:00 2001 From: Weizhen Wang Date: Tue, 14 Jun 2022 18:00:38 +0800 Subject: [PATCH 1/2] executor: close RecordSet in the ddl_test and write_test Signed-off-by: Weizhen Wang --- executor/ddl_test.go | 71 +++------ executor/write_test.go | 347 +++++++++++++++++++---------------------- 2 files changed, 184 insertions(+), 234 deletions(-) diff --git a/executor/ddl_test.go b/executor/ddl_test.go index eedc8f212147a..fe4b79fb767d6 100644 --- a/executor/ddl_test.go +++ b/executor/ddl_test.go @@ -390,11 +390,9 @@ func TestTruncateSequence(t *testing.T) { tk := testkit.NewTestKit(t, store) tk.MustExec("use test") tk.MustExec("create sequence if not exists seq") - _, err := tk.Exec("truncate table seq") - require.EqualError(t, err, "[schema:1146]Table 'test.seq' doesn't exist") + tk.MustGetErrMsg("truncate table seq", "[schema:1146]Table 'test.seq' doesn't exist") tk.MustExec("create sequence if not exists seq1 start 10 increment 2 maxvalue 10000 cycle") - _, err = tk.Exec("truncate table seq1") - require.EqualError(t, err, "[schema:1146]Table 'test.seq1' doesn't exist") + tk.MustGetErrMsg("truncate table seq1", "[schema:1146]Table 'test.seq1' doesn't exist") tk.MustExec("drop sequence if exists seq") tk.MustExec("drop sequence if exists seq1") } @@ -457,12 +455,10 @@ func TestCreateDropDatabase(t *testing.T) { tk.MustExec("create database drop_test;") tk.MustExec("use drop_test;") tk.MustExec("drop database drop_test;") - _, err := tk.Exec("drop table t;") - require.Equal(t, plannercore.ErrNoDB.Error(), err.Error()) - err = tk.ExecToErr("select * from t;") - require.Equal(t, plannercore.ErrNoDB.Error(), err.Error()) + tk.MustGetDBError("drop table t;", plannercore.ErrNoDB) + tk.MustGetDBError("select * from t;", plannercore.ErrNoDB) - _, err = tk.Exec("drop database mysql") + err := tk.ExecToErr("drop database mysql") require.Error(t, err) tk.MustExec("create database charset_test charset ascii;") @@ -518,7 +514,7 @@ func TestCreateDropTable(t *testing.T) { tk.MustExec("create table drop_test (a int)") tk.MustExec("drop table drop_test") - _, err := tk.Exec("drop table mysql.gc_delete_range") + err := tk.ExecToErr("drop table mysql.gc_delete_range") require.Error(t, err) } @@ -528,21 +524,14 @@ func TestCreateDropView(t *testing.T) { tk := testkit.NewTestKit(t, store) tk.MustExec("use test") tk.MustExec("create or replace view drop_test as select 1,2") - - _, err := tk.Exec("drop table drop_test") - require.EqualError(t, err, "[schema:1051]Unknown table 'test.drop_test'") + tk.MustGetErrMsg("drop table drop_test", "[schema:1051]Unknown table 'test.drop_test'") tk.MustExec("drop view if exists drop_test") - _, err = tk.Exec("drop view mysql.gc_delete_range") - require.EqualError(t, err, "Drop tidb system table 'mysql.gc_delete_range' is forbidden") - - _, err = tk.Exec("drop view drop_test") - require.EqualError(t, err, "[schema:1051]Unknown table 'test.drop_test'") - + tk.MustGetErrMsg("drop view mysql.gc_delete_range", "Drop tidb system table 'mysql.gc_delete_range' is forbidden") + tk.MustGetErrMsg("drop view drop_test", "[schema:1051]Unknown table 'test.drop_test'") tk.MustExec("create table t_v(a int)") - _, err = tk.Exec("drop view t_v") - require.EqualError(t, err, "[ddl:1347]'test.t_v' is not VIEW") + tk.MustGetErrMsg("drop view t_v", "[ddl:1347]'test.t_v' is not VIEW") tk.MustExec("create table t_v1(a int, b int);") tk.MustExec("create table t_v2(a int, b int);") @@ -652,15 +641,14 @@ func TestAlterTableModifyColumn(t *testing.T) { tk.MustExec("use test") tk.MustExec("drop table if exists mc") tk.MustExec("create table mc(c1 int, c2 varchar(10), c3 bit)") - _, err := tk.Exec("alter table mc modify column c1 short") + err := tk.ExecToErr("alter table mc modify column c1 short") require.Error(t, err) tk.MustExec("alter table mc modify column c1 bigint") - _, err = tk.Exec("alter table mc modify column c2 blob") + err = tk.ExecToErr("alter table mc modify column c2 blob") require.Error(t, err) - _, err = tk.Exec("alter table mc modify column c2 varchar(8)") - require.NoError(t, err) + tk.MustExec("alter table mc modify column c2 varchar(8)") tk.MustExec("alter table mc modify column c2 varchar(11)") tk.MustExec("alter table mc modify column c2 text(13)") tk.MustExec("alter table mc modify column c2 text") @@ -670,11 +658,11 @@ func TestAlterTableModifyColumn(t *testing.T) { expected := "CREATE TABLE `mc` (\n `c1` bigint(20) DEFAULT NULL,\n `c2` text DEFAULT NULL,\n `c3` bit(1) DEFAULT NULL\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin" require.Equal(t, expected, createSQL) tk.MustExec("create or replace view alter_view as select c1,c2 from mc") - _, err = tk.Exec("alter table alter_view modify column c2 text") + err = tk.ExecToErr("alter table alter_view modify column c2 text") require.Equal(t, dbterror.ErrWrongObject.GenWithStackByArgs("test", "alter_view", "BASE TABLE").Error(), err.Error()) tk.MustExec("drop view alter_view") tk.MustExec("create sequence alter_seq") - _, err = tk.Exec("alter table alter_seq modify column c int") + err = tk.ExecToErr("alter table alter_seq modify column c int") require.Equal(t, dbterror.ErrWrongObject.GenWithStackByArgs("test", "alter_seq", "BASE TABLE").Error(), err.Error()) tk.MustExec("drop sequence alter_seq") @@ -795,7 +783,7 @@ func TestColumnCharsetAndCollate(t *testing.T) { require.Equalf(t, tt.exptCharset, tb.Meta().Columns[0].GetCharset(), sql) require.Equalf(t, tt.exptCollate, tb.Meta().Columns[0].GetCollate(), sql) } else { - _, err := tk.Exec(sql) + err := tk.ExecToErr(sql) require.Errorf(t, err, sql) } } @@ -811,37 +799,32 @@ func TestTooLargeIdentifierLength(t *testing.T) { dbName1, dbName2 := strings.Repeat("a", mysql.MaxDatabaseNameLength), strings.Repeat("a", mysql.MaxDatabaseNameLength+1) tk.MustExec(fmt.Sprintf("create database %s", dbName1)) tk.MustExec(fmt.Sprintf("drop database %s", dbName1)) - _, err := tk.Exec(fmt.Sprintf("create database %s", dbName2)) - require.Equal(t, fmt.Sprintf("[ddl:1059]Identifier name '%s' is too long", dbName2), err.Error()) + tk.MustGetErrMsg(fmt.Sprintf("create database %s", dbName2), fmt.Sprintf("[ddl:1059]Identifier name '%s' is too long", dbName2)) // for table. tk.MustExec("use test") tableName1, tableName2 := strings.Repeat("b", mysql.MaxTableNameLength), strings.Repeat("b", mysql.MaxTableNameLength+1) tk.MustExec(fmt.Sprintf("create table %s(c int)", tableName1)) tk.MustExec(fmt.Sprintf("drop table %s", tableName1)) - _, err = tk.Exec(fmt.Sprintf("create table %s(c int)", tableName2)) - require.Equal(t, fmt.Sprintf("[ddl:1059]Identifier name '%s' is too long", tableName2), err.Error()) + tk.MustGetErrMsg(fmt.Sprintf("create table %s(c int)", tableName2), fmt.Sprintf("[ddl:1059]Identifier name '%s' is too long", tableName2)) // for column. tk.MustExec("drop table if exists t;") columnName1, columnName2 := strings.Repeat("c", mysql.MaxColumnNameLength), strings.Repeat("c", mysql.MaxColumnNameLength+1) tk.MustExec(fmt.Sprintf("create table t(%s int)", columnName1)) tk.MustExec("drop table t") - _, err = tk.Exec(fmt.Sprintf("create table t(%s int)", columnName2)) - require.Equal(t, fmt.Sprintf("[ddl:1059]Identifier name '%s' is too long", columnName2), err.Error()) + tk.MustGetErrMsg(fmt.Sprintf("create table t(%s int)", columnName2), fmt.Sprintf("[ddl:1059]Identifier name '%s' is too long", columnName2)) // for index. tk.MustExec("create table t(c int);") indexName1, indexName2 := strings.Repeat("d", mysql.MaxIndexIdentifierLen), strings.Repeat("d", mysql.MaxIndexIdentifierLen+1) tk.MustExec(fmt.Sprintf("create index %s on t(c)", indexName1)) tk.MustExec(fmt.Sprintf("drop index %s on t", indexName1)) - _, err = tk.Exec(fmt.Sprintf("create index %s on t(c)", indexName2)) - require.Equal(t, fmt.Sprintf("[ddl:1059]Identifier name '%s' is too long", indexName2), err.Error()) + tk.MustGetErrMsg(fmt.Sprintf("create index %s on t(c)", indexName2), fmt.Sprintf("[ddl:1059]Identifier name '%s' is too long", indexName2)) // for create table with index. tk.MustExec("drop table t;") - _, err = tk.Exec(fmt.Sprintf("create table t(c int, index %s(c));", indexName2)) - require.Equal(t, fmt.Sprintf("[ddl:1059]Identifier name '%s' is too long", indexName2), err.Error()) + tk.MustGetErrMsg(fmt.Sprintf("create table t(c int, index %s(c));", indexName2), fmt.Sprintf("[ddl:1059]Identifier name '%s' is too long", indexName2)) } func TestShardRowIDBits(t *testing.T) { @@ -1013,9 +996,7 @@ func TestAutoRandomBitsData(t *testing.T) { for i := -10; i < 10; i++ { tk.MustExec(fmt.Sprintf("insert into t values(%d, %d)", i+autoRandBitsUpperBound, i)) } - _, err := tk.Exec("insert into t (b) values (0)") - require.Error(t, err) - require.Equal(t, autoid.ErrAutoRandReadFailed.GenWithStackByArgs().Error(), err.Error()) + tk.MustGetErrMsg("insert into t (b) values (0)", autoid.ErrAutoRandReadFailed.GenWithStackByArgs().Error()) tk.MustExec("drop table t") // Test overflow. @@ -1023,17 +1004,13 @@ func TestAutoRandomBitsData(t *testing.T) { // Here we cannot fill the all values for a `bigint` column, // so firstly we rebase auto_rand to the position before overflow. tk.MustExec(fmt.Sprintf("insert into t values (%d, %d)", autoRandBitsUpperBound, 1)) - _, err = tk.Exec("insert into t (b) values (0)") - require.Error(t, err) - require.Equal(t, autoid.ErrAutoRandReadFailed.GenWithStackByArgs().Error(), err.Error()) + tk.MustGetErrMsg("insert into t (b) values (0)", autoid.ErrAutoRandReadFailed.GenWithStackByArgs().Error()) tk.MustExec("drop table t") tk.MustExec("create table t (a bigint primary key auto_random(15), b int)") tk.MustExec("insert into t values (1, 2)") tk.MustExec(fmt.Sprintf("update t set a = %d where a = 1", autoRandBitsUpperBound)) - _, err = tk.Exec("insert into t (b) values (0)") - require.Error(t, err) - require.Equal(t, autoid.ErrAutoRandReadFailed.GenWithStackByArgs().Error(), err.Error()) + tk.MustGetErrMsg("insert into t (b) values (0)", autoid.ErrAutoRandReadFailed.GenWithStackByArgs().Error()) tk.MustExec("drop table t") // Test insert negative integers explicitly won't trigger rebase. diff --git a/executor/write_test.go b/executor/write_test.go index 2cc17b1e1916b..aa2b77b4122b7 100644 --- a/executor/write_test.go +++ b/executor/write_test.go @@ -55,25 +55,25 @@ func TestInsert(t *testing.T) { errInsertSelectSQL := `insert insert_test (c1) values ();` tk.MustExec("begin") - _, err := tk.Exec(errInsertSelectSQL) + err := tk.ExecToErr(errInsertSelectSQL) require.Error(t, err) tk.MustExec("rollback") errInsertSelectSQL = `insert insert_test (c1, c2) values (1,2),(1);` tk.MustExec("begin") - _, err = tk.Exec(errInsertSelectSQL) + err = tk.ExecToErr(errInsertSelectSQL) require.Error(t, err) tk.MustExec("rollback") errInsertSelectSQL = `insert insert_test (xxx) values (3);` tk.MustExec("begin") - _, err = tk.Exec(errInsertSelectSQL) + err = tk.ExecToErr(errInsertSelectSQL) require.Error(t, err) tk.MustExec("rollback") errInsertSelectSQL = `insert insert_test_xxx (c1) values ();` tk.MustExec("begin") - _, err = tk.Exec(errInsertSelectSQL) + err = tk.ExecToErr(errInsertSelectSQL) require.Error(t, err) tk.MustExec("rollback") @@ -83,13 +83,13 @@ func TestInsert(t *testing.T) { errInsertSelectSQL = `insert insert_test set c1 = 4, c1 = 5;` tk.MustExec("begin") - _, err = tk.Exec(errInsertSelectSQL) + err = tk.ExecToErr(errInsertSelectSQL) require.Error(t, err) tk.MustExec("rollback") errInsertSelectSQL = `insert insert_test set xxx = 6;` tk.MustExec("begin") - _, err = tk.Exec(errInsertSelectSQL) + err = tk.ExecToErr(errInsertSelectSQL) require.Error(t, err) tk.MustExec("rollback") @@ -107,13 +107,13 @@ func TestInsert(t *testing.T) { errInsertSelectSQL = `insert insert_test_1 select c1 from insert_test;` tk.MustExec("begin") - _, err = tk.Exec(errInsertSelectSQL) + err = tk.ExecToErr(errInsertSelectSQL) require.Error(t, err) tk.MustExec("rollback") errInsertSelectSQL = `insert insert_test_1 values(default, default, default, default, default)` tk.MustExec("begin") - _, err = tk.Exec(errInsertSelectSQL) + err = tk.ExecToErr(errInsertSelectSQL) require.Error(t, err) tk.MustExec("rollback") @@ -133,7 +133,7 @@ func TestInsert(t *testing.T) { tk.MustExec(insertSQL) require.Empty(t, tk.Session().LastMessage()) - _, err = tk.Exec(`insert into insert_test (id, c2) values(1, 1) on duplicate key update t.c2 = 10`) + err = tk.ExecToErr(`insert into insert_test (id, c2) values(1, 1) on duplicate key update t.c2 = 10`) require.Error(t, err) // for on duplicate key @@ -153,13 +153,13 @@ func TestInsert(t *testing.T) { r.Check(testkit.Rows(rowStr)) tk.MustExec("create table insert_err (id int, c1 varchar(8))") - _, err = tk.Exec("insert insert_err values (1, 'abcdabcdabcd')") + err = tk.ExecToErr("insert insert_err values (1, 'abcdabcdabcd')") require.True(t, types.ErrDataTooLong.Equal(err)) - _, err = tk.Exec("insert insert_err values (1, '你好,世界')") + err = tk.ExecToErr("insert insert_err values (1, '你好,世界')") require.NoError(t, err) tk.MustExec("create table TEST1 (ID INT NOT NULL, VALUE INT DEFAULT NULL, PRIMARY KEY (ID))") - _, err = tk.Exec("INSERT INTO TEST1(id,value) VALUE(3,3) on DUPLICATE KEY UPDATE VALUE=4") + err = tk.ExecToErr("INSERT INTO TEST1(id,value) VALUE(3,3) on DUPLICATE KEY UPDATE VALUE=4") require.NoError(t, err) require.Empty(t, tk.Session().LastMessage()) @@ -172,21 +172,21 @@ func TestInsert(t *testing.T) { // issue 3235 tk.MustExec("drop table if exists t") tk.MustExec("create table t(c decimal(5, 5))") - _, err = tk.Exec("insert into t value(0)") + err = tk.ExecToErr("insert into t value(0)") require.NoError(t, err) - _, err = tk.Exec("insert into t value(1)") + err = tk.ExecToErr("insert into t value(1)") require.True(t, types.ErrWarnDataOutOfRange.Equal(err)) tk.MustExec("drop table if exists t") tk.MustExec("create table t(c binary(255))") - _, err = tk.Exec("insert into t value(1)") + err = tk.ExecToErr("insert into t value(1)") require.NoError(t, err) r = tk.MustQuery("select length(c) from t;") r.Check(testkit.Rows("255")) tk.MustExec("drop table if exists t") tk.MustExec("create table t(c varbinary(255))") - _, err = tk.Exec("insert into t value(1)") + err = tk.ExecToErr("insert into t value(1)") require.NoError(t, err) r = tk.MustQuery("select length(c) from t;") r.Check(testkit.Rows("1")) @@ -196,7 +196,7 @@ func TestInsert(t *testing.T) { tk.MustExec("create table t(c int)") tk.MustExec("set @origin_time_zone = @@time_zone") tk.MustExec("set @@time_zone = '+08:00'") - _, err = tk.Exec("insert into t value(Unix_timestamp('2002-10-27 01:00'))") + err = tk.ExecToErr("insert into t value(Unix_timestamp('2002-10-27 01:00'))") require.NoError(t, err) r = tk.MustQuery("select * from t;") r.Check(testkit.Rows("1035651600")) @@ -204,7 +204,7 @@ func TestInsert(t *testing.T) { // issue 3832 tk.MustExec("create table t1 (b char(0));") - _, err = tk.Exec(`insert into t1 values ("");`) + err = tk.ExecToErr(`insert into t1 values ("");`) require.NoError(t, err) // issue 3895 @@ -221,7 +221,7 @@ func TestInsert(t *testing.T) { // issue 4653 tk.MustExec("DROP TABLE IF EXISTS t;") tk.MustExec("CREATE TABLE t(a datetime);") - _, err = tk.Exec("INSERT INTO t VALUES('2017-00-00')") + err = tk.ExecToErr("INSERT INTO t VALUES('2017-00-00')") require.Error(t, err) tk.MustExec("set sql_mode = ''") tk.MustExec("INSERT INTO t VALUES('2017-00-00')") @@ -246,7 +246,7 @@ func TestInsert(t *testing.T) { tk.MustExec("drop table if exists t;") tk.MustExec("create table t(a bigint unsigned);") tk.MustExec(" set @orig_sql_mode = @@sql_mode; set @@sql_mode = 'strict_all_tables';") - _, err = tk.Exec("insert into t value (-1);") + err = tk.ExecToErr("insert into t value (-1);") require.True(t, types.ErrWarnDataOutOfRange.Equal(err)) tk.MustExec("set @@sql_mode = '';") tk.MustExec("insert into t value (-1);") @@ -273,7 +273,7 @@ func TestInsert(t *testing.T) { tk.MustExec("truncate table t") tk.MustExec("insert into t value(20070219173709.055870), (20070219173709.055), (20070219173709.055870123)") tk.MustQuery("select * from t").Check(testkit.Rows("17:37:09.055870", "17:37:09.055000", "17:37:09.055870")) - _, err = tk.Exec("insert into t value(-20070219173709.055870)") + err = tk.ExecToErr("insert into t value(-20070219173709.055870)") require.EqualError(t, err, "[table:1292]Incorrect time value: '-20070219173709.055870' for column 'a' at row 1") tk.MustExec("drop table if exists t") @@ -301,16 +301,16 @@ func TestInsert(t *testing.T) { tk.MustQuery("select * from t").Check(testkit.Rows("1 1")) tk.MustExec("create view v as select * from t") - _, err = tk.Exec("insert into v values(1,2)") + err = tk.ExecToErr("insert into v values(1,2)") require.EqualError(t, err, "insert into view v is not supported now") - _, err = tk.Exec("replace into v values(1,2)") + err = tk.ExecToErr("replace into v values(1,2)") require.EqualError(t, err, "replace into view v is not supported now") tk.MustExec("drop view v") tk.MustExec("create sequence seq") - _, err = tk.Exec("insert into seq values()") + err = tk.ExecToErr("insert into seq values()") require.EqualError(t, err, "insert into sequence seq is not supported now") - _, err = tk.Exec("replace into seq values()") + err = tk.ExecToErr("replace into seq values()") require.EqualError(t, err, "replace into sequence seq is not supported now") tk.MustExec("drop sequence seq") @@ -318,10 +318,10 @@ func TestInsert(t *testing.T) { tk.MustExec("drop table if exists t") tk.MustExec("create table t(name varchar(255), b int, c int, primary key(name(2)))") tk.MustExec("insert into t(name, b) values(\"cha\", 3)") - _, err = tk.Exec("insert into t(name, b) values(\"chb\", 3)") + err = tk.ExecToErr("insert into t(name, b) values(\"chb\", 3)") require.EqualError(t, err, "[kv:1062]Duplicate entry 'ch' for key 'PRIMARY'") tk.MustExec("insert into t(name, b) values(\"测试\", 3)") - _, err = tk.Exec("insert into t(name, b) values(\"测试\", 3)") + err = tk.ExecToErr("insert into t(name, b) values(\"测试\", 3)") require.EqualError(t, err, "[kv:1062]Duplicate entry '测试' for key 'PRIMARY'") } @@ -448,7 +448,7 @@ func TestInsertAutoInc(t *testing.T) { rowStr4 = fmt.Sprintf("%v %v", "0", "4") r.Check(testkit.Rows(rowStr4, rowStr1, rowStr2, rowStr3)) insertSQL = `insert into insert_autoinc_test(id, c1) values (0, 5)` - _, err := tk.Exec(insertSQL) + err := tk.ExecToErr(insertSQL) // ERROR 1062 (23000): Duplicate entry '0' for key 'PRIMARY' require.Error(t, err) insertSQL = `insert into insert_autoinc_test(c1) values (6)` @@ -512,7 +512,7 @@ func TestInsertIgnore(t *testing.T) { tk.MustExec("commit") cfg.SetGetError(errors.New("foo")) - _, err := tk.Exec("insert ignore into t values (1, 3)") + err := tk.ExecToErr("insert ignore into t values (1, 3)") require.Error(t, err) cfg.SetGetError(nil) @@ -521,13 +521,13 @@ func TestInsertIgnore(t *testing.T) { create table t (a bigint);` tk.MustExec(testSQL) testSQL = "insert ignore into t select '1a';" - _, err = tk.Exec(testSQL) + err = tk.ExecToErr(testSQL) require.NoError(t, err) require.Equal(t, tk.Session().LastMessage(), "Records: 1 Duplicates: 0 Warnings: 1") r = tk.MustQuery("SHOW WARNINGS") r.Check(testkit.Rows("Warning 1292 Truncated incorrect DOUBLE value: '1a'")) testSQL = "insert ignore into t values ('1a')" - _, err = tk.Exec(testSQL) + err = tk.ExecToErr(testSQL) require.NoError(t, err) require.Empty(t, tk.Session().LastMessage()) r = tk.MustQuery("SHOW WARNINGS") @@ -540,7 +540,7 @@ func TestInsertIgnore(t *testing.T) { testSQL = "insert ignore into t values (1,1);" tk.MustExec(testSQL) require.Empty(t, tk.Session().LastMessage()) - _, err = tk.Exec(testSQL) + err = tk.ExecToErr(testSQL) require.Empty(t, tk.Session().LastMessage()) require.NoError(t, err) r = tk.MustQuery("SHOW WARNINGS") @@ -748,7 +748,7 @@ commit;` tk.MustExec(`INSERT t1 VALUES (1, 1), (1, 1) ON DUPLICATE KEY UPDATE f1 = 2, f2 = 2;`) require.Equal(t, tk.Session().LastMessage(), "Records: 2 Duplicates: 1 Warnings: 0") tk.MustQuery(`SELECT * FROM t1 order by f1;`).Check(testkit.Rows("1 1", "2 2")) - _, err := tk.Exec(`INSERT t1 VALUES (1, 1) ON DUPLICATE KEY UPDATE f2 = null;`) + err := tk.ExecToErr(`INSERT t1 VALUES (1, 1) ON DUPLICATE KEY UPDATE f2 = null;`) require.Error(t, err) tk.MustExec(`INSERT IGNORE t1 VALUES (1, 1) ON DUPLICATE KEY UPDATE f2 = null;`) require.Empty(t, tk.Session().LastMessage()) @@ -968,25 +968,25 @@ func TestReplace(t *testing.T) { errReplaceSQL := `replace replace_test (c1) values ();` tk.MustExec("begin") - _, err := tk.Exec(errReplaceSQL) + err := tk.ExecToErr(errReplaceSQL) require.Error(t, err) tk.MustExec("rollback") errReplaceSQL = `replace replace_test (c1, c2) values (1,2),(1);` tk.MustExec("begin") - _, err = tk.Exec(errReplaceSQL) + err = tk.ExecToErr(errReplaceSQL) require.Error(t, err) tk.MustExec("rollback") errReplaceSQL = `replace replace_test (xxx) values (3);` tk.MustExec("begin") - _, err = tk.Exec(errReplaceSQL) + err = tk.ExecToErr(errReplaceSQL) require.Error(t, err) tk.MustExec("rollback") errReplaceSQL = `replace replace_test_xxx (c1) values ();` tk.MustExec("begin") - _, err = tk.Exec(errReplaceSQL) + err = tk.ExecToErr(errReplaceSQL) require.Error(t, err) tk.MustExec("rollback") @@ -996,13 +996,13 @@ func TestReplace(t *testing.T) { errReplaceSetSQL := `replace replace_test set c1 = 4, c1 = 5;` tk.MustExec("begin") - _, err = tk.Exec(errReplaceSetSQL) + err = tk.ExecToErr(errReplaceSetSQL) require.Error(t, err) tk.MustExec("rollback") errReplaceSetSQL = `replace replace_test set xxx = 6;` tk.MustExec("begin") - _, err = tk.Exec(errReplaceSetSQL) + err = tk.ExecToErr(errReplaceSetSQL) require.Error(t, err) tk.MustExec("rollback") @@ -1020,7 +1020,7 @@ func TestReplace(t *testing.T) { errReplaceSelectSQL := `replace replace_test_1 select c1 from replace_test;` tk.MustExec("begin") - _, err = tk.Exec(errReplaceSelectSQL) + err = tk.ExecToErr(errReplaceSelectSQL) require.Error(t, err) tk.MustExec("rollback") @@ -1219,25 +1219,25 @@ func TestPartitionedTableReplace(t *testing.T) { errReplaceSQL := `replace replace_test (c1) values ();` tk.MustExec("begin") - _, err := tk.Exec(errReplaceSQL) + err := tk.ExecToErr(errReplaceSQL) require.Error(t, err) tk.MustExec("rollback") errReplaceSQL = `replace replace_test (c1, c2) values (1,2),(1);` tk.MustExec("begin") - _, err = tk.Exec(errReplaceSQL) + err = tk.ExecToErr(errReplaceSQL) require.Error(t, err) tk.MustExec("rollback") errReplaceSQL = `replace replace_test (xxx) values (3);` tk.MustExec("begin") - _, err = tk.Exec(errReplaceSQL) + err = tk.ExecToErr(errReplaceSQL) require.Error(t, err) tk.MustExec("rollback") errReplaceSQL = `replace replace_test_xxx (c1) values ();` tk.MustExec("begin") - _, err = tk.Exec(errReplaceSQL) + err = tk.ExecToErr(errReplaceSQL) require.Error(t, err) tk.MustExec("rollback") @@ -1247,13 +1247,13 @@ func TestPartitionedTableReplace(t *testing.T) { errReplaceSetSQL := `replace replace_test set c1 = 4, c1 = 5;` tk.MustExec("begin") - _, err = tk.Exec(errReplaceSetSQL) + err = tk.ExecToErr(errReplaceSetSQL) require.Error(t, err) tk.MustExec("rollback") errReplaceSetSQL = `replace replace_test set xxx = 6;` tk.MustExec("begin") - _, err = tk.Exec(errReplaceSetSQL) + err = tk.ExecToErr(errReplaceSetSQL) require.Error(t, err) tk.MustExec("rollback") @@ -1278,7 +1278,7 @@ func TestPartitionedTableReplace(t *testing.T) { errReplaceSelectSQL := `replace replace_test_1 select c1 from replace_test;` tk.MustExec("begin") - _, err = tk.Exec(errReplaceSelectSQL) + err = tk.ExecToErr(errReplaceSelectSQL) require.Error(t, err) tk.MustExec("rollback") @@ -1357,37 +1357,37 @@ func TestHashPartitionedTableReplace(t *testing.T) { errReplaceSQL := `replace replace_test (c1) values ();` tk.MustExec("begin") - _, err := tk.Exec(errReplaceSQL) + err := tk.ExecToErr(errReplaceSQL) require.Error(t, err) tk.MustExec("rollback") errReplaceSQL = `replace replace_test (c1, c2) values (1,2),(1);` tk.MustExec("begin") - _, err = tk.Exec(errReplaceSQL) + err = tk.ExecToErr(errReplaceSQL) require.Error(t, err) tk.MustExec("rollback") errReplaceSQL = `replace replace_test (xxx) values (3);` tk.MustExec("begin") - _, err = tk.Exec(errReplaceSQL) + err = tk.ExecToErr(errReplaceSQL) require.Error(t, err) tk.MustExec("rollback") errReplaceSQL = `replace replace_test_xxx (c1) values ();` tk.MustExec("begin") - _, err = tk.Exec(errReplaceSQL) + err = tk.ExecToErr(errReplaceSQL) require.Error(t, err) tk.MustExec("rollback") errReplaceSetSQL := `replace replace_test set c1 = 4, c1 = 5;` tk.MustExec("begin") - _, err = tk.Exec(errReplaceSetSQL) + err = tk.ExecToErr(errReplaceSetSQL) require.Error(t, err) tk.MustExec("rollback") errReplaceSetSQL = `replace replace_test set xxx = 6;` tk.MustExec("begin") - _, err = tk.Exec(errReplaceSetSQL) + err = tk.ExecToErr(errReplaceSetSQL) require.Error(t, err) tk.MustExec("rollback") @@ -1408,7 +1408,7 @@ func TestHashPartitionedTableReplace(t *testing.T) { errReplaceSelectSQL := `replace replace_test_1 select c1 from replace_test;` tk.MustExec("begin") - _, err = tk.Exec(errReplaceSelectSQL) + err = tk.ExecToErr(errReplaceSelectSQL) require.Error(t, err) tk.MustExec("rollback") @@ -1515,7 +1515,7 @@ func TestPartitionedTableUpdate(t *testing.T) { r = tk.MustQuery("select * from t;") r.Check(testkit.Rows("8 aa", "9 bb")) - _, err := tk.Exec("update t set id = null where name = 'aa'") + err := tk.ExecToErr("update t set id = null where name = 'aa'") require.EqualError(t, err, "[table:1048]Column 'id' cannot be null") // Test that in a transaction, when a constraint failed in an update statement, the record is not inserted. @@ -1527,7 +1527,7 @@ func TestPartitionedTableUpdate(t *testing.T) { PARTITION p2 VALUES LESS THAN (16), PARTITION p3 VALUES LESS THAN (21))`) tk.MustExec("insert t values (1, 1), (2, 2);") - _, err = tk.Exec("update t set name = 1 where id = 2") + err = tk.ExecToErr("update t set name = 1 where id = 2") require.Error(t, err) tk.MustQuery("select * from t").Check(testkit.Rows("1 1", "2 2")) @@ -1539,7 +1539,7 @@ func TestPartitionedTableUpdate(t *testing.T) { PARTITION p1 VALUES LESS THAN (11))`) tk.MustExec("insert into t values (5)") tk.MustExec("insert into t values (7)") - _, err = tk.Exec("update ignore t set a = 5 where a = 7;") + err = tk.ExecToErr("update ignore t set a = 5 where a = 7;") require.NoError(t, err) require.Equal(t, tk.Session().LastMessage(), "Rows matched: 1 Changed: 0 Warnings: 1") r = tk.MustQuery("SHOW WARNINGS;") @@ -1547,7 +1547,7 @@ func TestPartitionedTableUpdate(t *testing.T) { tk.MustQuery("select * from t order by a").Check(testkit.Rows("5", "7")) // test update ignore for truncate as warning - _, err = tk.Exec("update ignore t set a = 1 where a = (select '2a')") + err = tk.ExecToErr("update ignore t set a = 1 where a = (select '2a')") require.NoError(t, err) r = tk.MustQuery("SHOW WARNINGS;") r.Check(testkit.Rows("Warning 1292 Truncated incorrect DOUBLE value: '2a'", "Warning 1292 Truncated incorrect DOUBLE value: '2a'")) @@ -1560,7 +1560,7 @@ func TestPartitionedTableUpdate(t *testing.T) { PARTITION p1 VALUES LESS THAN (11))`) tk.MustExec("insert into t values (5)") tk.MustExec("insert into t values (7)") - _, err = tk.Exec("update ignore t set a = 5 where a = 7;") + err = tk.ExecToErr("update ignore t set a = 5 where a = 7;") require.NoError(t, err) require.Equal(t, tk.Session().LastMessage(), "Rows matched: 1 Changed: 0 Warnings: 1") r = tk.MustQuery("SHOW WARNINGS;") @@ -1584,7 +1584,7 @@ func TestUpdateCastOnlyModifiedValues(t *testing.T) { require.Equal(t, tk.Session().LastMessage(), "Rows matched: 1 Changed: 1 Warnings: 0") r = tk.MustQuery("SELECT * FROM update_modified") r.Check(testkit.Rows("1 ")) - _, err := tk.Exec("update update_modified set col_1 = 2, col_2 = 'c'") + err := tk.ExecToErr("update update_modified set col_1 = 2, col_2 = 'c'") require.Error(t, err) r = tk.MustQuery("SELECT * FROM update_modified") r.Check(testkit.Rows("1 ")) @@ -1712,9 +1712,9 @@ func TestDelete(t *testing.T) { // Test delete ignore tk.MustExec("insert into delete_test values (2, 'abc')") - _, err := tk.Exec("delete from delete_test where id = (select '2a')") + err := tk.ExecToErr("delete from delete_test where id = (select '2a')") require.Error(t, err) - _, err = tk.Exec("delete ignore from delete_test where id = (select '2a')") + err = tk.ExecToErr("delete ignore from delete_test where id = (select '2a')") require.NoError(t, err) tk.CheckExecResult(1, 0) r := tk.MustQuery("SHOW WARNINGS;") @@ -1724,12 +1724,12 @@ func TestDelete(t *testing.T) { tk.CheckExecResult(1, 0) tk.MustExec("create view v as select * from delete_test") - _, err = tk.Exec("delete from v where name = 'aaa'") + err = tk.ExecToErr("delete from v where name = 'aaa'") require.EqualError(t, err, core.ErrViewInvalid.GenWithStackByArgs("test", "v").Error()) tk.MustExec("drop view v") tk.MustExec("create sequence seq") - _, err = tk.Exec("delete from seq") + err = tk.ExecToErr("delete from seq") require.EqualError(t, err, "delete sequence seq is not supported now") tk.MustExec("drop sequence seq") } @@ -1765,9 +1765,9 @@ func TestPartitionedTableDelete(t *testing.T) { // Test delete ignore tk.MustExec("insert into t values (2, 'abc')") - _, err := tk.Exec("delete from t where id = (select '2a')") + err := tk.ExecToErr("delete from t where id = (select '2a')") require.Error(t, err) - _, err = tk.Exec("delete ignore from t where id = (select '2a')") + err = tk.ExecToErr("delete ignore from t where id = (select '2a')") require.NoError(t, err) tk.CheckExecResult(1, 0) r := tk.MustQuery("SHOW WARNINGS;") @@ -1854,7 +1854,7 @@ func TestQualifiedDelete(t *testing.T) { tk.MustExec("delete a, b from t1 as a join t2 as b where a.c2 = b.c1") tk.CheckExecResult(2, 0) - _, err := tk.Exec("delete t1, t2 from t1 as a join t2 as b where a.c2 = b.c1") + err := tk.ExecToErr("delete t1, t2 from t1 as a join t2 as b where a.c2 = b.c1") require.Error(t, err) } @@ -1982,10 +1982,10 @@ func TestLoadData(t *testing.T) { tk.MustExec("use test") createSQL := `drop table if exists load_data_test; create table load_data_test (id int PRIMARY KEY AUTO_INCREMENT, c1 int, c2 varchar(255) default "def", c3 int);` - _, err := tk.Exec("load data local infile '/tmp/nonexistence.csv' into table load_data_test") + err := tk.ExecToErr("load data local infile '/tmp/nonexistence.csv' into table load_data_test") require.Error(t, err) tk.MustExec(createSQL) - _, err = tk.Exec("load data infile '/tmp/nonexistence.csv' into table load_data_test") + err = tk.ExecToErr("load data infile '/tmp/nonexistence.csv' into table load_data_test") require.Error(t, err) tk.MustExec("load data local infile '/tmp/nonexistence.csv' ignore into table load_data_test") ctx := tk.Session().(sessionctx.Context) @@ -2401,8 +2401,7 @@ func TestLatch(t *testing.T) { tk1.MustExec("begin") tk1.MustExec("update t set id = id + 1") tk2.MustExec("update t set id = id + 1") - _, err = tk1.Exec("commit") - require.True(t, kv.ErrWriteConflictInTiDB.Equal(err)) + tk1.MustGetDBError("commit", kv.ErrWriteConflictInTiDB) tk1.MustExec("set @@tidb_disable_txn_auto_retry = 0") tk1.MustExec("update t set id = id + 1") @@ -2556,11 +2555,11 @@ func TestDataTooLongErrMsg(t *testing.T) { tk := testkit.NewTestKit(t, store) tk.MustExec("use test") tk.MustExec("create table t(a varchar(2));") - _, err := tk.Exec("insert into t values('123');") + err := tk.ExecToErr("insert into t values('123');") require.True(t, types.ErrDataTooLong.Equal(err)) require.EqualError(t, err, "[types:1406]Data too long for column 'a' at row 1") tk.MustExec("insert into t values('12')") - _, err = tk.Exec("update t set a = '123' where a = '12';") + err = tk.ExecToErr("update t set a = '123' where a = '12';") require.True(t, types.ErrDataTooLong.Equal(err)) require.EqualError(t, err, "[types:1406]Data too long for column 'a' at row 1") } @@ -2648,7 +2647,7 @@ func TestReplaceLog(t *testing.T) { err = txn.Commit(context.Background()) require.NoError(t, err) - _, err = tk.Exec(`replace into testLog values (0, 0), (1, 1);`) + err = tk.ExecToErr(`replace into testLog values (0, 0), (1, 1);`) require.Error(t, err) require.EqualError(t, err, `can not be duplicated row, due to old row not found. handle 1 not found`) tk.MustQuery(`admin cleanup index testLog b;`).Check(testkit.Rows("1")) @@ -2676,7 +2675,7 @@ func TestRebaseIfNeeded(t *testing.T) { require.NoError(t, err) txn, err := ctx.Txn(true) require.NoError(t, err) - require.Nil(t, txn.Commit(context.Background())) + require.NoError(t, txn.Commit(context.Background())) tk.MustExec(`update t set b = 3 where a = 30001;`) tk.MustExec(`insert into t (b) values (4);`) @@ -2705,8 +2704,7 @@ func TestDeferConstraintCheckForDelete(t *testing.T) { tk.MustExec("begin") tk.MustExec("insert into t1 values(1, 3)") tk.MustExec("delete from t1 where j = 3") - _, err := tk.Exec("commit") - require.EqualError(t, err, "previous statement: delete from t1 where j = 3: [kv:1062]Duplicate entry '1' for key 'PRIMARY'") + tk.MustGetErrMsg("commit", "previous statement: delete from t1 where j = 3: [kv:1062]Duplicate entry '1' for key 'PRIMARY'") tk.MustExec("rollback") tk.MustExec("create table t2(i int, j int, unique index idx(i))") @@ -2714,8 +2712,7 @@ func TestDeferConstraintCheckForDelete(t *testing.T) { tk.MustExec("begin") tk.MustExec("insert into t2 values(1, 3)") tk.MustExec("delete from t2 where j = 3") - _, err = tk.Exec("commit") - require.EqualError(t, err, "previous statement: delete from t2 where j = 3: [kv:1062]Duplicate entry '1' for key 'idx'") + tk.MustGetErrMsg("commit", "previous statement: delete from t2 where j = 3: [kv:1062]Duplicate entry '1' for key 'idx'") tk.MustExec("admin check table t2") tk.MustExec("create table t3(i int, j int, primary key(i))") @@ -2751,14 +2748,14 @@ func TestDeferConstraintCheckForInsert(t *testing.T) { tk.MustExec(`drop table if exists t;create table t (a int primary key, b int);`) tk.MustExec(`insert into t values (1,2),(2,2)`) - _, err := tk.Exec("update t set a=a+1 where b=2") + err := tk.ExecToErr("update t set a=a+1 where b=2") require.Error(t, err) tk.MustExec(`drop table if exists t;create table t (i int key);`) tk.MustExec(`insert t values (1);`) tk.MustExec(`set tidb_constraint_check_in_place = 1;`) tk.MustExec(`begin;`) - _, err = tk.Exec(`insert t values (1);`) + err = tk.ExecToErr(`insert t values (1);`) require.Error(t, err) tk.MustExec(`update t set i = 2 where i = 1;`) tk.MustExec(`commit;`) @@ -2767,9 +2764,9 @@ func TestDeferConstraintCheckForInsert(t *testing.T) { tk.MustExec(`set tidb_constraint_check_in_place = 0;`) tk.MustExec("replace into t values (1),(2)") tk.MustExec("begin") - _, err = tk.Exec("update t set i = 2 where i = 1") + err = tk.ExecToErr("update t set i = 2 where i = 1") require.Error(t, err) - _, err = tk.Exec("insert into t values (1) on duplicate key update i = i + 1") + err = tk.ExecToErr("insert into t values (1) on duplicate key update i = i + 1") require.Error(t, err) tk.MustExec("rollback") @@ -2778,16 +2775,16 @@ func TestDeferConstraintCheckForInsert(t *testing.T) { tk.MustExec(`set tidb_constraint_check_in_place = 1;`) tk.MustExec(`set @@autocommit = 0;`) - _, err = tk.Exec("insert into t values (3, 1)") + err = tk.ExecToErr("insert into t values (3, 1)") require.Error(t, err) - _, err = tk.Exec("insert into t values (1, 3)") + err = tk.ExecToErr("insert into t values (1, 3)") require.Error(t, err) tk.MustExec("commit") tk.MustExec(`set tidb_constraint_check_in_place = 0;`) tk.MustExec("insert into t values (3, 1)") tk.MustExec("insert into t values (1, 3)") - _, err = tk.Exec("commit") + err = tk.ExecToErr("commit") require.Error(t, err) // Cover the temporary table. @@ -2798,12 +2795,12 @@ func TestDeferConstraintCheckForInsert(t *testing.T) { tk.MustExec("create global temporary table t (a int primary key, b int) on commit delete rows") tk.MustExec("begin") tk.MustExec("insert into t values (1, 1)") - _, err = tk.Exec(`insert into t values (1, 3)`) + err = tk.ExecToErr(`insert into t values (1, 3)`) require.Error(t, err) tk.MustExec("insert into t values (2, 2)") - _, err = tk.Exec("update t set a = a + 1 where a = 1") + err = tk.ExecToErr("update t set a = a + 1 where a = 1") require.Error(t, err) - _, err = tk.Exec("insert into t values (1, 3) on duplicated key update a = a + 1") + err = tk.ExecToErr("insert into t values (1, 3) on duplicated key update a = a + 1") require.Error(t, err) tk.MustExec("commit") @@ -2811,12 +2808,12 @@ func TestDeferConstraintCheckForInsert(t *testing.T) { tk.MustExec("create global temporary table t (a int, b int unique) on commit delete rows") tk.MustExec("begin") tk.MustExec("insert into t values (1, 1)") - _, err = tk.Exec(`insert into t values (3, 1)`) + err = tk.ExecToErr(`insert into t values (3, 1)`) require.Error(t, err) tk.MustExec("insert into t values (2, 2)") - _, err = tk.Exec("update t set b = b + 1 where a = 1") + err = tk.ExecToErr("update t set b = b + 1 where a = 1") require.Error(t, err) - _, err = tk.Exec("insert into t values (3, 1) on duplicated key update b = b + 1") + err = tk.ExecToErr("insert into t values (3, 1) on duplicated key update b = b + 1") require.Error(t, err) tk.MustExec("commit") @@ -2825,22 +2822,22 @@ func TestDeferConstraintCheckForInsert(t *testing.T) { tk.MustExec("create temporary table tl (a int primary key, b int)") tk.MustExec("begin") tk.MustExec("insert into tl values (1, 1)") - _, err = tk.Exec(`insert into tl values (1, 3)`) + err = tk.ExecToErr(`insert into tl values (1, 3)`) require.Error(t, err) tk.MustExec("insert into tl values (2, 2)") - _, err = tk.Exec("update tl set a = a + 1 where a = 1") + err = tk.ExecToErr("update tl set a = a + 1 where a = 1") require.Error(t, err) - _, err = tk.Exec("insert into tl values (1, 3) on duplicated key update a = a + 1") + err = tk.ExecToErr("insert into tl values (1, 3) on duplicated key update a = a + 1") require.Error(t, err) tk.MustExec("commit") tk.MustExec("begin") tk.MustQuery("select * from tl").Check(testkit.Rows("1 1", "2 2")) - _, err = tk.Exec(`insert into tl values (1, 3)`) + err = tk.ExecToErr(`insert into tl values (1, 3)`) require.Error(t, err) - _, err = tk.Exec("update tl set a = a + 1 where a = 1") + err = tk.ExecToErr("update tl set a = a + 1 where a = 1") require.Error(t, err) - _, err = tk.Exec("insert into tl values (1, 3) on duplicated key update a = a + 1") + err = tk.ExecToErr("insert into tl values (1, 3) on duplicated key update a = a + 1") require.Error(t, err) tk.MustExec("rollback") @@ -2848,22 +2845,22 @@ func TestDeferConstraintCheckForInsert(t *testing.T) { tk.MustExec("create temporary table tl (a int, b int unique)") tk.MustExec("begin") tk.MustExec("insert into tl values (1, 1)") - _, err = tk.Exec(`insert into tl values (3, 1)`) + err = tk.ExecToErr(`insert into tl values (3, 1)`) require.Error(t, err) tk.MustExec("insert into tl values (2, 2)") - _, err = tk.Exec("update tl set b = b + 1 where a = 1") + err = tk.ExecToErr("update tl set b = b + 1 where a = 1") require.Error(t, err) - _, err = tk.Exec("insert into tl values (3, 1) on duplicated key update b = b + 1") + err = tk.ExecToErr("insert into tl values (3, 1) on duplicated key update b = b + 1") require.Error(t, err) tk.MustExec("commit") tk.MustExec("begin") tk.MustQuery("select * from tl").Check(testkit.Rows("1 1", "2 2")) - _, err = tk.Exec(`insert into tl values (3, 1)`) + err = tk.ExecToErr(`insert into tl values (3, 1)`) require.Error(t, err) - _, err = tk.Exec("update tl set b = b + 1 where a = 1") + err = tk.ExecToErr("update tl set b = b + 1 where a = 1") require.Error(t, err) - _, err = tk.Exec("insert into tl values (3, 1) on duplicated key update b = b + 1") + err = tk.ExecToErr("insert into tl values (3, 1) on duplicated key update b = b + 1") require.Error(t, err) tk.MustExec("rollback") } @@ -2916,7 +2913,7 @@ func TestIssue11059(t *testing.T) { tk.MustExec("create table t (pk int primary key, uk int unique, v int)") tk.MustExec("insert into t values (2, 11, 215)") tk.MustExec("insert into t values (3, 7, 2111)") - _, err := tk.Exec("update t set pk = 2 where uk = 7") + err := tk.ExecToErr("update t set pk = 2 where uk = 7") require.Error(t, err) } @@ -2953,7 +2950,7 @@ func TestSetWithRefGenCol(t *testing.T) { tk.MustExec(`create table t2 (j int(11) GENERATED ALWAYS AS (i + 1) stored not null, i int(11) DEFAULT '5');`) tk.MustExec(`insert into t2 set i = j + 9`) tk.MustQuery("select * from t2").Check(testkit.Rows("10 9")) - _, err := tk.Exec(`insert into t2 set j = i + 1`) + err := tk.ExecToErr(`insert into t2 set j = i + 1`) require.Error(t, err) tk.MustExec(`insert into t2 set i = j + 100`) tk.MustQuery("select * from t2").Check(testkit.Rows("10 9", "101 100")) @@ -2961,7 +2958,7 @@ func TestSetWithRefGenCol(t *testing.T) { tk.MustExec(`create table t3(j int(11) GENERATED ALWAYS AS (i + 1) stored, i int(11) DEFAULT '5');`) tk.MustExec(`insert into t3 set i = j + 100`) tk.MustQuery("select * from t3").Check(testkit.Rows(" ")) - _, err = tk.Exec(`insert into t3 set j = i + 1`) + err = tk.ExecToErr(`insert into t3 set j = i + 1`) require.Error(t, err) } @@ -3054,10 +3051,8 @@ func TestWriteListPartitionTable(t *testing.T) { // Test insert error tk.MustExec("insert into t values (1, 'a')") - _, err := tk.Exec("insert into t values (1, 'd')") - require.EqualError(t, err, "[kv:1062]Duplicate entry '1' for key 'idx'") - _, err = tk.Exec("insert into t values (100, 'd')") - require.EqualError(t, err, "[table:1526]Table has no partition for value 100") + tk.MustGetErrMsg("insert into t values (1, 'd')", "[kv:1062]Duplicate entry '1' for key 'idx'") + tk.MustGetErrMsg("insert into t values (100, 'd')", "[table:1526]Table has no partition for value 100") tk.MustExec("admin check table t;") // Test select partition @@ -3104,9 +3099,9 @@ func TestWriteListColumnsPartitionTable(t *testing.T) { // Test insert error tk.MustExec("insert into t values (1, 'a')") - _, err := tk.Exec("insert into t values (1, 'd')") + err := tk.ExecToErr("insert into t values (1, 'd')") require.EqualError(t, err, "[kv:1062]Duplicate entry '1' for key 'idx'") - _, err = tk.Exec("insert into t values (100, 'd')") + err = tk.ExecToErr("insert into t values (100, 'd')") require.EqualError(t, err, "[table:1526]Table has no partition for value from column_list") tk.MustExec("admin check table t;") @@ -3139,7 +3134,7 @@ func TestWriteListPartitionTable1(t *testing.T) { // Test add unique index failed. tk.MustExec("insert into t values (1, 'a'),(1,'b')") - _, err := tk.Exec("alter table t add unique index idx (id)") + err := tk.ExecToErr("alter table t add unique index idx (id)") require.EqualError(t, err, "[kv:1062]Duplicate entry '1' for key 'idx'") // Test add unique index success. tk.MustExec("delete from t where name='b'") @@ -3164,7 +3159,7 @@ func TestWriteListPartitionTable1(t *testing.T) { tk.MustQuery("select * from t partition(p2) order by id").Check(testkit.Rows("4 e")) tk.MustQuery("select * from t partition(p3) order by id").Check(testkit.Rows()) // Test insert on duplicate error - _, err = tk.Exec("insert into t values (3, 'a'), (11,'x') on duplicate key update id=id+1") + err = tk.ExecToErr("insert into t values (3, 'a'), (11,'x') on duplicate key update id=id+1") require.EqualError(t, err, "[kv:1062]Duplicate entry '4' for key 'idx'") tk.MustQuery("select * from t order by id").Check(testkit.Rows("1 x", "3 x", "4 e", "5 g")) // Test insert ignore with duplicate @@ -3179,7 +3174,7 @@ func TestWriteListPartitionTable1(t *testing.T) { tk.MustQuery("select * from t partition(p0,p1,p2) order by id").Check(testkit.Rows("1 x", "3 x", "4 e", "5 g", "17 a")) tk.MustQuery("select * from t partition(p3) order by id").Check(testkit.Rows(" y", "15 a")) // Test insert meet no partition error. - _, err = tk.Exec("insert into t values (100, 'd')") + err = tk.ExecToErr("insert into t values (100, 'd')") require.EqualError(t, err, "[table:1526]Table has no partition for value 100") // --------------------------Test update--------------------------- @@ -3193,7 +3188,7 @@ func TestWriteListPartitionTable1(t *testing.T) { tk.MustExec("update t set name='y' where id < 3") tk.MustQuery("select * from t order by id").Check(testkit.Rows("1 y", "2 y", "3 c")) // Test update meet duplicate error. - _, err = tk.Exec("update t set id=2 where id = 1") + err = tk.ExecToErr("update t set id=2 where id = 1") require.EqualError(t, err, "[kv:1062]Duplicate entry '2' for key 'idx'") tk.MustQuery("select * from t order by id").Check(testkit.Rows("1 y", "2 y", "3 c")) @@ -3205,11 +3200,11 @@ func TestWriteListPartitionTable1(t *testing.T) { tk.MustExec("update t set id=id*10 where id in (1,2)") tk.MustQuery("select * from t order by id").Check(testkit.Rows("3 a", "10 a", "20 a")) // Test update meet duplicate error. - _, err = tk.Exec("update t set id=id+17 where id in (3,10)") + err = tk.ExecToErr("update t set id=id+17 where id in (3,10)") require.EqualError(t, err, "[kv:1062]Duplicate entry '20' for key 'idx'") tk.MustQuery("select * from t order by id").Check(testkit.Rows("3 a", "10 a", "20 a")) // Test update meet no partition error. - _, err = tk.Exec("update t set id=id*2 where id in (3,20)") + err = tk.ExecToErr("update t set id=id*2 where id in (3,20)") require.EqualError(t, err, "[table:1526]Table has no partition for value 40") tk.MustQuery("select * from t order by id").Check(testkit.Rows("3 a", "10 a", "20 a")) @@ -3228,7 +3223,7 @@ func TestWriteListPartitionTable1(t *testing.T) { tk.MustExec("replace into t values (1, 'x'),(7,'x')") tk.MustQuery("select * from t order by id").Check(testkit.Rows("1 x", "2 b", "3 c", "4 d", "7 x")) // Test replace meet no partition error. - _, err = tk.Exec("replace into t values (10,'x'),(50,'x')") + err = tk.ExecToErr("replace into t values (10,'x'),(50,'x')") require.EqualError(t, err, "[table:1526]Table has no partition for value 50") tk.MustQuery("select * from t order by id").Check(testkit.Rows("1 x", "2 b", "3 c", "4 d", "7 x")) @@ -3267,7 +3262,7 @@ func TestWriteListPartitionTable2(t *testing.T) { // Test add unique index failed. tk.MustExec("insert into t (id,name) values (1, 'a'),(1,'b')") - _, err := tk.Exec("alter table t add unique index idx (id,b)") + err := tk.ExecToErr("alter table t add unique index idx (id,b)") require.EqualError(t, err, "[kv:1062]Duplicate entry '1-2' for key 'idx'") // Test add unique index success. tk.MustExec("delete from t where name='b'") @@ -3292,7 +3287,7 @@ func TestWriteListPartitionTable2(t *testing.T) { tk.MustQuery("select id,name from t partition(p2) order by id").Check(testkit.Rows("4 e")) tk.MustQuery("select id,name from t partition(p3) order by id").Check(testkit.Rows()) // Test insert on duplicate error - _, err = tk.Exec("insert into t (id,name) values (3, 'a'), (11,'x') on duplicate key update id=id+1") + err = tk.ExecToErr("insert into t (id,name) values (3, 'a'), (11,'x') on duplicate key update id=id+1") require.EqualError(t, err, "[kv:1062]Duplicate entry '4-2' for key 'idx'") tk.MustQuery("select id,name from t order by id").Check(testkit.Rows("1 x", "3 x", "4 e", "5 g")) // Test insert ignore with duplicate @@ -3307,7 +3302,7 @@ func TestWriteListPartitionTable2(t *testing.T) { tk.MustQuery("select id,name from t partition(p0,p1,p2) order by id").Check(testkit.Rows("1 x", "3 x", "4 e", "5 g", "17 a")) tk.MustQuery("select id,name from t partition(p3) order by id").Check(testkit.Rows(" y", "15 a")) // Test insert meet no partition error. - _, err = tk.Exec("insert into t (id,name) values (100, 'd')") + err = tk.ExecToErr("insert into t (id,name) values (100, 'd')") require.EqualError(t, err, "[table:1526]Table has no partition for value 100") // --------------------------Test update--------------------------- @@ -3321,8 +3316,7 @@ func TestWriteListPartitionTable2(t *testing.T) { tk.MustExec("update t set name='y' where id < 3") tk.MustQuery("select id,name from t order by id").Check(testkit.Rows("1 y", "2 y", "3 c")) // Test update meet duplicate error. - _, err = tk.Exec("update t set id=2 where id = 1") - require.EqualError(t, err, "[kv:1062]Duplicate entry '2-2' for key 'idx'") + tk.MustGetErrMsg("update t set id=2 where id = 1", "[kv:1062]Duplicate entry '2-2' for key 'idx'") tk.MustQuery("select id,name from t order by id").Check(testkit.Rows("1 y", "2 y", "3 c")) // Test update multi-partitions @@ -3333,12 +3327,10 @@ func TestWriteListPartitionTable2(t *testing.T) { tk.MustExec("update t set id=id*10 where id in (1,2)") tk.MustQuery("select id,name from t order by id").Check(testkit.Rows("3 a", "10 a", "20 a")) // Test update meet duplicate error. - _, err = tk.Exec("update t set id=id+17 where id in (3,10)") - require.EqualError(t, err, "[kv:1062]Duplicate entry '20-2' for key 'idx'") + tk.MustGetErrMsg("update t set id=id+17 where id in (3,10)", "[kv:1062]Duplicate entry '20-2' for key 'idx'") tk.MustQuery("select id,name from t order by id").Check(testkit.Rows("3 a", "10 a", "20 a")) // Test update meet no partition error. - _, err = tk.Exec("update t set id=id*2 where id in (3,20)") - require.EqualError(t, err, "[table:1526]Table has no partition for value 40") + tk.MustGetErrMsg("update t set id=id*2 where id in (3,20)", "[table:1526]Table has no partition for value 40") tk.MustQuery("select id,name from t order by id").Check(testkit.Rows("3 a", "10 a", "20 a")) // --------------------------Test replace--------------------------- @@ -3356,8 +3348,7 @@ func TestWriteListPartitionTable2(t *testing.T) { tk.MustExec("replace into t (id,name) values (1, 'x'),(7,'x')") tk.MustQuery("select id,name from t order by id").Check(testkit.Rows("1 x", "2 b", "3 c", "4 d", "7 x")) // Test replace meet no partition error. - _, err = tk.Exec("replace into t (id,name) values (10,'x'),(50,'x')") - require.EqualError(t, err, "[table:1526]Table has no partition for value 50") + tk.MustGetErrMsg("replace into t (id,name) values (10,'x'),(50,'x')", "[table:1526]Table has no partition for value 50") tk.MustQuery("select id,name from t order by id").Check(testkit.Rows("1 x", "2 b", "3 c", "4 d", "7 x")) // --------------------------Test delete--------------------------- @@ -3394,8 +3385,7 @@ func TestWriteListColumnsPartitionTable1(t *testing.T) { // Test add unique index failed. tk.MustExec("insert into t values (1, 'a'),(1,'b')") - _, err := tk.Exec("alter table t add unique index idx (id)") - require.EqualError(t, err, "[kv:1062]Duplicate entry '1' for key 'idx'") + tk.MustGetErrMsg("alter table t add unique index idx (id)", "[kv:1062]Duplicate entry '1' for key 'idx'") // Test add unique index success. tk.MustExec("delete from t where name='b'") tk.MustExec("alter table t add unique index idx (id)") @@ -3419,8 +3409,7 @@ func TestWriteListColumnsPartitionTable1(t *testing.T) { tk.MustQuery("select * from t partition(p2) order by id").Check(testkit.Rows("4 e")) tk.MustQuery("select * from t partition(p3) order by id").Check(testkit.Rows()) // Test insert on duplicate error - _, err = tk.Exec("insert into t values (3, 'a'), (11,'x') on duplicate key update id=id+1") - require.EqualError(t, err, "[kv:1062]Duplicate entry '4' for key 'idx'") + tk.MustGetErrMsg("insert into t values (3, 'a'), (11,'x') on duplicate key update id=id+1", "[kv:1062]Duplicate entry '4' for key 'idx'") tk.MustQuery("select * from t order by id").Check(testkit.Rows("1 x", "3 x", "4 e", "5 g")) // Test insert ignore with duplicate tk.MustExec("insert ignore into t values (1, 'b'), (5,'a'),(null,'y')") @@ -3434,8 +3423,7 @@ func TestWriteListColumnsPartitionTable1(t *testing.T) { tk.MustQuery("select * from t partition(p0,p1,p2) order by id").Check(testkit.Rows("1 x", "3 x", "4 e", "5 g", "17 a")) tk.MustQuery("select * from t partition(p3) order by id").Check(testkit.Rows(" y", "15 a")) // Test insert meet no partition error. - _, err = tk.Exec("insert into t values (100, 'd')") - require.EqualError(t, err, "[table:1526]Table has no partition for value from column_list") + tk.MustGetErrMsg("insert into t values (100, 'd')", "[table:1526]Table has no partition for value from column_list") // --------------------------Test update--------------------------- // Test update 1 partition. @@ -3448,8 +3436,7 @@ func TestWriteListColumnsPartitionTable1(t *testing.T) { tk.MustExec("update t set name='y' where id < 3") tk.MustQuery("select * from t order by id").Check(testkit.Rows("1 y", "2 y", "3 c")) // Test update meet duplicate error. - _, err = tk.Exec("update t set id=2 where id = 1") - require.EqualError(t, err, "[kv:1062]Duplicate entry '2' for key 'idx'") + tk.MustGetErrMsg("update t set id=2 where id = 1", "[kv:1062]Duplicate entry '2' for key 'idx'") tk.MustQuery("select * from t order by id").Check(testkit.Rows("1 y", "2 y", "3 c")) // Test update multi-partitions @@ -3460,12 +3447,10 @@ func TestWriteListColumnsPartitionTable1(t *testing.T) { tk.MustExec("update t set id=id*10 where id in (1,2)") tk.MustQuery("select * from t order by id").Check(testkit.Rows("3 a", "10 a", "20 a")) // Test update meet duplicate error. - _, err = tk.Exec("update t set id=id+17 where id in (3,10)") - require.EqualError(t, err, "[kv:1062]Duplicate entry '20' for key 'idx'") + tk.MustGetErrMsg("update t set id=id+17 where id in (3,10)", "[kv:1062]Duplicate entry '20' for key 'idx'") tk.MustQuery("select * from t order by id").Check(testkit.Rows("3 a", "10 a", "20 a")) // Test update meet no partition error. - _, err = tk.Exec("update t set id=id*2 where id in (3,20)") - require.EqualError(t, err, "[table:1526]Table has no partition for value from column_list") + tk.MustGetErrMsg("update t set id=id*2 where id in (3,20)", "[table:1526]Table has no partition for value from column_list") tk.MustQuery("select * from t order by id").Check(testkit.Rows("3 a", "10 a", "20 a")) // --------------------------Test replace--------------------------- @@ -3483,8 +3468,7 @@ func TestWriteListColumnsPartitionTable1(t *testing.T) { tk.MustExec("replace into t values (1, 'x'),(7,'x')") tk.MustQuery("select * from t order by id").Check(testkit.Rows("1 x", "2 b", "3 c", "4 d", "7 x")) // Test replace meet no partition error. - _, err = tk.Exec("replace into t values (10,'x'),(100,'x')") - require.EqualError(t, err, "[table:1526]Table has no partition for value from column_list") + tk.MustGetErrMsg("replace into t values (10,'x'),(100,'x')", "[table:1526]Table has no partition for value from column_list") tk.MustQuery("select * from t order by id").Check(testkit.Rows("1 x", "2 b", "3 c", "4 d", "7 x")) // --------------------------Test delete--------------------------- @@ -3521,7 +3505,7 @@ func TestWriteListColumnsPartitionTable2(t *testing.T) { // Test add unique index failed. tk.MustExec("insert into t values ('w', 1, 1),('w', 1, 2)") - _, err := tk.Exec("alter table t add unique index idx (location,id)") + err := tk.ExecToErr("alter table t add unique index idx (location,id)") require.EqualError(t, err, "[kv:1062]Duplicate entry 'w-1' for key 'idx'") // Test add unique index success. tk.MustExec("delete from t where a=2") @@ -3548,8 +3532,7 @@ func TestWriteListColumnsPartitionTable2(t *testing.T) { tk.MustQuery("select * from t partition(p_west) order by id").Check(testkit.Rows()) // Test insert on duplicate error tk.MustExec("insert into t values ('w', 2, 2), ('w', 1, 1)") - _, err = tk.Exec("insert into t values ('w', 2, 3) on duplicate key update id=1") - require.EqualError(t, err, "[kv:1062]Duplicate entry 'w-1' for key 'idx'") + tk.MustGetErrMsg("insert into t values ('w', 2, 3) on duplicate key update id=1", "[kv:1062]Duplicate entry 'w-1' for key 'idx'") tk.MustQuery("select * from t partition(p_west) order by id").Check(testkit.Rows("w 1 1", "w 2 2")) // Test insert ignore with duplicate tk.MustExec("insert ignore into t values ('w', 2, 2), ('w', 3, 3), ('n', 10, 10)") @@ -3561,14 +3544,10 @@ func TestWriteListColumnsPartitionTable2(t *testing.T) { tk.MustQuery("select * from t partition(p_west) order by id").Check(testkit.Rows("w 1 1", "w 2 2", "w 3 3", "w 4 4")) tk.MustQuery("select * from t partition(p_south) order by id").Check(testkit.Rows("s 13 2", "s 14 14")) // Test insert meet no partition error. - _, err = tk.Exec("insert into t values ('w', 5, 5)") - require.EqualError(t, err, "[table:1526]Table has no partition for value from column_list") - _, err = tk.Exec("insert into t values ('s', 5, 5)") - require.EqualError(t, err, "[table:1526]Table has no partition for value from column_list") - _, err = tk.Exec("insert into t values ('s', 100, 5)") - require.EqualError(t, err, "[table:1526]Table has no partition for value from column_list") - _, err = tk.Exec("insert into t values ('x', 1, 5)") - require.EqualError(t, err, "[table:1526]Table has no partition for value from column_list") + tk.MustGetErrMsg("insert into t values ('w', 5, 5)", "[table:1526]Table has no partition for value from column_list") + tk.MustGetErrMsg("insert into t values ('s', 5, 5)", "[table:1526]Table has no partition for value from column_list") + tk.MustGetErrMsg("insert into t values ('s', 100, 5)", "[table:1526]Table has no partition for value from column_list") + tk.MustGetErrMsg("insert into t values ('x', 1, 5)", "[table:1526]Table has no partition for value from column_list") // --------------------------Test update--------------------------- // Test update 1 partition. @@ -3585,7 +3564,7 @@ func TestWriteListColumnsPartitionTable2(t *testing.T) { tk.MustExec("update t set a=a+id where id>1") tk.MustQuery("select * from t partition(p_west) order by id,a").Check(testkit.Rows("w 1 5", "w 2 5", "w 3 6")) // Test update meet duplicate error. - _, err = tk.Exec("update t set id=id+1 where location='w' and id<2") + err = tk.ExecToErr("update t set id=id+1 where location='w' and id<2") require.EqualError(t, err, "[kv:1062]Duplicate entry 'w-2' for key 'idx'") tk.MustQuery("select * from t partition(p_west) order by id,a").Check(testkit.Rows("w 1 5", "w 2 5", "w 3 6")) @@ -3603,11 +3582,11 @@ func TestWriteListColumnsPartitionTable2(t *testing.T) { tk.MustExec("update t set a=a+1 where location='n' and id=11") tk.MustQuery("select * from t order by id").Check(testkit.Rows("w 1 4", "w 2 4", "e 8 9", "n 11 15")) // Test update meet duplicate error. - _, err = tk.Exec("update t set id=id+1 where location='w' and id in (1,2)") + err = tk.ExecToErr("update t set id=id+1 where location='w' and id in (1,2)") require.EqualError(t, err, "[kv:1062]Duplicate entry 'w-2' for key 'idx'") tk.MustQuery("select * from t order by id").Check(testkit.Rows("w 1 4", "w 2 4", "e 8 9", "n 11 15")) // Test update meet no partition error. - _, err = tk.Exec("update t set id=id+3 where location='w' and id in (1,2)") + err = tk.ExecToErr("update t set id=id+3 where location='w' and id in (1,2)") require.EqualError(t, err, "[table:1526]Table has no partition for value from column_list") tk.MustQuery("select * from t order by id").Check(testkit.Rows("w 1 4", "w 2 4", "e 8 9", "n 11 15")) // Test update that move from partition 1 to partition 2. @@ -3628,14 +3607,10 @@ func TestWriteListColumnsPartitionTable2(t *testing.T) { tk.MustExec("replace into t values ('w', 1, 2),('n', 10, 10)") tk.MustQuery("select * from t order by id").Check(testkit.Rows("w 1 2", "e 5 5", "n 9 9", "n 10 10")) // Test replace meet no partition error. - _, err = tk.Exec("replace into t values ('w', 5, 5)") - require.EqualError(t, err, "[table:1526]Table has no partition for value from column_list") - _, err = tk.Exec("replace into t values ('s', 5, 5)") - require.EqualError(t, err, "[table:1526]Table has no partition for value from column_list") - _, err = tk.Exec("replace into t values ('s', 100, 5)") - require.EqualError(t, err, "[table:1526]Table has no partition for value from column_list") - _, err = tk.Exec("replace into t values ('x', 1, 5)") - require.EqualError(t, err, "[table:1526]Table has no partition for value from column_list") + tk.MustGetErrMsg("replace into t values ('w', 5, 5)", "[table:1526]Table has no partition for value from column_list") + tk.MustGetErrMsg("replace into t values ('s', 5, 5)", "[table:1526]Table has no partition for value from column_list") + tk.MustGetErrMsg("replace into t values ('s', 100, 5)", "[table:1526]Table has no partition for value from column_list") + tk.MustGetErrMsg("replace into t values ('x', 1, 5)", "[table:1526]Table has no partition for value from column_list") // --------------------------Test delete--------------------------- // Test delete 1 partition. @@ -3664,7 +3639,7 @@ func TestWriteListPartitionTableIssue21437(t *testing.T) { tk.MustExec("set @@session.tidb_enable_list_partition = ON") tk.MustExec("drop table if exists t") tk.MustExec(`create table t (a int) partition by list (a%10) (partition p0 values in (0,1));`) - _, err := tk.Exec("replace into t values (null)") + err := tk.ExecToErr("replace into t values (null)") require.EqualError(t, err, "[table:1526]Table has no partition for value NULL") } @@ -3757,10 +3732,10 @@ func TestListPartitionWithGeneratedColumn(t *testing.T) { tk.MustQuery("select count(1) from t").Check(testkit.Rows("4")) // Test for insert meet no partition error - _, err := tk.Exec("insert into t (a) values (11)") + err := tk.ExecToErr("insert into t (a) values (11)") require.True(t, table.ErrNoPartitionForGivenValue.Equal(err)) // Test for update meet no partition error - _, err = tk.Exec("update t set a=a+10 where a = 2") + err = tk.ExecToErr("update t set a=a+10 where a = 2") require.True(t, table.ErrNoPartitionForGivenValue.Equal(err)) } } @@ -3800,10 +3775,10 @@ func TestListPartitionWithGeneratedColumn1(t *testing.T) { tk.MustQuery("select count(1) from t").Check(testkit.Rows("4")) // Test for insert meet no partition error - _, err := tk.Exec("insert into t (a) values (11)") + err := tk.ExecToErr("insert into t (a) values (11)") require.EqualError(t, err, "[table:1526]Table has no partition for value 2011") // Test for update meet no partition error - _, err = tk.Exec("update t set a=a+10 where a = 2") + err = tk.ExecToErr("update t set a=a+10 where a = 2") require.EqualError(t, err, "[table:1526]Table has no partition for value 2012") tk.MustExec("delete from t") @@ -3826,11 +3801,9 @@ func TestListPartitionWithGeneratedColumn1(t *testing.T) { tk.MustQuery("select count(1) from t").Check(testkit.Rows("4")) // Test for insert meet no partition error - _, err = tk.Exec("insert into t (a) values (2011)") - require.EqualError(t, err, "[table:1526]Table has no partition for value 2011") + tk.MustGetErrMsg("insert into t (a) values (2011)", "[table:1526]Table has no partition for value 2011") // Test for update meet no partition error - _, err = tk.Exec("update t set a=a+10 where a = 2002") - require.EqualError(t, err, "[table:1526]Table has no partition for value 2012") + tk.MustGetErrMsg("update t set a=a+10 where a = 2002", "[table:1526]Table has no partition for value 2012") } } @@ -3911,7 +3884,7 @@ func TestIssue22496(t *testing.T) { tk.MustExec("use test") tk.MustExec("drop table if exists t12") tk.MustExec("create table t12(d decimal(15,2));") - _, err := tk.Exec("insert into t12 values('1,9999.00')") + err := tk.ExecToErr("insert into t12 values('1,9999.00')") require.Error(t, err) tk.MustExec("set sql_mode=''") tk.MustExec("insert into t12 values('1,999.00');") @@ -4021,7 +3994,7 @@ func TestUpdate(t *testing.T) { tk.MustExec("begin") tk.MustExec("create table update_test(id int not null auto_increment, name varchar(255), index(id))") tk.MustExec("insert into update_test(name) values ('aa')") - _, err := tk.Exec("update update_test set id = null where name = 'aa'") + err := tk.ExecToErr("update update_test set id = null where name = 'aa'") require.EqualError(t, err, "[table:1048]Column 'id' cannot be null") tk.MustExec("drop table update_test") @@ -4038,7 +4011,7 @@ func TestUpdate(t *testing.T) { tk.MustExec("create table update_unique (id int primary key, name int unique)") tk.MustExec("insert update_unique values (1, 1), (2, 2);") tk.MustExec("begin") - _, err = tk.Exec("update update_unique set name = 1 where id = 2") + err = tk.ExecToErr("update update_unique set name = 1 where id = 2") require.Error(t, err) tk.MustExec("commit") tk.MustQuery("select * from update_unique").Check(testkit.Rows("1 1", "2 2")) @@ -4048,7 +4021,7 @@ func TestUpdate(t *testing.T) { tk.MustExec("create table t(a bigint, primary key (a));") tk.MustExec("insert into t values (1)") tk.MustExec("insert into t values (2)") - _, err = tk.Exec("update ignore t set a = 1 where a = 2;") + err = tk.ExecToErr("update ignore t set a = 1 where a = 2;") require.NoError(t, err) require.Equal(t, tk.Session().LastMessage(), "Rows matched: 1 Changed: 0 Warnings: 1") r = tk.MustQuery("SHOW WARNINGS;") @@ -4056,7 +4029,7 @@ func TestUpdate(t *testing.T) { tk.MustQuery("select * from t").Check(testkit.Rows("1", "2")) // test update ignore for truncate as warning - _, err = tk.Exec("update ignore t set a = 1 where a = (select '2a')") + err = tk.ExecToErr("update ignore t set a = 1 where a = (select '2a')") require.NoError(t, err) r = tk.MustQuery("SHOW WARNINGS;") r.Check(testkit.Rows("Warning 1292 Truncated incorrect DOUBLE value: '2a'", "Warning 1292 Truncated incorrect DOUBLE value: '2a'", "Warning 1062 Duplicate entry '1' for key 'PRIMARY'")) @@ -4069,7 +4042,7 @@ func TestUpdate(t *testing.T) { tk.MustExec("create table t(a bigint, unique key I_uniq (a));") tk.MustExec("insert into t values (1)") tk.MustExec("insert into t values (2)") - _, err = tk.Exec("update ignore t set a = 1 where a = 2;") + err = tk.ExecToErr("update ignore t set a = 1 where a = 2;") require.NoError(t, err) require.Equal(t, tk.Session().LastMessage(), "Rows matched: 1 Changed: 0 Warnings: 1") r = tk.MustQuery("SHOW WARNINGS;") @@ -4143,7 +4116,7 @@ func TestUpdate(t *testing.T) { tk.MustExec("drop table t") tk.MustExec("CREATE TABLE `t` ( `c1` year DEFAULT NULL, `c2` year DEFAULT NULL, `c3` date DEFAULT NULL, `c4` datetime DEFAULT NULL, KEY `idx` (`c1`,`c2`))") - _, err = tk.Exec("UPDATE t SET c2=16777215 WHERE c1>= -8388608 AND c1 < -9 ORDER BY c1 LIMIT 2") + err = tk.ExecToErr("UPDATE t SET c2=16777215 WHERE c1>= -8388608 AND c1 < -9 ORDER BY c1 LIMIT 2") require.NoError(t, err) tk.MustGetErrCode("update (select * from t) t set c1 = 1111111", mysql.ErrNonUpdatableTable) @@ -4161,7 +4134,7 @@ func TestUpdate(t *testing.T) { // issue 7237, update subquery table should be forbidden tk.MustExec("drop table t") tk.MustExec("create table t (k int, v int)") - _, err = tk.Exec("update t, (select * from t) as b set b.k = t.k") + err = tk.ExecToErr("update t, (select * from t) as b set b.k = t.k") require.EqualError(t, err, "[planner:1288]The target table b of the UPDATE is not updatable") tk.MustExec("update t, (select * from t) as b set t.k = b.k") @@ -4176,7 +4149,7 @@ func TestUpdate(t *testing.T) { tk.MustExec("drop table if exists t;") tk.MustExec("create table t (c1 float(1,1));") tk.MustExec("insert into t values (0.0);") - _, err = tk.Exec("update t set c1 = 2.0;") + err = tk.ExecToErr("update t set c1 = 2.0;") require.True(t, types.ErrWarnDataOutOfRange.Equal(err)) tk.MustExec("drop table if exists t") @@ -4191,7 +4164,7 @@ func TestUpdate(t *testing.T) { tk.MustExec("set @@sql_mode=@orig_sql_mode;") tk.MustExec("create view v as select * from t") - _, err = tk.Exec("update v set a = '2000-11-11'") + err = tk.ExecToErr("update v set a = '2000-11-11'") require.EqualError(t, err, core.ErrViewInvalid.GenWithStackByArgs("test", "v").Error()) tk.MustExec("drop view v") @@ -4281,7 +4254,7 @@ func TestListColumnsPartitionWithGlobalIndex(t *testing.T) { //tk.MustQuery("select a from t partition (p1) order by a").Check(testkit.Rows("bbb")) tk.MustQuery("select * from t where a = 'bbb' order by a").Check(testkit.Rows("bbb b")) // Test insert meet duplicate error. - _, err := tk.Exec("insert into t (a) values ('abc')") + err := tk.ExecToErr("insert into t (a) values ('abc')") require.Error(t, err) // Test insert on duplicate update tk.MustExec("insert into t (a) values ('abc') on duplicate key update a='bbc'") From 7b535b01f08f3f0093d6b6708cc8e1fdea9f388e Mon Sep 17 00:00:00 2001 From: Weizhen Wang Date: Tue, 14 Jun 2022 18:58:45 +0800 Subject: [PATCH 2/2] executor: close RecordSet in the ddl_test and write_test Signed-off-by: Weizhen Wang --- executor/ddl_test.go | 142 +++++++++++++++++-------------------------- testkit/testkit.go | 9 +++ 2 files changed, 64 insertions(+), 87 deletions(-) diff --git a/executor/ddl_test.go b/executor/ddl_test.go index fe4b79fb767d6..bf9da41257e04 100644 --- a/executor/ddl_test.go +++ b/executor/ddl_test.go @@ -102,14 +102,11 @@ func TestCreateTable(t *testing.T) { tk := testkit.NewTestKit(t, store) tk.MustExec("use test") // Test create an exist database - _, err := tk.Exec("CREATE database test") - require.Error(t, err) + tk.MustExecToErr("CREATE database test") // Test create an exist table tk.MustExec("CREATE TABLE create_test (id INT NOT NULL DEFAULT 1, name varchar(255), PRIMARY KEY(id));") - - _, err = tk.Exec("CREATE TABLE create_test (id INT NOT NULL DEFAULT 1, name varchar(255), PRIMARY KEY(id));") - require.Error(t, err) + tk.MustExecToErr("CREATE TABLE create_test (id INT NOT NULL DEFAULT 1, name varchar(255), PRIMARY KEY(id));") // Test "if not exist" tk.MustExec("CREATE TABLE if not exists test(id INT NOT NULL DEFAULT 1, name varchar(255), PRIMARY KEY(id));") @@ -169,14 +166,12 @@ func TestCreateTable(t *testing.T) { // test Err case for multiple collate specified in column when create. tk.MustExec("drop table if exists test_err_multiple_collate;") - _, err = tk.Exec("create table test_err_multiple_collate (a char(1) charset utf8mb4 collate utf8_unicode_ci collate utf8_general_ci) charset utf8mb4 collate utf8mb4_bin") - require.Error(t, err) - require.Equal(t, dbterror.ErrCollationCharsetMismatch.GenWithStackByArgs("utf8_unicode_ci", "utf8mb4").Error(), err.Error()) + tk.MustGetErrMsg("create table test_err_multiple_collate (a char(1) charset utf8mb4 collate utf8_unicode_ci collate utf8_general_ci) charset utf8mb4 collate utf8mb4_bin", + dbterror.ErrCollationCharsetMismatch.GenWithStackByArgs("utf8_unicode_ci", "utf8mb4").Error()) tk.MustExec("drop table if exists test_err_multiple_collate;") - _, err = tk.Exec("create table test_err_multiple_collate (a char(1) collate utf8_unicode_ci collate utf8mb4_general_ci) charset utf8mb4 collate utf8mb4_bin") - require.Error(t, err) - require.Equal(t, dbterror.ErrCollationCharsetMismatch.GenWithStackByArgs("utf8mb4_general_ci", "utf8").Error(), err.Error()) + tk.MustGetErrMsg("create table test_err_multiple_collate (a char(1) collate utf8_unicode_ci collate utf8mb4_general_ci) charset utf8mb4 collate utf8mb4_bin", + dbterror.ErrCollationCharsetMismatch.GenWithStackByArgs("utf8mb4_general_ci", "utf8").Error()) // table option is auto-increment tk.MustExec("drop table if exists create_auto_increment_test;") @@ -458,8 +453,7 @@ func TestCreateDropDatabase(t *testing.T) { tk.MustGetDBError("drop table t;", plannercore.ErrNoDB) tk.MustGetDBError("select * from t;", plannercore.ErrNoDB) - err := tk.ExecToErr("drop database mysql") - require.Error(t, err) + tk.MustExecToErr("drop database mysql") tk.MustExec("create database charset_test charset ascii;") tk.MustQuery("show create database charset_test;").Check(testkit.RowsWithSep("|", @@ -513,9 +507,7 @@ func TestCreateDropTable(t *testing.T) { tk.MustExec("drop table if exists drop_test") tk.MustExec("create table drop_test (a int)") tk.MustExec("drop table drop_test") - - err := tk.ExecToErr("drop table mysql.gc_delete_range") - require.Error(t, err) + tk.MustExecToErr("drop table mysql.gc_delete_range") } func TestCreateDropView(t *testing.T) { @@ -627,8 +619,7 @@ func TestAddNotNullColumnNoDefault(t *testing.T) { require.Equal(t, "0", col2.OriginDefaultValue) tk.MustQuery("select * from nn").Check(testkit.Rows("1 0", "2 0")) - _, err = tk.Exec("insert nn (c1) values (3)") - require.Error(t, err) + tk.MustExecToErr("insert nn (c1) values (3)") tk.MustExec("set sql_mode=''") tk.MustExec("insert nn (c1) values (3)") tk.MustQuery("select * from nn").Check(testkit.Rows("1 0", "2 0", "3 0")) @@ -641,13 +632,10 @@ func TestAlterTableModifyColumn(t *testing.T) { tk.MustExec("use test") tk.MustExec("drop table if exists mc") tk.MustExec("create table mc(c1 int, c2 varchar(10), c3 bit)") - err := tk.ExecToErr("alter table mc modify column c1 short") - require.Error(t, err) + tk.MustExecToErr("alter table mc modify column c1 short") tk.MustExec("alter table mc modify column c1 bigint") - err = tk.ExecToErr("alter table mc modify column c2 blob") - require.Error(t, err) - + tk.MustExecToErr("alter table mc modify column c2 blob") tk.MustExec("alter table mc modify column c2 varchar(8)") tk.MustExec("alter table mc modify column c2 varchar(11)") tk.MustExec("alter table mc modify column c2 text(13)") @@ -658,19 +646,18 @@ func TestAlterTableModifyColumn(t *testing.T) { expected := "CREATE TABLE `mc` (\n `c1` bigint(20) DEFAULT NULL,\n `c2` text DEFAULT NULL,\n `c3` bit(1) DEFAULT NULL\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin" require.Equal(t, expected, createSQL) tk.MustExec("create or replace view alter_view as select c1,c2 from mc") - err = tk.ExecToErr("alter table alter_view modify column c2 text") - require.Equal(t, dbterror.ErrWrongObject.GenWithStackByArgs("test", "alter_view", "BASE TABLE").Error(), err.Error()) + tk.MustGetErrMsg("alter table alter_view modify column c2 text", + dbterror.ErrWrongObject.GenWithStackByArgs("test", "alter_view", "BASE TABLE").Error()) tk.MustExec("drop view alter_view") tk.MustExec("create sequence alter_seq") - err = tk.ExecToErr("alter table alter_seq modify column c int") - require.Equal(t, dbterror.ErrWrongObject.GenWithStackByArgs("test", "alter_seq", "BASE TABLE").Error(), err.Error()) + tk.MustGetErrMsg("alter table alter_seq modify column c int", + dbterror.ErrWrongObject.GenWithStackByArgs("test", "alter_seq", "BASE TABLE").Error()) tk.MustExec("drop sequence alter_seq") // test multiple collate modification in column. tk.MustExec("drop table if exists modify_column_multiple_collate") tk.MustExec("create table modify_column_multiple_collate (a char(1) collate utf8_bin collate utf8_general_ci) charset utf8mb4 collate utf8mb4_bin") - _, err = tk.Exec("alter table modify_column_multiple_collate modify column a char(1) collate utf8mb4_bin;") - require.NoError(t, err) + tk.MustExec("alter table modify_column_multiple_collate modify column a char(1) collate utf8mb4_bin;") tt, err := domain.GetDomain(tk.Session()).InfoSchema().TableByName(model.NewCIStr("test"), model.NewCIStr("modify_column_multiple_collate")) require.NoError(t, err) require.Equal(t, "utf8mb4", tt.Cols()[0].GetCharset()) @@ -692,15 +679,11 @@ func TestAlterTableModifyColumn(t *testing.T) { // test Err case for multiple collate modification in column. tk.MustExec("drop table if exists err_modify_multiple_collate;") tk.MustExec("create table err_modify_multiple_collate (a char(1) collate utf8_bin collate utf8_general_ci) charset utf8mb4 collate utf8mb4_bin") - _, err = tk.Exec("alter table err_modify_multiple_collate modify column a char(1) charset utf8mb4 collate utf8_bin;") - require.Error(t, err) - require.Equal(t, dbterror.ErrCollationCharsetMismatch.GenWithStackByArgs("utf8_bin", "utf8mb4").Error(), err.Error()) + tk.MustGetErrMsg("alter table err_modify_multiple_collate modify column a char(1) charset utf8mb4 collate utf8_bin;", dbterror.ErrCollationCharsetMismatch.GenWithStackByArgs("utf8_bin", "utf8mb4").Error()) tk.MustExec("drop table if exists err_modify_multiple_collate;") tk.MustExec("create table err_modify_multiple_collate (a char(1) collate utf8_bin collate utf8_general_ci) charset utf8mb4 collate utf8mb4_bin") - _, err = tk.Exec("alter table err_modify_multiple_collate modify column a char(1) collate utf8_bin collate utf8mb4_bin;") - require.Error(t, err) - require.Equal(t, dbterror.ErrCollationCharsetMismatch.GenWithStackByArgs("utf8mb4_bin", "utf8").Error(), err.Error()) + tk.MustGetErrMsg("alter table err_modify_multiple_collate modify column a char(1) collate utf8_bin collate utf8mb4_bin;", dbterror.ErrCollationCharsetMismatch.GenWithStackByArgs("utf8mb4_bin", "utf8").Error()) } @@ -1134,7 +1117,7 @@ func TestAutoRandomTableOption(t *testing.T) { // Alter auto_random_base on non auto_random table. tk.MustExec("create table alter_auto_random_normal (a int)") - _, err = tk.Exec("alter table alter_auto_random_normal auto_random_base = 100") + err = tk.ExecToErr("alter table alter_auto_random_normal auto_random_base = 100") require.Error(t, err) require.Contains(t, err.Error(), autoid.AutoRandomRebaseNotApplicable) } @@ -1304,45 +1287,36 @@ func TestIllegalFunctionCall4GeneratedColumns(t *testing.T) { tk := testkit.NewTestKit(t, store) tk.MustExec("use test") // Test create an exist database - _, err := tk.Exec("CREATE database test") - require.Error(t, err) - - _, err = tk.Exec("create table t1 (b double generated always as (rand()) virtual);") - require.Equal(t, dbterror.ErrGeneratedColumnFunctionIsNotAllowed.GenWithStackByArgs("b").Error(), err.Error()) - - _, err = tk.Exec("create table t1 (a varchar(64), b varchar(1024) generated always as (load_file(a)) virtual);") - require.Equal(t, dbterror.ErrGeneratedColumnFunctionIsNotAllowed.GenWithStackByArgs("b").Error(), err.Error()) - - _, err = tk.Exec("create table t1 (a datetime generated always as (curdate()) virtual);") - require.Equal(t, dbterror.ErrGeneratedColumnFunctionIsNotAllowed.GenWithStackByArgs("a").Error(), err.Error()) - - _, err = tk.Exec("create table t1 (a datetime generated always as (current_time()) virtual);") - require.Equal(t, dbterror.ErrGeneratedColumnFunctionIsNotAllowed.GenWithStackByArgs("a").Error(), err.Error()) - - _, err = tk.Exec("create table t1 (a datetime generated always as (current_timestamp()) virtual);") - require.Equal(t, dbterror.ErrGeneratedColumnFunctionIsNotAllowed.GenWithStackByArgs("a").Error(), err.Error()) - - _, err = tk.Exec("create table t1 (a datetime, b varchar(10) generated always as (localtime()) virtual);") - require.Equal(t, dbterror.ErrGeneratedColumnFunctionIsNotAllowed.GenWithStackByArgs("b").Error(), err.Error()) - - _, err = tk.Exec("create table t1 (a varchar(1024) generated always as (uuid()) virtual);") - require.Equal(t, dbterror.ErrGeneratedColumnFunctionIsNotAllowed.GenWithStackByArgs("a").Error(), err.Error()) - - _, err = tk.Exec("create table t1 (a varchar(1024), b varchar(1024) generated always as (is_free_lock(a)) virtual);") - require.Equal(t, dbterror.ErrGeneratedColumnFunctionIsNotAllowed.GenWithStackByArgs("b").Error(), err.Error()) + tk.MustExecToErr("CREATE database test") + + tk.MustGetErrMsg("create table t1 (b double generated always as (rand()) virtual);", + dbterror.ErrGeneratedColumnFunctionIsNotAllowed.GenWithStackByArgs("b").Error()) + tk.MustGetErrMsg("create table t1 (a varchar(64), b varchar(1024) generated always as (load_file(a)) virtual);", + dbterror.ErrGeneratedColumnFunctionIsNotAllowed.GenWithStackByArgs("b").Error()) + tk.MustGetErrMsg("create table t1 (a datetime generated always as (curdate()) virtual);", + dbterror.ErrGeneratedColumnFunctionIsNotAllowed.GenWithStackByArgs("a").Error()) + tk.MustGetErrMsg("create table t1 (a datetime generated always as (current_time()) virtual);", + dbterror.ErrGeneratedColumnFunctionIsNotAllowed.GenWithStackByArgs("a").Error()) + tk.MustGetErrMsg("create table t1 (a datetime generated always as (current_timestamp()) virtual);", + dbterror.ErrGeneratedColumnFunctionIsNotAllowed.GenWithStackByArgs("a").Error()) + tk.MustGetErrMsg("create table t1 (a datetime, b varchar(10) generated always as (localtime()) virtual);", + dbterror.ErrGeneratedColumnFunctionIsNotAllowed.GenWithStackByArgs("b").Error()) + tk.MustGetErrMsg("create table t1 (a varchar(1024) generated always as (uuid()) virtual);", + dbterror.ErrGeneratedColumnFunctionIsNotAllowed.GenWithStackByArgs("a").Error()) + tk.MustGetErrMsg("create table t1 (a varchar(1024), b varchar(1024) generated always as (is_free_lock(a)) virtual);", + dbterror.ErrGeneratedColumnFunctionIsNotAllowed.GenWithStackByArgs("b").Error()) tk.MustExec("create table t1 (a bigint not null primary key auto_increment, b bigint, c bigint as (b + 1));") - _, err = tk.Exec("alter table t1 add column d varchar(1024) generated always as (database());") - require.Equal(t, dbterror.ErrGeneratedColumnFunctionIsNotAllowed.GenWithStackByArgs("d").Error(), err.Error()) + tk.MustGetErrMsg("alter table t1 add column d varchar(1024) generated always as (database());", + dbterror.ErrGeneratedColumnFunctionIsNotAllowed.GenWithStackByArgs("d").Error()) tk.MustExec("alter table t1 add column d bigint generated always as (b + 1); ") - _, err = tk.Exec("alter table t1 modify column d bigint generated always as (connection_id());") - require.Equal(t, dbterror.ErrGeneratedColumnFunctionIsNotAllowed.GenWithStackByArgs("d").Error(), err.Error()) - - _, err = tk.Exec("alter table t1 change column c cc bigint generated always as (connection_id());") - require.Equal(t, dbterror.ErrGeneratedColumnFunctionIsNotAllowed.GenWithStackByArgs("cc").Error(), err.Error()) + tk.MustGetErrMsg("alter table t1 modify column d bigint generated always as (connection_id());", + dbterror.ErrGeneratedColumnFunctionIsNotAllowed.GenWithStackByArgs("d").Error()) + tk.MustGetErrMsg("alter table t1 change column c cc bigint generated always as (connection_id());", + dbterror.ErrGeneratedColumnFunctionIsNotAllowed.GenWithStackByArgs("cc").Error()) } func TestGeneratedColumnRelatedDDL(t *testing.T) { @@ -1354,25 +1328,21 @@ func TestGeneratedColumnRelatedDDL(t *testing.T) { _, err := tk.Exec("CREATE database test") require.Error(t, err) - _, err = tk.Exec("create table t1 (a bigint not null primary key auto_increment, b bigint as (a + 1));") - require.Equal(t, dbterror.ErrGeneratedColumnRefAutoInc.GenWithStackByArgs("b").Error(), err.Error()) - + tk.MustGetErrMsg("create table t1 (a bigint not null primary key auto_increment, b bigint as (a + 1));", + dbterror.ErrGeneratedColumnRefAutoInc.GenWithStackByArgs("b").Error()) tk.MustExec("create table t1 (a bigint not null primary key auto_increment, b bigint, c bigint as (b + 1));") - - _, err = tk.Exec("alter table t1 add column d bigint generated always as (a + 1);") - require.Equal(t, dbterror.ErrGeneratedColumnRefAutoInc.GenWithStackByArgs("d").Error(), err.Error()) - + tk.MustGetErrMsg("alter table t1 add column d bigint generated always as (a + 1);", + dbterror.ErrGeneratedColumnRefAutoInc.GenWithStackByArgs("d").Error()) tk.MustExec("alter table t1 add column d bigint generated always as (b + 1);") - - _, err = tk.Exec("alter table t1 modify column d bigint generated always as (a + 1);") - require.Equal(t, dbterror.ErrGeneratedColumnRefAutoInc.GenWithStackByArgs("d").Error(), err.Error()) + tk.MustGetErrMsg("alter table t1 modify column d bigint generated always as (a + 1);", + dbterror.ErrGeneratedColumnRefAutoInc.GenWithStackByArgs("d").Error()) // This mysql compatibility check can be disabled using tidb_enable_auto_increment_in_generated tk.MustExec("set session tidb_enable_auto_increment_in_generated = 1;") tk.MustExec("alter table t1 modify column d bigint generated always as (a + 1);") - _, err = tk.Exec("alter table t1 add column e bigint as (z + 1);") - require.Equal(t, dbterror.ErrBadField.GenWithStackByArgs("z", "generated column function").Error(), err.Error()) + tk.MustGetErrMsg("alter table t1 add column e bigint as (z + 1);", + dbterror.ErrBadField.GenWithStackByArgs("z", "generated column function").Error()) tk.MustExec("drop table t1;") @@ -1568,8 +1538,7 @@ func TestRenameTable(t *testing.T) { tk.MustExec("insert rename2.t1 values ()") result = tk.MustQuery("select * from rename2.t1") result.Check(testkit.Rows("1", "100000", "100001")) - _, err := tk.Exec("insert rename1.t values ()") - require.Error(t, err) + tk.MustExecToErr("insert rename1.t values ()") tk.MustExec("drop database rename1") tk.MustExec("drop database rename2") } @@ -1580,13 +1549,12 @@ func TestAutoIncrementColumnErrorMessage(t *testing.T) { tk := testkit.NewTestKit(t, store) tk.MustExec("use test") // Test create an exist database - _, err := tk.Exec("CREATE database test") - require.Error(t, err) + tk.MustExecToErr("CREATE database test") tk.MustExec("CREATE TABLE t1 (t1_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY);") - _, err = tk.Exec("CREATE INDEX idx1 ON t1 ((t1_id + t1_id));") - require.Equal(t, dbterror.ErrExpressionIndexCanNotRefer.GenWithStackByArgs("idx1").Error(), err.Error()) + tk.MustGetErrMsg("CREATE INDEX idx1 ON t1 ((t1_id + t1_id));", + dbterror.ErrExpressionIndexCanNotRefer.GenWithStackByArgs("idx1").Error()) // This mysql compatibility check can be disabled using tidb_enable_auto_increment_in_generated tk.MustExec("SET SESSION tidb_enable_auto_increment_in_generated = 1;") diff --git a/testkit/testkit.go b/testkit/testkit.go index 83f0476e53499..912b5292dad10 100644 --- a/testkit/testkit.go +++ b/testkit/testkit.go @@ -259,6 +259,15 @@ func (tk *TestKit) ExecToErr(sql string, args ...interface{}) error { return err } +// MustExecToErr executes a sql statement and must return Error. +func (tk *TestKit) MustExecToErr(sql string, args ...interface{}) { + res, err := tk.Exec(sql, args...) + if res != nil { + tk.require.NoError(res.Close()) + } + tk.require.Error(err) +} + func newSession(t testing.TB, store kv.Storage) session.Session { se, err := session.CreateSession4Test(store) require.NoError(t, err)