Skip to content

Commit

Permalink
feature: 同步支持mysql error expected
Browse files Browse the repository at this point in the history
  • Loading branch information
Rennbon committed Jan 7, 2022
2 parents 3d2b396 + 862ac54 commit 2b4c0a3
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions kit.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,13 @@ func (mk *mockit) MysqlExecExpect(ep ExpectParam, tb testing.TB) {
mk.sqlEx.ExpectCommit()
}

var errorInterface = reflect.TypeOf((*error)(nil)).Elem()

func (mk *mockit) MysqlQueryExpect(ep ExpectParam, tb testing.TB) {
var (
rows *sqlmock.Rows
err error
rows *sqlmock.Rows
errExpected bool
err error
)
p := ep.(*expectParam)
if len(p.method) == 0 {
Expand All @@ -175,9 +178,13 @@ func (mk *mockit) MysqlQueryExpect(ep ExpectParam, tb testing.TB) {
} else {
if len(p.returns) != 1 {
tb.Fatal("the query must return one result")

}
rows, err = sqlmock.NewRowsFromInterface(p.returns[0], mk.ormTag)
typ := reflect.TypeOf(p.returns[0])
if typ.Implements(errorInterface) {
errExpected = true
} else {
rows, err = sqlmock.NewRowsFromInterface(p.returns[0], mk.ormTag)
}
}
if err != nil {
tb.Fatalf("new rows failed:%s", err)
Expand All @@ -186,7 +193,11 @@ func (mk *mockit) MysqlQueryExpect(ep ExpectParam, tb testing.TB) {
for _, v := range p.args {
args = append(args, v)
}
mk.sqlEx.ExpectQuery(p.method).WithArgs(args...).WillReturnRows(rows)
if errExpected {
mk.sqlEx.ExpectQuery(p.method).WithArgs(args...).WillReturnError(p.returns[0].(error))
} else {
mk.sqlEx.ExpectQuery(p.method).WithArgs(args...).WillReturnRows(rows)
}
}

func (mk *mockit) InterfaceExpect(ep ExpectParam, tb testing.TB) {
Expand Down

0 comments on commit 2b4c0a3

Please sign in to comment.