From 7dcc7a48802824e5f6ec777dba943ae492b2c398 Mon Sep 17 00:00:00 2001 From: HFO4 <912394456@qq.com> Date: Fri, 21 Feb 2020 12:09:43 +0800 Subject: [PATCH] Test: Task list / file delete --- pkg/authn/auth_test.go | 4 +++- pkg/filesystem/manage_test.go | 25 +++++++------------------ pkg/serializer/setting_test.go | 8 ++++++++ pkg/serializer/user_test.go | 8 ++++++++ pkg/thumb/image_test.go | 20 ++++++++++++++++++++ 5 files changed, 46 insertions(+), 19 deletions(-) diff --git a/pkg/authn/auth_test.go b/pkg/authn/auth_test.go index 43344e5..eac390d 100644 --- a/pkg/authn/auth_test.go +++ b/pkg/authn/auth_test.go @@ -1,13 +1,15 @@ package authn import ( + "github.com/HFO4/cloudreve/pkg/cache" "github.com/stretchr/testify/assert" "testing" ) func TestInit(t *testing.T) { asserts := assert.New(t) - + cache.Set("setting_siteURL", "http://cloudreve.org", 0) + cache.Set("setting_siteName", "Cloudreve", 0) asserts.NotPanics(func() { Init() }) diff --git a/pkg/filesystem/manage_test.go b/pkg/filesystem/manage_test.go index 83970ba..f27971e 100644 --- a/pkg/filesystem/manage_test.go +++ b/pkg/filesystem/manage_test.go @@ -306,7 +306,7 @@ func TestFileSystem_Delete(t *testing.T) { }} ctx := context.Background() - // 全部未成功 + //全部未成功 { // 列出要删除的目录 mock.ExpectQuery("SELECT(.+)"). @@ -326,6 +326,8 @@ func TestFileSystem_Delete(t *testing.T) { mock.ExpectQuery("SELECT(.+)").WillReturnRows(sqlmock.NewRows([]string{"id", "name", "source_name", "policy_id", "size"}).AddRow(1, "1.txt", "1.txt", 603, 2)) mock.ExpectQuery("SELECT(.+)files(.+)"). WillReturnRows(sqlmock.NewRows([]string{"id", "policy_id", "source_name"})) + // 查找软连接 + mock.ExpectQuery("SELECT(.+)").WillReturnRows(sqlmock.NewRows([]string{"id"})) // 查询上传策略 mock.ExpectQuery("SELECT(.+)").WillReturnRows(sqlmock.NewRows([]string{"id", "type"}).AddRow(603, "local")) // 删除文件记录 @@ -338,29 +340,16 @@ func TestFileSystem_Delete(t *testing.T) { mock.ExpectExec("UPDATE(.+)shares"). WillReturnResult(sqlmock.NewResult(0, 3)) mock.ExpectCommit() - // 归还容量 - mock.ExpectBegin() - mock.ExpectExec("UPDATE(.+)users"). - WillReturnResult(sqlmock.NewResult(0, 3)) - mock.ExpectCommit() - // 删除目录 - mock.ExpectBegin() - mock.ExpectExec("DELETE(.+)folders"). - WillReturnResult(sqlmock.NewResult(0, 3)) - mock.ExpectCommit() - // 删除对应分享 - mock.ExpectBegin() - mock.ExpectExec("UPDATE(.+)shares"). - WillReturnResult(sqlmock.NewResult(0, 3)) - mock.ExpectCommit() err := fs.Delete(ctx, []uint{1}, []uint{1}) asserts.Error(err) asserts.Equal(203, err.(serializer.AppError).Code) - asserts.Equal(uint64(0), fs.User.Storage) + asserts.Equal(uint64(3), fs.User.Storage) + asserts.NoError(mock.ExpectationsWereMet()) } - // 全部成功 + //全部成功 { + fs.CleanTargets() file, err := os.Create("1.txt") file2, err := os.Create("2.txt") file.Close() diff --git a/pkg/serializer/setting_test.go b/pkg/serializer/setting_test.go index 458fce9..0263757 100644 --- a/pkg/serializer/setting_test.go +++ b/pkg/serializer/setting_test.go @@ -35,3 +35,11 @@ func TestBuildSiteConfig(t *testing.T) { }) asserts.Equal(uint(5), res.Data.(SiteConfig).User.ID) } + +func TestBuildTaskList(t *testing.T) { + asserts := assert.New(t) + tasks := []model.Task{{}} + + res := BuildTaskList(tasks, 1) + asserts.NotNil(res) +} diff --git a/pkg/serializer/user_test.go b/pkg/serializer/user_test.go index 1c2159a..a56c9c0 100644 --- a/pkg/serializer/user_test.go +++ b/pkg/serializer/user_test.go @@ -5,6 +5,7 @@ import ( "github.com/DATA-DOG/go-sqlmock" model "github.com/HFO4/cloudreve/models" "github.com/HFO4/cloudreve/pkg/cache" + "github.com/duo-labs/webauthn/webauthn" "github.com/jinzhu/gorm" "github.com/stretchr/testify/assert" "testing" @@ -110,3 +111,10 @@ func TestBuildTagRes(t *testing.T) { asserts.Equal("", res[0].Expression) asserts.Equal("exp", res[1].Expression) } + +func TestBuildWebAuthnList(t *testing.T) { + asserts := assert.New(t) + credentials := []webauthn.Credential{{}} + res := BuildWebAuthnList(credentials) + asserts.Len(res, 1) +} diff --git a/pkg/thumb/image_test.go b/pkg/thumb/image_test.go index 3411aa9..bdea05e 100644 --- a/pkg/thumb/image_test.go +++ b/pkg/thumb/image_test.go @@ -2,6 +2,7 @@ package thumb import ( "fmt" + "github.com/HFO4/cloudreve/pkg/cache" "github.com/HFO4/cloudreve/pkg/util" "github.com/stretchr/testify/assert" "image" @@ -99,3 +100,22 @@ func TestThumb_Save(t *testing.T) { asserts.True(util.Exists("TestThumb_Save.png")) } + +func TestThumb_CreateAvatar(t *testing.T) { + asserts := assert.New(t) + file := CreateTestImage() + defer file.Close() + + thumb, err := NewThumbFromFile(file, "123.jpg") + asserts.NoError(err) + + cache.Set("setting_avatar_path", "tests", 0) + cache.Set("setting_avatar_size_s", "50", 0) + cache.Set("setting_avatar_size_m", "130", 0) + cache.Set("setting_avatar_size_l", "200", 0) + + asserts.NoError(thumb.CreateAvatar(1)) + asserts.True(util.Exists("tests/avatar_1_1.png")) + asserts.True(util.Exists("tests/avatar_1_2.png")) + asserts.True(util.Exists("tests/avatar_1_0.png")) +}