|
|
|
@ -2,10 +2,13 @@ package filesystem
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"errors"
|
|
|
|
|
"github.com/DATA-DOG/go-sqlmock"
|
|
|
|
|
model "github.com/HFO4/cloudreve/models"
|
|
|
|
|
"github.com/HFO4/cloudreve/pkg/serializer"
|
|
|
|
|
"github.com/jinzhu/gorm"
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
"os"
|
|
|
|
|
"testing"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
@ -170,3 +173,192 @@ func TestFileSystem_CreateDirectory(t *testing.T) {
|
|
|
|
|
asserts.NoError(err)
|
|
|
|
|
asserts.NoError(mock.ExpectationsWereMet())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestFileSystem_ListDeleteFiles(t *testing.T) {
|
|
|
|
|
asserts := assert.New(t)
|
|
|
|
|
fs := &FileSystem{User: &model.User{
|
|
|
|
|
Model: gorm.Model{
|
|
|
|
|
ID: 1,
|
|
|
|
|
},
|
|
|
|
|
}}
|
|
|
|
|
|
|
|
|
|
// 成功
|
|
|
|
|
{
|
|
|
|
|
mock.ExpectQuery("SELECT(.+)").WillReturnRows(sqlmock.NewRows([]string{"id", "name"}).AddRow(1, "1.txt").AddRow(2, "2.txt"))
|
|
|
|
|
err := fs.ListDeleteFiles(context.Background(), []string{"/"})
|
|
|
|
|
asserts.NoError(err)
|
|
|
|
|
asserts.NoError(mock.ExpectationsWereMet())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 失败
|
|
|
|
|
{
|
|
|
|
|
mock.ExpectQuery("SELECT(.+)").WillReturnError(errors.New("error"))
|
|
|
|
|
err := fs.ListDeleteFiles(context.Background(), []string{"/"})
|
|
|
|
|
asserts.Error(err)
|
|
|
|
|
asserts.Equal(serializer.CodeDBError, err.(serializer.AppError).Code)
|
|
|
|
|
asserts.NoError(mock.ExpectationsWereMet())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestFileSystem_ListDeleteDirs(t *testing.T) {
|
|
|
|
|
asserts := assert.New(t)
|
|
|
|
|
fs := &FileSystem{User: &model.User{
|
|
|
|
|
Model: gorm.Model{
|
|
|
|
|
ID: 1,
|
|
|
|
|
},
|
|
|
|
|
}}
|
|
|
|
|
|
|
|
|
|
// 成功
|
|
|
|
|
{
|
|
|
|
|
mock.ExpectQuery("SELECT(.+)").
|
|
|
|
|
WillReturnRows(
|
|
|
|
|
sqlmock.NewRows([]string{"id"}).
|
|
|
|
|
AddRow(1).
|
|
|
|
|
AddRow(2).
|
|
|
|
|
AddRow(3),
|
|
|
|
|
)
|
|
|
|
|
mock.ExpectQuery("SELECT(.+)").
|
|
|
|
|
WithArgs(1, 2, 3).
|
|
|
|
|
WillReturnRows(
|
|
|
|
|
sqlmock.NewRows([]string{"id", "name"}).
|
|
|
|
|
AddRow(4, "1.txt").
|
|
|
|
|
AddRow(5, "2.txt").
|
|
|
|
|
AddRow(6, "3.txt"),
|
|
|
|
|
)
|
|
|
|
|
err := fs.ListDeleteDirs(context.Background(), []string{"/"})
|
|
|
|
|
asserts.NoError(err)
|
|
|
|
|
asserts.Len(fs.FileTarget, 3)
|
|
|
|
|
asserts.Len(fs.DirTarget, 3)
|
|
|
|
|
asserts.NoError(mock.ExpectationsWereMet())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 检索文件发生错误
|
|
|
|
|
{
|
|
|
|
|
mock.ExpectQuery("SELECT(.+)").
|
|
|
|
|
WillReturnRows(
|
|
|
|
|
sqlmock.NewRows([]string{"id"}).
|
|
|
|
|
AddRow(1).
|
|
|
|
|
AddRow(2).
|
|
|
|
|
AddRow(3),
|
|
|
|
|
)
|
|
|
|
|
mock.ExpectQuery("SELECT(.+)").
|
|
|
|
|
WithArgs(1, 2, 3).
|
|
|
|
|
WillReturnError(errors.New("error"))
|
|
|
|
|
err := fs.ListDeleteDirs(context.Background(), []string{"/"})
|
|
|
|
|
asserts.Error(err)
|
|
|
|
|
asserts.Len(fs.DirTarget, 6)
|
|
|
|
|
asserts.NoError(mock.ExpectationsWereMet())
|
|
|
|
|
}
|
|
|
|
|
// 检索目录发生错误
|
|
|
|
|
{
|
|
|
|
|
mock.ExpectQuery("SELECT(.+)").
|
|
|
|
|
WillReturnError(errors.New("error"))
|
|
|
|
|
err := fs.ListDeleteDirs(context.Background(), []string{"/"})
|
|
|
|
|
asserts.Error(err)
|
|
|
|
|
asserts.NoError(mock.ExpectationsWereMet())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestFileSystem_Delete(t *testing.T) {
|
|
|
|
|
asserts := assert.New(t)
|
|
|
|
|
fs := &FileSystem{User: &model.User{
|
|
|
|
|
Model: gorm.Model{
|
|
|
|
|
ID: 1,
|
|
|
|
|
},
|
|
|
|
|
Storage: 3,
|
|
|
|
|
Group: model.Group{MaxStorage: 3},
|
|
|
|
|
}}
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
|
|
// 全部未成功
|
|
|
|
|
{
|
|
|
|
|
mock.ExpectQuery("SELECT(.+)").
|
|
|
|
|
WillReturnRows(
|
|
|
|
|
sqlmock.NewRows([]string{"id"}).
|
|
|
|
|
AddRow(1).
|
|
|
|
|
AddRow(2).
|
|
|
|
|
AddRow(3),
|
|
|
|
|
)
|
|
|
|
|
mock.ExpectQuery("SELECT(.+)").
|
|
|
|
|
WithArgs(1, 2, 3).
|
|
|
|
|
WillReturnRows(
|
|
|
|
|
sqlmock.NewRows([]string{"id", "name", "source_name", "policy_id", "size"}).
|
|
|
|
|
AddRow(4, "1.txt", "1.txt", 2, 1),
|
|
|
|
|
)
|
|
|
|
|
mock.ExpectQuery("SELECT(.+)").WillReturnRows(sqlmock.NewRows([]string{"id", "name", "source_name", "policy_id", "size"}).AddRow(1, "1.txt", "1.txt", 1, 2))
|
|
|
|
|
mock.ExpectQuery("SELECT(.+)files(.+)").
|
|
|
|
|
WillReturnRows(sqlmock.NewRows([]string{"id", "policy_id", "source_name"}))
|
|
|
|
|
// 查询上传策略
|
|
|
|
|
mock.ExpectQuery("SELECT(.+)").WillReturnRows(sqlmock.NewRows([]string{"id", "type"}).AddRow(1, "local"))
|
|
|
|
|
mock.ExpectQuery("SELECT(.+)").WillReturnRows(sqlmock.NewRows([]string{"id", "type"}).AddRow(1, "local"))
|
|
|
|
|
// 删除文件记录
|
|
|
|
|
mock.ExpectBegin()
|
|
|
|
|
mock.ExpectExec("UPDATE(.+)delete(.+)").
|
|
|
|
|
WillReturnResult(sqlmock.NewResult(0, 3))
|
|
|
|
|
mock.ExpectCommit()
|
|
|
|
|
// 归还容量
|
|
|
|
|
mock.ExpectBegin()
|
|
|
|
|
mock.ExpectExec("UPDATE(.+)").
|
|
|
|
|
WillReturnResult(sqlmock.NewResult(0, 3))
|
|
|
|
|
mock.ExpectCommit()
|
|
|
|
|
// 删除目录
|
|
|
|
|
mock.ExpectBegin()
|
|
|
|
|
mock.ExpectExec("UPDATE(.+)delete(.+)").
|
|
|
|
|
WillReturnResult(sqlmock.NewResult(0, 3))
|
|
|
|
|
mock.ExpectCommit()
|
|
|
|
|
|
|
|
|
|
err := fs.Delete(ctx, []string{"/"}, []string{"2.txt"})
|
|
|
|
|
asserts.Error(err)
|
|
|
|
|
asserts.Equal(203, err.(serializer.AppError).Code)
|
|
|
|
|
asserts.Equal(uint64(0), fs.User.Storage)
|
|
|
|
|
}
|
|
|
|
|
// 全部成功
|
|
|
|
|
{
|
|
|
|
|
file, err := os.Create("1.txt")
|
|
|
|
|
file2, err := os.Create("2.txt")
|
|
|
|
|
file.Close()
|
|
|
|
|
file2.Close()
|
|
|
|
|
asserts.NoError(err)
|
|
|
|
|
mock.ExpectQuery("SELECT(.+)").
|
|
|
|
|
WillReturnRows(
|
|
|
|
|
sqlmock.NewRows([]string{"id"}).
|
|
|
|
|
AddRow(1).
|
|
|
|
|
AddRow(2).
|
|
|
|
|
AddRow(3),
|
|
|
|
|
)
|
|
|
|
|
mock.ExpectQuery("SELECT(.+)").
|
|
|
|
|
WithArgs(1, 2, 3).
|
|
|
|
|
WillReturnRows(
|
|
|
|
|
sqlmock.NewRows([]string{"id", "name", "source_name", "policy_id", "size"}).
|
|
|
|
|
AddRow(4, "1.txt", "1.txt", 2, 1),
|
|
|
|
|
)
|
|
|
|
|
mock.ExpectQuery("SELECT(.+)").WillReturnRows(sqlmock.NewRows([]string{"id", "name", "source_name", "policy_id", "size"}).AddRow(1, "2.txt", "2.txt", 1, 2))
|
|
|
|
|
mock.ExpectQuery("SELECT(.+)files(.+)").
|
|
|
|
|
WillReturnRows(sqlmock.NewRows([]string{"id", "policy_id", "source_name"}))
|
|
|
|
|
// 查询上传策略
|
|
|
|
|
mock.ExpectQuery("SELECT(.+)").WillReturnRows(sqlmock.NewRows([]string{"id", "type"}).AddRow(1, "local"))
|
|
|
|
|
mock.ExpectQuery("SELECT(.+)").WillReturnRows(sqlmock.NewRows([]string{"id", "type"}).AddRow(1, "local"))
|
|
|
|
|
// 删除文件记录
|
|
|
|
|
mock.ExpectBegin()
|
|
|
|
|
mock.ExpectExec("UPDATE(.+)delete(.+)").
|
|
|
|
|
WillReturnResult(sqlmock.NewResult(0, 3))
|
|
|
|
|
mock.ExpectCommit()
|
|
|
|
|
// 归还容量
|
|
|
|
|
mock.ExpectBegin()
|
|
|
|
|
mock.ExpectExec("UPDATE(.+)").
|
|
|
|
|
WillReturnResult(sqlmock.NewResult(0, 3))
|
|
|
|
|
mock.ExpectCommit()
|
|
|
|
|
// 删除目录
|
|
|
|
|
mock.ExpectBegin()
|
|
|
|
|
mock.ExpectExec("UPDATE(.+)delete(.+)").
|
|
|
|
|
WillReturnResult(sqlmock.NewResult(0, 3))
|
|
|
|
|
mock.ExpectCommit()
|
|
|
|
|
|
|
|
|
|
fs.FileTarget = []model.File{}
|
|
|
|
|
fs.DirTarget = []model.Folder{}
|
|
|
|
|
err = fs.Delete(ctx, []string{"/"}, []string{"2.txt"})
|
|
|
|
|
asserts.NoError(err)
|
|
|
|
|
asserts.Equal(uint64(0), fs.User.Storage)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|