Skip to content

Commit

Permalink
PR feedback: test page size
Browse files Browse the repository at this point in the history
  • Loading branch information
vroldanbet committed Jul 15, 2024
1 parent 3179e65 commit 6a6c01a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
7 changes: 6 additions & 1 deletion internal/commands/permission.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,8 @@ func expandCmdFunc(cmd *cobra.Command, args []string) error {
return nil
}

var newLookupResourcesPageCallbackForTests func(readByPage uint)

func lookupResourcesCmdFunc(cmd *cobra.Command, args []string) error {
objectNS := args[0]
relation := args[1]
Expand Down Expand Up @@ -493,7 +495,10 @@ func lookupResourcesCmdFunc(cmd *cobra.Command, args []string) error {
}
}

if count == 0 || pageLimit == 0 {
if newLookupResourcesPageCallbackForTests != nil {
newLookupResourcesPageCallbackForTests(count)
}
if count == 0 || pageLimit == 0 || count < uint(pageLimit) {
log.Trace().Interface("request", request).Uint32("page-limit", pageLimit).Uint("count", totalCount).Send()
break
}
Expand Down
15 changes: 15 additions & 0 deletions internal/commands/permission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ func TestLookupResourcesCommand(t *testing.T) {
_, err = c.WriteRelationships(ctx, &v1.WriteRelationshipsRequest{Updates: updates})
require.NoError(t, err)

// we override this to obtain the results being printed and validate them
previous := console.Println
defer func() {
console.Println = previous
Expand All @@ -147,25 +148,39 @@ func TestLookupResourcesCommand(t *testing.T) {
count += len(values)
}

// use test callback to make sure pagination is correct
var receivedPageSizes []uint
newLookupResourcesPageCallbackForTests = func(readPageSize uint) {
receivedPageSizes = append(receivedPageSizes, readPageSize)
}
defer func() {
newLookupResourcesPageCallbackForTests = nil
}()

// test no page size, server computes returns all resources in one go
cmd := testLookupResourcesCommand(t, 0)
err = lookupResourcesCmdFunc(cmd, []string{"test/resource", "read", "test/user:1"})
require.NoError(t, err)
require.Equal(t, 10, count)
require.EqualValues(t, []uint{10}, receivedPageSizes)

// use page size same as number of elements
count = 0
receivedPageSizes = nil
cmd = testLookupResourcesCommand(t, 10)
err = lookupResourcesCmdFunc(cmd, []string{"test/resource", "read", "test/user:1"})
require.NoError(t, err)
require.Equal(t, 10, count)
require.EqualValues(t, []uint{10, 0}, receivedPageSizes)

// use odd page size
count = 0
receivedPageSizes = nil
cmd = testLookupResourcesCommand(t, 3)
err = lookupResourcesCmdFunc(cmd, []string{"test/resource", "read", "test/user:1"})
require.NoError(t, err)
require.Equal(t, 10, count)
require.EqualValues(t, []uint{3, 3, 3, 1}, receivedPageSizes)
}

func testLookupResourcesCommand(t *testing.T, limit uint32) *cobra.Command {
Expand Down

0 comments on commit 6a6c01a

Please sign in to comment.