fix UT and Sonar problem
This commit is contained in:
parent
be6a204232
commit
deda13f3e4
@ -20,7 +20,9 @@ import (
|
|||||||
"github.com/beego/beego/v2/core/logs"
|
"github.com/beego/beego/v2/core/logs"
|
||||||
)
|
)
|
||||||
|
|
||||||
const mockCtxKey = "beego-orm-mock"
|
type mockCtxKeyType string
|
||||||
|
|
||||||
|
const mockCtxKey = mockCtxKeyType("beego-orm-mock")
|
||||||
|
|
||||||
func CtxWithMock(ctx context.Context, mock ...*Mock) context.Context {
|
func CtxWithMock(ctx context.Context, mock ...*Mock) context.Context {
|
||||||
return context.WithValue(ctx, mockCtxKey, mock)
|
return context.WithValue(ctx, mockCtxKey, mock)
|
||||||
|
|||||||
@ -24,7 +24,7 @@ import (
|
|||||||
|
|
||||||
"github.com/beego/beego/v2/client/orm"
|
"github.com/beego/beego/v2/client/orm"
|
||||||
)
|
)
|
||||||
|
const mockErrorMsg = "mock error"
|
||||||
func init() {
|
func init() {
|
||||||
orm.RegisterModel(&User{})
|
orm.RegisterModel(&User{})
|
||||||
}
|
}
|
||||||
@ -65,7 +65,7 @@ func TestMockInsertOrUpdateWithCtx(t *testing.T) {
|
|||||||
func TestMockRead(t *testing.T) {
|
func TestMockRead(t *testing.T) {
|
||||||
s := StartMock()
|
s := StartMock()
|
||||||
defer s.Clear()
|
defer s.Clear()
|
||||||
err := errors.New("mock error")
|
err := errors.New(mockErrorMsg)
|
||||||
s.Mock(MockRead((&User{}).TableName(), func(data interface{}) {
|
s.Mock(MockRead((&User{}).TableName(), func(data interface{}) {
|
||||||
u := data.(*User)
|
u := data.(*User)
|
||||||
u.Name = "Tom"
|
u.Name = "Tom"
|
||||||
@ -100,7 +100,7 @@ func TestMockQueryTableWithCtx(t *testing.T) {
|
|||||||
func TestMockTable(t *testing.T) {
|
func TestMockTable(t *testing.T) {
|
||||||
s := StartMock()
|
s := StartMock()
|
||||||
defer s.Clear()
|
defer s.Clear()
|
||||||
mock := errors.New("mock error")
|
mock := errors.New(mockErrorMsg)
|
||||||
s.Mock(MockTable((&User{}).TableName(), mock))
|
s.Mock(MockTable((&User{}).TableName(), mock))
|
||||||
o := orm.NewOrm()
|
o := orm.NewOrm()
|
||||||
res := o.Read(&User{})
|
res := o.Read(&User{})
|
||||||
@ -110,7 +110,7 @@ func TestMockTable(t *testing.T) {
|
|||||||
func TestMockInsertMultiWithCtx(t *testing.T) {
|
func TestMockInsertMultiWithCtx(t *testing.T) {
|
||||||
s := StartMock()
|
s := StartMock()
|
||||||
defer s.Clear()
|
defer s.Clear()
|
||||||
mock := errors.New("mock error")
|
mock := errors.New(mockErrorMsg)
|
||||||
s.Mock(MockInsertMultiWithCtx((&User{}).TableName(), 12, mock))
|
s.Mock(MockInsertMultiWithCtx((&User{}).TableName(), 12, mock))
|
||||||
o := orm.NewOrm()
|
o := orm.NewOrm()
|
||||||
res, err := o.InsertMulti(11, []interface{}{&User{}})
|
res, err := o.InsertMulti(11, []interface{}{&User{}})
|
||||||
@ -121,7 +121,7 @@ func TestMockInsertMultiWithCtx(t *testing.T) {
|
|||||||
func TestMockInsertWithCtx(t *testing.T) {
|
func TestMockInsertWithCtx(t *testing.T) {
|
||||||
s := StartMock()
|
s := StartMock()
|
||||||
defer s.Clear()
|
defer s.Clear()
|
||||||
mock := errors.New("mock error")
|
mock := errors.New(mockErrorMsg)
|
||||||
s.Mock(MockInsertWithCtx((&User{}).TableName(), 13, mock))
|
s.Mock(MockInsertWithCtx((&User{}).TableName(), 13, mock))
|
||||||
o := orm.NewOrm()
|
o := orm.NewOrm()
|
||||||
res, err := o.Insert(&User{})
|
res, err := o.Insert(&User{})
|
||||||
@ -132,7 +132,7 @@ func TestMockInsertWithCtx(t *testing.T) {
|
|||||||
func TestMockUpdateWithCtx(t *testing.T) {
|
func TestMockUpdateWithCtx(t *testing.T) {
|
||||||
s := StartMock()
|
s := StartMock()
|
||||||
defer s.Clear()
|
defer s.Clear()
|
||||||
mock := errors.New("mock error")
|
mock := errors.New(mockErrorMsg)
|
||||||
s.Mock(MockUpdateWithCtx((&User{}).TableName(), 12, mock))
|
s.Mock(MockUpdateWithCtx((&User{}).TableName(), 12, mock))
|
||||||
o := orm.NewOrm()
|
o := orm.NewOrm()
|
||||||
res, err := o.Update(&User{})
|
res, err := o.Update(&User{})
|
||||||
@ -143,7 +143,7 @@ func TestMockUpdateWithCtx(t *testing.T) {
|
|||||||
func TestMockLoadRelatedWithCtx(t *testing.T) {
|
func TestMockLoadRelatedWithCtx(t *testing.T) {
|
||||||
s := StartMock()
|
s := StartMock()
|
||||||
defer s.Clear()
|
defer s.Clear()
|
||||||
mock := errors.New("mock error")
|
mock := errors.New(mockErrorMsg)
|
||||||
s.Mock(MockLoadRelatedWithCtx((&User{}).TableName(), "T", 12, mock))
|
s.Mock(MockLoadRelatedWithCtx((&User{}).TableName(), "T", 12, mock))
|
||||||
o := orm.NewOrm()
|
o := orm.NewOrm()
|
||||||
res, err := o.LoadRelated(&User{}, "T")
|
res, err := o.LoadRelated(&User{}, "T")
|
||||||
@ -154,7 +154,7 @@ func TestMockLoadRelatedWithCtx(t *testing.T) {
|
|||||||
func TestMockMethod(t *testing.T) {
|
func TestMockMethod(t *testing.T) {
|
||||||
s := StartMock()
|
s := StartMock()
|
||||||
defer s.Clear()
|
defer s.Clear()
|
||||||
mock := errors.New("mock error")
|
mock := errors.New(mockErrorMsg)
|
||||||
s.Mock(MockMethod("ReadWithCtx", mock))
|
s.Mock(MockMethod("ReadWithCtx", mock))
|
||||||
o := orm.NewOrm()
|
o := orm.NewOrm()
|
||||||
err := o.Read(&User{})
|
err := o.Read(&User{})
|
||||||
@ -164,7 +164,7 @@ func TestMockMethod(t *testing.T) {
|
|||||||
func TestMockReadForUpdateWithCtx(t *testing.T) {
|
func TestMockReadForUpdateWithCtx(t *testing.T) {
|
||||||
s := StartMock()
|
s := StartMock()
|
||||||
defer s.Clear()
|
defer s.Clear()
|
||||||
mock := errors.New("mock error")
|
mock := errors.New(mockErrorMsg)
|
||||||
s.Mock(MockReadForUpdateWithCtx((&User{}).TableName(), func(data interface{}) {
|
s.Mock(MockReadForUpdateWithCtx((&User{}).TableName(), func(data interface{}) {
|
||||||
u := data.(*User)
|
u := data.(*User)
|
||||||
u.Name = "Tom"
|
u.Name = "Tom"
|
||||||
@ -189,7 +189,7 @@ func TestMockRawWithCtx(t *testing.T) {
|
|||||||
func TestMockReadOrCreateWithCtx(t *testing.T) {
|
func TestMockReadOrCreateWithCtx(t *testing.T) {
|
||||||
s := StartMock()
|
s := StartMock()
|
||||||
defer s.Clear()
|
defer s.Clear()
|
||||||
mock := errors.New("mock error")
|
mock := errors.New(mockErrorMsg)
|
||||||
s.Mock(MockReadOrCreateWithCtx((&User{}).TableName(), func(data interface{}) {
|
s.Mock(MockReadOrCreateWithCtx((&User{}).TableName(), func(data interface{}) {
|
||||||
u := data.(*User)
|
u := data.(*User)
|
||||||
u.Name = "Tom"
|
u.Name = "Tom"
|
||||||
@ -206,7 +206,7 @@ func TestMockReadOrCreateWithCtx(t *testing.T) {
|
|||||||
func TestTransactionClosure(t *testing.T) {
|
func TestTransactionClosure(t *testing.T) {
|
||||||
s := StartMock()
|
s := StartMock()
|
||||||
defer s.Clear()
|
defer s.Clear()
|
||||||
mock := errors.New("mock error")
|
mock := errors.New(mockErrorMsg)
|
||||||
s.Mock(MockRead((&User{}).TableName(), func(data interface{}) {
|
s.Mock(MockRead((&User{}).TableName(), func(data interface{}) {
|
||||||
u := data.(*User)
|
u := data.(*User)
|
||||||
u.Name = "Tom"
|
u.Name = "Tom"
|
||||||
@ -219,7 +219,7 @@ func TestTransactionClosure(t *testing.T) {
|
|||||||
func TestTransactionManually(t *testing.T) {
|
func TestTransactionManually(t *testing.T) {
|
||||||
s := StartMock()
|
s := StartMock()
|
||||||
defer s.Clear()
|
defer s.Clear()
|
||||||
mock := errors.New("mock error")
|
mock := errors.New(mockErrorMsg)
|
||||||
s.Mock(MockRead((&User{}).TableName(), func(data interface{}) {
|
s.Mock(MockRead((&User{}).TableName(), func(data interface{}) {
|
||||||
u := data.(*User)
|
u := data.(*User)
|
||||||
u.Name = "Tom"
|
u.Name = "Tom"
|
||||||
@ -232,7 +232,7 @@ func TestTransactionManually(t *testing.T) {
|
|||||||
func TestTransactionRollback(t *testing.T) {
|
func TestTransactionRollback(t *testing.T) {
|
||||||
s := StartMock()
|
s := StartMock()
|
||||||
defer s.Clear()
|
defer s.Clear()
|
||||||
mock := errors.New("mock error")
|
mock := errors.New(mockErrorMsg)
|
||||||
s.Mock(MockRead((&User{}).TableName(), nil, errors.New("read error")))
|
s.Mock(MockRead((&User{}).TableName(), nil, errors.New("read error")))
|
||||||
s.Mock(MockRollback(mock))
|
s.Mock(MockRollback(mock))
|
||||||
_, err := originalTx()
|
_, err := originalTx()
|
||||||
@ -242,7 +242,7 @@ func TestTransactionRollback(t *testing.T) {
|
|||||||
func TestTransactionCommit(t *testing.T) {
|
func TestTransactionCommit(t *testing.T) {
|
||||||
s := StartMock()
|
s := StartMock()
|
||||||
defer s.Clear()
|
defer s.Clear()
|
||||||
mock := errors.New("mock error")
|
mock := errors.New(mockErrorMsg)
|
||||||
s.Mock(MockRead((&User{}).TableName(), func(data interface{}) {
|
s.Mock(MockRead((&User{}).TableName(), func(data interface{}) {
|
||||||
u := data.(*User)
|
u := data.(*User)
|
||||||
u.Name = "Tom"
|
u.Name = "Tom"
|
||||||
@ -280,7 +280,7 @@ func originalTxUsingClosure() (*User, error) {
|
|||||||
u := &User{}
|
u := &User{}
|
||||||
var err error
|
var err error
|
||||||
o := orm.NewOrm()
|
o := orm.NewOrm()
|
||||||
o.DoTx(func(ctx context.Context, txOrm orm.TxOrmer) error {
|
_ = o.DoTx(func(ctx context.Context, txOrm orm.TxOrmer) error {
|
||||||
err = txOrm.Read(u)
|
err = txOrm.Read(u)
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|||||||
@ -26,6 +26,26 @@ type DoNothingQueryM2Mer struct {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (d *DoNothingQueryM2Mer) AddWithCtx(ctx context.Context, i ...interface{}) (int64, error) {
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *DoNothingQueryM2Mer) RemoveWithCtx(ctx context.Context, i ...interface{}) (int64, error) {
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *DoNothingQueryM2Mer) ExistWithCtx(ctx context.Context, i interface{}) bool {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *DoNothingQueryM2Mer) ClearWithCtx(ctx context.Context) (int64, error) {
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *DoNothingQueryM2Mer) CountWithCtx(ctx context.Context) (int64, error) {
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (d *DoNothingQueryM2Mer) Add(i ...interface{}) (int64, error) {
|
func (d *DoNothingQueryM2Mer) Add(i ...interface{}) (int64, error) {
|
||||||
return 0, nil
|
return 0, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@ -15,13 +15,63 @@
|
|||||||
package mock
|
package mock
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
"github.com/beego/beego/v2/client/orm"
|
"github.com/beego/beego/v2/client/orm"
|
||||||
|
"github.com/beego/beego/v2/client/orm/clauses/order_clause"
|
||||||
)
|
)
|
||||||
|
|
||||||
// DoNothingQuerySetter do nothing
|
// DoNothingQuerySetter do nothing
|
||||||
// usually you use this to build your mock QuerySetter
|
// usually you use this to build your mock QuerySetter
|
||||||
type DoNothingQuerySetter struct {
|
type DoNothingQuerySetter struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *DoNothingQuerySetter) OrderClauses(orders ...*order_clause.Order) orm.QuerySeter {
|
||||||
|
return d
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *DoNothingQuerySetter) CountWithCtx(ctx context.Context) (int64, error) {
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *DoNothingQuerySetter) ExistWithCtx(ctx context.Context) bool {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *DoNothingQuerySetter) UpdateWithCtx(ctx context.Context, values orm.Params) (int64, error) {
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *DoNothingQuerySetter) DeleteWithCtx(ctx context.Context) (int64, error) {
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *DoNothingQuerySetter) PrepareInsertWithCtx(ctx context.Context) (orm.Inserter, error) {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *DoNothingQuerySetter) AllWithCtx(ctx context.Context, container interface{}, cols ...string) (int64, error) {
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *DoNothingQuerySetter) OneWithCtx(ctx context.Context, container interface{}, cols ...string) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *DoNothingQuerySetter) ValuesWithCtx(ctx context.Context, results *[]orm.Params, exprs ...string) (int64, error) {
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *DoNothingQuerySetter) ValuesListWithCtx(ctx context.Context, results *[]orm.ParamsList, exprs ...string) (int64, error) {
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *DoNothingQuerySetter) ValuesFlatWithCtx(ctx context.Context, result *orm.ParamsList, expr string) (int64, error) {
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *DoNothingQuerySetter) Aggregate(s string) orm.QuerySeter {
|
||||||
|
return d
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *DoNothingQuerySetter) Filter(s string, i ...interface{}) orm.QuerySeter {
|
func (d *DoNothingQuerySetter) Filter(s string, i ...interface{}) orm.QuerySeter {
|
||||||
|
|||||||
@ -40,7 +40,6 @@ func TestOrmStub_FilterChain(t *testing.T) {
|
|||||||
arg := inv.Args[0]
|
arg := inv.Args[0]
|
||||||
j := arg.(int)
|
j := arg.(int)
|
||||||
inv.Args[0] = j + 1
|
inv.Args[0] = j + 1
|
||||||
return
|
|
||||||
})
|
})
|
||||||
os.Mock(m)
|
os.Mock(m)
|
||||||
|
|
||||||
|
|||||||
@ -1,54 +0,0 @@
|
|||||||
// Copyright 2020 beego
|
|
||||||
//
|
|
||||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
// you may not use this file except in compliance with the License.
|
|
||||||
// You may obtain a copy of the License at
|
|
||||||
//
|
|
||||||
// http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
//
|
|
||||||
// Unless required by applicable law or agreed to in writing, software
|
|
||||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
// See the License for the specific language governing permissions and
|
|
||||||
// limitations under the License.
|
|
||||||
|
|
||||||
package error
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Code int32
|
|
||||||
|
|
||||||
func (c Code) ToInt32() int32 {
|
|
||||||
return int32(c)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
type Error struct {
|
|
||||||
Code Code
|
|
||||||
Msg string
|
|
||||||
Cause error
|
|
||||||
}
|
|
||||||
|
|
||||||
func (be *Error) String() string {
|
|
||||||
return fmt.Sprintf("code: %d, msg: %s", be.Code.ToInt32(), be.Msg)
|
|
||||||
}
|
|
||||||
|
|
||||||
func New(code Code, msg string) *Error {
|
|
||||||
return &Error{
|
|
||||||
Code: code,
|
|
||||||
Msg: msg,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func Wrap(cause error, code Code, msg string) {
|
|
||||||
errors.Wrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
func Convert(err error) *Error {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
Loading…
x
Reference in New Issue
Block a user