parent
2b853dddd3
commit
fc5b7d42c8
@ -0,0 +1,196 @@
|
||||
package task
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/DATA-DOG/go-sqlmock"
|
||||
model "github.com/HFO4/cloudreve/models"
|
||||
"github.com/HFO4/cloudreve/pkg/cache"
|
||||
"github.com/HFO4/cloudreve/pkg/util"
|
||||
"github.com/jinzhu/gorm"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCompressTask_Props(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
task := &CompressTask{
|
||||
User: &model.User{},
|
||||
}
|
||||
asserts.NotEmpty(task.Props())
|
||||
asserts.Equal(CompressTaskType, task.Type())
|
||||
asserts.EqualValues(0, task.Creator())
|
||||
asserts.Nil(task.Model())
|
||||
}
|
||||
|
||||
func TestCompressTask_SetStatus(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
task := &CompressTask{
|
||||
User: &model.User{},
|
||||
TaskModel: &model.Task{
|
||||
Model: gorm.Model{ID: 1},
|
||||
},
|
||||
}
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectExec("UPDATE(.+)").WillReturnResult(sqlmock.NewResult(1, 1))
|
||||
mock.ExpectCommit()
|
||||
task.SetStatus(3)
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
}
|
||||
|
||||
func TestCompressTask_SetError(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
task := &CompressTask{
|
||||
User: &model.User{},
|
||||
TaskModel: &model.Task{
|
||||
Model: gorm.Model{ID: 1},
|
||||
},
|
||||
zipPath: "test/TestCompressTask_SetError",
|
||||
}
|
||||
zipFile, _ := util.CreatNestedFile("test/TestCompressTask_SetError")
|
||||
zipFile.Close()
|
||||
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectExec("UPDATE(.+)").WillReturnResult(sqlmock.NewResult(1, 1))
|
||||
mock.ExpectCommit()
|
||||
|
||||
task.SetErrorMsg("error")
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
asserts.False(util.Exists("test/TestCompressTask_SetError"))
|
||||
asserts.Equal("error", task.GetError().Msg)
|
||||
}
|
||||
|
||||
func TestCompressTask_Do(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
task := &CompressTask{
|
||||
TaskModel: &model.Task{
|
||||
Model: gorm.Model{ID: 1},
|
||||
},
|
||||
}
|
||||
|
||||
// 无法创建文件系统
|
||||
{
|
||||
task.User = &model.User{
|
||||
Policy: model.Policy{
|
||||
Type: "unknown",
|
||||
},
|
||||
}
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectExec("UPDATE(.+)").WillReturnResult(sqlmock.NewResult(1,
|
||||
1))
|
||||
mock.ExpectCommit()
|
||||
task.Do()
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
asserts.NotEmpty(task.GetError().Msg)
|
||||
}
|
||||
|
||||
// 压缩出错
|
||||
{
|
||||
task.User = &model.User{
|
||||
Policy: model.Policy{
|
||||
Type: "mock",
|
||||
},
|
||||
}
|
||||
task.TaskProps.Dirs = []uint{1}
|
||||
// 更新进度
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectExec("UPDATE(.+)").WillReturnResult(sqlmock.NewResult(1,
|
||||
1))
|
||||
mock.ExpectCommit()
|
||||
// 查找目录
|
||||
mock.ExpectQuery("SELECT(.+)").WillReturnError(errors.New("error"))
|
||||
// 更新错误
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectExec("UPDATE(.+)").WillReturnResult(sqlmock.NewResult(1,
|
||||
1))
|
||||
mock.ExpectCommit()
|
||||
task.Do()
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
asserts.NotEmpty(task.GetError().Msg)
|
||||
}
|
||||
|
||||
// 上传出错
|
||||
{
|
||||
task.User = &model.User{
|
||||
Policy: model.Policy{
|
||||
Type: "mock",
|
||||
MaxSize: 1,
|
||||
},
|
||||
}
|
||||
task.TaskProps.Dirs = []uint{1}
|
||||
cache.Set("setting_temp_path", "test", 0)
|
||||
// 更新进度
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectExec("UPDATE(.+)").WillReturnResult(sqlmock.NewResult(1,
|
||||
1))
|
||||
mock.ExpectCommit()
|
||||
// 查找目录
|
||||
mock.ExpectQuery("SELECT(.+)folders").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(1))
|
||||
// 查找文件
|
||||
mock.ExpectQuery("SELECT(.+)files").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id"}))
|
||||
// 查找子文件
|
||||
mock.ExpectQuery("SELECT(.+)files").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id"}))
|
||||
// 查找子目录
|
||||
mock.ExpectQuery("SELECT(.+)folders").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id"}))
|
||||
// 更新错误
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectExec("UPDATE(.+)").WillReturnResult(sqlmock.NewResult(1,
|
||||
1))
|
||||
mock.ExpectCommit()
|
||||
task.Do()
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
asserts.NotEmpty(task.GetError().Msg)
|
||||
asserts.True(util.IsEmpty("test/compress"))
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewCompressTask(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
|
||||
// 成功
|
||||
{
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectExec("INSERT(.+)").WillReturnResult(sqlmock.NewResult(1, 1))
|
||||
mock.ExpectCommit()
|
||||
job, err := NewCompressTask(&model.User{}, "/", []uint{12}, []uint{})
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
asserts.NotNil(job)
|
||||
asserts.NoError(err)
|
||||
}
|
||||
|
||||
// 失败
|
||||
{
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectExec("INSERT(.+)").WillReturnError(errors.New("error"))
|
||||
mock.ExpectRollback()
|
||||
job, err := NewCompressTask(&model.User{}, "/", []uint{12}, []uint{})
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
asserts.Nil(job)
|
||||
asserts.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewCompressTaskFromModel(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
|
||||
// 成功
|
||||
{
|
||||
mock.ExpectQuery("SELECT(.+)").WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(1))
|
||||
job, err := NewCompressTaskFromModel(&model.Task{Props: "{}"})
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
asserts.NoError(err)
|
||||
asserts.NotNil(job)
|
||||
}
|
||||
|
||||
// JSON解析失败
|
||||
{
|
||||
mock.ExpectQuery("SELECT(.+)").WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(1))
|
||||
job, err := NewCompressTaskFromModel(&model.Task{Props: ""})
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
asserts.Error(err)
|
||||
asserts.Nil(job)
|
||||
}
|
||||
}
|
@ -0,0 +1,139 @@
|
||||
package task
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/DATA-DOG/go-sqlmock"
|
||||
model "github.com/HFO4/cloudreve/models"
|
||||
"github.com/jinzhu/gorm"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDecompressTask_Props(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
task := &DecompressTask{
|
||||
User: &model.User{},
|
||||
}
|
||||
asserts.NotEmpty(task.Props())
|
||||
asserts.Equal(DecompressTaskType, task.Type())
|
||||
asserts.EqualValues(0, task.Creator())
|
||||
asserts.Nil(task.Model())
|
||||
}
|
||||
|
||||
func TestDecompressTask_SetStatus(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
task := &DecompressTask{
|
||||
User: &model.User{},
|
||||
TaskModel: &model.Task{
|
||||
Model: gorm.Model{ID: 1},
|
||||
},
|
||||
}
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectExec("UPDATE(.+)").WillReturnResult(sqlmock.NewResult(1, 1))
|
||||
mock.ExpectCommit()
|
||||
task.SetStatus(3)
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
}
|
||||
|
||||
func TestDecompressTask_SetError(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
task := &DecompressTask{
|
||||
User: &model.User{},
|
||||
TaskModel: &model.Task{
|
||||
Model: gorm.Model{ID: 1},
|
||||
},
|
||||
zipPath: "test/TestCompressTask_SetError",
|
||||
}
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectExec("UPDATE(.+)").WillReturnResult(sqlmock.NewResult(1, 1))
|
||||
mock.ExpectCommit()
|
||||
|
||||
task.SetErrorMsg("error", nil)
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
asserts.Equal("error", task.GetError().Msg)
|
||||
}
|
||||
|
||||
func TestDecompressTask_Do(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
task := &DecompressTask{
|
||||
TaskModel: &model.Task{
|
||||
Model: gorm.Model{ID: 1},
|
||||
},
|
||||
}
|
||||
|
||||
// 无法创建文件系统
|
||||
{
|
||||
task.User = &model.User{
|
||||
Policy: model.Policy{
|
||||
Type: "unknown",
|
||||
},
|
||||
}
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectExec("UPDATE(.+)").WillReturnResult(sqlmock.NewResult(1,
|
||||
1))
|
||||
mock.ExpectCommit()
|
||||
task.Do()
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
asserts.NotEmpty(task.GetError().Msg)
|
||||
}
|
||||
|
||||
// 压缩文件不存在
|
||||
{
|
||||
task.User = &model.User{
|
||||
Policy: model.Policy{
|
||||
Type: "mock",
|
||||
},
|
||||
}
|
||||
task.TaskProps.Src = "test"
|
||||
task.Do()
|
||||
asserts.NotEmpty(task.GetError().Msg)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewDecompressTask(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
|
||||
// 成功
|
||||
{
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectExec("INSERT(.+)").WillReturnResult(sqlmock.NewResult(1, 1))
|
||||
mock.ExpectCommit()
|
||||
job, err := NewDecompressTask(&model.User{}, "/", "/")
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
asserts.NotNil(job)
|
||||
asserts.NoError(err)
|
||||
}
|
||||
|
||||
// 失败
|
||||
{
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectExec("INSERT(.+)").WillReturnError(errors.New("error"))
|
||||
mock.ExpectRollback()
|
||||
job, err := NewDecompressTask(&model.User{}, "/", "/")
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
asserts.Nil(job)
|
||||
asserts.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewDecompressTaskFromModel(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
|
||||
// 成功
|
||||
{
|
||||
mock.ExpectQuery("SELECT(.+)").WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(1))
|
||||
job, err := NewDecompressTaskFromModel(&model.Task{Props: "{}"})
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
asserts.NoError(err)
|
||||
asserts.NotNil(job)
|
||||
}
|
||||
|
||||
// JSON解析失败
|
||||
{
|
||||
mock.ExpectQuery("SELECT(.+)").WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(1))
|
||||
job, err := NewDecompressTaskFromModel(&model.Task{Props: ""})
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
asserts.Error(err)
|
||||
asserts.Nil(job)
|
||||
}
|
||||
}
|
@ -0,0 +1,73 @@
|
||||
package task
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/DATA-DOG/go-sqlmock"
|
||||
model "github.com/HFO4/cloudreve/models"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestRecord(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
job := &TransferTask{
|
||||
User: &model.User{Policy: model.Policy{Type: "unknown"}},
|
||||
}
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectExec("INSERT(.+)").WillReturnResult(sqlmock.NewResult(1, 1))
|
||||
mock.ExpectCommit()
|
||||
_, err := Record(job)
|
||||
asserts.NoError(err)
|
||||
}
|
||||
|
||||
func TestResume(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
|
||||
// 没有任务
|
||||
{
|
||||
mock.ExpectQuery("SELECT(.+)").WithArgs(Queued).WillReturnRows(sqlmock.NewRows([]string{"type"}))
|
||||
Resume()
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetJobFromModel(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
|
||||
// CompressTaskType
|
||||
{
|
||||
task := &model.Task{
|
||||
Status: 0,
|
||||
Type: CompressTaskType,
|
||||
}
|
||||
mock.ExpectQuery("SELECT(.+)users(.+)").WillReturnError(errors.New("error"))
|
||||
job, err := GetJobFromModel(task)
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
asserts.Nil(job)
|
||||
asserts.Error(err)
|
||||
}
|
||||
// DecompressTaskType
|
||||
{
|
||||
task := &model.Task{
|
||||
Status: 0,
|
||||
Type: DecompressTaskType,
|
||||
}
|
||||
mock.ExpectQuery("SELECT(.+)users(.+)").WillReturnError(errors.New("error"))
|
||||
job, err := GetJobFromModel(task)
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
asserts.Nil(job)
|
||||
asserts.Error(err)
|
||||
}
|
||||
// TransferTaskType
|
||||
{
|
||||
task := &model.Task{
|
||||
Status: 0,
|
||||
Type: TransferTaskType,
|
||||
}
|
||||
mock.ExpectQuery("SELECT(.+)users(.+)").WillReturnError(errors.New("error"))
|
||||
job, err := GetJobFromModel(task)
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
asserts.Nil(job)
|
||||
asserts.Error(err)
|
||||
}
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
package task
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"github.com/DATA-DOG/go-sqlmock"
|
||||
model "github.com/HFO4/cloudreve/models"
|
||||
"github.com/HFO4/cloudreve/pkg/cache"
|
||||
"github.com/jinzhu/gorm"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var mock sqlmock.Sqlmock
|
||||
|
||||
// TestMain 初始化数据库Mock
|
||||
func TestMain(m *testing.M) {
|
||||
var db *sql.DB
|
||||
var err error
|
||||
db, mock, err = sqlmock.New()
|
||||
if err != nil {
|
||||
panic("An error was not expected when opening a stub database connection")
|
||||
}
|
||||
model.DB, _ = gorm.Open("mysql", db)
|
||||
defer db.Close()
|
||||
m.Run()
|
||||
}
|
||||
|
||||
func TestInit(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
cache.Set("setting_max_worker_num", "10", 0)
|
||||
mock.ExpectQuery("SELECT(.+)").WithArgs(Queued).WillReturnRows(sqlmock.NewRows([]string{"type"}).AddRow(-1))
|
||||
Init()
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
asserts.Len(TaskPoll.idleWorker, 10)
|
||||
}
|
||||
|
||||
func TestPool_Submit(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
pool := &Pool{
|
||||
idleWorker: make(chan int, 1),
|
||||
}
|
||||
pool.Add(1)
|
||||
job := &MockJob{
|
||||
DoFunc: func() {
|
||||
|
||||
},
|
||||
}
|
||||
asserts.NotPanics(func() {
|
||||
pool.Submit(job)
|
||||
})
|
||||
}
|
@ -0,0 +1,154 @@
|
||||
package task
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/DATA-DOG/go-sqlmock"
|
||||
model "github.com/HFO4/cloudreve/models"
|
||||
"github.com/jinzhu/gorm"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestTransferTask_Props(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
task := &TransferTask{
|
||||
User: &model.User{},
|
||||
}
|
||||
asserts.NotEmpty(task.Props())
|
||||
asserts.Equal(TransferTaskType, task.Type())
|
||||
asserts.EqualValues(0, task.Creator())
|
||||
asserts.Nil(task.Model())
|
||||
}
|
||||
|
||||
func TestTransferTask_SetStatus(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
task := &TransferTask{
|
||||
User: &model.User{},
|
||||
TaskModel: &model.Task{
|
||||
Model: gorm.Model{ID: 1},
|
||||
},
|
||||
}
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectExec("UPDATE(.+)").WillReturnResult(sqlmock.NewResult(1, 1))
|
||||
mock.ExpectCommit()
|
||||
task.SetStatus(3)
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
}
|
||||
|
||||
func TestTransferTask_SetError(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
task := &TransferTask{
|
||||
User: &model.User{},
|
||||
TaskModel: &model.Task{
|
||||
Model: gorm.Model{ID: 1},
|
||||
},
|
||||
}
|
||||
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectExec("UPDATE(.+)").WillReturnResult(sqlmock.NewResult(1, 1))
|
||||
mock.ExpectCommit()
|
||||
|
||||
task.SetErrorMsg("error", nil)
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
asserts.Equal("error", task.GetError().Msg)
|
||||
}
|
||||
|
||||
func TestTransferTask_Do(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
task := &TransferTask{
|
||||
TaskModel: &model.Task{
|
||||
Model: gorm.Model{ID: 1},
|
||||
},
|
||||
}
|
||||
|
||||
// 无法创建文件系统
|
||||
{
|
||||
task.TaskProps.Parent = "test/not_exist"
|
||||
task.User = &model.User{
|
||||
Policy: model.Policy{
|
||||
Type: "unknown",
|
||||
},
|
||||
}
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectExec("UPDATE(.+)").WillReturnResult(sqlmock.NewResult(1,
|
||||
1))
|
||||
mock.ExpectCommit()
|
||||
task.Do()
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
asserts.NotEmpty(task.GetError().Msg)
|
||||
}
|
||||
|
||||
// 上传出错
|
||||
{
|
||||
task.User = &model.User{
|
||||
Policy: model.Policy{
|
||||
Type: "mock",
|
||||
},
|
||||
}
|
||||
task.TaskProps.Src = []string{"test/not_exist"}
|
||||
task.TaskProps.Parent = "test/not_exist"
|
||||
// 更新进度
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectExec("UPDATE(.+)").WillReturnResult(sqlmock.NewResult(1,
|
||||
1))
|
||||
mock.ExpectCommit()
|
||||
// 更新错误
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectExec("UPDATE(.+)").WillReturnResult(sqlmock.NewResult(1,
|
||||
1))
|
||||
mock.ExpectCommit()
|
||||
task.Do()
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
asserts.NotEmpty(task.GetError().Msg)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewTransferTask(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
|
||||
// 成功
|
||||
{
|
||||
mock.ExpectQuery("SELECT(.+)").WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(1))
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectExec("INSERT(.+)").WillReturnResult(sqlmock.NewResult(1, 1))
|
||||
mock.ExpectCommit()
|
||||
job, err := NewTransferTask(1, []string{}, "/", "/")
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
asserts.NotNil(job)
|
||||
asserts.NoError(err)
|
||||
}
|
||||
|
||||
// 失败
|
||||
{
|
||||
mock.ExpectQuery("SELECT(.+)").WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(1))
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectExec("INSERT(.+)").WillReturnError(errors.New("error"))
|
||||
mock.ExpectRollback()
|
||||
job, err := NewTransferTask(1, []string{}, "/", "/")
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
asserts.Nil(job)
|
||||
asserts.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewTransferTaskFromModel(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
|
||||
// 成功
|
||||
{
|
||||
mock.ExpectQuery("SELECT(.+)").WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(1))
|
||||
job, err := NewTransferTaskFromModel(&model.Task{Props: "{}"})
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
asserts.NoError(err)
|
||||
asserts.NotNil(job)
|
||||
}
|
||||
|
||||
// JSON解析失败
|
||||
{
|
||||
mock.ExpectQuery("SELECT(.+)").WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(1))
|
||||
job, err := NewTransferTaskFromModel(&model.Task{Props: "?"})
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
asserts.Error(err)
|
||||
asserts.Nil(job)
|
||||
}
|
||||
}
|
@ -0,0 +1,80 @@
|
||||
package task
|
||||
|
||||
import (
|
||||
model "github.com/HFO4/cloudreve/models"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type MockJob struct {
|
||||
Err *JobError
|
||||
Status int
|
||||
DoFunc func()
|
||||
}
|
||||
|
||||
func (job *MockJob) Type() int {
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (job *MockJob) Creator() uint {
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (job *MockJob) Props() string {
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (job *MockJob) Model() *model.Task {
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (job *MockJob) SetStatus(status int) {
|
||||
job.Status = status
|
||||
}
|
||||
|
||||
func (job *MockJob) Do() {
|
||||
job.DoFunc()
|
||||
}
|
||||
|
||||
func (job *MockJob) SetError(*JobError) {
|
||||
}
|
||||
|
||||
func (job *MockJob) GetError() *JobError {
|
||||
return job.Err
|
||||
}
|
||||
|
||||
func TestGeneralWorker_Do(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
worker := &GeneralWorker{}
|
||||
job := &MockJob{}
|
||||
|
||||
// 正常
|
||||
{
|
||||
job.DoFunc = func() {
|
||||
}
|
||||
worker.Do(job)
|
||||
asserts.Equal(Complete, job.Status)
|
||||
}
|
||||
|
||||
// 有错误
|
||||
{
|
||||
job.DoFunc = func() {
|
||||
}
|
||||
job.Status = Queued
|
||||
job.Err = &JobError{Msg: "error"}
|
||||
worker.Do(job)
|
||||
asserts.Equal(Error, job.Status)
|
||||
}
|
||||
|
||||
// 有致命错误
|
||||
{
|
||||
job.DoFunc = func() {
|
||||
panic("mock fatal error")
|
||||
}
|
||||
job.Status = Queued
|
||||
job.Err = nil
|
||||
worker.Do(job)
|
||||
asserts.Equal(Error, job.Status)
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue