test(dao-stringx): add dao stringx test

This commit is contained in:
bandl 2021-11-18 15:35:21 +08:00
parent 499322c080
commit 6b2f3fb3be
2 changed files with 82 additions and 5 deletions

View File

@ -1,5 +0,0 @@
package dao
import (
_ "gitee.com/wheat-os/wheatCache/conf"
)

View File

@ -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)
// }