Skip to content

Commit

Permalink
Fix select page (#84)
Browse files Browse the repository at this point in the history
Fix select page
  • Loading branch information
keepsilence233 authored May 27, 2024
1 parent 75724bf commit ffcc4a9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
6 changes: 6 additions & 0 deletions gplus/dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ func (dao Dao[T]) NewQuery() (*QueryCond[T], *T) {
}

func NewPage[T any](current, size int) *Page[T] {
if current <= 0 {
current = 1
}
if size <= 0 {
size = 10
}
return &Page[T]{Current: current, Size: size}
}

Expand Down
20 changes: 20 additions & 0 deletions tests/dao_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,26 @@ func TestSelectPage(t *testing.T) {

}

func TestSelectPageDefaultPageParam(t *testing.T) {
deleteOldData()
users := getUsers()
gplus.InsertBatch[User](users)

query, model := gplus.NewQuery[User]()
page := gplus.NewPage[User](0, 0)
query.Eq(&model.Username, users[0].Username).Or().Eq(&model.Username, users[5].Username)
resultPage, db := gplus.SelectPage(page, query)
if db.Error != nil {
t.Errorf("errors happened when selectByIds : %v", db.Error)
}
if resultPage.Total != 2 {
t.Errorf("page total expects: %v, got %v", 2, resultPage.Total)
}

AssertObjEqual(t, page.Current, 1)
AssertObjEqual(t, page.Size, 10)
}

func TestSelectPageGeneric2(t *testing.T) {
deleteOldData()
users := getUsers()
Expand Down

0 comments on commit ffcc4a9

Please sign in to comment.