diff --git a/storage/dao/dao_test.go b/storage/dao/dao_test.go deleted file mode 100644 index 1909f2d..0000000 --- a/storage/dao/dao_test.go +++ /dev/null @@ -1,5 +0,0 @@ -package dao - -import ( - _ "gitee.com/wheat-os/wheatCache/conf" -) diff --git a/storage/dao/stringx_test.go b/storage/dao/stringx_test.go new file mode 100644 index 0000000..5e53c21 --- /dev/null +++ b/storage/dao/stringx_test.go @@ -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) + +// }