test(storage-stringx): add stringx test

This commit is contained in:
Pyroo 2021-11-25 20:27:05 +08:00
parent 64a02b642e
commit d6792444a7
1 changed files with 89 additions and 5 deletions

View File

@ -1,6 +1,7 @@
package dao
import (
// "reflect"
"testing"
"gitee.com/wheat-os/wheatCache/pkg/lru"
@ -74,9 +75,92 @@ func TestDao_Reduce(t *testing.T) {
require.Error(t, err)
}
// func TestDao_SetBit_GetBit(t *testing.T) {
// baseKey := proto.NewBaseKey("test")
// lru := lru.NewLRUCache()
// dao := NewDao(lru)
func TestDao_SetBit_GitBit(t *testing.T) {
baseKey := proto.NewBaseKey("s")
lru := lru.NewLRUCache()
dao := NewDao(lru)
// }
_, err := dao.GetBit(baseKey, 8)
require.Error(t, err)
_, err = dao.SetBit(baseKey, true, 8)
require.NoError(t, err)
resp, err := dao.GetBit(baseKey, 8)
require.NoError(t, err)
require.Equal(t, resp.Val, true)
resp, err = dao.GetBit(baseKey, 7)
require.NoError(t, err)
require.Equal(t, resp.Val, false)
resp, err = dao.GetBit(baseKey, 9)
require.NoError(t, err)
require.Equal(t, resp.Val, false)
}
func TestDao_GetRange(t *testing.T) {
baseKey := proto.NewBaseKey("s")
lru := lru.NewLRUCache()
dao := NewDao(lru)
_, err := dao.GetRange(baseKey, 0, 5)
require.Error(t, err)
_, err = dao.Set(baseKey, "abcdef")
require.NoError(t, err)
resp, err := dao.GetRange(baseKey, 0, 3)
require.NoError(t, err)
require.Equal(t, resp.Result, "abc")
resp, err = dao.GetRange(baseKey, 0, 7)
require.Error(t, err)
}
func TestDao_GetSet(t *testing.T) {
baseKey := proto.NewBaseKey("s")
lru := lru.NewLRUCache()
dao := NewDao(lru)
_, err := dao.Set(baseKey, "a")
require.NoError(t, err)
resp, err := dao.GetSet(baseKey, "a")
require.NoError(t, err)
require.Equal(t, resp.Result, "a")
// TODO 这个测试有问题,等待修复,问题详情: https://gitee.com/wheat-os/wheat-cache/issues/I4IV41
resp, err = dao.GetSet(baseKey, "s")
require.NoError(t, err)
require.Equal(t, resp.Result, nil)
}
func TestDao_StrLen(t *testing.T) {
baseKey := proto.NewBaseKey("s")
lru := lru.NewLRUCache()
dao := NewDao(lru)
_, err := dao.StrLen(baseKey)
require.Error(t, err)
_, err = dao.Set(baseKey, "abc")
require.NoError(t, err)
resp, err := dao.StrLen(baseKey)
require.NoError(t, err)
require.Equal(t, resp.Length, int32(3))
}
func TestDao_Setnx(t *testing.T) {
baseKey := proto.NewBaseKey("s")
lru := lru.NewLRUCache()
dao := NewDao(lru)
_, err := dao.Setnx(baseKey, "abc")
require.NoError(t, err)
resp, err := dao.Get(baseKey)
require.NoError(t, err)
require.Equal(t, resp.Result, "abc")
_, err = dao.Setnx(baseKey, "abc")
require.Error(t, err)
}