Test: model.Tags / modifications

This commit is contained in:
HFO4
2020-02-12 13:19:25 +08:00
parent 15f4b1819b
commit bb63ea7142
17 changed files with 352 additions and 55 deletions

View File

@@ -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)
}

View File

@@ -92,7 +92,7 @@ func BuildUser(user model.User) User {
ShareDownload: user.Group.OptionsSerialized.ShareDownload,
CompressEnabled: user.Group.OptionsSerialized.ArchiveTask,
},
Tags: BuildTagRes(tags),
Tags: buildTagRes(tags),
}
}
@@ -121,20 +121,20 @@ func BuildUserStorageResponse(user model.User) Response {
}
}
// BuildTagRes 构建标签列表
func BuildTagRes(tags []model.Tag) []tag {
// buildTagRes 构建标签列表
func buildTagRes(tags []model.Tag) []tag {
res := make([]tag, 0, len(tags))
for i := 0; i < len(tags); i++ {
newTag := tag{
ID: hashid.HashID(tags[i].ID, hashid.TagID),
Name: tags[i].Name,
Icon: tags[i].Icon,
Color: tags[i].Color,
Type: tags[i].Type,
Expression: tags[i].Expression,
ID: hashid.HashID(tags[i].ID, hashid.TagID),
Name: tags[i].Name,
Icon: tags[i].Icon,
Color: tags[i].Color,
Type: tags[i].Type,
}
if newTag.Type == 0 {
newTag.Expression = ""
if newTag.Type != 0 {
newTag.Expression = tags[i].Expression
}
res = append(res, newTag)
}

View File

@@ -1,18 +1,38 @@
package serializer
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 TestBuildUser(t *testing.T) {
asserts := assert.New(t)
user := model.User{
Policy: model.Policy{MaxSize: 1024 * 1024},
}
mock.ExpectQuery("SELECT(.+)").WillReturnRows(sqlmock.NewRows([]string{"id"}))
res := BuildUser(user)
asserts.NoError(mock.ExpectationsWereMet())
asserts.Equal("1.00mb", res.Policy.MaxSize)
}
@@ -72,3 +92,21 @@ func TestBuildUserStorageResponse(t *testing.T) {
asserts.Equal(uint64(5), res.Data.(storage).Free)
}
}
func TestBuildTagRes(t *testing.T) {
asserts := assert.New(t)
tags := []model.Tag{
{
Type: 0,
Expression: "exp",
},
{
Type: 1,
Expression: "exp",
},
}
res := buildTagRes(tags)
asserts.Len(res, 2)
asserts.Equal("", res[0].Expression)
asserts.Equal("exp", res[1].Expression)
}