Skip to content

Commit

Permalink
Refactor test
Browse files Browse the repository at this point in the history
  • Loading branch information
jarrel-b committed Aug 21, 2023
1 parent efd4cea commit e000e6c
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 24 deletions.
62 changes: 50 additions & 12 deletions publicapi/pagination.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,14 @@ type timeIDPagingParams struct {

func (p *timeIDPaginator) paginate(before *string, after *string, first *int, last *int) ([]any, PageInfo, error) {
queryFunc := func(limit int32, pagingForward bool) ([]any, error) {
beforeCur := timeIDCursor{Time: defaultCursorBeforeTime, ID: defaultCursorBeforeID}
afterCur := timeIDCursor{Time: defaultCursorAfterTime, ID: defaultCursorAfterID}
beforeCur := timeIDCursor{
Time: defaultCursorBeforeTime,
ID: defaultCursorBeforeID,
}
afterCur := timeIDCursor{
Time: defaultCursorAfterTime,
ID: defaultCursorAfterID,
}

if before != nil {
if err := beforeCur.Unpack(*before); err != nil {
Expand Down Expand Up @@ -279,8 +285,14 @@ func (p *sharedFollowersPaginator) paginate(before *string, after *string, first
queryFunc := func(limit int32, pagingForward bool) ([]interface{}, error) {
// The shared followers query orders results in descending order when
// paging forward (vs. ascending order which is more typical).
beforeCur := timeIDCursor{Time: time.Date(1970, 1, 1, 1, 1, 1, 1, time.UTC), ID: defaultCursorBeforeID}
afterCur := timeIDCursor{Time: time.Date(3000, 1, 1, 1, 1, 1, 1, time.UTC), ID: defaultCursorAfterID}
beforeCur := timeIDCursor{
Time: time.Date(1970, 1, 1, 1, 1, 1, 1, time.UTC),
ID: defaultCursorBeforeID,
}
afterCur := timeIDCursor{
Time: time.Date(3000, 1, 1, 1, 1, 1, 1, time.UTC),
ID: defaultCursorAfterID,
}

if before != nil {
if err := beforeCur.Unpack(*before); err != nil {
Expand Down Expand Up @@ -346,8 +358,18 @@ type sharedContractsPaginator struct {

func (p *sharedContractsPaginator) paginate(before *string, after *string, first *int, last *int) ([]interface{}, PageInfo, error) {
queryFunc := func(limit int32, pagingForward bool) ([]interface{}, error) {
beforeCur := boolBootIntIDCursor{Bool1: false, Bool2: false, Int: -1, ID: defaultCursorBeforeID}
afterCur := boolBootIntIDCursor{Bool1: true, Bool2: true, Int: math.MaxInt32, ID: defaultCursorAfterID}
beforeCur := boolBootIntIDCursor{
Bool1: false,
Bool2: false,
Int: -1,
ID: defaultCursorBeforeID,
}
afterCur := boolBootIntIDCursor{
Bool1: true,
Bool2: true,
Int: math.MaxInt32,
ID: defaultCursorAfterID,
}

if before != nil {
if err := beforeCur.Unpack(*before); err != nil {
Expand Down Expand Up @@ -411,8 +433,16 @@ type boolTimeIDPaginator struct {

func (p *boolTimeIDPaginator) paginate(before *string, after *string, first *int, last *int) ([]interface{}, PageInfo, error) {
queryFunc := func(limit int32, pagingForward bool) ([]interface{}, error) {
beforeCur := boolTimeIDCursor{Bool: true, Time: defaultCursorBeforeTime, ID: defaultCursorBeforeID}
afterCur := boolTimeIDCursor{Bool: false, Time: defaultCursorAfterTime, ID: defaultCursorAfterID}
beforeCur := boolTimeIDCursor{
Bool: true,
Time: defaultCursorBeforeTime,
ID: defaultCursorBeforeID,
}
afterCur := boolTimeIDCursor{
Bool: false,
Time: defaultCursorAfterTime,
ID: defaultCursorAfterID,
}

if before != nil {
if err := beforeCur.Unpack(*before); err != nil {
Expand Down Expand Up @@ -561,8 +591,8 @@ func (p *positionPaginator) paginate(before *string, after *string, first *int,
CurAfterPos: defaultCursorAfterPosition,
}

beforeCur := positionCursor{}
afterCur := positionCursor{}
var beforeCur positionCursor
var afterCur positionCursor

for _, opt := range opts {
opt(&args)
Expand Down Expand Up @@ -628,8 +658,16 @@ type intTimeIDPagingParams struct {

func (p *intTimeIDPaginator) paginate(before *string, after *string, first *int, last *int) ([]interface{}, PageInfo, error) {
queryFunc := func(limit int32, pagingForward bool) ([]interface{}, error) {
beforeCur := intTimeIDCursor{Int: math.MaxInt32, Time: defaultCursorBeforeTime, ID: defaultCursorBeforeID}
afterCur := intTimeIDCursor{Int: 0, Time: defaultCursorAfterTime, ID: defaultCursorAfterID}
beforeCur := intTimeIDCursor{
Int: math.MaxInt32,
Time: defaultCursorBeforeTime,
ID: defaultCursorBeforeID,
}
afterCur := intTimeIDCursor{
Int: 0,
Time: defaultCursorAfterTime,
ID: defaultCursorAfterID,
}

if before != nil {
if err := beforeCur.Unpack(*before); err != nil {
Expand Down
32 changes: 20 additions & 12 deletions publicapi/pagination_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,26 @@ import (
func TestMain(t *testing.T) {
t.Run("test cursor pagination", func(t *testing.T) {
t.Run("cursor encodes expected types", func(t *testing.T) {
_, err := pack(
time.Now(),
true,
1,
int64(1),
uint64(1),
"id",
persist.DBID("id"),
[]persist.DBID{"id0", "id1"},
[]persist.FeedEntityType{0, 1},
)
assert.NoError(t, err)
types := []struct {
title string
typ any
}{
{title: "can encode time", typ: time.Now()},
{title: "can encode bool", typ: true},
{title: "can encode int", typ: 1},
{title: "can encode int64", typ: int64(1)},
{title: "can encode uint64", typ: uint64(1)},
{title: "can encode stirng", typ: "id"},
{title: "can encode dbid", typ: persist.DBID("id")},
{title: "can encode slice of dbids", typ: []persist.DBID{"id0", "id1"}},
{title: "can encode slice of feed entity types", typ: []persist.FeedEntityType{0, 1}},
}
for _, typ := range types {
t.Run(typ.title, func(t *testing.T) {
_, err := pack(typ.typ)
assert.NoError(t, err)
})
}
})

t.Run("cursor pagination returns expected edges", func(t *testing.T) {
Expand Down

0 comments on commit e000e6c

Please sign in to comment.