fix(orm): txOrm miss debug log
This commit is contained in:
parent
556e743c84
commit
0e9b2c27ea
@ -65,7 +65,7 @@
|
||||
- Fix 4734: do not reset id in Delete function. [4738](https://github.com/beego/beego/pull/4738) [4742](https://github.com/beego/beego/pull/4742)
|
||||
- Fix 4699: Remove Remove goyaml2 dependency. [4755](https://github.com/beego/beego/pull/4755)
|
||||
- Fix 4698: Prompt error when config format is incorrect. [4757](https://github.com/beego/beego/pull/4757)
|
||||
|
||||
- Fix 4674: Tx Orm missing debug log [4756](https://github.com/beego/beego/pull/4756)
|
||||
## Fix Sonar
|
||||
|
||||
- [4677](https://github.com/beego/beego/pull/4677)
|
||||
|
||||
@ -536,6 +536,11 @@ func (o *orm) BeginWithCtxAndOpts(ctx context.Context, opts *sql.TxOptions) (TxO
|
||||
db: &TxDB{tx: tx},
|
||||
},
|
||||
}
|
||||
|
||||
if Debug {
|
||||
_txOrm.db = newDbQueryLog(o.alias, _txOrm.db)
|
||||
}
|
||||
|
||||
var taskTxOrm TxOrmer = _txOrm
|
||||
return taskTxOrm, nil
|
||||
}
|
||||
|
||||
@ -2886,3 +2886,59 @@ func TestContextCanceled(t *testing.T) {
|
||||
_, err = qs.Filter("UserName", "slene").CountWithCtx(ctx)
|
||||
throwFail(t, AssertIs(err, context.Canceled))
|
||||
}
|
||||
|
||||
func TestDebugLog(t *testing.T) {
|
||||
|
||||
txCommitFn := func() {
|
||||
o := NewOrm()
|
||||
o.DoTx(func(ctx context.Context, txOrm TxOrmer) (txerr error) {
|
||||
_, txerr = txOrm.QueryTable(&User{}).Count()
|
||||
return
|
||||
})
|
||||
}
|
||||
|
||||
txRollbackFn := func() {
|
||||
o := NewOrm()
|
||||
o.DoTx(func(ctx context.Context, txOrm TxOrmer) (txerr error) {
|
||||
user := NewUser()
|
||||
user.UserName = "slene"
|
||||
user.Email = "vslene@gmail.com"
|
||||
user.Password = "pass"
|
||||
user.Status = 3
|
||||
user.IsStaff = true
|
||||
user.IsActive = true
|
||||
|
||||
txOrm.Insert(user)
|
||||
txerr = fmt.Errorf("mock error")
|
||||
return
|
||||
})
|
||||
}
|
||||
|
||||
Debug = true
|
||||
output1 := captureDebugLogOutput(txCommitFn)
|
||||
|
||||
assert.Contains(t, output1, "START TRANSACTION")
|
||||
assert.Contains(t, output1, "COMMIT")
|
||||
|
||||
output2 := captureDebugLogOutput(txRollbackFn)
|
||||
|
||||
assert.Contains(t, output2, "START TRANSACTION")
|
||||
assert.Contains(t, output2, "ROLLBACK")
|
||||
|
||||
Debug = false
|
||||
output1 = captureDebugLogOutput(txCommitFn)
|
||||
assert.EqualValues(t, output1, "")
|
||||
|
||||
output2 = captureDebugLogOutput(txRollbackFn)
|
||||
assert.EqualValues(t, output2, "")
|
||||
}
|
||||
|
||||
func captureDebugLogOutput(f func()) string {
|
||||
var buf bytes.Buffer
|
||||
DebugLog.SetOutput(&buf)
|
||||
defer func() {
|
||||
DebugLog.SetOutput(os.Stderr)
|
||||
}()
|
||||
f()
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user