forked from cloudreve/Cloudreve
-
Notifications
You must be signed in to change notification settings - Fork 0
/
group_test.go
77 lines (65 loc) · 1.59 KB
/
group_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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_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)
}
}