Skip to content

Commit

Permalink
Fix: Keyset order by default (#19)
Browse files Browse the repository at this point in the history
* fix order keyset
  • Loading branch information
roneli committed May 30, 2024
1 parent aab0680 commit 2249818
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 7 deletions.
67 changes: 61 additions & 6 deletions execute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ type User struct {
Email string
}

type randomNumbers struct {
ID int64 `db:"id"`
Number int64 `db:"number"`
}

func TestSelectOne(t *testing.T) {
ctx := context.Background()
conn, err := pgx.Connect(ctx, testPostgresURI)
Expand Down Expand Up @@ -201,6 +206,51 @@ func TestInsertMany(t *testing.T) {
}
}

func TestSelectPaginationWithManyRows(t *testing.T) {
ctx := context.Background()
conn, err := pgx.Connect(ctx, testPostgresURI)
require.Nil(t, err)
defer func() {
err := conn.Close(context.Background())
require.Nil(t, err)
}()
tableTests := []struct {
name string
tableName string
paginationOptions *goqux.PaginationOptions
options []goqux.SelectOption
expectedPages int
}{
{
name: "paginated_select_keyset_with_many_rows",
tableName: "random_numbers",
paginationOptions: &goqux.PaginationOptions{
PageSize: 10,
KeySet: []string{"ID"},
},
expectedPages: 10,
},
}
for _, tt := range tableTests {
t.Run(tt.name, func(t *testing.T) {
paginator, err := goqux.SelectPagination[randomNumbers](ctx, conn, tt.tableName, tt.paginationOptions, tt.options...)
require.Nil(t, err)
totalPages := 0
previousId := int64(0)
for paginator.HasMorePages() {
models, err := paginator.NextPage()
require.Nil(t, err)
for _, i := range models {
require.Greater(t, i.ID, previousId)
previousId = i.ID
}
totalPages += 1
}
require.GreaterOrEqual(t, totalPages, tt.expectedPages)
})
}
}

func TestSelectPagination(t *testing.T) {
ctx := context.Background()
conn, err := pgx.Connect(ctx, testPostgresURI)
Expand All @@ -211,13 +261,15 @@ func TestSelectPagination(t *testing.T) {
}()
tableTests := []struct {
name string
tableName string
paginationOptions *goqux.PaginationOptions
options []goqux.SelectOption
expectedResult interface{}
expectedPages int
}{
{
name: "paginated_select_single_page",
name: "paginated_select_single_page",
tableName: "select_users",
paginationOptions: &goqux.PaginationOptions{
PageSize: 100,
},
Expand All @@ -226,7 +278,8 @@ func TestSelectPagination(t *testing.T) {
expectedPages: 1,
},
{
name: "paginated_select",
name: "paginated_select",
tableName: "select_users",
paginationOptions: &goqux.PaginationOptions{
PageSize: 1,
},
Expand All @@ -235,7 +288,8 @@ func TestSelectPagination(t *testing.T) {
expectedPages: 3,
},
{
name: "paginated_select_with_filters",
name: "paginated_select_with_filters",
tableName: "select_users",
paginationOptions: &goqux.PaginationOptions{
PageSize: 1,
},
Expand All @@ -244,7 +298,8 @@ func TestSelectPagination(t *testing.T) {
expectedPages: 2,
},
{
name: "paginated_select_keyset",
name: "paginated_select_keyset",
tableName: "select_users",
paginationOptions: &goqux.PaginationOptions{
PageSize: 1,
KeySet: []string{"ID"},
Expand All @@ -255,7 +310,7 @@ func TestSelectPagination(t *testing.T) {
}
for _, tt := range tableTests {
t.Run(tt.name, func(t *testing.T) {
paginator, err := goqux.SelectPagination[User](ctx, conn, "select_users", tt.paginationOptions, tt.options...)
paginator, err := goqux.SelectPagination[User](ctx, conn, tt.tableName, tt.paginationOptions, tt.options...)
require.Nil(t, err)
allModels := make([]User, 0)
totalPages := 0
Expand All @@ -273,7 +328,7 @@ func TestSelectPagination(t *testing.T) {
}
}

func TestStopOnPaginations(t *testing.T) {
func TestStopOnPagination(t *testing.T) {
ctx := context.Background()
conn, err := pgx.Connect(ctx, testPostgresURI)
require.Nil(t, err)
Expand Down
3 changes: 3 additions & 0 deletions select.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ func WithSelectOffset(offset uint) SelectOption {
func WithKeySet(columns []string, values []any) SelectOption {
return func(table exp.IdentifierExpression, s *goqu.SelectDataset) *goqu.SelectDataset {
if values == nil {
for _, c := range columns {
s = s.Order(table.Col(strcase.ToSnake(c)).Asc())
}
return s
}
for i, c := range columns {
Expand Down
17 changes: 16 additions & 1 deletion testing/init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,19 @@ CREATE TABLE IF NOT EXISTS "insert_posts"
"created_at" TIMESTAMP NOT NULL DEFAULT NOW(),
"updated_at" TIMESTAMP NOT NULL DEFAULT NOW(),
"user_id" INTEGER NOT NULL REFERENCES "users" ("id")
);
);
-- create a table with two columns one is a serial number and another is a random number, we will then
-- generate a series and insert 100 rows into the table
CREATE TABLE IF NOT EXISTS "random_numbers"
(
"id" SERIAL PRIMARY KEY,
"number" INTEGER NOT NULL
);

-- Insert 100 rows into the random_numbers table
DO $$
BEGIN
FOR i IN 1..100 LOOP
INSERT INTO "random_numbers" ("number") VALUES (floor(random() * 1000));
END LOOP;
END $$;

0 comments on commit 2249818

Please sign in to comment.