Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support ORM mock #4407

Merged
merged 6 commits into from
Jan 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# developing
- ORM mock. [4407](https://github.com/beego/beego/pull/4407)
- Add sonar check and ignore test. [4432](https://github.com/beego/beego/pull/4432) [4433](https://github.com/beego/beego/pull/4433)
- Update changlog.yml to check every PR to develop branch.[4427](https://github.com/beego/beego/pull/4427)
- Fix 4396: Add context.param module into adapter. [4398](https://github.com/beego/beego/pull/4398)
Expand Down
63 changes: 63 additions & 0 deletions client/orm/mock/condition.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// 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 mock

import (
"context"

"github.com/beego/beego/v2/client/orm"
)

type Mock struct {
cond Condition
resp []interface{}
cb func(inv *orm.Invocation)
}

func NewMock(cond Condition, resp []interface{}, cb func(inv *orm.Invocation)) *Mock {
return &Mock{
cond: cond,
resp: resp,
cb: cb,
}
}

type Condition interface {
Match(ctx context.Context, inv *orm.Invocation) bool
}

type SimpleCondition struct {
tableName string
method string
}

func NewSimpleCondition(tableName string, methodName string) Condition {
return &SimpleCondition{
tableName: tableName,
method: methodName,
}
}

func (s *SimpleCondition) Match(ctx context.Context, inv *orm.Invocation) bool {
res := true
if len(s.tableName) != 0 {
res = res && (s.tableName == inv.GetTableName())
}

if len(s.method) != 0 {
res = res && (s.method == inv.Method)
}
return res
}
41 changes: 41 additions & 0 deletions client/orm/mock/condition_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// 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 mock

import (
"context"
"testing"

"github.com/stretchr/testify/assert"

"github.com/beego/beego/v2/client/orm"
)

func TestSimpleCondition_Match(t *testing.T) {
cond := NewSimpleCondition("", "")
res := cond.Match(context.Background(), &orm.Invocation{})
assert.True(t, res)
cond = NewSimpleCondition("hello", "")
assert.False(t, cond.Match(context.Background(), &orm.Invocation{}))

cond = NewSimpleCondition("", "A")
assert.False(t, cond.Match(context.Background(), &orm.Invocation{
Method: "B",
}))

assert.True(t, cond.Match(context.Background(), &orm.Invocation{
Method: "A",
}))
}
40 changes: 40 additions & 0 deletions client/orm/mock/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// 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 mock

import (
"context"

"github.com/beego/beego/v2/core/logs"
)

type mockCtxKeyType string

const mockCtxKey = mockCtxKeyType("beego-orm-mock")

func CtxWithMock(ctx context.Context, mock ...*Mock) context.Context {
return context.WithValue(ctx, mockCtxKey, mock)
}

func mockFromCtx(ctx context.Context) []*Mock {
ms := ctx.Value(mockCtxKey)
if ms != nil {
if res, ok := ms.([]*Mock); ok {
return res
}
logs.Error("mockCtxKey found in context, but value is not type []*Mock")
}
return nil
}
29 changes: 29 additions & 0 deletions client/orm/mock/context_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// 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 mock

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
)

func TestCtx(t *testing.T) {
ms := make([]*Mock, 0, 4)
ctx := CtxWithMock(context.Background(), ms...)
res := mockFromCtx(ctx)
assert.Equal(t, ms, res)
}
72 changes: 72 additions & 0 deletions client/orm/mock/mock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// 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 mock

import (
"context"

"github.com/beego/beego/v2/client/orm"
)

var stub = newOrmStub()

func init() {
orm.AddGlobalFilterChain(stub.FilterChain)
}

type Stub interface {
Mock(m *Mock)
Clear()
}

type OrmStub struct {
ms []*Mock
}

func StartMock() Stub {
return stub
}

func newOrmStub() *OrmStub {
return &OrmStub{
ms: make([]*Mock, 0, 4),
}
}

func (o *OrmStub) Mock(m *Mock) {
o.ms = append(o.ms, m)
}

func (o *OrmStub) Clear() {
o.ms = make([]*Mock, 0, 4)
}

func (o *OrmStub) FilterChain(next orm.Filter) orm.Filter {
return func(ctx context.Context, inv *orm.Invocation) []interface{} {

ms := mockFromCtx(ctx)
ms = append(ms, o.ms...)

for _, mock := range ms {
if mock.cond.Match(ctx, inv) {
if mock.cb != nil {
mock.cb(inv)
}
return mock.resp
}
}
return next(ctx, inv)
}
}
Loading