From b5e43b2e915f0bacf840ab1e4e6c7b914008664c Mon Sep 17 00:00:00 2001 From: elraphty Date: Tue, 3 Dec 2024 15:22:27 +0100 Subject: [PATCH] feat: test GetPaginationParams --- utils/utils.go | 13 ++-- utils/utils_test.go | 166 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 174 insertions(+), 5 deletions(-) diff --git a/utils/utils.go b/utils/utils.go index 541367879..790f32e21 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -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 @@ -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" } diff --git a/utils/utils_test.go b/utils/utils_test.go index 7f5cb74dd..8ebccc319 100644 --- a/utils/utils_test.go +++ b/utils/utils_test.go @@ -2,6 +2,8 @@ package utils import ( "fmt" + "net/http" + "net/url" "strings" "testing" @@ -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) + }) + } +}