parent
15f4b1819b
commit
bb63ea7142
@ -0,0 +1,63 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/DATA-DOG/go-sqlmock"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestTag_Create(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
tag := Tag{}
|
||||
|
||||
// 成功
|
||||
{
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectExec("INSERT(.+)").WillReturnResult(sqlmock.NewResult(1, 1))
|
||||
mock.ExpectCommit()
|
||||
id, err := tag.Create()
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
asserts.NoError(err)
|
||||
asserts.EqualValues(1, id)
|
||||
}
|
||||
|
||||
// 失败
|
||||
{
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectExec("INSERT(.+)").WillReturnError(errors.New("error"))
|
||||
mock.ExpectRollback()
|
||||
id, err := tag.Create()
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
asserts.Error(err)
|
||||
asserts.EqualValues(0, id)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteTagByID(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectExec("UPDATE(.+)").WillReturnResult(sqlmock.NewResult(1, 1))
|
||||
mock.ExpectCommit()
|
||||
err := DeleteTagByID(1, 2)
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
asserts.NoError(err)
|
||||
}
|
||||
|
||||
func TestGetTagsByUID(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
mock.ExpectQuery("SELECT(.+)").WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(1))
|
||||
res, err := GetTagsByUID(1)
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
asserts.NoError(err)
|
||||
asserts.Len(res, 1)
|
||||
}
|
||||
|
||||
func TestGetTagsByID(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
mock.ExpectQuery("SELECT(.+)").WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(1))
|
||||
res, err := GetTasksByID(1)
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
asserts.NoError(err)
|
||||
asserts.EqualValues(1, res.ID)
|
||||
}
|
@ -0,0 +1,92 @@
|
||||
package serializer
|
||||
|
||||
import (
|
||||
model "github.com/HFO4/cloudreve/models"
|
||||
"github.com/HFO4/cloudreve/pkg/cache"
|
||||
"github.com/jinzhu/gorm"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/zyxar/argo/rpc"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestBuildFinishedListResponse(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
tasks := []model.Download{
|
||||
{
|
||||
StatusInfo: rpc.StatusInfo{
|
||||
Files: []rpc.FileInfo{
|
||||
{
|
||||
Path: "/file/name.txt",
|
||||
},
|
||||
},
|
||||
},
|
||||
Task: &model.Task{
|
||||
Model: gorm.Model{},
|
||||
Error: "error",
|
||||
},
|
||||
},
|
||||
{
|
||||
StatusInfo: rpc.StatusInfo{
|
||||
Files: []rpc.FileInfo{
|
||||
{
|
||||
Path: "/file/name1.txt",
|
||||
},
|
||||
{
|
||||
Path: "/file/name2.txt",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
tasks[1].StatusInfo.BitTorrent.Info.Name = "name.txt"
|
||||
res := BuildFinishedListResponse(tasks).Data.([]FinishedListResponse)
|
||||
asserts.Len(res, 2)
|
||||
asserts.Equal("name.txt", res[1].Name)
|
||||
asserts.Equal("name.txt", res[0].Name)
|
||||
asserts.Equal("name.txt", res[0].Files[0].Path)
|
||||
asserts.Equal("name1.txt", res[1].Files[0].Path)
|
||||
asserts.Equal("name2.txt", res[1].Files[1].Path)
|
||||
asserts.EqualValues(0, res[0].TaskStatus)
|
||||
asserts.Equal("error", res[0].TaskError)
|
||||
}
|
||||
|
||||
func TestBuildDownloadingResponse(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
cache.Set("setting_aria2_interval", "10", 0)
|
||||
tasks := []model.Download{
|
||||
{
|
||||
StatusInfo: rpc.StatusInfo{
|
||||
Files: []rpc.FileInfo{
|
||||
{
|
||||
Path: "/file/name.txt",
|
||||
},
|
||||
},
|
||||
},
|
||||
Task: &model.Task{
|
||||
Model: gorm.Model{},
|
||||
Error: "error",
|
||||
},
|
||||
},
|
||||
{
|
||||
StatusInfo: rpc.StatusInfo{
|
||||
Files: []rpc.FileInfo{
|
||||
{
|
||||
Path: "/file/name1.txt",
|
||||
},
|
||||
{
|
||||
Path: "/file/name2.txt",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
tasks[1].StatusInfo.BitTorrent.Info.Name = "name.txt"
|
||||
|
||||
res := BuildDownloadingResponse(tasks).Data.([]DownloadListResponse)
|
||||
asserts.Len(res, 2)
|
||||
asserts.Equal("name1.txt", res[1].Name)
|
||||
asserts.Equal("name.txt", res[0].Name)
|
||||
asserts.Equal("name.txt", res[0].Info.Files[0].Path)
|
||||
asserts.Equal("name1.txt", res[1].Info.Files[0].Path)
|
||||
asserts.Equal("name2.txt", res[1].Info.Files[1].Path)
|
||||
}
|
Loading…
Reference in new issue