1 Star 0 Fork 0

铁匠/daox

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
tx_test.go 2.41 KB
一键复制 编辑 原始数据 按行查看 历史
铁匠 提交于 2024-04-15 13:41 . Dev (#35)
package daox_test
import (
"context"
"database/sql"
"errors"
"testing"
"github.com/DATA-DOG/go-sqlmock"
"github.com/jmoiron/sqlx"
"github.com/stretchr/testify/assert"
"github.com/fengjx/daox"
"github.com/fengjx/daox/sqlbuilder"
"github.com/fengjx/daox/sqlbuilder/ql"
)
func TestTxManager_ExecTx(t *testing.T) {
type testCase struct {
name string
mockHandler func(mock sqlmock.Sqlmock)
sourceFunc func(t *testing.T, db *sql.DB) *sqlx.DB
txFun daox.TxFun
wantErr error
}
testCases := []testCase{
{
name: "commit",
mockHandler: func(mock sqlmock.Sqlmock) {
mock.ExpectBegin()
mock.ExpectExec("UPDATE `blog`").WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectExec("INSERT INTO `blog_viewer`").WithArgs(100, 1).WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectCommit()
},
sourceFunc: func(t *testing.T, db *sql.DB) *sqlx.DB {
dbx := sqlx.NewDb(db, "mysql")
return dbx
},
txFun: func(txCtx context.Context, tx *sqlx.Tx) error {
execSQL, args, err := sqlbuilder.NewUpdater("blog").
Set("views", 100).
Where(ql.C(ql.Col("id").EQ(1))).
SQLArgs()
if err != nil {
return err
}
if _, err = tx.Exec(execSQL, args...); err != nil {
return err
}
execSQL, err = sqlbuilder.NewInserter("blog_viewer").
Columns("user_id", "blog_id").
NameSQL()
if _, err = tx.Exec(execSQL, 100, 1); err != nil {
return err
}
return nil
},
wantErr: nil,
},
{
name: "rollback",
mockHandler: func(mock sqlmock.Sqlmock) {
mock.ExpectBegin()
mock.ExpectRollback()
},
sourceFunc: func(t *testing.T, db *sql.DB) *sqlx.DB {
dbx := sqlx.NewDb(db, "mysql")
return dbx
},
txFun: func(txCtx context.Context, tx *sqlx.Tx) error {
return errors.New("rollback")
},
wantErr: errors.New("rollback"),
},
}
doTC := func(tc testCase) {
mockDB, mock, err := sqlmock.New()
if err != nil {
t.Fatal(err)
}
defer func(db *sql.DB) { _ = db.Close() }(mockDB)
db := tc.sourceFunc(t, mockDB)
tc.mockHandler(mock)
ctx := context.Background()
manager := daox.NewTxManager(db)
err = manager.ExecTx(ctx, func(txCtx context.Context, tx *sqlx.Tx) error {
return tc.txFun(txCtx, tx)
})
assert.Equal(t, tc.wantErr, err)
if err != nil {
return
}
if err = mock.ExpectationsWereMet(); err != nil {
t.Error(err)
}
}
for _, tc := range testCases {
doTC(tc)
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/fengjx/daox.git
[email protected]:fengjx/daox.git
fengjx
daox
daox
master

搜索帮助