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.
94 lines
1.9 KiB
94 lines
1.9 KiB
package model
|
|
|
|
import (
|
|
"github.com/DATA-DOG/go-sqlmock"
|
|
"github.com/jinzhu/gorm"
|
|
"github.com/pkg/errors"
|
|
"github.com/stretchr/testify/assert"
|
|
"testing"
|
|
)
|
|
|
|
func TestGetGroupByID(t *testing.T) {
|
|
asserts := assert.New(t)
|
|
|
|
//找到用户组时
|
|
groupRows := sqlmock.NewRows([]string{"id", "name", "policies"}).
|
|
AddRow(1, "管理员", "[1]")
|
|
mock.ExpectQuery("^SELECT (.+)").WillReturnRows(groupRows)
|
|
|
|
group, err := GetGroupByID(1)
|
|
asserts.NoError(err)
|
|
asserts.Equal(Group{
|
|
Model: gorm.Model{
|
|
ID: 1,
|
|
},
|
|
Name: "管理员",
|
|
Policies: "[1]",
|
|
PolicyList: []uint{1},
|
|
}, group)
|
|
|
|
//未找到用户时
|
|
mock.ExpectQuery("^SELECT (.+)").WillReturnError(errors.New("not found"))
|
|
group, err = GetGroupByID(1)
|
|
asserts.Error(err)
|
|
asserts.Equal(Group{}, group)
|
|
}
|
|
|
|
func TestGroup_AfterFind(t *testing.T) {
|
|
asserts := assert.New(t)
|
|
|
|
testCase := Group{
|
|
Model: gorm.Model{
|
|
ID: 1,
|
|
},
|
|
Name: "管理员",
|
|
Policies: "[1]",
|
|
}
|
|
err := testCase.AfterFind()
|
|
asserts.NoError(err)
|
|
asserts.Equal(testCase.PolicyList, []uint{1})
|
|
|
|
testCase.Policies = "[1,2,3,4,5]"
|
|
err = testCase.AfterFind()
|
|
asserts.NoError(err)
|
|
asserts.Equal(testCase.PolicyList, []uint{1, 2, 3, 4, 5})
|
|
|
|
testCase.Policies = "[1,2,3,4,5"
|
|
err = testCase.AfterFind()
|
|
asserts.Error(err)
|
|
|
|
testCase.Policies = "[]"
|
|
err = testCase.AfterFind()
|
|
asserts.NoError(err)
|
|
asserts.Equal(testCase.PolicyList, []uint{})
|
|
}
|
|
|
|
func TestGroup_GetAria2Option(t *testing.T) {
|
|
asserts := assert.New(t)
|
|
group := Group{}
|
|
// 格式错误
|
|
{
|
|
group.Aria2Option = "123"
|
|
asserts.Equal([3]bool{false, false, false}, group.GetAria2Option())
|
|
}
|
|
|
|
// 正常
|
|
{
|
|
group.Aria2Option = "1,1,0"
|
|
asserts.Equal([3]bool{true, true, false}, group.GetAria2Option())
|
|
}
|
|
}
|
|
|
|
func TestGroup_BeforeSave(t *testing.T) {
|
|
asserts := assert.New(t)
|
|
group := Group{
|
|
PolicyList: []uint{1, 2, 3},
|
|
}
|
|
{
|
|
err := group.BeforeSave()
|
|
asserts.NoError(err)
|
|
asserts.Equal("[1,2,3]", group.Policies)
|
|
}
|
|
|
|
}
|