forked from p93542168/wheat-cache
!95 add dao test stringx
Merge pull request !95 from bandl/test-dao-mock
This commit is contained in:
commit
5e89f37e40
1
go.mod
1
go.mod
|
@ -3,6 +3,7 @@ module gitee.com/wheat-os/wheatCache
|
|||
go 1.16
|
||||
|
||||
require (
|
||||
github.com/golang/mock v1.5.0
|
||||
github.com/spf13/cobra v1.2.1
|
||||
github.com/spf13/viper v1.8.1
|
||||
github.com/stretchr/testify v1.7.0
|
||||
|
|
1
go.sum
1
go.sum
|
@ -89,6 +89,7 @@ github.com/golang/mock v1.4.0/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt
|
|||
github.com/golang/mock v1.4.1/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw=
|
||||
github.com/golang/mock v1.4.3/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw=
|
||||
github.com/golang/mock v1.4.4/go.mod h1:l3mdAwkq5BuhzHwde/uurv3sEJeZMXNpwsxVWU71h+4=
|
||||
github.com/golang/mock v1.5.0 h1:jlYHihg//f7RRwuPfptm04yp4s7O6Kw8EZiVYIGcH0g=
|
||||
github.com/golang/mock v1.5.0/go.mod h1:CWnOUgYIOo4TcNZ0wHX3YZCqsaM1I1Jvs6v3mP3KVu8=
|
||||
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||
|
|
4
makefile
4
makefile
|
@ -52,3 +52,7 @@ init-conf:
|
|||
.PHONY: gen-service
|
||||
gen-service:
|
||||
@python3 ./shell/make_service.py
|
||||
|
||||
.PHONY: gen-mock
|
||||
gen-mock:
|
||||
@mockgen -source=./pkg/proto/storage.pb.go CommServerClient > ./mock/storage/mock_client.gen.go
|
File diff suppressed because it is too large
Load Diff
|
@ -1,5 +0,0 @@
|
|||
package dao
|
||||
|
||||
import (
|
||||
_ "gitee.com/wheat-os/wheatCache/conf"
|
||||
)
|
|
@ -0,0 +1,82 @@
|
|||
package dao
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"gitee.com/wheat-os/wheatCache/pkg/lru"
|
||||
"gitee.com/wheat-os/wheatCache/pkg/proto"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestDao_Set_Get(t *testing.T) {
|
||||
baseKey := proto.NewBaseKey("abbs")
|
||||
lru := lru.NewLRUCache()
|
||||
dao := NewDao(lru)
|
||||
|
||||
_, err := dao.Set(baseKey, "bbq")
|
||||
require.NoError(t, err)
|
||||
resp, err := dao.Get(baseKey)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, resp.Result, "bbq")
|
||||
}
|
||||
|
||||
func TestDao_Add(t *testing.T) {
|
||||
baseKey := proto.NewBaseKey("test")
|
||||
lru := lru.NewLRUCache()
|
||||
dao := NewDao(lru)
|
||||
|
||||
// 整数 add
|
||||
dao.Set(baseKey, "1")
|
||||
dao.Add(baseKey, 2)
|
||||
resp, err := dao.Get(baseKey)
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Equal(t, resp.Result, "3")
|
||||
|
||||
// 浮点数
|
||||
dao.Set(baseKey, "1.1")
|
||||
dao.Add(baseKey, 2)
|
||||
resp, err = dao.Get(baseKey)
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Equal(t, resp.Result, "3.10")
|
||||
|
||||
// 字符串
|
||||
dao.Set(baseKey, "1awd.1")
|
||||
_, err = dao.Add(baseKey, 2)
|
||||
require.Error(t, err)
|
||||
}
|
||||
|
||||
func TestDao_Reduce(t *testing.T) {
|
||||
baseKey := proto.NewBaseKey("test")
|
||||
lru := lru.NewLRUCache()
|
||||
dao := NewDao(lru)
|
||||
|
||||
// 整数 add
|
||||
dao.Set(baseKey, "1")
|
||||
dao.Reduce(baseKey, 2)
|
||||
resp, err := dao.Get(baseKey)
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Equal(t, resp.Result, "-1")
|
||||
|
||||
// 浮点数
|
||||
dao.Set(baseKey, "1.1")
|
||||
dao.Reduce(baseKey, 2)
|
||||
resp, err = dao.Get(baseKey)
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Equal(t, resp.Result, "-0.90")
|
||||
|
||||
// 字符串
|
||||
dao.Set(baseKey, "1awd.1")
|
||||
_, err = dao.Reduce(baseKey, 2)
|
||||
require.Error(t, err)
|
||||
}
|
||||
|
||||
// func TestDao_SetBit_GetBit(t *testing.T) {
|
||||
// baseKey := proto.NewBaseKey("test")
|
||||
// lru := lru.NewLRUCache()
|
||||
// dao := NewDao(lru)
|
||||
|
||||
// }
|
|
@ -4,9 +4,14 @@ import (
|
|||
"sync"
|
||||
|
||||
"gitee.com/wheat-os/wheatCache/pkg/proto"
|
||||
"github.com/golang/mock/gomock"
|
||||
)
|
||||
|
||||
var (
|
||||
oneGatewayClient sync.Once
|
||||
gatewayClient proto.CommServerClient
|
||||
)
|
||||
|
||||
var (
|
||||
GateWayCtrl *gomock.Controller
|
||||
)
|
||||
|
|
|
@ -5,10 +5,22 @@ import (
|
|||
|
||||
"gitee.com/wheat-os/wheatCache/client"
|
||||
"gitee.com/wheat-os/wheatCache/client/middle"
|
||||
_ "gitee.com/wheat-os/wheatCache/conf"
|
||||
mockClient "gitee.com/wheat-os/wheatCache/mock/storage"
|
||||
"gitee.com/wheat-os/wheatCache/pkg/errorx"
|
||||
"gitee.com/wheat-os/wheatCache/pkg/proto"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
func NewGatewayClient() (proto.CommServerClient, error) {
|
||||
|
||||
if viper.GetString("env") == "dev" {
|
||||
if GateWayCtrl == nil {
|
||||
return nil, errorx.New("mock ctrl not init")
|
||||
}
|
||||
return mockClient.NewMockCommServerClient(GateWayCtrl), nil
|
||||
}
|
||||
|
||||
oneGatewayClient.Do(func() {
|
||||
cli, err := client.NewWheatClient("127.0.0.1:5891", middle.WithUnaryColonyClient)
|
||||
if err == nil {
|
||||
|
|
|
@ -0,0 +1,36 @@
|
|||
package external
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
_ "gitee.com/wheat-os/wheatCache/conf"
|
||||
mockClient "gitee.com/wheat-os/wheatCache/mock/storage"
|
||||
"gitee.com/wheat-os/wheatCache/pkg/proto"
|
||||
"github.com/golang/mock/gomock"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestNewGatewayClient(t *testing.T) {
|
||||
ctrl := gomock.NewController(t)
|
||||
defer ctrl.Finish()
|
||||
GateWayCtrl = ctrl
|
||||
|
||||
cli, err := NewGatewayClient()
|
||||
require.NoError(t, err)
|
||||
mockClient := cli.(*mockClient.MockCommServerClient)
|
||||
ctx := context.Background()
|
||||
|
||||
mockClient.EXPECT().Get(ctx, gomock.Any()).Return(&proto.GetResponse{
|
||||
Result: "mockData",
|
||||
}, nil)
|
||||
|
||||
resp, err := mockClient.Get(ctx, nil)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, resp.Result, "mockData")
|
||||
|
||||
resp, err = mockClient.Get(ctx, nil)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, resp.Result, "mockData")
|
||||
|
||||
}
|
Loading…
Reference in New Issue