-
Notifications
You must be signed in to change notification settings - Fork 189
/
context_test.go
150 lines (117 loc) · 3.82 KB
/
context_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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package admin_test
import (
"fmt"
"testing"
"github.com/qor/admin"
. "github.com/qor/admin/tests/dummy"
"github.com/qor/qor"
"github.com/qor/roles"
)
// Template helpers test
func TestUrlForAdmin(t *testing.T) {
context := &admin.Context{Admin: Admin}
rootLink := context.URLFor(Admin)
if rootLink != "/admin" {
t.Error("Admin link not generated by URLFor")
}
}
func TestUrlForResource(t *testing.T) {
context := &admin.Context{Admin: Admin}
user := Admin.GetResource("User")
userLink := context.URLFor(user)
if userLink != "/admin/users" {
t.Error("resource link not generated by URLFor")
}
}
type Store struct {
ID string `gorm:"primary_key" sql:"type:varchar(20)"`
Name string
}
func TestUrlForResourceWithSpecialPrimaryKey(t *testing.T) {
db.AutoMigrate(&Store{})
context := &admin.Context{Admin: Admin, Context: &qor.Context{}}
context.SetDB(db)
storeRes := Admin.AddResource(&Store{}, &admin.Config{Permission: roles.Allow(roles.CRUD, roles.Anyone)})
s := Store{ID: "00022 %alert%29", Name: "test"}
if err := db.Save(&s).Error; err != nil {
t.Fatal(err)
}
storeLink := context.URLFor(s, storeRes)
if storeLink != "/admin/stores/00022%20%25alert%2529" {
t.Error("special primary key is not escaped properly")
}
}
func TestUrlForResourceName(t *testing.T) {
user := &User{Name: "test"}
db.Create(&user)
context := &admin.Context{Admin: Admin, Context: &qor.Context{}}
context.SetDB(db)
userLink := context.URLFor(user)
if userLink != "/admin/users/"+fmt.Sprintf("%v", user.ID) {
t.Error("resource link not generated by URLFor")
}
}
func TestPagination(t *testing.T) {
context := &admin.Context{Admin: Admin}
context.Resource = &admin.Resource{Config: &admin.Config{PageCount: 10}}
context.Searcher = &admin.Searcher{Context: context}
// Test no pagination if total result count is less than PageCount
for _, count := range []int{8, 10} {
context.Searcher.Pagination.Total = count
if context.Pagination() != nil {
t.Error(fmt.Sprintf("Don't display pagination if only has one page (%v)", count))
}
}
context.Searcher.Pagination.CurrentPage = 3
if context.Pagination() == nil {
t.Error("Should show pagination for page without records")
}
// Test current page 1
context.Searcher.Pagination.Total = 1000
context.Searcher.Pagination.Pages = 10
context.Searcher.Pagination.CurrentPage = 1
pages := context.Pagination().Pages
if !pages[0].Current {
t.Error("first page not set as current page")
}
if !pages[len(pages)-2].IsNext && pages[len(pages)-2].Page != 2 {
t.Error("Should have next page arrow")
}
// +1 for "Next page" link which is a "Page" too
// +1 for "Last page"
if len(pages) != 8+1+1 {
t.Error("visible pages in current context beyond the bound of VISIBLE_PAGE_COUNT")
}
// Test current page 8 => the length between start and end less than MAX_VISIBLE_PAGES
context.Searcher.Pagination.Pages = 10
context.Searcher.Pagination.CurrentPage = 8
pages = context.Pagination().Pages
if !pages[7].Current {
t.Error("visible previous pages count incorrect")
}
if !pages[1].IsPrevious && pages[1].Page != 7 {
t.Error("Should have previous page arrow")
}
// +1 for "Prev"
// +1 for "First page"
if len(pages) != 8+1+1 {
t.Error("visible pages in current context beyond the bound of VISIBLE_PAGE_COUNT")
}
// Test current page at last
context.Searcher.Pagination.Pages = 10
context.Searcher.Pagination.CurrentPage = 10
pages = context.Pagination().Pages
if !pages[len(pages)-1].Current {
t.Error("last page is not the current page")
}
if len(pages) != 8+2 {
t.Error("visible pages count is incorrect")
}
// Test current page at last but total page count less than VISIBLE_PAGE_COUNT
context.Searcher.Pagination.Pages = 5
context.Searcher.Pagination.CurrentPage = 5
pages = context.Pagination().Pages
if len(pages) != 5 {
t.Error("incorrect pages count")
}
}