Skip to content

Commit

Permalink
Do not wrap a wrapped schema store
Browse files Browse the repository at this point in the history
Problem:
Pagination is broken when the schema store is wrapped twice

Solution:
Do not wrap a wrapped schema store
  • Loading branch information
gitlawr authored and Craig Jellick committed May 1, 2019
1 parent 5bf0d4b commit f18ee0d
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
4 changes: 4 additions & 0 deletions store/wrapper/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import (
)

func Wrap(store types.Store) types.Store {
if _, ok := store.(*StoreWrapper); ok {
return store
}

return &StoreWrapper{
store: store,
}
Expand Down
52 changes: 52 additions & 0 deletions store/wrapper/wrapper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package wrapper

import (
"testing"

"github.com/rancher/norman/api/handler"
"github.com/rancher/norman/parse"
"github.com/rancher/norman/store/empty"
"github.com/rancher/norman/types"
"github.com/stretchr/testify/assert"
)

type testStore struct {
empty.Store
}

func (t *testStore) List(apiContext *types.APIContext, schema *types.Schema, opt *types.QueryOptions) ([]map[string]interface{}, error) {
return []map[string]interface{}{{"1": "1"}, {"2": "2"}, {"3": "3"}}, nil
}

func TestWrap(t *testing.T) {
store := &testStore{}
limit := int64(1)
opt := &types.QueryOptions{
Pagination: &types.Pagination{
Limit: &limit,
},
}
apiContext := &types.APIContext{
SubContextAttributeProvider: &parse.DefaultSubContextAttributeProvider{},
QueryFilter: handler.QueryFilter,
Pagination: opt.Pagination,
}

wrapped := Wrap(store)
if _, err := wrapped.List(apiContext, &types.Schema{}, opt); err != nil {
t.Fatal(err)
}
assert.True(t, apiContext.Pagination.Partial)
assert.Equal(t, int64(3), *apiContext.Pagination.Total)
assert.Equal(t, int64(1), *apiContext.Pagination.Limit)

wrappedTwice := Wrap(wrapped)
apiContext.Pagination = opt.Pagination
if _, err := wrappedTwice.List(apiContext, &types.Schema{}, opt); err != nil {
t.Fatal(err)
}
assert.True(t, apiContext.Pagination.Partial)
assert.Equal(t, int64(3), *apiContext.Pagination.Total)
assert.Equal(t, int64(1), *apiContext.Pagination.Limit)

}

0 comments on commit f18ee0d

Please sign in to comment.