From 8d99c66784b0d51adabada946efa5ff46fab644d Mon Sep 17 00:00:00 2001 From: Yiding Cui Date: Tue, 17 Jul 2018 18:39:50 +0800 Subject: [PATCH] fix test. --- ddl/db_change_test.go | 35 +++++++++++++++++++++++------------ plan/planbuilder.go | 14 +++----------- 2 files changed, 26 insertions(+), 23 deletions(-) diff --git a/ddl/db_change_test.go b/ddl/db_change_test.go index b3d7f02ec90e1..ded18b2944428 100644 --- a/ddl/db_change_test.go +++ b/ddl/db_change_test.go @@ -132,13 +132,13 @@ func (s *testStateChangeSuite) TestTwoStates(c *C) { testInfo.sqlInfos[0].sql = "insert into t (c1, c2, c3, c4) value(2, 'b', 'N', '2017-07-02')" testInfo.sqlInfos[1].sql = "insert into t (c1, c2, c3, d3, c4) value(3, 'b', 'N', 'a', '2017-07-03')" unknownColErr := errors.New("unknown column d3") - testInfo.sqlInfos[1].cases[0].expectedErr = unknownColErr - testInfo.sqlInfos[1].cases[1].expectedErr = unknownColErr - testInfo.sqlInfos[1].cases[2].expectedErr = unknownColErr - testInfo.sqlInfos[1].cases[3].expectedErr = unknownColErr + testInfo.sqlInfos[1].cases[0].expectedCompileErr = unknownColErr + testInfo.sqlInfos[1].cases[1].expectedCompileErr = unknownColErr + testInfo.sqlInfos[1].cases[2].expectedCompileErr = unknownColErr + testInfo.sqlInfos[1].cases[3].expectedCompileErr = unknownColErr testInfo.sqlInfos[2].sql = "update t set c2 = 'c2_update'" testInfo.sqlInfos[3].sql = "replace into t values(5, 'e', 'N', '2017-07-05')'" - testInfo.sqlInfos[3].cases[4].expectedErr = errors.New("Column count doesn't match value count at row 1") + testInfo.sqlInfos[3].cases[4].expectedCompileErr = errors.New("Column count doesn't match value count at row 1") alterTableSQL := "alter table t add column d3 enum('a', 'b') not null default 'a' after c3" s.test(c, "", alterTableSQL, testInfo) // TODO: Add more DDL statements. @@ -227,10 +227,11 @@ func (s *testStateChangeSuite) test(c *C, tableName, alterTableSQL string, testI } type stateCase struct { - session session.Session - rawStmt ast.StmtNode - stmt ast.Statement - expectedErr error + session session.Session + rawStmt ast.StmtNode + stmt ast.Statement + expectedExecErr error + expectedCompileErr error } type sqlInfo struct { @@ -299,6 +300,13 @@ func (t *testExecInfo) compileSQL(idx int) (err error) { return errors.Trace(err) } c.stmt, err = compiler.Compile(ctx, c.rawStmt) + if c.expectedCompileErr != nil { + if err == nil { + err = errors.Errorf("expected error %s but got nil", c.expectedCompileErr) + } else if strings.Contains(err.Error(), c.expectedCompileErr.Error()) { + err = nil + } + } if err != nil { return errors.Trace(err) } @@ -309,11 +317,14 @@ func (t *testExecInfo) compileSQL(idx int) (err error) { func (t *testExecInfo) execSQL(idx int) error { for _, sqlInfo := range t.sqlInfos { c := sqlInfo.cases[idx] + if c.expectedCompileErr != nil { + continue + } _, err := c.stmt.Exec(context.TODO()) - if c.expectedErr != nil { + if c.expectedExecErr != nil { if err == nil { - err = errors.Errorf("expected error %s but got nil", c.expectedErr) - } else if strings.Contains(err.Error(), c.expectedErr.Error()) { + err = errors.Errorf("expected error %s but got nil", c.expectedExecErr) + } else if strings.Contains(err.Error(), c.expectedExecErr.Error()) { err = nil } } diff --git a/plan/planbuilder.go b/plan/planbuilder.go index 31dc30ef4e7ed..476c04b12b9ca 100644 --- a/plan/planbuilder.go +++ b/plan/planbuilder.go @@ -1088,15 +1088,8 @@ func (b *planBuilder) buildValuesListOfInsert(insert *ast.InsertStmt, insertPlan return } - // If there is col_list, length of value_list should be the same with col_list. - if len(insert.Columns) > 0 && len(insert.Columns) != len(insert.Lists[0]) { - b.err = ErrWrongValueCountOnRow.GenByArgs(1) - return - } - - // If value_list is not empty, its length must be the same with the number of column in this table. - valueListNotEmpty := len(insert.Lists) > 0 && len(insert.Lists[0]) > 0 - if valueListNotEmpty { + if len(insert.Columns) > 0 || len(insert.Lists[0]) > 0 { + // If value_list is not empty or the col_list is not empty, length of value_list should be the same with col_list's. if len(insert.Lists[0]) != len(affectedValuesCols) { b.err = ErrWrongValueCountOnRow.GenByArgs(1) return @@ -1108,8 +1101,7 @@ func (b *planBuilder) buildValuesListOfInsert(insert *ast.InsertStmt, insertPlan return } } - - // If the value_list is empty and we have generated column, we can still write to this table. + // If the value_list and col_list is empty and we have generated column, we can still write to this table. // i.e. insert into t values(); can be executed successfully if t have generated column. }