Fix: failed unit test due to missing mock.ExpectWereMatch()

pull/247/head
HFO4 5 years ago
parent 6eff13c80a
commit a445da5286

@ -26,24 +26,37 @@ type Object struct {
}
// Rename 重命名对象
func (fs *FileSystem) Rename(ctx context.Context, src, new string) (err error) {
func (fs *FileSystem) Rename(ctx context.Context, dir, file []uint, new string) (err error) {
// 验证新名字
if !fs.ValidateLegalName(ctx, new) || !fs.ValidateExtension(ctx, new) {
return ErrIllegalObjectName
}
// 如果源对象是文件
fileExist, file := fs.IsFileExist(src)
if fileExist {
err = file.Rename(new)
return err
if len(file) > 0 {
fileObject, err := model.GetFilesByIDs([]uint{file[0]}, fs.User.ID)
if err != nil || len(fileObject) == 0 {
return ErrPathNotExist
}
err = fileObject[0].Rename(new)
if err != nil {
return ErrFileExisted
}
return nil
}
// 源对象是目录
folderExist, folder := fs.IsPathExist(src)
if folderExist {
err = folder.Rename(new)
return err
if len(dir) > 0 {
folderObject, err := model.GetFoldersByIDs([]uint{dir[0]}, fs.User.ID)
if err != nil || len(folderObject) == 0 {
return ErrPathNotExist
}
err = folderObject[0].Rename(new)
if err != nil {
return ErrFileExisted
}
return nil
}
return ErrPathNotExist

@ -411,7 +411,7 @@ func TestFileSystem_Copy(t *testing.T) {
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(1, 1))
// 1
mock.ExpectQuery("SELECT(.+)").
WithArgs(1, 1, "src").
WithArgs(1, 1, "dst").
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(2, 1))
// 根目录
mock.ExpectQuery("SELECT(.+)").
@ -419,17 +419,17 @@ func TestFileSystem_Copy(t *testing.T) {
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(1, 1))
// 1
mock.ExpectQuery("SELECT(.+)").
WithArgs(1, 1, "dst").
WithArgs(1, 1, "src").
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(2, 1))
err := fs.Copy(ctx, []uint{1}, []uint{}, "/src", "/dst")
asserts.Error(err)
asserts.NoError(mock.ExpectationsWereMet())
}
}
func TestFileSystem_Move(t *testing.T) {
conf.DatabaseConfig.Type = "mysql"
asserts := assert.New(t)
fs := &FileSystem{User: &model.User{
Model: gorm.Model{
@ -458,7 +458,7 @@ func TestFileSystem_Move(t *testing.T) {
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(1, 1))
// 1
mock.ExpectQuery("SELECT(.+)").
WithArgs(1, 1, "src").
WithArgs(1, 1, "dst").
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(2, 1))
// 根目录
mock.ExpectQuery("SELECT(.+)").
@ -466,9 +466,10 @@ func TestFileSystem_Move(t *testing.T) {
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(1, 1))
// 1
mock.ExpectQuery("SELECT(.+)").
WithArgs(1, 1, "dst").
WithArgs(1, 1, "src").
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(2, 1))
err := fs.Move(ctx, []uint{1}, []uint{}, "/src", "/dst")
asserts.Error(err)
asserts.NoError(mock.ExpectationsWereMet())
}
}

@ -6,7 +6,6 @@ import (
"github.com/HFO4/cloudreve/pkg/serializer"
"github.com/gin-gonic/gin"
"net/http"
"time"
)
// FileDownloadService 文件下载服务path为文件完整路径
@ -32,7 +31,7 @@ func (service *FileDownloadService) Download(ctx context.Context, c *gin.Context
// 设置文件名
c.Header("Content-Disposition", "attachment; filename=\""+fs.FileTarget[0].Name+"\"")
// 发送文件
http.ServeContent(c.Writer, c.Request, "", time.Time{}, rs)
http.ServeContent(c.Writer, c.Request, "", fs.FileTarget[0].UpdatedAt, rs)
return serializer.Response{
Code: 0,

@ -16,8 +16,8 @@ type ItemMoveService struct {
// ItemRenameService 处理多文件/目录重命名
type ItemRenameService struct {
Src string `json:"src" binding:"required,min=1,max=65535,ne=/"`
NewName string `json:"new_name" binding:"required,min=1,max=255"`
Src ItemService `json:"src" binding:"exists"`
NewName string `json:"new_name" binding:"required,min=1,max=255"`
}
// ItemService 处理多文件/目录相关服务
@ -93,6 +93,11 @@ func (service *ItemMoveService) Copy(ctx context.Context, c *gin.Context) serial
// Rename 重命名对象
func (service *ItemRenameService) Rename(ctx context.Context, c *gin.Context) serializer.Response {
// 重命名作只能对一个目录或文件对象进行操作
if len(service.Src.Items)+len(service.Src.Dirs) > 1 {
return serializer.ParamErr("只能操作一个对象", nil)
}
// 创建文件系统
fs, err := filesystem.NewFileSystemFromContext(c)
if err != nil {
@ -100,7 +105,7 @@ func (service *ItemRenameService) Rename(ctx context.Context, c *gin.Context) se
}
// 重命名对象
err = fs.Rename(ctx, service.Src, service.NewName)
err = fs.Rename(ctx, service.Src.Dirs, service.Src.Items, service.NewName)
if err != nil {
return serializer.Err(serializer.CodeNotSet, err.Error(), err)
}

Loading…
Cancel
Save