mirror of
https://github.com/halejohn/Cloudreve.git
synced 2026-01-26 09:34:57 +08:00
Test: list folder & filesystem/path & model/folder/list
This commit is contained in:
@@ -35,7 +35,7 @@ func GetFileByPathAndName(path string, name string, uid uint) (File, error) {
|
||||
return file, result.Error
|
||||
}
|
||||
|
||||
// GetChildFile 查找目录下子文件 TODO:test
|
||||
// GetChildFile 查找目录下子文件
|
||||
func (folder *Folder) GetChildFile() ([]File, error) {
|
||||
var files []File
|
||||
result := DB.Where("folder_id = ?", folder.ID).Find(&files)
|
||||
|
||||
@@ -3,6 +3,7 @@ package model
|
||||
import (
|
||||
"errors"
|
||||
"github.com/DATA-DOG/go-sqlmock"
|
||||
"github.com/jinzhu/gorm"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
@@ -41,3 +42,27 @@ func TestFile_Create(t *testing.T) {
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
|
||||
}
|
||||
|
||||
func TestFolder_GetChildFile(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
folder := &Folder{
|
||||
Model: gorm.Model{
|
||||
ID: 1,
|
||||
},
|
||||
}
|
||||
|
||||
// 找不到
|
||||
mock.ExpectQuery("SELECT(.+)folder_id(.+)").WithArgs(1).WillReturnError(errors.New("error"))
|
||||
files, err := folder.GetChildFile()
|
||||
asserts.Error(err)
|
||||
asserts.Len(files, 0)
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
|
||||
// 找到了
|
||||
mock.ExpectQuery("SELECT(.+)folder_id(.+)").WithArgs(1).WillReturnRows(sqlmock.NewRows([]string{"name", "id"}).AddRow("1.txt", 1).AddRow("2.txt", 2))
|
||||
files, err = folder.GetChildFile()
|
||||
asserts.NoError(err)
|
||||
asserts.Len(files, 2)
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ type Folder struct {
|
||||
PositionAbsolute string `gorm:"size:65536"`
|
||||
}
|
||||
|
||||
// Create 创建目录 TODO:test
|
||||
// Create 创建目录
|
||||
func (folder *Folder) Create() (uint, error) {
|
||||
if err := DB.Create(folder).Error; err != nil {
|
||||
util.Log().Warning("无法插入目录记录, %s", err)
|
||||
@@ -32,7 +32,7 @@ func GetFolderByPath(path string, uid uint) (Folder, error) {
|
||||
return folder, result.Error
|
||||
}
|
||||
|
||||
// GetChildFolder 查找子目录 TODO:test
|
||||
// GetChildFolder 查找子目录
|
||||
func (folder *Folder) GetChildFolder() ([]Folder, error) {
|
||||
var folders []Folder
|
||||
result := DB.Where("parent_id = ?", folder.ID).Find(&folders)
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/DATA-DOG/go-sqlmock"
|
||||
"github.com/jinzhu/gorm"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
@@ -22,3 +24,51 @@ func TestGetFolderByPath(t *testing.T) {
|
||||
asserts.Error(err)
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
}
|
||||
|
||||
func TestFolder_Create(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
folder := &Folder{
|
||||
Name: "new folder",
|
||||
}
|
||||
|
||||
// 插入成功
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectExec("INSERT(.+)").WillReturnResult(sqlmock.NewResult(5, 1))
|
||||
mock.ExpectCommit()
|
||||
fid, err := folder.Create()
|
||||
asserts.NoError(err)
|
||||
asserts.Equal(uint(5), fid)
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
|
||||
// 插入失败
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectExec("INSERT(.+)").WillReturnError(errors.New("error"))
|
||||
mock.ExpectRollback()
|
||||
fid, err = folder.Create()
|
||||
asserts.Error(err)
|
||||
asserts.Equal(uint(0), fid)
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
}
|
||||
|
||||
func TestFolder_GetChildFolder(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
folder := &Folder{
|
||||
Model: gorm.Model{
|
||||
ID: 1,
|
||||
},
|
||||
}
|
||||
|
||||
// 找不到
|
||||
mock.ExpectQuery("SELECT(.+)parent_id(.+)").WithArgs(1).WillReturnError(errors.New("error"))
|
||||
files, err := folder.GetChildFolder()
|
||||
asserts.Error(err)
|
||||
asserts.Len(files, 0)
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
|
||||
// 找到了
|
||||
mock.ExpectQuery("SELECT(.+)parent_id(.+)").WithArgs(1).WillReturnRows(sqlmock.NewRows([]string{"name", "id"}).AddRow("1.txt", 1).AddRow("2.txt", 2))
|
||||
files, err = folder.GetChildFolder()
|
||||
asserts.NoError(err)
|
||||
asserts.Len(files, 2)
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user