mirror of
https://github.com/halejohn/Cloudreve.git
synced 2026-01-26 09:34:57 +08:00
Test: modified folder methods and filesystem
This commit is contained in:
@@ -22,7 +22,6 @@ func TestFileSystem_AddFile(t *testing.T) {
|
||||
Model: gorm.Model{
|
||||
ID: 1,
|
||||
},
|
||||
PositionAbsolute: "/我的文件",
|
||||
}
|
||||
fs := FileSystem{
|
||||
User: &model.User{
|
||||
@@ -79,26 +78,35 @@ func TestFileSystem_GetContent(t *testing.T) {
|
||||
asserts.NoError(err)
|
||||
_ = file.Close()
|
||||
|
||||
mock.ExpectQuery("SELECT(.+)").
|
||||
WithArgs(1).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(1))
|
||||
mock.ExpectQuery("SELECT(.+)").WillReturnRows(sqlmock.NewRows([]string{"id", "name", "policy_id"}).AddRow(1, "TestFileSystem_GetContent.txt", 1))
|
||||
mock.ExpectQuery("SELECT(.+)poli(.+)").WillReturnRows(sqlmock.NewRows([]string{"id", "type"}).AddRow(1, "unknown"))
|
||||
|
||||
rs, err = fs.GetContent(ctx, "TestFileSystem_GetContent.txt")
|
||||
rs, err = fs.GetContent(ctx, "/TestFileSystem_GetContent.txt")
|
||||
asserts.Error(err)
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
|
||||
// 打开文件失败
|
||||
mock.ExpectQuery("SELECT(.+)").
|
||||
WithArgs(1).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(1))
|
||||
mock.ExpectQuery("SELECT(.+)").WillReturnRows(sqlmock.NewRows([]string{"id", "name", "policy_id"}).AddRow(1, "TestFileSystem_GetContent.txt", 1))
|
||||
mock.ExpectQuery("SELECT(.+)poli(.+)").WillReturnRows(sqlmock.NewRows([]string{"id", "type", "source_name"}).AddRow(1, "local", "not exist"))
|
||||
|
||||
rs, err = fs.GetContent(ctx, "TestFileSystem_GetContent.txt")
|
||||
rs, err = fs.GetContent(ctx, "/TestFileSystem_GetContent.txt")
|
||||
asserts.Equal(serializer.CodeIOFailed, err.(serializer.AppError).Code)
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
|
||||
// 打开成功
|
||||
mock.ExpectQuery("SELECT(.+)").
|
||||
WithArgs(1).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(1))
|
||||
mock.ExpectQuery("SELECT(.+)").WillReturnRows(sqlmock.NewRows([]string{"id", "name", "policy_id", "source_name"}).AddRow(1, "TestFileSystem_GetContent.txt", 1, "TestFileSystem_GetContent.txt"))
|
||||
mock.ExpectQuery("SELECT(.+)poli(.+)").WillReturnRows(sqlmock.NewRows([]string{"id", "type"}).AddRow(1, "local"))
|
||||
|
||||
rs, err = fs.GetContent(ctx, "TestFileSystem_GetContent.txt")
|
||||
rs, err = fs.GetContent(ctx, "/TestFileSystem_GetContent.txt")
|
||||
asserts.NoError(err)
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
}
|
||||
@@ -122,20 +130,26 @@ func TestFileSystem_GetDownloadContent(t *testing.T) {
|
||||
asserts.NoError(err)
|
||||
_ = file.Close()
|
||||
|
||||
mock.ExpectQuery("SELECT(.+)").
|
||||
WithArgs(1).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(1))
|
||||
mock.ExpectQuery("SELECT(.+)").WillReturnRows(sqlmock.NewRows([]string{"id", "name", "policy_id", "source_name"}).AddRow(1, "TestFileSystem_GetDownloadContent.txt", 1, "TestFileSystem_GetDownloadContent.txt"))
|
||||
mock.ExpectQuery("SELECT(.+)poli(.+)").WillReturnRows(sqlmock.NewRows([]string{"id", "type"}).AddRow(1, "local"))
|
||||
|
||||
// 无限速
|
||||
_, err = fs.GetDownloadContent(ctx, "TestFileSystem_GetDownloadContent.txt")
|
||||
_, err = fs.GetDownloadContent(ctx, "/TestFileSystem_GetDownloadContent.txt")
|
||||
asserts.NoError(err)
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
|
||||
// 有限速
|
||||
mock.ExpectQuery("SELECT(.+)").
|
||||
WithArgs(1).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(1))
|
||||
mock.ExpectQuery("SELECT(.+)").WillReturnRows(sqlmock.NewRows([]string{"id", "name", "policy_id", "source_name"}).AddRow(1, "TestFileSystem_GetDownloadContent.txt", 1, "TestFileSystem_GetDownloadContent.txt"))
|
||||
mock.ExpectQuery("SELECT(.+)poli(.+)").WillReturnRows(sqlmock.NewRows([]string{"id", "type"}).AddRow(1, "local"))
|
||||
|
||||
fs.User.Group.SpeedLimit = 1
|
||||
_, err = fs.GetDownloadContent(ctx, "TestFileSystem_GetDownloadContent.txt")
|
||||
_, err = fs.GetDownloadContent(ctx, "/TestFileSystem_GetDownloadContent.txt")
|
||||
asserts.NoError(err)
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"github.com/HFO4/cloudreve/pkg/util"
|
||||
"path"
|
||||
)
|
||||
|
||||
// Hook 钩子函数
|
||||
@@ -118,10 +117,10 @@ func GenericAfterUpload(ctx context.Context, fs *FileSystem) error {
|
||||
}
|
||||
|
||||
// 检查文件是否存在
|
||||
if ok, _ := fs.IsFileExist(path.Join(
|
||||
virtualPath,
|
||||
if ok, _ := fs.IsChildFileExist(
|
||||
folder,
|
||||
ctx.Value(FileHeaderCtx).(FileHeader).GetFileName(),
|
||||
)); ok {
|
||||
); ok {
|
||||
return ErrFileExisted
|
||||
}
|
||||
|
||||
|
||||
@@ -104,9 +104,13 @@ func TestGenericAfterUpload(t *testing.T) {
|
||||
ctx = context.WithValue(ctx, SavePathCtx, "")
|
||||
|
||||
// 正常
|
||||
mock.ExpectQuery("SELECT(.+)folders(.+)").WillReturnRows(
|
||||
mock.NewRows([]string{"name"}).AddRow("我的文件"),
|
||||
)
|
||||
mock.ExpectQuery("SELECT(.+)").
|
||||
WithArgs(1).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(1, 1))
|
||||
// 1
|
||||
mock.ExpectQuery("SELECT(.+)").
|
||||
WithArgs(1, 1, "我的文件").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(2, 1))
|
||||
mock.ExpectQuery("SELECT(.+)files(.+)").WillReturnError(errors.New("not found"))
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectExec("INSERT(.+)files(.+)").WillReturnResult(sqlmock.NewResult(1, 1))
|
||||
@@ -125,9 +129,13 @@ func TestGenericAfterUpload(t *testing.T) {
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
|
||||
// 文件已存在
|
||||
mock.ExpectQuery("SELECT(.+)folders(.+)").WillReturnRows(
|
||||
mock.NewRows([]string{"name"}).AddRow("我的文件"),
|
||||
)
|
||||
mock.ExpectQuery("SELECT(.+)").
|
||||
WithArgs(1).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(1, 1))
|
||||
// 1
|
||||
mock.ExpectQuery("SELECT(.+)").
|
||||
WithArgs(1, 1, "我的文件").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(2, 1))
|
||||
mock.ExpectQuery("SELECT(.+)files(.+)").WillReturnRows(
|
||||
mock.NewRows([]string{"name"}).AddRow("test.txt"),
|
||||
)
|
||||
@@ -136,9 +144,14 @@ func TestGenericAfterUpload(t *testing.T) {
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
|
||||
// 插入失败
|
||||
mock.ExpectQuery("SELECT(.+)folders(.+)").WillReturnRows(
|
||||
mock.NewRows([]string{"name"}).AddRow("我的文件"),
|
||||
)
|
||||
mock.ExpectQuery("SELECT(.+)").
|
||||
WithArgs(1).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(1, 1))
|
||||
// 1
|
||||
mock.ExpectQuery("SELECT(.+)").
|
||||
WithArgs(1, 1, "我的文件").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(2, 1))
|
||||
|
||||
mock.ExpectQuery("SELECT(.+)files(.+)").WillReturnError(errors.New("not found"))
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectExec("INSERT(.+)files(.+)").WillReturnError(errors.New("error"))
|
||||
@@ -217,7 +230,10 @@ func TestHookIsFileExist(t *testing.T) {
|
||||
}}
|
||||
ctx := context.WithValue(context.Background(), PathCtx, "/test.txt")
|
||||
{
|
||||
mock.ExpectQuery("SELECT(.+)").WithArgs(uint(1), "/", "test.txt").WillReturnRows(
|
||||
mock.ExpectQuery("SELECT(.+)").
|
||||
WithArgs(1).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(1, 1))
|
||||
mock.ExpectQuery("SELECT(.+)").WithArgs(uint(1), "test.txt").WillReturnRows(
|
||||
sqlmock.NewRows([]string{"Name"}).AddRow("s"),
|
||||
)
|
||||
err := HookIsFileExist(ctx, fs)
|
||||
@@ -225,7 +241,10 @@ func TestHookIsFileExist(t *testing.T) {
|
||||
asserts.NoError(err)
|
||||
}
|
||||
{
|
||||
mock.ExpectQuery("SELECT(.+)").WithArgs(uint(1), "/", "test.txt").WillReturnRows(
|
||||
mock.ExpectQuery("SELECT(.+)").
|
||||
WithArgs(1).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(1, 1))
|
||||
mock.ExpectQuery("SELECT(.+)").WithArgs(uint(1), "test.txt").WillReturnRows(
|
||||
sqlmock.NewRows([]string{"Name"}),
|
||||
)
|
||||
err := HookIsFileExist(ctx, fs)
|
||||
|
||||
@@ -238,7 +238,7 @@ func (fs *FileSystem) List(ctx context.Context, dirPath string, pathProcessor fu
|
||||
return []Object{}, nil
|
||||
}
|
||||
|
||||
var parentPath = path.Join(folder.PositionTemp, folder.Name)
|
||||
var parentPath = path.Join(folder.Position, folder.Name)
|
||||
var childFolders []model.Folder
|
||||
var childFiles []model.File
|
||||
|
||||
|
||||
@@ -392,26 +392,37 @@ func TestFileSystem_Copy(t *testing.T) {
|
||||
|
||||
// 目录不存在
|
||||
{
|
||||
mock.ExpectQuery("SELECT(.+)").WithArgs(uint(1), "/dst").WillReturnRows(
|
||||
mock.ExpectQuery("SELECT(.+)").WillReturnRows(
|
||||
sqlmock.NewRows([]string{"name"}),
|
||||
)
|
||||
mock.ExpectQuery("SELECT(.+)").WithArgs(uint(1), "/src").WillReturnRows(
|
||||
mock.ExpectQuery("SELECT(.+)").WillReturnRows(
|
||||
sqlmock.NewRows([]string{"name"}),
|
||||
)
|
||||
err := fs.Copy(ctx, []string{}, []string{}, "/src", "/dst")
|
||||
err := fs.Copy(ctx, []uint{}, []uint{}, "/src", "/dst")
|
||||
asserts.Equal(ErrPathNotExist, err)
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
}
|
||||
|
||||
// 复制目录出错
|
||||
{
|
||||
mock.ExpectQuery("SELECT(.+)").WithArgs(uint(1), "/src").WillReturnRows(
|
||||
sqlmock.NewRows([]string{"name"}).AddRow("123"),
|
||||
)
|
||||
mock.ExpectQuery("SELECT(.+)").WithArgs(uint(1), "/dst").WillReturnRows(
|
||||
sqlmock.NewRows([]string{"name"}).AddRow("123"),
|
||||
)
|
||||
err := fs.Copy(ctx, []string{"test"}, []string{}, "/src", "/dst")
|
||||
// 根目录
|
||||
mock.ExpectQuery("SELECT(.+)").
|
||||
WithArgs(1).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(1, 1))
|
||||
// 1
|
||||
mock.ExpectQuery("SELECT(.+)").
|
||||
WithArgs(1, 1, "src").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(2, 1))
|
||||
// 根目录
|
||||
mock.ExpectQuery("SELECT(.+)").
|
||||
WithArgs(1).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(1, 1))
|
||||
// 1
|
||||
mock.ExpectQuery("SELECT(.+)").
|
||||
WithArgs(1, 1, "dst").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(2, 1))
|
||||
|
||||
err := fs.Copy(ctx, []uint{1}, []uint{}, "/src", "/dst")
|
||||
asserts.Error(err)
|
||||
}
|
||||
|
||||
@@ -434,20 +445,30 @@ func TestFileSystem_Move(t *testing.T) {
|
||||
mock.ExpectQuery("SELECT(.+)").WillReturnRows(
|
||||
sqlmock.NewRows([]string{"name"}),
|
||||
)
|
||||
err := fs.Move(ctx, []string{}, []string{}, "/src", "/dst")
|
||||
err := fs.Move(ctx, []uint{}, []uint{}, "/src", "/dst")
|
||||
asserts.Equal(ErrPathNotExist, err)
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
}
|
||||
|
||||
// 移动目录出错
|
||||
{
|
||||
mock.ExpectQuery("SELECT(.+)").WithArgs(uint(1), "/src").WillReturnRows(
|
||||
sqlmock.NewRows([]string{"name"}).AddRow("123"),
|
||||
)
|
||||
mock.ExpectQuery("SELECT(.+)").WithArgs(uint(1), "/dst").WillReturnRows(
|
||||
sqlmock.NewRows([]string{"name"}).AddRow("123"),
|
||||
)
|
||||
err := fs.Move(ctx, []string{"test"}, []string{}, "/src", "/dst")
|
||||
// 根目录
|
||||
mock.ExpectQuery("SELECT(.+)").
|
||||
WithArgs(1).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(1, 1))
|
||||
// 1
|
||||
mock.ExpectQuery("SELECT(.+)").
|
||||
WithArgs(1, 1, "src").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(2, 1))
|
||||
// 根目录
|
||||
mock.ExpectQuery("SELECT(.+)").
|
||||
WithArgs(1).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(1, 1))
|
||||
// 1
|
||||
mock.ExpectQuery("SELECT(.+)").
|
||||
WithArgs(1, 1, "dst").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(2, 1))
|
||||
err := fs.Move(ctx, []uint{1}, []uint{}, "/src", "/dst")
|
||||
asserts.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ func (fs *FileSystem) IsPathExist(path string) (bool, *model.Folder) {
|
||||
}
|
||||
|
||||
// 递归步入目录
|
||||
// TODO:装入树形缓存
|
||||
var currentFolder *model.Folder
|
||||
for _, folderName := range pathList {
|
||||
var err error
|
||||
|
||||
@@ -128,3 +128,25 @@ func TestFileSystem_IsPathExist(t *testing.T) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestFileSystem_IsChildFileExist(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
fs := &FileSystem{User: &model.User{
|
||||
Model: gorm.Model{
|
||||
ID: 1,
|
||||
},
|
||||
}}
|
||||
folder := model.Folder{
|
||||
Model: gorm.Model{ID: 1},
|
||||
Name: "123",
|
||||
Position: "/",
|
||||
}
|
||||
|
||||
mock.ExpectQuery("SELECT(.+)").
|
||||
WithArgs(1, "321").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id", "name"}).AddRow(2, "321"))
|
||||
exist, childFile := fs.IsChildFileExist(&folder, "321")
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
asserts.True(exist)
|
||||
asserts.Equal("/123/321", childFile.Position)
|
||||
}
|
||||
|
||||
@@ -19,3 +19,11 @@ func TestFillSlash(t *testing.T) {
|
||||
asserts.Equal("/", FillSlash(""))
|
||||
asserts.Equal("/123/", FillSlash("/123"))
|
||||
}
|
||||
|
||||
func TestSplitPath(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
asserts.Equal([]string{}, SplitPath(""))
|
||||
asserts.Equal([]string{}, SplitPath("1"))
|
||||
asserts.Equal([]string{"/"}, SplitPath("/"))
|
||||
asserts.Equal([]string{"/", "123", "321"}, SplitPath("/123/321"))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user