From eb0014437b40eb515bbd7c28c305c18a3ccd5a21 Mon Sep 17 00:00:00 2001 From: bandl <1658002533@qq.com> Date: Sun, 17 Oct 2021 21:06:24 +0800 Subject: [PATCH] doc(init-doc): refactor doc --- doc/{ => pkg/lru}/LRU设计.md | 0 .../lru}/RDB + AOF 的事务解决方案.md | 8 +-- doc/test.md | 0 doc/开发规范/单元测试.md | 54 +++++++++++++++++++ doc/{ => 开发规范}/开发流程.md | 0 5 files changed, 58 insertions(+), 4 deletions(-) rename doc/{ => pkg/lru}/LRU设计.md (100%) rename doc/{ => pkg/lru}/RDB + AOF 的事务解决方案.md (79%) delete mode 100644 doc/test.md create mode 100644 doc/开发规范/单元测试.md rename doc/{ => 开发规范}/开发流程.md (100%) diff --git a/doc/LRU设计.md b/doc/pkg/lru/LRU设计.md similarity index 100% rename from doc/LRU设计.md rename to doc/pkg/lru/LRU设计.md diff --git a/doc/RDB + AOF 的事务解决方案.md b/doc/pkg/lru/RDB + AOF 的事务解决方案.md similarity index 79% rename from doc/RDB + AOF 的事务解决方案.md rename to doc/pkg/lru/RDB + AOF 的事务解决方案.md index e547dfa..2348607 100644 --- a/doc/RDB + AOF 的事务解决方案.md +++ b/doc/pkg/lru/RDB + AOF 的事务解决方案.md @@ -1,5 +1,5 @@ -### RDB + AOF 的事务解决方案 - - - +### RDB + AOF 的事务解决方案 + + + ![事务RDB方案](https://gitee.com/timedb/img/raw/master/images/事务RDB方案.svg) \ No newline at end of file diff --git a/doc/test.md b/doc/test.md deleted file mode 100644 index e69de29..0000000 diff --git a/doc/开发规范/单元测试.md b/doc/开发规范/单元测试.md new file mode 100644 index 0000000..6c326c7 --- /dev/null +++ b/doc/开发规范/单元测试.md @@ -0,0 +1,54 @@ +### 单元测试文档 + +#### 样例 +```go +package dao + +import ( + "testing" + + _ "gitee.com/timedb/wheatCache/conf" + "gitee.com/timedb/wheatCache/pkg/lru" + "gitee.com/timedb/wheatCache/pkg/proto" + "github.com/stretchr/testify/require" +) + +// 规范1. 每个 package 应该都至少有一个单测 + +// 规范2. 包里有公用的 mock 数据应该分离出来。 +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) + + // 规范3. 使用 require 包来完成单测 + require.NoError(t, err) + } + +} + +// 规范4. 单元测试应该尽可能覆盖全部情况,不要使用依赖注入。 +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) +} + + +``` \ No newline at end of file diff --git a/doc/开发流程.md b/doc/开发规范/开发流程.md similarity index 100% rename from doc/开发流程.md rename to doc/开发规范/开发流程.md