Test: filesystem/file, filesystem, filesystem/hook, model/file/Create

pull/247/head
HFO4 5 years ago
parent 438ce02420
commit 7cb27b2102

@ -1,6 +1,7 @@
package model
import (
"errors"
"github.com/DATA-DOG/go-sqlmock"
"github.com/stretchr/testify/assert"
"testing"
@ -16,3 +17,27 @@ func TestGetFileByPathAndName(t *testing.T) {
asserts.Equal("1.cia", file.Name)
asserts.NoError(mock.ExpectationsWereMet())
}
func TestFile_Create(t *testing.T) {
asserts := assert.New(t)
file := File{
Name: "123",
}
mock.ExpectBegin()
mock.ExpectExec("INSERT(.+)").WillReturnResult(sqlmock.NewResult(5, 1))
mock.ExpectCommit()
fileID, err := file.Create()
asserts.NoError(err)
asserts.Equal(uint(5), fileID)
asserts.Equal(uint(5), file.ID)
asserts.NoError(mock.ExpectationsWereMet())
mock.ExpectBegin()
mock.ExpectExec("INSERT(.+)").WillReturnError(errors.New("error"))
mock.ExpectRollback()
fileID, err = file.Create()
asserts.Error(err)
asserts.NoError(mock.ExpectationsWereMet())
}

@ -52,7 +52,7 @@ func Init() {
//db.SetLogger(util.Log())
if err != nil {
util.Log().Panic("连接数据库不成功", err)
util.Log().Panic("连接数据库不成功, %s", err)
}
//设置连接池

@ -45,7 +45,7 @@ func migration() {
// 迁移完毕后写入版本锁 version.lock
err := conf.WriteVersionLock()
if err != nil {
util.Log().Warning("无法写入版本控制锁 version.lock, ", err)
util.Log().Warning("无法写入版本控制锁 version.lock, %s", err)
}
}
@ -66,7 +66,7 @@ func addDefaultPolicy() {
IsOriginLinkEnable: false,
}
if err := DB.Create(&defaultPolicy).Error; err != nil {
util.Log().Panic("无法创建初始存储策略, ", err)
util.Log().Panic("无法创建初始存储策略, %s", err)
}
}
}

@ -3,9 +3,12 @@ package filesystem
import "errors"
var (
UnknownPolicyTypeError = errors.New("未知存储策略类型")
FileSizeTooBigError = errors.New("单个文件尺寸太大")
FileExtensionNotAllowedError = errors.New("不允许上传此类型的文件")
InsufficientCapacityError = errors.New("容量空间不足")
IlegalObjectNameError = errors.New("目标名称非法")
ErrUnknownPolicyType = errors.New("未知存储策略类型")
ErrFileSizeTooBig = errors.New("单个文件尺寸太大")
ErrFileExtensionNotAllowed = errors.New("不允许上传此类型的文件")
ErrInsufficientCapacity = errors.New("容量空间不足")
ErrIllegalObjectName = errors.New("目标名称非法")
ErrInsertFileRecord = errors.New("无法插入文件记录")
ErrFileExisted = errors.New("同名文件已存在")
ErrPathNotExist = errors.New("路径不存在")
)

@ -0,0 +1,52 @@
package filesystem
import (
"context"
"github.com/DATA-DOG/go-sqlmock"
model "github.com/HFO4/cloudreve/models"
"github.com/HFO4/cloudreve/pkg/filesystem/local"
"github.com/jinzhu/gorm"
"github.com/stretchr/testify/assert"
"testing"
)
func TestFileSystem_AddFile(t *testing.T) {
asserts := assert.New(t)
file := local.FileData{
Size: 5,
Name: "1.txt",
}
folder := model.Folder{
Model: gorm.Model{
ID: 1,
},
PositionAbsolute: "/我的文件",
}
fs := FileSystem{
User: &model.User{
Model: gorm.Model{
ID: 1,
},
Policy: model.Policy{
Model: gorm.Model{
ID: 1,
},
},
},
}
ctx := context.WithValue(context.Background(), FileHeaderCtx, file)
ctx = context.WithValue(ctx, SavePathCtx, "/Uploads/1_sad.txt")
_, err := fs.AddFile(ctx, &folder)
asserts.Error(err)
mock.ExpectBegin()
mock.ExpectExec("INSERT(.+)").WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectCommit()
f, err := fs.AddFile(ctx, &folder)
asserts.NoError(err)
asserts.Equal("/Uploads/1_sad.txt", f.SourceName)
}

