Skip to content

Commit

Permalink
Merge pull request #11 from ConductorOne/marcos/fix/looping-query
Browse files Browse the repository at this point in the history
test: add pagination tests
  • Loading branch information
ggreer authored Jul 16, 2024
2 parents 31902d6 + 3795619 commit 941f02f
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 5 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: tests
on:
on:
workflow_dispatch:
push:
jobs:
Expand All @@ -26,16 +26,16 @@ jobs:
- name: Build baton-mongodb-atlas
run: go build ./cmd/baton-mongodb-atlas
- name: Run baton-mongodb-atlas-cmd
run: ./baton-mongodb-atlas
run: ./baton-mongodb-atlas
- name: Revoke grants
if: ${{ env.REVOKE_GRANT }} != ''
if: ${{ env.REVOKE_GRANT != ''}}
run: |
echo "Syncing resources..."
./baton-mongodb-atlas
echo "Testing revoking"
./baton-mongodb-atlas --log-level=debug --revoke-grant ${{ env.REVOKE_GRANT }}
- name: Grant entitlements
if: ${{ env.GRANT_ENTITLEMENT }} != '' && ${{ env.GRANT_PRINCIPAL }} != '' && ${{ env.GRANT_PRINCIPAL_TYPE }} != ''
if: ${{ env.GRANT_ENTITLEMENT != '' && env.GRANT_PRINCIPAL != '' && env.GRANT_PRINCIPAL_TYPE != '' }}
run: |
echo "Syncing resources..."
./baton-mongodb-atlas
Expand Down
118 changes: 118 additions & 0 deletions pkg/connector/pagination_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package connector

import (
"fmt"
"testing"

v2 "github.com/conductorone/baton-sdk/pb/c1/connector/v2"
"github.com/conductorone/baton-sdk/pkg/pagination"
"github.com/stretchr/testify/assert"
)

func TestPagination(t *testing.T) {
t.Run("isLastPage", func(t *testing.T) {
testCases := []struct {
count int
pageSize int
expected bool
}{
{1, 1, false},
{0, 1, true},
{2, 1, false},
}
for _, testCase := range testCases {
t.Run(fmt.Sprintf("%d %d", testCase.count, testCase.pageSize), func(t *testing.T) {
assert.Equal(t, testCase.expected, isLastPage(testCase.count, testCase.pageSize))
})
}
})

t.Run("getPageFromPageToken", func(t *testing.T) {
testCases := []struct {
pageToken string
expected int
}{
{"", 0},
{"0", 0},
{"1", 1},
}
for _, testCase := range testCases {
t.Run(testCase.pageToken, func(t *testing.T) {
page, err := getPageFromPageToken(testCase.pageToken)
assert.NoError(t, err)
assert.Equal(t, testCase.expected, page)
})
}
})

t.Run("parsePageToken", func(t *testing.T) {
testCases := []struct {
token string
resources []*v2.ResourceId
expected int
}{
{"", nil, 0},
{`{"current_state": {"token": "2"}}`, nil, 2},
{
`{"current_state": {"token": "2"}}`,
[]*v2.ResourceId{
{
ResourceType: "",
Resource: "",
},
},
2,
},
{
"",
[]*v2.ResourceId{
{
ResourceType: "",
Resource: "",
},
},
0,
},
}
for _, testCase := range testCases {
t.Run(testCase.token, func(t *testing.T) {
_, page, err := parsePageToken(testCase.token, testCase.resources...)
assert.NoError(t, err)
assert.Equal(t, testCase.expected, page)
})
}
})

t.Run("getPageTokenFromPage", func(t *testing.T) {
testCases := []struct {
nextPage int
expected string
}{
{
0,
`{"states":null,"current_state":{"token":"0","resource_type_id":"","resource_id":""}}`,
},
{
10,
`{"states":null,"current_state":{"token":"10","resource_type_id":"","resource_id":""}}`,
},
}
for _, testCase := range testCases {
t.Run(fmt.Sprintf("%d", testCase.nextPage), func(t *testing.T) {
bag := &pagination.Bag{}
bag.Push(pagination.PageState{})
token, err := getPageTokenFromPage(bag, testCase.nextPage)
assert.NoError(t, err)
assert.Equal(t, testCase.expected, token)
assert.Equal(t, fmt.Sprintf("%d", testCase.nextPage), bag.Current().Token)
})
}

t.Run("should error on empty bag", func(t *testing.T) {
pageToken, err := getPageTokenFromPage(&pagination.Bag{}, 0)

assert.NotNil(t, err)
assert.Equal(t, "", pageToken)
})
})
}
15 changes: 14 additions & 1 deletion pkg/connector/projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,20 @@ func (p *projectBuilder) List(ctx context.Context, parentResourceID *v2.Resource
return nil, "", nil, err
}

projects, _, err := p.client.ProjectsApi.ListProjects(ctx).PageNum(page).ItemsPerPage(resourcePageSize).IncludeCount(true).Execute()
logger := ctxzap.Extract(ctx)
logger.Debug(
"fetching a page of projects",
zap.Int("pageNum", page),
zap.Int("ItemsPerPage", resourcePageSize),
)

projects, _, err := p.client.
ProjectsApi.
ListProjects(ctx).
PageNum(page).
ItemsPerPage(resourcePageSize).
IncludeCount(true).
Execute()
if err != nil {
return nil, "", nil, err
}
Expand Down

0 comments on commit 941f02f

Please sign in to comment.