Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

提供了分组的装饰器实现 #33

Merged
merged 12 commits into from
Jan 1, 2024
Merged

提供了分组的装饰器实现 #33

merged 12 commits into from
Jan 1, 2024

Conversation

wureny
Copy link
Contributor

@wureny wureny commented Dec 30, 2023

#32

Copy link

codecov bot commented Dec 30, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (8c6eedc) 97.94% compared to head (04bc9da) 98.04%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #33      +/-   ##
==========================================
+ Coverage   97.94%   98.04%   +0.10%     
==========================================
  Files           4        6       +2     
  Lines         632      665      +33     
==========================================
+ Hits          619      652      +33     
  Misses          9        9              
  Partials        4        4              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@flycash flycash left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这边操作有点不太对。实际上你只需要实现一个 namespace cache 的装饰器,然后随便搞个测试就可以。

也就是说,memory 和 redis 那两个部分呢是不需要修改的。

道理也很简单,如果用户要使用 namespace 的特性,他们自己手动创建 memory 或者 redis 的实现,而后自己来组装。

你可以预期,我们将来会有很多的装饰器,所以不可能每一个装饰器都改过去 memory 那些地方。

在测试的时候,你可以考虑生成一个 mock 的实现,然后里面这个mock 实现来构建 namespaceCache。在测试里面重点检测 namespace 是否真的正确处理了 key 就可以。

@wureny
Copy link
Contributor Author

wureny commented Dec 31, 2023

已更新

Copy link
Contributor

@flycash flycash left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. 所有的方法都必须要测试一遍,你需要检查每一个方法都正确处理了 key。
  2. 把 mock 生成的文件重命名为 mock_cache_test.go,或者你重新生成,然后指定这个文件名。
  3. 按照 table driven 的形态来编写测试

namespacecache.go Outdated Show resolved Hide resolved
Copy link
Contributor

@flycash flycash left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我的意思是=。=,有一个 cache 的 mock 代码,放到这个文件里面。

另外 namespace 的测试,放到另外一个文件。

  • mock_cache_test.go
  • namespace_cache.go
  • namespace_cache_test.go

}

// 测试当key只有一个的时候
func TestNamespaceCache_Delete2(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

你这个可以和前面的合并在一起。这样设计测试用例:

testCases := []struct{
    name string
    keys []string
    mock func(ctrl *gomock.Controller) Cache
    wantCnt int64
    wantError
}

然后在测试代码里面:

c := NamespaceCache{
    Cache: tc.mock(ctrl)
}
c.Delete(ctx, tc.keys...)

整体来说,我看到你这里是遵循了 Goland IDE 生成的测试模板。后面其实犯不着,因为它的模板很难用。

@flycash flycash merged commit 37fe769 into ecodeclub:main Jan 1, 2024
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants