From 6b2f3fb3be15c50ba29352f74428637da7c06e42 Mon Sep 17 00:00:00 2001 From: bandl <1658002533@qq.com> Date: Thu, 18 Nov 2021 15:35:21 +0800 Subject: [PATCH] test(dao-stringx): add dao stringx test --- storage/dao/dao_test.go | 5 --- storage/dao/stringx_test.go | 82 +++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 5 deletions(-) delete mode 100644 storage/dao/dao_test.go create mode 100644 storage/dao/stringx_test.go 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) + +// }