test(dao): add dao test

This commit is contained in:
bandl 2021-10-17 20:03:13 +08:00
parent 3c68787ff4
commit a00f10c70e
2 changed files with 68 additions and 24 deletions

View File

@ -4,12 +4,24 @@ import (
"testing" "testing"
_ "gitee.com/timedb/wheatCache/conf" _ "gitee.com/timedb/wheatCache/conf"
"gitee.com/timedb/wheatCache/pkg/logx"
"gitee.com/timedb/wheatCache/pkg/lru" "gitee.com/timedb/wheatCache/pkg/lru"
"gitee.com/timedb/wheatCache/pkg/proto" "gitee.com/timedb/wheatCache/pkg/proto"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
func mockData(t *testing.T, d *Dao) {
values := []string{"1", "1.3", "abcdefg"}
for _, val := range values {
key := &proto.BaseKey{
Key: val,
}
_, err := d.Set(key, val)
require.NoError(t, err)
}
}
func TestDao_Set(t *testing.T) { func TestDao_Set(t *testing.T) {
lruCache := lru.NewLRUCache() lruCache := lru.NewLRUCache()
dao := NewDao(lruCache) dao := NewDao(lruCache)
@ -17,8 +29,61 @@ func TestDao_Set(t *testing.T) {
Key: "abc", Key: "abc",
} }
res, err := dao.Get(key) res, err := dao.Set(key, "abc")
require.NoError(t, err)
require.Equal(t, res, "abc")
res, err = dao.Get(key)
require.NoError(t, err)
require.Equal(t, res, "abc")
}
func TestDao_Add(t *testing.T) {
lruCache := lru.NewLRUCache()
dao := NewDao(lruCache)
mockData(t, dao)
resp, err := dao.Add(&proto.BaseKey{Key: "1"}, 2)
require.NoError(t, err)
require.Equal(t, resp, "3")
resp, err = dao.Add(&proto.BaseKey{Key: "1.3"}, 2)
require.NoError(t, err)
require.Equal(t, resp, "3.30")
_, err = dao.Add(&proto.BaseKey{Key: "abcdefg"}, 2)
require.Error(t, err) require.Error(t, err)
logx.Infoln(res) }
func TestDao_Reduce(t *testing.T) {
lruCache := lru.NewLRUCache()
dao := NewDao(lruCache)
mockData(t, dao)
resp, err := dao.Reduce(&proto.BaseKey{Key: "1"}, 2)
require.NoError(t, err)
require.Equal(t, resp, "-1")
resp, err = dao.Reduce(&proto.BaseKey{Key: "1.3"}, 2)
require.NoError(t, err)
require.Equal(t, resp, "-0.70")
_, err = dao.Reduce(&proto.BaseKey{Key: "abcdefg"}, 2)
require.Error(t, err)
}
func TestDao_Setbit(t *testing.T) {
lruCache := lru.NewLRUCache()
dao := NewDao(lruCache)
key := &proto.BaseKey{
Key: "abc",
}
err := dao.Setbit(key, true, 3089)
require.NoError(t, err)
re, err := dao.GetBit(key, 3089)
require.NoError(t, err)
require.Equal(t, re, true)
} }

View File

@ -1,21 +0,0 @@
package server
import (
"context"
"fmt"
"gitee.com/timedb/wheatCache/pkg/proto"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
"testing"
)
func TestServerSingle_Get(t *testing.T) {
comm, err := grpc.Dial("127.0.0.1:5890", grpc.WithInsecure())
require.NoError(t, err)
ctx := context.Background()
cli := proto.NewCommServerClient(comm)
resp, err := cli.Get(ctx, &proto.GetRequest{})
require.NoError(t, err)
fmt.Println(resp)
}