You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
118 lines
2.8 KiB
118 lines
2.8 KiB
package task
|
|
|
|
import (
|
|
"errors"
|
|
"testing"
|
|
|
|
"github.com/DATA-DOG/go-sqlmock"
|
|
model "github.com/cloudreve/Cloudreve/v3/models"
|
|
"github.com/jinzhu/gorm"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestRecycleTask_Props(t *testing.T) {
|
|
asserts := assert.New(t)
|
|
task := &RecycleTask{
|
|
User: &model.User{},
|
|
}
|
|
asserts.NotEmpty(task.Props())
|
|
asserts.Equal(RecycleTaskType, task.Type())
|
|
asserts.EqualValues(0, task.Creator())
|
|
asserts.Nil(task.Model())
|
|
}
|
|
|
|
func TestRecycleTask_SetStatus(t *testing.T) {
|
|
asserts := assert.New(t)
|
|
task := &RecycleTask{
|
|
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 TestRecycleTask_SetError(t *testing.T) {
|
|
asserts := assert.New(t)
|
|
task := &RecycleTask{
|
|
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 TestNewRecycleTask(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 := NewRecycleTask(&model.Download{
|
|
Model: gorm.Model{ID: 1},
|
|
GID: "test_g_id",
|
|
Parent: "/",
|
|
UserID: 1,
|
|
NodeID: 1,
|
|
})
|
|
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 := NewRecycleTask(&model.Download{
|
|
Model: gorm.Model{ID: 1},
|
|
GID: "test_g_id",
|
|
Parent: "test/not_exist",
|
|
UserID: 1,
|
|
NodeID: 1,
|
|
})
|
|
asserts.NoError(mock.ExpectationsWereMet())
|
|
asserts.Nil(job)
|
|
asserts.Error(err)
|
|
}
|
|
}
|
|
|
|
func TestNewRecycleTaskFromModel(t *testing.T) {
|
|
asserts := assert.New(t)
|
|
|
|
// 成功
|
|
{
|
|
mock.ExpectQuery("SELECT(.+)").WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(1))
|
|
job, err := NewRecycleTaskFromModel(&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 := NewRecycleTaskFromModel(&model.Task{Props: "?"})
|
|
asserts.NoError(mock.ExpectationsWereMet())
|
|
asserts.Error(err)
|
|
asserts.Nil(job)
|
|
}
|
|
}
|