@ -4,6 +4,7 @@ import (
"context"
"github.com/HFO4/cloudreve/models"
"github.com/HFO4/cloudreve/pkg/filesystem/local"
testMock "github.com/stretchr/testify/mock"
"io"
)
@ -27,6 +28,10 @@ type Handler interface {
// FileSystem 管理文件的文件系统
type FileSystem struct {
/*
*/
testMock.Mock
/*
*/
@ -59,7 +64,7 @@ func NewFileSystem(user *model.User) (*FileSystem, error) {
case "local":
handler = local.Handler{}
default:
return nil, UnknownPolicyTypeError
return nil, ErrUnknownPolicyType
}
// TODO 分配默认钩子

@ -0,0 +1,24 @@
package filesystem
import (
model "github.com/HFO4/cloudreve/models"
"github.com/stretchr/testify/assert"
"testing"
)
func TestNewFileSystem(t *testing.T) {
asserts := assert.New(t)
user := model.User{
Policy: model.Policy{
Type: "local",
},
}
fs, err := NewFileSystem(&user)
asserts.NoError(err)
asserts.NotNil(fs.Handler)
user.Policy.Type = "unknown"
fs, err = NewFileSystem(&user)
asserts.Error(err)
}

@ -13,22 +13,22 @@ func GenericBeforeUpload(ctx context.Context, fs *FileSystem) error {
// 验证单文件尺寸
if !fs.ValidateFileSize(ctx, file.GetSize()) {
return FileSizeTooBigError
return ErrFileSizeTooBig
}
// 验证文件名
if !fs.ValidateLegalName(ctx, file.GetFileName()) {
return IlegalObjectNameError
return ErrIllegalObjectName
}
// 验证扩展名
if !fs.ValidateExtension(ctx, file.GetFileName()) {
return FileExtensionNotAllowedError
return ErrFileExtensionNotAllowed
}
// 验证并扣除容量
if !fs.ValidateCapacity(ctx, file.GetSize()) {
return InsufficientCapacityError
return ErrInsufficientCapacity
}
return nil
}
@ -61,7 +61,7 @@ func GenericAfterUpload(ctx context.Context, fs *FileSystem) error {
// 检查路径是否存在
isExist, folder := fs.IsPathExist(virtualPath)
if !isExist {
return errors.New("路径\"" + virtualPath + "\"不存在")
return ErrPathNotExist
}
// 检查文件是否存在
@ -69,13 +69,13 @@ func GenericAfterUpload(ctx context.Context, fs *FileSystem) error {
virtualPath,
ctx.Value(FileHeaderCtx).(FileHeader).GetFileName(),
)) {
return errors.New("同名文件已存在")
return ErrFileExisted
}
// 向数据库中插入记录
file, err := fs.AddFile(ctx, &folder)
file, err := fs.AddFile(ctx, folder)
if err != nil {
return errors.New("无法插入文件记录")
return ErrInsertFileRecord
}
// TODO 是否需要立即获取图像大小?

@ -5,6 +5,7 @@ import (
model "github.com/HFO4/cloudreve/models"
"github.com/HFO4/cloudreve/pkg/filesystem/local"
"github.com/stretchr/testify/assert"
"os"
"testing"
)
@ -45,3 +46,49 @@ func TestGenericBeforeUpload(t *testing.T) {
ctx = context.WithValue(context.Background(), FileHeaderCtx, file)
asserts.Error(GenericBeforeUpload(ctx, &fs))
}
func TestGenericAfterUploadCanceled(t *testing.T) {
asserts := assert.New(t)
f, err := os.Create("TestGenericAfterUploadCanceled")
asserts.NoError(err)
f.Close()
file := local.FileStream{
Size: 5,
Name: "TestGenericAfterUploadCanceled",
}
ctx := context.WithValue(context.Background(), SavePathCtx, "TestGenericAfterUploadCanceled")
ctx = context.WithValue(ctx, FileHeaderCtx, file)
fs := FileSystem{
User: &model.User{Storage: 5},
Handler: local.Handler{},
}
// 成功
err = GenericAfterUploadCanceled(ctx, &fs)
asserts.NoError(err)
asserts.Equal(uint64(0), fs.User.Storage)
f, err = os.Create("TestGenericAfterUploadCanceled")
asserts.NoError(err)
f.Close()
// 容量不能再降低
err = GenericAfterUploadCanceled(ctx, &fs)
asserts.Error(err)
//文件不存在
fs.User.Storage = 5
err = GenericAfterUploadCanceled(ctx, &fs)
asserts.NoError(err)
}
//func TestGenericAfterUpload(t *testing.T) {
// asserts := assert.New(t)
// testObj := FileSystem{}
// ctx := context.WithValue(context.Background(),FileHeaderCtx,local.FileStream{
// VirtualPath: "/我的文件",
// Name: "test.txt",
// })
//
//
//}

@ -6,6 +6,7 @@ import (
"github.com/stretchr/testify/assert"
"io"
"io/ioutil"
"os"
"strings"
"testing"
)
@ -42,3 +43,27 @@ func TestHandler_Put(t *testing.T) {
}
}
}
func TestHandler_Delete(t *testing.T) {
asserts := assert.New(t)
handler := Handler{}
ctx := context.Background()
file, err := os.Create("test.file")
asserts.NoError(err)
_ = file.Close()
list, err := handler.Delete(ctx, []string{"test.file"})
asserts.Equal([]string{"test.file"}, list)
asserts.NoError(err)
file, err = os.Create("test.file")
asserts.NoError(err)
_ = file.Close()
list, err = handler.Delete(ctx, []string{"test.file", "test.notexist"})
asserts.Equal([]string{"test.file"}, list)
asserts.Error(err)
list, err = handler.Delete(ctx, []string{"test.notexist"})
asserts.Equal([]string{}, list)
asserts.Error(err)
}

@ -12,9 +12,9 @@ import (
// IsPathExist 返回给定目录是否存在
// 如果存在就返回目录
func (fs *FileSystem) IsPathExist(path string) (bool, model.Folder) {
func (fs *FileSystem) IsPathExist(path string) (bool, *model.Folder) {
folder, err := model.GetFolderByPath(path, fs.User.ID)
return err == nil, folder
return err == nil, &folder
}
// IsFileExist 返回给定路径的文件是否存在

Loading…
Cancel
Save