Test: moveOrCopyFolder / File

pull/247/head
HFO4 5 years ago
parent 362a7c389d
commit 5d50e7ed1e

@ -6,6 +6,7 @@ require (
github.com/DATA-DOG/go-sqlmock v1.3.3 github.com/DATA-DOG/go-sqlmock v1.3.3
github.com/fatih/color v1.7.0 github.com/fatih/color v1.7.0
github.com/gin-contrib/cors v1.3.0 github.com/gin-contrib/cors v1.3.0
github.com/gin-contrib/pprof v1.2.1
github.com/gin-contrib/sessions v0.0.1 github.com/gin-contrib/sessions v0.0.1
github.com/gin-gonic/gin v1.4.0 github.com/gin-gonic/gin v1.4.0
github.com/go-ini/ini v1.50.0 github.com/go-ini/ini v1.50.0

@ -1,6 +1,7 @@
package middleware package middleware
import ( import (
"github.com/HFO4/cloudreve/pkg/conf"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"testing" "testing"
@ -9,10 +10,19 @@ import (
func TestSession(t *testing.T) { func TestSession(t *testing.T) {
asserts := assert.New(t) asserts := assert.New(t)
{
handler := Session("2333") handler := Session("2333")
asserts.NotNil(handler) asserts.NotNil(handler)
asserts.NotNil(Store) asserts.NotNil(Store)
asserts.IsType(emptyFunc(), handler) asserts.IsType(emptyFunc(), handler)
}
{
conf.RedisConfig.Server = "123"
asserts.Panics(func() {
Session("2333")
})
}
} }
func emptyFunc() gin.HandlerFunc { func emptyFunc() gin.HandlerFunc {

@ -261,6 +261,11 @@ func (folder *Folder) MoveOrCopyFolderTo(dirs []string, dstFolder *Folder, isCop
index := 0 index := 0
for len(toBeMoved) != 0 { for len(toBeMoved) != 0 {
innerIndex := index % len(toBeMoved) innerIndex := index % len(toBeMoved)
index++
// 限制循环次数
if index > 65535 {
return 0, errors.New("循环超出限制")
}
// 如果是顶级父目录,直接删除,不需要复制 // 如果是顶级父目录,直接删除,不需要复制
if toBeMoved[innerIndex].PositionAbsolute == ignorePath { if toBeMoved[innerIndex].PositionAbsolute == ignorePath {

@ -303,3 +303,394 @@ func TestFolder_MoveOrCopyFileTo(t *testing.T) {
asserts.Equal(uint64(0), storage) asserts.Equal(uint64(0), storage)
} }
} }
func TestFolder_MoveOrCopyFolderTo_Copy(t *testing.T) {
conf.DatabaseConfig.Type = "mysql"
asserts := assert.New(t)
// 父目录
parFolder := Folder{
OwnerID: 1,
PositionAbsolute: "/",
}
// 目标目录
dstFolder := Folder{
Model: gorm.Model{ID: 10},
PositionAbsolute: "/dst",
}
// 测试复制目录结构
// test(2)
// 1(3) 2.txt
// 3(4) 4.txt
// 正常情况 成功
{
// 查找所有递归子目录,包括自身
mock.ExpectQuery("SELECT(.+)").
WithArgs(1, sqlmock.AnyArg(), 1, "/test").
WillReturnRows(
sqlmock.NewRows([]string{"id", "parent_id", "name", "position", "position_absolute"}).
AddRow(3, 2, "1", "/test", "/test/1").
AddRow(2, 1, "test", "/", "/test").
AddRow(4, 3, "3", "/test/1", "/test/1/3"),
)
// 查找顶级待复制目录
mock.ExpectQuery("SELECT(.+)").
WithArgs("/test", 1).
WillReturnRows(
sqlmock.NewRows(
[]string{"id", "parent_id", "name", "position", "position_absolute"}).
AddRow(2, 1, "test", "/", "/test"),
)
// 更新顶级目录
mock.ExpectBegin()
mock.ExpectExec("INSERT(.+)").
WillReturnResult(sqlmock.NewResult(5, 1))
mock.ExpectCommit()
// 更新子目录
mock.ExpectBegin()
mock.ExpectExec("INSERT(.+)").
WillReturnResult(sqlmock.NewResult(6, 1))
mock.ExpectCommit()
mock.ExpectBegin()
mock.ExpectExec("INSERT(.+)").
WillReturnResult(sqlmock.NewResult(7, 1))
mock.ExpectCommit()
// 获取子目录下的所有子文件
mock.ExpectQuery("SELECT(.+)").
WithArgs(1, 3, 2, 4).
WillReturnRows(
sqlmock.NewRows([]string{"id", "folder_id", "dir", "size"}).
AddRow(1, 2, "/test", 10).
AddRow(2, 3, "/test/1", 20),
)
// 更新子文件记录
mock.ExpectBegin()
mock.ExpectExec("INSERT(.+)").WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectCommit()
mock.ExpectBegin()
mock.ExpectExec("INSERT(.+)").WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectCommit()
storage, err := parFolder.MoveOrCopyFolderTo([]string{"/test"}, &dstFolder, true)
asserts.NoError(mock.ExpectationsWereMet())
asserts.NoError(err)
asserts.Equal(uint64(30), storage)
}
// 处理子目录时死循环避免
{
// 查找所有递归子目录,包括自身
mock.ExpectQuery("SELECT(.+)").
WithArgs(1, sqlmock.AnyArg(), 1, "/test").
WillReturnRows(
sqlmock.NewRows([]string{"id", "parent_id", "name", "position", "position_absolute"}).
AddRow(3, 2, "1", "/test", "/test/1").
AddRow(2, 1, "test", "/", "/1").
AddRow(4, 3, "3", "/test/1", "/test/1/3"),
)
// 查找顶级待复制目录
mock.ExpectQuery("SELECT(.+)").
WithArgs("/test", 1).
WillReturnRows(
sqlmock.NewRows(
[]string{"id", "parent_id", "name", "position", "position_absolute"}).
AddRow(2, 1, "test", "/", "/test"),
)
// 更新顶级目录
mock.ExpectBegin()
mock.ExpectExec("INSERT(.+)").
WillReturnResult(sqlmock.NewResult(5, 1))
mock.ExpectCommit()
// 更新子目录
mock.ExpectBegin()
mock.ExpectExec("INSERT(.+)").
WillReturnResult(sqlmock.NewResult(6, 1))
mock.ExpectCommit()
mock.ExpectBegin()
mock.ExpectExec("INSERT(.+)").
WillReturnResult(sqlmock.NewResult(6, 1))
mock.ExpectCommit()
storage, err := parFolder.MoveOrCopyFolderTo([]string{"/test"}, &dstFolder, true)
asserts.NoError(mock.ExpectationsWereMet())
asserts.Error(err)
asserts.Equal(uint64(0), storage)
}
// 检索子目录出错
{
// 查找所有递归子目录,包括自身
mock.ExpectQuery("SELECT(.+)").
WithArgs(1, sqlmock.AnyArg(), 1, "/test").
WillReturnError(errors.New("error"))
storage, err := parFolder.MoveOrCopyFolderTo([]string{"/test"}, &dstFolder, true)
asserts.NoError(mock.ExpectationsWereMet())
asserts.Error(err)
asserts.Equal(uint64(0), storage)
}
// 寻找原始目录出错
{
// 查找所有递归子目录,包括自身
mock.ExpectQuery("SELECT(.+)").
WithArgs(1, sqlmock.AnyArg(), 1, "/test").
WillReturnRows(
sqlmock.NewRows([]string{"id", "parent_id", "name", "position", "position_absolute"}).
AddRow(3, 2, "1", "/test", "/test/1").
AddRow(2, 1, "test", "/", "/test").
AddRow(4, 3, "3", "/test/1", "/test/1/3"),
)
// 查找顶级待复制目录
mock.ExpectQuery("SELECT(.+)").
WithArgs("/test", 1).
WillReturnError(errors.New("error"))
storage, err := parFolder.MoveOrCopyFolderTo([]string{"/test"}, &dstFolder, true)
asserts.NoError(mock.ExpectationsWereMet())
asserts.Error(err)
asserts.Equal(uint64(0), storage)
}
// 更新顶级目录出错
{
// 查找所有递归子目录,包括自身
mock.ExpectQuery("SELECT(.+)").
WithArgs(1, sqlmock.AnyArg(), 1, "/test").
WillReturnRows(
sqlmock.NewRows([]string{"id", "parent_id", "name", "position", "position_absolute"}).
AddRow(3, 2, "1", "/test", "/test/1").
AddRow(2, 1, "test", "/", "/test").
AddRow(4, 3, "3", "/test/1", "/test/1/3"),
)
// 查找顶级待复制目录
mock.ExpectQuery("SELECT(.+)").
WithArgs("/test", 1).
WillReturnRows(
sqlmock.NewRows(
[]string{"id", "parent_id", "name", "position", "position_absolute"}).
AddRow(2, 1, "test", "/", "/test"),
)
// 更新顶级目录
mock.ExpectBegin()
mock.ExpectExec("INSERT(.+)").
WillReturnError(errors.New("error"))
mock.ExpectRollback()
storage, err := parFolder.MoveOrCopyFolderTo([]string{"/test"}, &dstFolder, true)
asserts.NoError(mock.ExpectationsWereMet())
asserts.Error(err)
asserts.Equal(uint64(0), storage)
}
// 复制子目录,一个成功一个失败
{
// 查找所有递归子目录,包括自身
mock.ExpectQuery("SELECT(.+)").
WithArgs(1, sqlmock.AnyArg(), 1, "/test").
WillReturnRows(
sqlmock.NewRows([]string{"id", "parent_id", "name", "position", "position_absolute"}).
AddRow(3, 2, "1", "/test", "/test/1").
AddRow(2, 1, "test", "/", "/test").
AddRow(4, 3, "3", "/test/1", "/test/1/3"),
)
// 查找顶级待复制目录
mock.ExpectQuery("SELECT(.+)").
WithArgs("/test", 1).
WillReturnRows(
sqlmock.NewRows(
[]string{"id", "parent_id", "name", "position", "position_absolute"}).
AddRow(2, 1, "test", "/", "/test"),
)
// 更新顶级目录
mock.ExpectBegin()
mock.ExpectExec("INSERT(.+)").
WillReturnResult(sqlmock.NewResult(5, 1))
mock.ExpectCommit()
// 更新子目录
mock.ExpectBegin()
mock.ExpectExec("INSERT(.+)").
WillReturnResult(sqlmock.NewResult(6, 1))
mock.ExpectCommit()
mock.ExpectBegin()
mock.ExpectExec("INSERT(.+)").
WillReturnError(errors.New("error"))
mock.ExpectRollback()
storage, err := parFolder.MoveOrCopyFolderTo([]string{"/test"}, &dstFolder, true)
asserts.NoError(mock.ExpectationsWereMet())
asserts.Error(err)
asserts.Equal(uint64(0), storage)
}
// 复制文件,一个成功一个失败
{
// 查找所有递归子目录,包括自身
mock.ExpectQuery("SELECT(.+)").
WithArgs(1, sqlmock.AnyArg(), 1, "/test").
WillReturnRows(
sqlmock.NewRows([]string{"id", "parent_id", "name", "position", "position_absolute"}).
AddRow(3, 2, "1", "/test", "/test/1").
AddRow(2, 1, "test", "/", "/test").
AddRow(4, 3, "3", "/test/1", "/test/1/3"),
)
// 查找顶级待复制目录
mock.ExpectQuery("SELECT(.+)").
WithArgs("/test", 1).
WillReturnRows(
sqlmock.NewRows(
[]string{"id", "parent_id", "name", "position", "position_absolute"}).
AddRow(2, 1, "test", "/", "/test"),
)
// 更新顶级目录
mock.ExpectBegin()
mock.ExpectExec("INSERT(.+)").
WillReturnResult(sqlmock.NewResult(5, 1))
mock.ExpectCommit()
// 更新子目录
mock.ExpectBegin()
mock.ExpectExec("INSERT(.+)").
WillReturnResult(sqlmock.NewResult(6, 1))
mock.ExpectCommit()
mock.ExpectBegin()
mock.ExpectExec("INSERT(.+)").
WillReturnResult(sqlmock.NewResult(7, 1))
mock.ExpectCommit()
// 获取子目录下的所有子文件
mock.ExpectQuery("SELECT(.+)").
WithArgs(1, 3, 2, 4).
WillReturnRows(
sqlmock.NewRows([]string{"id", "folder_id", "dir", "size"}).
AddRow(1, 2, "/test", 10).
AddRow(2, 3, "/test/1", 20),
)
// 更新子文件记录
mock.ExpectBegin()
mock.ExpectExec("INSERT(.+)").WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectCommit()
mock.ExpectBegin()
mock.ExpectExec("INSERT(.+)").WillReturnError(errors.New("error"))
mock.ExpectRollback()
storage, err := parFolder.MoveOrCopyFolderTo([]string{"/test"}, &dstFolder, true)
asserts.NoError(mock.ExpectationsWereMet())
asserts.NoError(err)
asserts.Equal(uint64(10), storage)
}
}
func TestFolder_MoveOrCopyFolderTo_Move(t *testing.T) {
conf.DatabaseConfig.Type = "mysql"
asserts := assert.New(t)
// 父目录
parFolder := Folder{
OwnerID: 1,
PositionAbsolute: "/",
}
// 目标目录
dstFolder := Folder{
Model: gorm.Model{ID: 10},
PositionAbsolute: "/dst",
}
// 测试复制目录结构
// test(2)
// 1(3) 2.txt
// 3(4) 4.txt
// 正常情况 成功
{
// 查找所有递归子目录,包括自身
mock.ExpectQuery("SELECT(.+)").
WithArgs(1, sqlmock.AnyArg(), 1, "/test").
WillReturnRows(
sqlmock.NewRows([]string{"id", "parent_id", "name", "position", "position_absolute"}).
AddRow(3, 2, "1", "/test", "/test/1").
AddRow(2, 1, "test", "/", "/test").
AddRow(4, 3, "3", "/test/1", "/test/1/3"),
)
// 更改顶级要移动目录的父目录指向
mock.ExpectBegin()
mock.ExpectExec("UPDATE(.+)").
WithArgs(10, "/dst", "/dst/", sqlmock.AnyArg(), "/test", 1).
WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectCommit()
// 移动子目录
mock.ExpectBegin()
mock.ExpectExec("UPDATE(.+)").
WithArgs("/dst/test", "/dst/test/1", sqlmock.AnyArg(), 3).
WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectCommit()
mock.ExpectBegin()
mock.ExpectExec("UPDATE(.+)").
WithArgs("/dst/test/1", "/dst/test/1/3", sqlmock.AnyArg(), 4).
WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectCommit()
// 获取子文件
mock.ExpectQuery("SELECT(.+)").
WithArgs(1, 3, 2, 4).
WillReturnRows(
sqlmock.NewRows([]string{"id", "folder_id", "dir", "size"}).
AddRow(1, 2, "/test", 10).
AddRow(2, 3, "/test/1", 20),
)
// 移动子文件
mock.ExpectBegin()
mock.ExpectExec("UPDATE(.+)").
WithArgs("/dst/test", sqlmock.AnyArg(), 1).
WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectCommit()
mock.ExpectBegin()
mock.ExpectExec("UPDATE(.+)").
WithArgs("/dst/test/1", sqlmock.AnyArg(), 2).
WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectCommit()
storage, err := parFolder.MoveOrCopyFolderTo([]string{"/test"}, &dstFolder, false)
asserts.NoError(mock.ExpectationsWereMet())
asserts.NoError(err)
asserts.Equal(uint64(0), storage)
}
// 无法移动顶层目录
{
// 查找所有递归子目录,包括自身
mock.ExpectQuery("SELECT(.+)").
WithArgs(1, sqlmock.AnyArg(), 1, "/test").
WillReturnRows(
sqlmock.NewRows([]string{"id", "parent_id", "name", "position", "position_absolute"}).
AddRow(3, 2, "1", "/test", "/test/1").
AddRow(2, 1, "test", "/", "/test").
AddRow(4, 3, "3", "/test/1", "/test/1/3"),
)
// 更改顶级要移动目录的父目录指向
mock.ExpectBegin()
mock.ExpectExec("UPDATE(.+)").
WithArgs(10, "/dst", "/dst/", sqlmock.AnyArg(), "/test", 1).
WillReturnError(errors.New("error"))
mock.ExpectRollback()
storage, err := parFolder.MoveOrCopyFolderTo([]string{"/test"}, &dstFolder, false)
asserts.NoError(mock.ExpectationsWereMet())
asserts.Error(err)
asserts.Equal(uint64(0), storage)
}
}

@ -24,13 +24,13 @@ type Group struct {
// GetAria2Option 获取用户离线下载设备 // GetAria2Option 获取用户离线下载设备
// TODO:测试 // TODO:测试
func (group *Group) GetAria2Option() [3]bool { func (group *Group) GetAria2Option() [3]bool {
if len(group.Aria2Option) != 3 { if len(group.Aria2Option) != 5 {
return [3]bool{false, false, false} return [3]bool{false, false, false}
} }
return [3]bool{ return [3]bool{
group.Aria2Option[0] == '1', group.Aria2Option[0] == '1',
group.Aria2Option[1] == '1',
group.Aria2Option[2] == '1', group.Aria2Option[2] == '1',
group.Aria2Option[4] == '1',
} }
} }

@ -62,3 +62,32 @@ func TestGroup_AfterFind(t *testing.T) {
asserts.NoError(err) asserts.NoError(err)
asserts.Equal(testCase.PolicyList, []uint{}) asserts.Equal(testCase.PolicyList, []uint{})
} }
func TestGroup_GetAria2Option(t *testing.T) {
asserts := assert.New(t)
group := Group{}
// 格式错误
{
group.Aria2Option = "123"
asserts.Equal([3]bool{false, false, false}, group.GetAria2Option())
}
// 正常
{
group.Aria2Option = "1,1,0"
asserts.Equal([3]bool{true, true, false}, group.GetAria2Option())
}
}
func TestGroup_BeforeSave(t *testing.T) {
asserts := assert.New(t)
group := Group{
PolicyList: []uint{1, 2, 3},
}
{
err := group.BeforeSave()
asserts.NoError(err)
asserts.Equal("[1,2,3]", group.Policies)
}
}

@ -0,0 +1,16 @@
package model
import (
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
"testing"
)
func TestMigration(t *testing.T) {
asserts := assert.New(t)
gin.SetMode(gin.TestMode)
asserts.NotPanics(func() {
migration()
})
}

@ -5,12 +5,14 @@ import (
"github.com/HFO4/cloudreve/pkg/conf" "github.com/HFO4/cloudreve/pkg/conf"
"github.com/HFO4/cloudreve/routers/controllers" "github.com/HFO4/cloudreve/routers/controllers"
"github.com/gin-contrib/cors" "github.com/gin-contrib/cors"
"github.com/gin-contrib/pprof"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
// InitRouter 初始化路由 // InitRouter 初始化路由
func InitRouter() *gin.Engine { func InitRouter() *gin.Engine {
r := gin.Default() r := gin.Default()
pprof.Register(r)
/* /*

Loading…
Cancel
Save