Skip to content

Commit

Permalink
fix test.
Browse files Browse the repository at this point in the history
  • Loading branch information
winoros committed Jul 17, 2018
1 parent 78a3660 commit 8d99c66
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 23 deletions.
35 changes: 23 additions & 12 deletions ddl/db_change_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand All @@ -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
}
}
Expand Down
14 changes: 3 additions & 11 deletions plan/planbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
}

Expand Down

0 comments on commit 8d99c66

Please sign in to comment.