forked from gobuffalo/pop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pagination_test.go
72 lines (56 loc) · 1.36 KB
/
pagination_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
package pop_test
import (
"net/url"
"reflect"
"testing"
"github.com/markbates/going/nulls"
"github.com/markbates/pop"
"github.com/stretchr/testify/require"
)
func Test_NewPaginator(t *testing.T) {
a := require.New(t)
p := pop.NewPaginator(1, 10)
a.Equal(p.Offset, 0)
p = pop.NewPaginator(2, 10)
a.Equal(p.Offset, 10)
p = pop.NewPaginator(2, 30)
a.Equal(p.Offset, 30)
}
func Test_NewPaginatorFromParams(t *testing.T) {
a := require.New(t)
params := url.Values{}
p := pop.NewPaginatorFromParams(params)
a.Equal(p.Page, 1)
a.Equal(p.PerPage, 20)
params.Set("page", "2")
p = pop.NewPaginatorFromParams(params)
a.Equal(p.Page, 2)
a.Equal(p.PerPage, 20)
params.Set("per_page", "30")
p = pop.NewPaginatorFromParams(params)
a.Equal(p.Page, 2)
a.Equal(p.PerPage, 30)
}
func Test_Pagination(t *testing.T) {
transaction(func(tx *pop.Connection) {
a := require.New(t)
for _, name := range []string{"Mark", "Joe", "Jane"} {
user := User{Name: nulls.NewString(name)}
err := tx.Create(&user)
a.NoError(err)
}
u := Users{}
q := tx.Paginate(1, 2)
err := q.All(&u)
a.NoError(err)
a.Equal(len(u), 2)
p := q.Paginator
a.Equal(p.CurrentEntriesSize, 2)
a.Equal(p.TotalEntriesSize, 3)
a.Equal(p.TotalPages, 2)
u = Users{}
err = tx.Where("name = 'Mark'").All(&u)
a.NoError(err)
a.Equal(reflect.ValueOf(&u).Elem().Len(), 1)
})
}