From 1ce9c3b701140d537048e1c92886eb1650a44bf5 Mon Sep 17 00:00:00 2001 From: Weizhen Wang Date: Mon, 13 Jun 2022 23:56:55 +0800 Subject: [PATCH] clean code Signed-off-by: Weizhen Wang --- ddl/db_cache_test.go | 4 +- ddl/db_integration_test.go | 63 ++++++------------ ddl/db_partition_test.go | 90 +++++++------------------ ddl/db_table_test.go | 110 ++++++++++--------------------- ddl/failtest/fail_db_test.go | 30 ++++----- ddl/placement_policy_test.go | 8 +-- ddl/placement_sql_test.go | 4 +- privilege/privileges/BUILD.bazel | 1 - testkit/testkit.go | 6 ++ 9 files changed, 103 insertions(+), 213 deletions(-) diff --git a/ddl/db_cache_test.go b/ddl/db_cache_test.go index 3ffcbcb18b576..8343cad58f37b 100644 --- a/ddl/db_cache_test.go +++ b/ddl/db_cache_test.go @@ -22,7 +22,6 @@ import ( "github.com/pingcap/tidb/errno" "github.com/pingcap/tidb/parser/auth" "github.com/pingcap/tidb/parser/model" - "github.com/pingcap/tidb/parser/terror" "github.com/pingcap/tidb/session" "github.com/pingcap/tidb/store/mockstore" "github.com/pingcap/tidb/testkit" @@ -170,8 +169,7 @@ func TestAlterTableCache(t *testing.T) { tk.MustExec("begin") tk.MustExec("insert into t1 set a=1;") tk2.MustExec("alter table t1 cache;") - _, err = tk.Exec("commit") - require.True(t, terror.ErrorEqual(domain.ErrInfoSchemaChanged, err)) + tk.MustGetDBError("commit", domain.ErrInfoSchemaChanged) /* Test can skip schema checker */ tk.MustExec("begin") tk.MustExec("alter table t1 nocache") diff --git a/ddl/db_integration_test.go b/ddl/db_integration_test.go index 3148283059998..fff7e16f8a33c 100644 --- a/ddl/db_integration_test.go +++ b/ddl/db_integration_test.go @@ -119,13 +119,8 @@ func TestInvalidDefault(t *testing.T) { tk.MustExec("USE test;") - _, err := tk.Exec("create table t(c1 decimal default 1.7976931348623157E308)") - require.Error(t, err) - require.Truef(t, terror.ErrorEqual(err, types.ErrInvalidDefault), "err %v", err) - - _, err = tk.Exec("create table t( c1 varchar(2) default 'TiDB');") - require.Error(t, err) - require.Truef(t, terror.ErrorEqual(err, types.ErrInvalidDefault), "err %v", err) + tk.MustGetDBError("create table t(c1 decimal default 1.7976931348623157E308)", types.ErrInvalidDefault) + tk.MustGetDBError("create table t( c1 varchar(2) default 'TiDB');", types.ErrInvalidDefault) } // TestKeyWithoutLength for issue #13452 @@ -133,12 +128,9 @@ func TestKeyWithoutLengthCreateTable(t *testing.T) { store, clean := testkit.CreateMockStore(t) defer clean() tk := testkit.NewTestKit(t, store) - tk.MustExec("USE test") - - _, err := tk.Exec("create table t_without_length (a text primary key)") - require.Error(t, err) - require.Regexp(t, ".*BLOB/TEXT column 'a' used in key specification without a key length", err.Error()) + tk.MustMatchErrMsg("create table t_without_length (a text primary key)", + ".*BLOB/TEXT column 'a' used in key specification without a key length") } // TestInvalidNameWhenCreateTable for issue #3848 @@ -3371,19 +3363,15 @@ func TestAvoidCreateViewOnLocalTemporaryTable(t *testing.T) { require.True(t, core.ErrViewSelectTemporaryTable.Equal(err)) tk.MustGetErrMsg("select * from v5", "[schema:1146]Table 'test.v5' doesn't exist") - _, err = tk.Exec("create view v6 as select * from tt0 where tt0.a=(select max(tt1.b) from tt1)") + err = tk.ExecToErr("create view v6 as select * from tt0 where tt0.a=(select max(tt1.b) from tt1)") require.True(t, core.ErrViewSelectTemporaryTable.Equal(err)) - _, err = tk.Exec("select * from v6") - require.Error(t, err) - require.Equal(t, "[schema:1146]Table 'test.v6' doesn't exist", err.Error()) + tk.MustGetErrMsg("select * from v6", "[schema:1146]Table 'test.v6' doesn't exist") - _, err = tk.Exec("create view v7 as select * from tt0 where tt0.b=(select max(tt1.b) from tt1 where tt0.a=tt1.a)") + err = tk.ExecToErr("create view v7 as select * from tt0 where tt0.b=(select max(tt1.b) from tt1 where tt0.a=tt1.a)") require.True(t, core.ErrViewSelectTemporaryTable.Equal(err)) - _, err = tk.Exec("select * from v7") - require.Error(t, err) - require.Equal(t, "[schema:1146]Table 'test.v7' doesn't exist", err.Error()) + tk.MustGetErrMsg("select * from v7", "[schema:1146]Table 'test.v7' doesn't exist") - _, err = tk.Exec("create or replace view v0 as select * from tt1") + err = tk.ExecToErr("create or replace view v0 as select * from tt1") require.True(t, core.ErrViewSelectTemporaryTable.Equal(err)) } @@ -3413,8 +3401,7 @@ func TestDropTemporaryTable(t *testing.T) { tk.MustExec("create temporary table if not exists b_local_temp_table (id int)") tk.MustQuery("select * from b_local_temp_table").Check(testkit.Rows()) tk.MustExec("drop table b_local_temp_table") - _, err := tk.Exec("select * from b_local_temp_table") - require.Equal(t, "[schema:1146]Table 'test.b_local_temp_table' doesn't exist", err.Error()) + tk.MustGetErrMsg("select * from b_local_temp_table", "[schema:1146]Table 'test.b_local_temp_table' doesn't exist") // TODO: test drop real data // Check if we have a normal and local temporary table in the same db with the same name, @@ -3427,8 +3414,7 @@ func TestDropTemporaryTable(t *testing.T) { sequenceTable := external.GetTableByName(t, tk, "test", "b_table_local_and_normal") require.Equal(t, model.TempTableNone, sequenceTable.Meta().TempTableType) tk.MustExec("drop table if exists b_table_local_and_normal") - _, err = tk.Exec("select * from b_table_local_and_normal") - require.Equal(t, "[schema:1146]Table 'test.b_table_local_and_normal' doesn't exist", err.Error()) + tk.MustGetErrMsg("select * from b_table_local_and_normal", "[schema:1146]Table 'test.b_table_local_and_normal' doesn't exist") // Check dropping local temporary tables should not commit current transaction implicitly. tk.MustExec("drop table if exists check_data_normal_table") @@ -3473,21 +3459,19 @@ func TestDropTemporaryTable(t *testing.T) { tk.MustExec("drop table if exists a_normal_table_2") tk.MustExec("create table a_normal_table_2 (id int)") defer tk.MustExec("drop table if exists a_normal_table_2") - _, err = tk.Exec("drop table a_local_temp_table_3, a_local_temp_table_4, a_local_temp_table_5, a_normal_table_2, a_local_temp_table_6") - require.Equal(t, "[schema:1051]Unknown table 'test.a_local_temp_table_6'", err.Error()) + tk.MustGetErrMsg("drop table a_local_temp_table_3, a_local_temp_table_4, a_local_temp_table_5, a_normal_table_2, a_local_temp_table_6", "[schema:1051]Unknown table 'test.a_local_temp_table_6'") tk.MustExec("drop table if exists check_data_normal_table_3") tk.MustExec("create table check_data_normal_table_3 (id int)") defer tk.MustExec("drop table if exists check_data_normal_table_3") tk.MustExec("create temporary table a_local_temp_table_6 (id int)") - _, err = tk.Exec("drop table check_data_normal_table_3, check_data_normal_table_7, a_local_temp_table_6") - require.Equal(t, "[schema:1051]Unknown table 'test.check_data_normal_table_7'", err.Error()) + tk.MustGetErrMsg("drop table check_data_normal_table_3, check_data_normal_table_7, a_local_temp_table_6", "[schema:1051]Unknown table 'test.check_data_normal_table_7'") // Check filter out data from removed local temp tables tk.MustExec("create temporary table a_local_temp_table_7 (id int)") ctx := tk.Session() require.Nil(t, sessiontxn.NewTxn(context.Background(), ctx)) - _, err = ctx.Txn(true) + _, err := ctx.Txn(true) require.NoError(t, err) sessionVars := tk.Session().GetSessionVars() sessVarsTempTable := sessionVars.LocalTemporaryTables @@ -3505,8 +3489,7 @@ func TestDropTemporaryTable(t *testing.T) { tk.MustExec("drop table if exists a_local_temp_table_7") tk.MustExec("commit") - _, err = tk.Exec("select * from a_local_temp_table_7") - require.Equal(t, "[schema:1146]Table 'test.a_local_temp_table_7' doesn't exist", err.Error()) + tk.MustGetErrMsg("select * from a_local_temp_table_7", "[schema:1146]Table 'test.a_local_temp_table_7' doesn't exist") memData := sessionVars.TemporaryTableData iter, err := memData.Iter(tablePrefix, endTablePrefix) require.NoError(t, err) @@ -3524,8 +3507,7 @@ func TestDropTemporaryTable(t *testing.T) { // Check drop not exists table in transaction. tk.MustExec("begin") tk.MustExec("create temporary table a_local_temp_table_8 (id int)") - _, err = tk.Exec("drop table a_local_temp_table_8, a_local_temp_table_9_not_exist") - require.Equal(t, "[schema:1051]Unknown table 'test.a_local_temp_table_9_not_exist'", err.Error()) + tk.MustGetErrMsg("drop table a_local_temp_table_8, a_local_temp_table_9_not_exist", "[schema:1051]Unknown table 'test.a_local_temp_table_9_not_exist'") tk.MustQuery("select * from a_local_temp_table_8").Check(testkit.Rows()) } @@ -3973,18 +3955,11 @@ func TestInvalidPartitionNameWhenCreateTable(t *testing.T) { defer tk.MustExec("drop database invalidPartitionNames") tk.MustExec("USE invalidPartitionNames") - _, err := tk.Exec("create table t(a int) partition by range (a) (partition p0 values less than (0), partition `p1 ` values less than (3))") - require.Error(t, err) - require.Truef(t, terror.ErrorEqual(err, dbterror.ErrWrongPartitionName), "err %v", err) - - _, err = tk.Exec("create table t(a int) partition by range (a) (partition `` values less than (0), partition `p1` values less than (3))") - require.Error(t, err) - require.Truef(t, terror.ErrorEqual(err, dbterror.ErrWrongPartitionName), "err %v", err) + tk.MustGetDBError("create table t(a int) partition by range (a) (partition p0 values less than (0), partition `p1 ` values less than (3))", dbterror.ErrWrongPartitionName) + tk.MustGetDBError("create table t(a int) partition by range (a) (partition `` values less than (0), partition `p1` values less than (3))", dbterror.ErrWrongPartitionName) tk.MustExec("create table t(a int) partition by range (a) (partition `p0` values less than (0), partition `p1` values less than (3))") - _, err = tk.Exec("alter table t add partition (partition `p2 ` values less than (5))") - require.Error(t, err) - require.Truef(t, terror.ErrorEqual(err, dbterror.ErrWrongPartitionName), "err %v", err) + tk.MustGetDBError("alter table t add partition (partition `p2 ` values less than (5))", dbterror.ErrWrongPartitionName) } func TestDDLLastInfo(t *testing.T) { diff --git a/ddl/db_partition_test.go b/ddl/db_partition_test.go index bfd2dca76a6bf..9621ab002efe2 100644 --- a/ddl/db_partition_test.go +++ b/ddl/db_partition_test.go @@ -157,7 +157,7 @@ func TestCreateTableWithPartition(t *testing.T) { );` tk.MustGetErrCode(sql4, tmysql.ErrPartitionMaxvalue) - _, err = tk.Exec(`CREATE TABLE rc ( + tk.MustExec(`CREATE TABLE rc ( a INT NOT NULL, b INT NOT NULL, c INT NOT NULL @@ -168,7 +168,6 @@ func TestCreateTableWithPartition(t *testing.T) { partition p3 values less than (65,30,13), partition p4 values less than (maxvalue,30,40) );`) - require.NoError(t, err) sql6 := `create table employees ( id int not null, @@ -211,7 +210,7 @@ func TestCreateTableWithPartition(t *testing.T) { );` tk.MustGetErrCode(sql9, tmysql.ErrPartitionFunctionIsNotAllowed) - _, err = tk.Exec(`CREATE TABLE t9 ( + tk.MustGetDBError(`CREATE TABLE t9 ( a INT NOT NULL, b INT NOT NULL, c INT NOT NULL @@ -220,8 +219,7 @@ func TestCreateTableWithPartition(t *testing.T) { partition p0 values less than (10), partition p2 values less than (20), partition p3 values less than (20) - );`) - require.True(t, dbterror.ErrRangeNotIncreasing.Equal(err)) + );`, dbterror.ErrRangeNotIncreasing) tk.MustGetErrCode(`create TABLE t10 (c1 int,c2 int) partition by range(c1 / c2 ) (partition p0 values less than (2));`, tmysql.ErrPartitionFunctionIsNotAllowed) @@ -3088,11 +3086,8 @@ func TestPartitionErrorCode(t *testing.T) { ) partition by hash(store_id) partitions 4;`) - _, err := tk.Exec("alter table employees add partition partitions 8;") - require.True(t, dbterror.ErrUnsupportedAddPartition.Equal(err)) - - _, err = tk.Exec("alter table employees add partition (partition p5 values less than (42));") - require.True(t, dbterror.ErrUnsupportedAddPartition.Equal(err)) + tk.MustGetDBError("alter table employees add partition partitions 8;", dbterror.ErrUnsupportedAddPartition) + tk.MustGetDBError("alter table employees add partition (partition p5 values less than (42));", dbterror.ErrUnsupportedAddPartition) // coalesce partition tk.MustExec(`create table clients ( @@ -3103,16 +3098,14 @@ func TestPartitionErrorCode(t *testing.T) { ) partition by hash( month(signed) ) partitions 12;`) - _, err = tk.Exec("alter table clients coalesce partition 4;") - require.True(t, dbterror.ErrUnsupportedCoalescePartition.Equal(err)) + tk.MustGetDBError("alter table clients coalesce partition 4;", dbterror.ErrUnsupportedCoalescePartition) tk.MustExec(`create table t_part (a int key) partition by range(a) ( partition p0 values less than (10), partition p1 values less than (20) );`) - _, err = tk.Exec("alter table t_part coalesce partition 4;") - require.True(t, dbterror.ErrCoalesceOnlyOnHashPartition.Equal(err)) + tk.MustGetDBError("alter table t_part coalesce partition 4;", dbterror.ErrCoalesceOnlyOnHashPartition) tk.MustGetErrCode(`alter table t_part reorganize partition p0, p1 into ( partition p0 values less than (1980));`, tmysql.ErrUnsupportedDDLOperation) @@ -3135,9 +3128,7 @@ func TestPartitionErrorCode(t *testing.T) { tk2 := testkit.NewTestKit(t, store) tk2.MustExec("use test") tk2.MustExec("alter table t truncate partition p0;") - - _, err = tk1.Exec("commit") - require.NoError(t, err) + tk1.MustExec("commit") } func TestConstAndTimezoneDepent(t *testing.T) { @@ -3337,8 +3328,7 @@ func TestCommitWhenSchemaChange(t *testing.T) { tk.MustExec("insert into nt values (1), (3), (5);") tk2.MustExec("alter table pt exchange partition p1 with table nt;") tk.MustExec("insert into nt values (7), (9);") - _, err = tk.Session().Execute(context.Background(), "commit") - require.True(t, domain.ErrInfoSchemaChanged.Equal(err)) + tk.MustGetDBError("commit", domain.ErrInfoSchemaChanged) tk.MustExec("admin check table pt") tk.MustQuery("select * from pt").Check(testkit.Rows()) @@ -3349,8 +3339,7 @@ func TestCommitWhenSchemaChange(t *testing.T) { tk.MustExec("insert into pt values (1), (3), (5);") tk2.MustExec("alter table pt exchange partition p1 with table nt;") tk.MustExec("insert into pt values (7), (9);") - _, err = tk.Session().Execute(context.Background(), "commit") - require.True(t, domain.ErrInfoSchemaChanged.Equal(err)) + tk.MustGetDBError("commit", domain.ErrInfoSchemaChanged) tk.MustExec("admin check table pt") tk.MustQuery("select * from pt").Check(testkit.Rows()) @@ -3365,34 +3354,28 @@ func TestCreatePartitionTableWithWrongType(t *testing.T) { tk.MustExec("use test") tk.MustExec("drop table if exists t") var err error - _, err = tk.Exec(`create table t( + tk.MustGetDBError(`create table t( b int(10) ) partition by range columns (b) ( partition p0 values less than (0x10), partition p3 values less than (0x20) - )`) - require.Error(t, err) - require.True(t, dbterror.ErrWrongTypeColumnValue.Equal(err)) + )`, dbterror.ErrWrongTypeColumnValue) - _, err = tk.Exec(`create table t( + tk.MustGetDBError(`create table t( b int(10) ) partition by range columns (b) ( partition p0 values less than ('g'), partition p3 values less than ('k') - )`) - require.Error(t, err) - require.True(t, dbterror.ErrWrongTypeColumnValue.Equal(err)) + )`, dbterror.ErrWrongTypeColumnValue) - _, err = tk.Exec(`create table t( + tk.MustGetDBError(`create table t( b char(10) ) partition by range columns (b) ( partition p0 values less than (30), partition p3 values less than (60) - )`) - require.Error(t, err) - require.True(t, dbterror.ErrWrongTypeColumnValue.Equal(err)) + )`, dbterror.ErrWrongTypeColumnValue) - _, err = tk.Exec(`create table t( + err = tk.ExecToErr(`create table t( b datetime ) partition by range columns (b) ( partition p0 values less than ('g'), @@ -3422,35 +3405,13 @@ func TestAddPartitionForTableWithWrongType(t *testing.T) { partition p0 values less than ('2020-09-01') )`) - var err error - - _, err = tk.Exec("alter table t_int add partition (partition p1 values less than ('g'))") - require.Error(t, err) - require.True(t, dbterror.ErrWrongTypeColumnValue.Equal(err)) - - _, err = tk.Exec("alter table t_int add partition (partition p1 values less than (0x20))") - require.Error(t, err) - require.True(t, dbterror.ErrWrongTypeColumnValue.Equal(err)) - - _, err = tk.Exec("alter table t_char add partition (partition p1 values less than (0x20))") - require.Error(t, err) - require.True(t, dbterror.ErrRangeNotIncreasing.Equal(err)) - - _, err = tk.Exec("alter table t_char add partition (partition p1 values less than (10))") - require.Error(t, err) - require.True(t, dbterror.ErrWrongTypeColumnValue.Equal(err)) - - _, err = tk.Exec("alter table t_date add partition (partition p1 values less than ('m'))") - require.Error(t, err) - require.True(t, dbterror.ErrWrongTypeColumnValue.Equal(err)) - - _, err = tk.Exec("alter table t_date add partition (partition p1 values less than (0x20))") - require.Error(t, err) - require.True(t, dbterror.ErrWrongTypeColumnValue.Equal(err)) - - _, err = tk.Exec("alter table t_date add partition (partition p1 values less than (20))") - require.Error(t, err) - require.True(t, dbterror.ErrWrongTypeColumnValue.Equal(err)) + tk.MustGetDBError("alter table t_int add partition (partition p1 values less than ('g'))", dbterror.ErrWrongTypeColumnValue) + tk.MustGetDBError("alter table t_int add partition (partition p1 values less than (0x20))", dbterror.ErrWrongTypeColumnValue) + tk.MustGetDBError("alter table t_char add partition (partition p1 values less than (0x20))", dbterror.ErrRangeNotIncreasing) + tk.MustGetDBError("alter table t_char add partition (partition p1 values less than (10))", dbterror.ErrWrongTypeColumnValue) + tk.MustGetDBError("alter table t_date add partition (partition p1 values less than ('m'))", dbterror.ErrWrongTypeColumnValue) + tk.MustGetDBError("alter table t_date add partition (partition p1 values less than (0x20))", dbterror.ErrWrongTypeColumnValue) + tk.MustGetDBError("alter table t_date add partition (partition p1 values less than (20))", dbterror.ErrWrongTypeColumnValue) } func TestPartitionListWithTimeType(t *testing.T) { @@ -3517,8 +3478,7 @@ func TestAddTableWithPartition(t *testing.T) { tk.MustGetErrCode("create temporary table local_partition_table (a int, b int) partition by hash(a) partitions 3;", errno.ErrPartitionNoTemporary) tk.MustExec("drop table if exists local_partition_table;") tk.MustExec("drop table if exists partition_table;") - _, err = tk.Exec("create table partition_table (a int, b int) partition by hash(a) partitions 3;") - require.NoError(t, err) + tk.MustExec("create table partition_table (a int, b int) partition by hash(a) partitions 3;") tk.MustExec("drop table if exists partition_table;") tk.MustExec("drop table if exists local_partition_range_table;") tk.MustGetErrCode(`create temporary table local_partition_range_table (c1 smallint(6) not null, c2 char(5) default null) partition by range ( c1 ) ( diff --git a/ddl/db_table_test.go b/ddl/db_table_test.go index 2d6bff92cee44..a385acd7e3427 100644 --- a/ddl/db_table_test.go +++ b/ddl/db_table_test.go @@ -648,38 +648,27 @@ func TestLockTables(t *testing.T) { tk.MustExec("lock tables t1 read") tk.MustQuery("select * from t1") tk2.MustQuery("select * from t1") - _, err := tk.Exec("insert into t1 set a=1") - require.True(t, terror.ErrorEqual(err, infoschema.ErrTableNotLockedForWrite)) - _, err = tk.Exec("update t1 set a=1") - require.True(t, terror.ErrorEqual(err, infoschema.ErrTableNotLockedForWrite)) - _, err = tk.Exec("delete from t1") - require.True(t, terror.ErrorEqual(err, infoschema.ErrTableNotLockedForWrite)) + tk.MustGetDBError("insert into t1 set a=1", infoschema.ErrTableNotLockedForWrite) + tk.MustGetDBError("update t1 set a=1", infoschema.ErrTableNotLockedForWrite) + tk.MustGetDBError("delete from t1", infoschema.ErrTableNotLockedForWrite) - err = tk2.ExecToErr("insert into t1 set a=1") - require.True(t, terror.ErrorEqual(err, infoschema.ErrTableLocked)) - err = tk2.ExecToErr("update t1 set a=1") - require.True(t, terror.ErrorEqual(err, infoschema.ErrTableLocked)) - err = tk2.ExecToErr("delete from t1") - require.True(t, terror.ErrorEqual(err, infoschema.ErrTableLocked)) + tk2.MustGetDBError("insert into t1 set a=1", infoschema.ErrTableLocked) + tk2.MustGetDBError("update t1 set a=1", infoschema.ErrTableLocked) + tk2.MustGetDBError("delete from t1", infoschema.ErrTableLocked) tk2.MustExec("lock tables t1 read") - err = tk2.ExecToErr("insert into t1 set a=1") - require.True(t, terror.ErrorEqual(err, infoschema.ErrTableNotLockedForWrite)) + tk2.MustGetDBError("insert into t1 set a=1", infoschema.ErrTableNotLockedForWrite) // Test write lock. - _, err = tk.Exec("lock tables t1 write") - require.True(t, terror.ErrorEqual(err, infoschema.ErrTableLocked)) + tk.MustGetDBError("lock tables t1 write", infoschema.ErrTableLocked) tk2.MustExec("unlock tables") tk.MustExec("lock tables t1 write") tk.MustQuery("select * from t1") tk.MustExec("delete from t1") tk.MustExec("insert into t1 set a=1") - err = tk2.ExecToErr("select * from t1") - require.True(t, terror.ErrorEqual(err, infoschema.ErrTableLocked)) - err = tk2.ExecToErr("insert into t1 set a=1") - require.True(t, terror.ErrorEqual(err, infoschema.ErrTableLocked)) - err = tk2.ExecToErr("lock tables t1 write") - require.True(t, terror.ErrorEqual(err, infoschema.ErrTableLocked)) + tk2.MustGetDBError("select * from t1", infoschema.ErrTableLocked) + tk2.MustGetDBError("insert into t1 set a=1", infoschema.ErrTableLocked) + tk2.MustGetDBError("lock tables t1 write", infoschema.ErrTableLocked) // Test write local lock. tk.MustExec("lock tables t1 write local") @@ -688,18 +677,13 @@ func TestLockTables(t *testing.T) { tk.MustExec("insert into t1 set a=1") tk2.MustQuery("select * from t1") - err = tk2.ExecToErr("delete from t1") - require.True(t, terror.ErrorEqual(err, infoschema.ErrTableLocked)) - err = tk2.ExecToErr("insert into t1 set a=1") - require.True(t, terror.ErrorEqual(err, infoschema.ErrTableLocked)) - err = tk2.ExecToErr("lock tables t1 write") - require.True(t, terror.ErrorEqual(err, infoschema.ErrTableLocked)) - err = tk2.ExecToErr("lock tables t1 read") - require.True(t, terror.ErrorEqual(err, infoschema.ErrTableLocked)) + tk2.MustGetDBError("delete from t1", infoschema.ErrTableLocked) + tk2.MustGetDBError("insert into t1 set a=1", infoschema.ErrTableLocked) + tk2.MustGetDBError("lock tables t1 write", infoschema.ErrTableLocked) + tk2.MustGetDBError("lock tables t1 read", infoschema.ErrTableLocked) // Test none unique table. - _, err = tk.Exec("lock tables t1 read, t1 write") - require.True(t, terror.ErrorEqual(err, infoschema.ErrNonuniqTable)) + tk.MustGetDBError("lock tables t1 read, t1 write", infoschema.ErrNonuniqTable) // Test lock table by other session in transaction and commit without retry. tk.MustExec("unlock tables") @@ -708,9 +692,8 @@ func TestLockTables(t *testing.T) { tk.MustExec("begin") tk.MustExec("insert into t1 set a=1") tk2.MustExec("lock tables t1 write") - _, err = tk.Exec("commit") - require.Error(t, err) - require.Equal(t, "previous statement: insert into t1 set a=1: [domain:8028]Information schema is changed during the execution of the statement(for example, table definition may be updated by other DDL ran in parallel). If you see this error often, try increasing `tidb_max_delta_schema_count`. [try again later]", err.Error()) + tk.MustGetErrMsg("commit", + "previous statement: insert into t1 set a=1: [domain:8028]Information schema is changed during the execution of the statement(for example, table definition may be updated by other DDL ran in parallel). If you see this error often, try increasing `tidb_max_delta_schema_count`. [try again later]") // Test lock table by other session in transaction and commit with retry. tk.MustExec("unlock tables") @@ -719,8 +702,7 @@ func TestLockTables(t *testing.T) { tk.MustExec("begin") tk.MustExec("insert into t1 set a=1") tk2.MustExec("lock tables t1 write") - _, err = tk.Exec("commit") - require.Truef(t, terror.ErrorEqual(err, infoschema.ErrTableLocked), "err: %v\n", err) + tk.MustGetDBError("commit", infoschema.ErrTableLocked) // Test for lock the same table multiple times. tk2.MustExec("lock tables t1 write") @@ -748,59 +730,45 @@ func TestLockTables(t *testing.T) { tk.MustExec("lock tables t1 write, t2 read") tk.MustExec("truncate table t1") tk.MustExec("insert into t1 set a=1") - err = tk2.ExecToErr("insert into t1 set a=1") - require.True(t, terror.ErrorEqual(err, infoschema.ErrTableLocked)) + tk2.MustGetDBError("insert into t1 set a=1", infoschema.ErrTableLocked) // Test for lock unsupported schema tables. - err = tk2.ExecToErr("lock tables performance_schema.global_status write") - require.True(t, terror.ErrorEqual(err, infoschema.ErrAccessDenied)) - err = tk2.ExecToErr("lock tables information_schema.tables write") - require.True(t, terror.ErrorEqual(err, infoschema.ErrAccessDenied)) - err = tk2.ExecToErr("lock tables mysql.db write") - require.True(t, terror.ErrorEqual(err, infoschema.ErrAccessDenied)) + tk2.MustGetDBError("lock tables performance_schema.global_status write", infoschema.ErrAccessDenied) + tk2.MustGetDBError("lock tables information_schema.tables write", infoschema.ErrAccessDenied) + tk2.MustGetDBError("lock tables mysql.db write", infoschema.ErrAccessDenied) // Test create table/view when session is holding the table locks. tk.MustExec("unlock tables") tk.MustExec("lock tables t1 write, t2 read") - _, err = tk.Exec("create table t3 (a int)") - require.True(t, terror.ErrorEqual(err, infoschema.ErrTableNotLocked)) - _, err = tk.Exec("create view v1 as select * from t1;") - require.True(t, terror.ErrorEqual(err, infoschema.ErrTableNotLocked)) + tk.MustGetDBError("create table t3 (a int)", infoschema.ErrTableNotLocked) + tk.MustGetDBError("create view v1 as select * from t1;", infoschema.ErrTableNotLocked) // Test for locking view was not supported. tk.MustExec("unlock tables") tk.MustExec("create view v1 as select * from t1;") - _, err = tk.Exec("lock tables v1 read") - require.True(t, terror.ErrorEqual(err, table.ErrUnsupportedOp)) + tk.MustGetDBError("lock tables v1 read", table.ErrUnsupportedOp) // Test for locking sequence was not supported. tk.MustExec("unlock tables") tk.MustExec("create sequence seq") - _, err = tk.Exec("lock tables seq read") - require.True(t, terror.ErrorEqual(err, table.ErrUnsupportedOp)) + tk.MustGetDBError("lock tables seq read", table.ErrUnsupportedOp) tk.MustExec("drop sequence seq") // Test for create/drop/alter database when session is holding the table locks. tk.MustExec("unlock tables") tk.MustExec("lock table t1 write") - _, err = tk.Exec("drop database test") - require.True(t, terror.ErrorEqual(err, table.ErrLockOrActiveTransaction)) - _, err = tk.Exec("create database test_lock") - require.True(t, terror.ErrorEqual(err, table.ErrLockOrActiveTransaction)) - _, err = tk.Exec("alter database test charset='utf8mb4'") - require.True(t, terror.ErrorEqual(err, table.ErrLockOrActiveTransaction)) + tk.MustGetDBError("drop database test", table.ErrLockOrActiveTransaction) + tk.MustGetDBError("create database test_lock", table.ErrLockOrActiveTransaction) + tk.MustGetDBError("alter database test charset='utf8mb4'", table.ErrLockOrActiveTransaction) // Test alter/drop database when other session is holding the table locks of the database. tk2.MustExec("create database test_lock2") - err = tk2.ExecToErr("drop database test") - require.True(t, terror.ErrorEqual(err, infoschema.ErrTableLocked)) - err = tk2.ExecToErr("alter database test charset='utf8mb4'") - require.True(t, terror.ErrorEqual(err, infoschema.ErrTableLocked)) + tk2.MustGetDBError("drop database test", infoschema.ErrTableLocked) + tk2.MustGetDBError("alter database test charset='utf8mb4'", infoschema.ErrTableLocked) // Test for admin cleanup table locks. tk.MustExec("unlock tables") tk.MustExec("lock table t1 write, t2 write") - err = tk2.ExecToErr("lock tables t1 write, t2 read") - require.True(t, terror.ErrorEqual(err, infoschema.ErrTableLocked)) + tk2.MustGetDBError("lock tables t1 write, t2 read", infoschema.ErrTableLocked) tk2.MustExec("admin cleanup table lock t1,t2") checkTableLock(t, tk, "test", "t1", model.TableLockNone) checkTableLock(t, tk, "test", "t2", model.TableLockNone) @@ -868,18 +836,12 @@ func TestDDLWithInvalidTableInfo(t *testing.T) { tk.MustExec("create table t (a bigint, b int, c int generated always as (b+1)) partition by hash(a) partitions 4;") // Test drop partition column. - _, err = tk.Exec("alter table t drop column a;") - require.Error(t, err) // TODO: refine the error message to compatible with MySQL - require.Equal(t, "[planner:1054]Unknown column 'a' in 'expression'", err.Error()) + tk.MustGetErrMsg("alter table t drop column a;", "[planner:1054]Unknown column 'a' in 'expression'") // Test modify column with invalid expression. - _, err = tk.Exec("alter table t modify column c int GENERATED ALWAYS AS ((case when (a = 0) then 0when (a > 0) then (b / a) end));") - require.Error(t, err) - require.Equal(t, "[parser:1064]You have an error in your SQL syntax; check the manual that corresponds to your TiDB version for the right syntax to use line 1 column 97 near \"then (b / a) end));\" ", err.Error()) + tk.MustGetErrMsg("alter table t modify column c int GENERATED ALWAYS AS ((case when (a = 0) then 0when (a > 0) then (b / a) end));", "[parser:1064]You have an error in your SQL syntax; check the manual that corresponds to your TiDB version for the right syntax to use line 1 column 97 near \"then (b / a) end));\" ") // Test add column with invalid expression. - _, err = tk.Exec("alter table t add column d int GENERATED ALWAYS AS ((case when (a = 0) then 0when (a > 0) then (b / a) end));") - require.Error(t, err) - require.Equal(t, "[parser:1064]You have an error in your SQL syntax; check the manual that corresponds to your TiDB version for the right syntax to use line 1 column 94 near \"then (b / a) end));\" ", err.Error()) + tk.MustGetErrMsg("alter table t add column d int GENERATED ALWAYS AS ((case when (a = 0) then 0when (a > 0) then (b / a) end));", "[parser:1064]You have an error in your SQL syntax; check the manual that corresponds to your TiDB version for the right syntax to use line 1 column 94 near \"then (b / a) end));\" ") } func TestAddColumn2(t *testing.T) { diff --git a/ddl/failtest/fail_db_test.go b/ddl/failtest/fail_db_test.go index 874acd12b5bbc..b1755f484254e 100644 --- a/ddl/failtest/fail_db_test.go +++ b/ddl/failtest/fail_db_test.go @@ -102,15 +102,15 @@ func TestHalfwayCancelOperations(t *testing.T) { }() tk.MustExec("create table tx(a int)") tk.MustExec("insert into tx values(1)") - _, err = tk.Exec("rename table tx to ty") + err = tk.ExecToErr("rename table tx to ty") require.Error(t, err) tk.MustExec("create table ty(a int)") tk.MustExec("insert into ty values(2)") - _, err = tk.Exec("rename table ty to tz, tx to ty") + err = tk.ExecToErr("rename table ty to tz, tx to ty") require.Error(t, err) - _, err = tk.Exec("select * from tz") + err = tk.ExecToErr("select * from tz") require.Error(t, err) - _, err = tk.Exec("rename table tx to ty, ty to tz") + err = tk.ExecToErr("rename table tx to ty, ty to tz") require.Error(t, err) tk.MustQuery("select * from ty").Check(testkit.Rows("2")) // Make sure that the table's data has not been deleted. @@ -134,7 +134,7 @@ func TestHalfwayCancelOperations(t *testing.T) { tk.MustExec("insert into nt values(7)") tk.MustExec("set @@tidb_enable_exchange_partition=1") defer tk.MustExec("set @@tidb_enable_exchange_partition=0") - _, err = tk.Exec("alter table pt exchange partition p1 with table nt") + err = tk.ExecToErr("alter table pt exchange partition p1 with table nt") require.Error(t, err) tk.MustQuery("select * from pt").Check(testkit.Rows("1", "3", "5")) @@ -263,7 +263,7 @@ func TestFailSchemaSyncer(t *testing.T) { time.Sleep(100 * time.Millisecond) } require.True(t, s.dom.SchemaValidator.IsStarted()) - _, err = tk.Exec("insert into t values(1)") + err = tk.ExecToErr("insert into t values(1)") require.NoError(t, err) } @@ -483,8 +483,7 @@ func TestModifyColumn(t *testing.T) { ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin")) tk.MustExec("admin check table t") tk.MustExec("insert into t values(111, 222, 333)") - _, err = tk.Exec("alter table t change column a aa tinyint after c") - require.EqualError(t, err, "[types:1690]constant 222 overflows tinyint") + tk.MustGetErrMsg("alter table t change column a aa tinyint after c", "[types:1690]constant 222 overflows tinyint") tk.MustExec("alter table t change column a aa mediumint after c") tk.MustQuery("show create table t").Check(testkit.Rows("t CREATE TABLE `t` (\n" + " `bb` mediumint(9) DEFAULT NULL,\n" + @@ -500,17 +499,12 @@ func TestModifyColumn(t *testing.T) { // Test unsupported statements. tk.MustExec("create table t1(a int) partition by hash (a) partitions 2") - _, err = tk.Exec("alter table t1 modify column a mediumint") - require.EqualError(t, err, "[ddl:8200]Unsupported modify column: table is partition table") + tk.MustGetErrMsg("alter table t1 modify column a mediumint", "[ddl:8200]Unsupported modify column: table is partition table") tk.MustExec("create table t2(id int, a int, b int generated always as (abs(a)) virtual, c int generated always as (a+1) stored)") - _, err = tk.Exec("alter table t2 modify column b mediumint") - require.EqualError(t, err, "[ddl:8200]Unsupported modify column: newCol IsGenerated false, oldCol IsGenerated true") - _, err = tk.Exec("alter table t2 modify column c mediumint") - require.EqualError(t, err, "[ddl:8200]Unsupported modify column: newCol IsGenerated false, oldCol IsGenerated true") - _, err = tk.Exec("alter table t2 modify column a mediumint generated always as(id+1) stored") - require.EqualError(t, err, "[ddl:8200]Unsupported modify column: newCol IsGenerated true, oldCol IsGenerated false") - _, err = tk.Exec("alter table t2 modify column a mediumint") - require.EqualError(t, err, "[ddl:8200]Unsupported modify column: oldCol is a dependent column 'a' for generated column") + tk.MustGetErrMsg("alter table t2 modify column b mediumint", "[ddl:8200]Unsupported modify column: newCol IsGenerated false, oldCol IsGenerated true") + tk.MustGetErrMsg("alter table t2 modify column c mediumint", "[ddl:8200]Unsupported modify column: newCol IsGenerated false, oldCol IsGenerated true") + tk.MustGetErrMsg("alter table t2 modify column a mediumint generated always as(id+1) stored", "[ddl:8200]Unsupported modify column: newCol IsGenerated true, oldCol IsGenerated false") + tk.MustGetErrMsg("alter table t2 modify column a mediumint", "[ddl:8200]Unsupported modify column: oldCol is a dependent column 'a' for generated column") // Test multiple rows of data. tk.MustExec("create table t3(a int not null default 1, b int default 2, c int not null default 0, primary key(c), index idx(b), index idx1(a), index idx2(b, c))") diff --git a/ddl/placement_policy_test.go b/ddl/placement_policy_test.go index 9df630774dc8b..b4b71e76cd422 100644 --- a/ddl/placement_policy_test.go +++ b/ddl/placement_policy_test.go @@ -940,9 +940,7 @@ func TestPolicyCacheAndPolicyDependency(t *testing.T) { require.Equal(t, true, in()) // Test drop policy can't succeed cause there are still some table depend on them. - _, err := tk.Exec("drop placement policy x") - require.Error(t, err) - require.Equal(t, "[ddl:8241]Placement policy 'x' is still in use", err.Error()) + tk.MustGetErrMsg("drop placement policy x", "[ddl:8241]Placement policy 'x' is still in use") // Drop depended table t firstly. tk.MustExec("drop table if exists t") @@ -951,9 +949,7 @@ func TestPolicyCacheAndPolicyDependency(t *testing.T) { require.Equal(t, 1, len(dependencies)) require.Equal(t, tbl2.Meta().ID, dependencies[0]) - _, err = tk.Exec("drop placement policy x") - require.Error(t, err) - require.Equal(t, "[ddl:8241]Placement policy 'x' is still in use", err.Error()) + tk.MustGetErrMsg("drop placement policy x", "[ddl:8241]Placement policy 'x' is still in use") // Drop depended table t2 secondly. tk.MustExec("drop table if exists t2") diff --git a/ddl/placement_sql_test.go b/ddl/placement_sql_test.go index 42ef1e29986e8..4d560831d4044 100644 --- a/ddl/placement_sql_test.go +++ b/ddl/placement_sql_test.go @@ -136,9 +136,9 @@ PARTITION BY RANGE (c) ( tk.MustExec("set @@autocommit = 0") tk.MustExec("begin") tk.MustExec(testcase.sql) - _, err = tk.Exec("commit") + err = tk.ExecToErr("commit") } else { - _, err = tk.Exec(testcase.sql) + err = tk.ExecToErr(testcase.sql) } if testcase.err == nil { require.NoError(t, err) diff --git a/privilege/privileges/BUILD.bazel b/privilege/privileges/BUILD.bazel index 356a4a022d04d..fc3084c3ff602 100644 --- a/privilege/privileges/BUILD.bazel +++ b/privilege/privileges/BUILD.bazel @@ -12,7 +12,6 @@ go_library( deps = [ "//errno", "//infoschema", - "//infoschema/perfschema", "//parser/ast", "//parser/auth", "//parser/mysql", diff --git a/testkit/testkit.go b/testkit/testkit.go index c8c0cc0d861c5..83f0476e53499 100644 --- a/testkit/testkit.go +++ b/testkit/testkit.go @@ -290,6 +290,12 @@ func (tk *TestKit) MustGetErrMsg(sql string, errStr string) { tk.require.EqualError(err, errStr) } +// MustGetDBError executes a sql statement and assert its terror. +func (tk *TestKit) MustGetDBError(sql string, dberr *terror.Error) { + err := tk.ExecToErr(sql) + tk.require.Truef(terror.ErrorEqual(err, dberr), "err %v", err) +} + // MustContainErrMsg executes a sql statement and assert its error message containing errStr. func (tk *TestKit) MustContainErrMsg(sql string, errStr interface{}) { err := tk.ExecToErr(sql)