Skip to content

Commit

Permalink
Merge pull request #2075 from stakwork/feat/pagination-params-test
Browse files Browse the repository at this point in the history
Feat: test GetPaginationParams
  • Loading branch information
elraphty authored Dec 3, 2024
2 parents 3b261f4 + b5e43b2 commit 119ed6f
Show file tree
Hide file tree
Showing 2 changed files with 174 additions and 5 deletions.
13 changes: 8 additions & 5 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@ func GetPaginationParams(r *http.Request) (int, int, string, string, string) {
}

keys := r.URL.Query()
page := keys.Get("page")
limit := keys.Get("limit")
sortBy := keys.Get("sortBy")
direction := keys.Get("direction")
// trim spaces
page := strings.TrimSpace(keys.Get("page"))
// trim spaces
limit := strings.TrimSpace(keys.Get("limit"))
// convert to lowercase and trim spaces
sortBy := strings.ToLower(strings.TrimSpace(keys.Get("sortBy")))
direction := strings.ToLower(strings.TrimSpace(keys.Get("direction")))
search := keys.Get("search")

// convert string to int
Expand All @@ -33,7 +36,7 @@ func GetPaginationParams(r *http.Request) (int, int, string, string, string) {
if sortBy == "" {
sortBy = "created"
}
if direction == "" {
if direction == "" || direction != "asc" {
direction = "desc"
}

Expand Down
166 changes: 166 additions & 0 deletions utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package utils

import (
"fmt"
"net/http"
"net/url"
"strings"
"testing"

Expand Down Expand Up @@ -99,3 +101,167 @@ func TestBuildV2KeysendBodyData(t *testing.T) {
})
}
}

func TestGetPaginationParams(t *testing.T) {
tests := []struct {
name string
query string
expectedOffset int
expectedLimit int
expectedSortBy string
expectedDir string
expectedSearch string
}{
{
name: "Standard Input with All Parameters Present",
query: "page=2&limit=10&sortBy=created&direction=asc&search=test",
expectedOffset: 10,
expectedLimit: 10,
expectedSortBy: "created",
expectedDir: "asc",
expectedSearch: "test",
},
{
name: "Standard Input with Default Parameters",
query: "page=1&limit=5",
expectedOffset: 0,
expectedLimit: 5,
expectedSortBy: "created",
expectedDir: "desc",
expectedSearch: "",
},
{
name: "Minimum Values for Page and Limit",
query: "page=1&limit=1",
expectedOffset: 0,
expectedLimit: 1,
expectedSortBy: "created",
expectedDir: "desc",
expectedSearch: "",
},
{
name: "Zero Values for Page and Limit",
query: "page=0&limit=0",
expectedOffset: 0,
expectedLimit: 1,
expectedSortBy: "created",
expectedDir: "desc",
expectedSearch: "",
},
{
name: "Negative Values for Page and Limit",
query: "page=-1&limit=-10",
expectedOffset: 0,
expectedLimit: -10,
expectedSortBy: "created",
expectedDir: "desc",
expectedSearch: "",
},
{
name: "Non-Integer Values for Page and Limit",
query: "page=abc&limit=xyz",
expectedOffset: 0,
expectedLimit: 1,
expectedSortBy: "created",
expectedDir: "desc",
expectedSearch: "",
},
{
name: "Null Request",
query: "",
expectedOffset: 0,
expectedLimit: 1,
expectedSortBy: "created",
expectedDir: "desc",
expectedSearch: "",
},
{
name: "Large Values for Page and Limit",
query: "page=10000&limit=1000",
expectedOffset: 9999000,
expectedLimit: 1000,
expectedSortBy: "created",
expectedDir: "desc",
expectedSearch: "",
},
{
name: "Empty Query Parameters",
query: "",
expectedOffset: 0,
expectedLimit: 1,
expectedSortBy: "created",
expectedDir: "desc",
expectedSearch: "",
},
{
name: "Only Search Parameter Present",
query: "search=example",
expectedOffset: 0,
expectedLimit: 1,
expectedSortBy: "created",
expectedDir: "desc",
expectedSearch: "example",
},
{
name: "Invalid Direction Value",
query: "direction=upwards",
expectedOffset: 0,
expectedLimit: 1,
expectedSortBy: "created",
expectedDir: "desc",
expectedSearch: "",
},
{
name: "Invalid SortBy Value",
query: "sortBy=unknown",
expectedOffset: 0,
expectedLimit: 1,
expectedSortBy: "unknown",
expectedDir: "desc",
expectedSearch: "",
},
{
name: "Mixed Valid and Invalid Parameters",
query: "page=3&limit=abc&sortBy=unknown&direction=upwards",
expectedOffset: 2,
expectedLimit: 1,
expectedSortBy: "unknown",
expectedDir: "desc",
expectedSearch: "",
},
{
name: "Whitespace in Parameters",
query: "page= 2 &limit= 10&sortBy= created &direction= asc ",
expectedOffset: 10,
expectedLimit: 10,
expectedSortBy: "created",
expectedDir: "asc",
expectedSearch: "",
},
{
name: "Case Sensitivity in Parameters",
query: "sortBy=CREATED&direction=ASC",
expectedOffset: 0,
expectedLimit: 1,
expectedSortBy: "created",
expectedDir: "asc",
expectedSearch: "",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req := &http.Request{
URL: &url.URL{
RawQuery: tt.query,
},
}
offset, limit, sortBy, direction, search := GetPaginationParams(req)
assert.Equal(t, tt.expectedOffset, offset)
assert.Equal(t, tt.expectedLimit, limit)
assert.Equal(t, tt.expectedSortBy, sortBy)
assert.Equal(t, tt.expectedDir, direction)
assert.Equal(t, tt.expectedSearch, search)
})
}
}

0 comments on commit 119ed6f

Please sign in to comment.