paginationtest

package
v0.13.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 20, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package paginationtest provides a reusable test suite for cursor-based pagination.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ListOpts

type ListOpts struct {
	Limit int
	Order string // "asc" or "desc"
	Next  string
	Prev  string
}

ListOpts contains options for a paginated list request.

type ListResult

type ListResult[T any] struct {
	Items []T
	Next  string
	Prev  string
}

ListResult contains the result of a paginated list request.

type Suite

type Suite[T any] struct {
	Name        string                                     // Store name for test output
	NewItem     func(index int) T                          // Create item at index (0=oldest)
	InsertMany  func(ctx context.Context, items []T) error // Batch insert
	List        func(ctx context.Context, opts ListOpts) (ListResult[T], error)
	GetID       func(T) string                  // Unique ID for comparison
	Matches     func(T) bool                    // Filter predicate (optional)
	AfterInsert func(ctx context.Context) error // Flush hook (optional)
	Cleanup     func(ctx context.Context) error // Reset state (optional)
}

Suite is a reusable test suite for cursor-based pagination.

Basic Usage

For testing pagination without filters, provide NewItem and InsertMany:

suite := paginationtest.Suite[*Event]{
    Name:       "pglogstore",
    NewItem:    func(i int) *Event { ... },
    InsertMany: func(ctx context.Context, items []*Event) error { ... },
    List:       func(ctx context.Context, opts ListOpts) (ListResult[*Event], error) { ... },
    GetID:      func(e *Event) string { return e.ID },
}
suite.Run(t)

Testing with Filters

When testing pagination with filters, use Matches to tell the suite which created items should appear in results:

suite := paginationtest.Suite[*Event]{
    NewItem: func(i int) *Event {
        typ := "order.created"
        if i%2 == 0 { typ = "order.updated" }
        return &Event{Type: typ, ...}
    },
    List: func(ctx context.Context, opts ListOpts) (ListResult[*Event], error) {
        return store.ListEvents(ctx, ListEventsRequest{EventType: "order.created", ...})
    },
    Matches: func(e *Event) bool {
        return e.Type == "order.created"  // mirrors filter in List
    },
}

Async/Eventually Consistent Stores

For stores with async writes, use AfterInsert:

suite := paginationtest.Suite[*Event]{
    AfterInsert: func(ctx context.Context) error {
        time.Sleep(100 * time.Millisecond)
        return nil
    },
}

func (Suite[T]) Run

func (s Suite[T]) Run(t *testing.T)

Run executes all pagination test cases.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL