From e07adb9d970ff992013b539220eba4ad36bb190b Mon Sep 17 00:00:00 2001 From: HFO4 <912394456@qq.com> Date: Tue, 26 Nov 2019 11:52:09 +0800 Subject: [PATCH] Test: filesystem/hooks --- pkg/filesystem/hooks_test.go | 46 ++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/pkg/filesystem/hooks_test.go b/pkg/filesystem/hooks_test.go index b135410..7257906 100644 --- a/pkg/filesystem/hooks_test.go +++ b/pkg/filesystem/hooks_test.go @@ -149,3 +149,49 @@ func TestGenericAfterUpload(t *testing.T) { asserts.NoError(mock.ExpectationsWereMet()) } + +func TestFileSystem_Use(t *testing.T) { + asserts := assert.New(t) + fs := FileSystem{} + + hook := func(ctx context.Context, fs *FileSystem) error { + return nil + } + + // 添加一个 + fs.Use("BeforeUpload", hook) + asserts.Len(fs.BeforeUpload, 1) + + // 添加一个 + fs.Use("BeforeUpload", hook) + asserts.Len(fs.BeforeUpload, 2) + + // 不存在 + fs.Use("BeforeUpload2333", hook) +} + +func TestFileSystem_Trigger(t *testing.T) { + asserts := assert.New(t) + fs := FileSystem{ + User: &model.User{}, + } + ctx := context.Background() + + hook := func(ctx context.Context, fs *FileSystem) error { + fs.User.Storage++ + return nil + } + + // 一个 + fs.Use("BeforeUpload", hook) + err := fs.Trigger(ctx, fs.BeforeUpload) + asserts.NoError(err) + asserts.Equal(uint64(1), fs.User.Storage) + + // 多个 + fs.Use("BeforeUpload", hook) + fs.Use("BeforeUpload", hook) + err = fs.Trigger(ctx, fs.BeforeUpload) + asserts.NoError(err) + asserts.Equal(uint64(4), fs.User.Storage) +}