Skip to content

Commit

Permalink
Add tests & change count to int64
Browse files Browse the repository at this point in the history
  • Loading branch information
raphaelvigee committed Nov 21, 2020
1 parent a3428b6 commit 3b73e8c
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func main() {
CreatedAt: base.Add(6 * time.Hour),
})

// Define the pagination criterias
// Define the pagination criteria
pg := paginator.New(paginator.Options{
Driver: gorm.Driver{
Columns: []*gorm.Column{
Expand Down
2 changes: 1 addition & 1 deletion driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type Driver interface {

type Executor interface {
Query(dst interface{}) error
Count() (uint64, error)
Count() (int64, error)
}

type PageInfo struct {
Expand Down
4 changes: 2 additions & 2 deletions driver/gorm/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ func (g gormDriverPage) Query(dst interface{}) error {
return g.tx.Find(dst).Error
}

func (g gormDriverPage) Count() (uint64, error) {
func (g gormDriverPage) Count() (int64, error) {
if g.tx == nil {
return 0, nil
}

var c int64
err := g.tx.Count(&c).Error

return uint64(c), err
return c, err
}

func (g gormDriverPage) Info() driver.PageInfo {
Expand Down
12 changes: 10 additions & 2 deletions driver/gorm/paginator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ func testPaginator(t *testing.T, columns []*Column, typ cursor.Type, limit int,
ec, _ := res.Cursor(int64(limit-1))
assert.Equal(t, ec, res.PageInfo.EndCursor)

c, err := res.Count()
assert.NoError(t, err)
assert.Equal(t, int64(len(s.names)), c)

var users []User
err = res.Query(&users)
assert.NoError(t, err)
Expand Down Expand Up @@ -153,17 +157,21 @@ func TestFactory_Empty(t *testing.T) {
Driver: Driver{Columns: simpleColumns},
})

c, err := pg.Cursor("", cursor.After, 2)
csr, err := pg.Cursor("", cursor.After, 2)
assert.NoError(t, err)

res, err := pg.Paginate(c, tx)
res, err := pg.Paginate(csr, tx)
assert.NoError(t, err)

assert.False(t, res.PageInfo.HasPreviousPage)
assert.False(t, res.PageInfo.HasNextPage)
assert.Empty(t, res.PageInfo.StartCursor)
assert.Empty(t, res.PageInfo.EndCursor)

c, err := res.Count()
assert.NoError(t, err)
assert.Equal(t, int64(0), c)

var users []User
err = res.Query(&users)
assert.NoError(t, err)
Expand Down

0 comments on commit 3b73e8c

Please sign in to comment.