Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Unit Tests] - GetPaginationParams #2011

Open
tomsmith8 opened this issue Nov 29, 2024 · 0 comments
Open

[Unit Tests] - GetPaginationParams #2011

tomsmith8 opened this issue Nov 29, 2024 · 0 comments

Comments

@tomsmith8
Copy link

Unit Test Coverage for " GetPaginationParams"


Stakwork Run


Unit Test Code


package utils_test

import (
  "net/http"
  "net/http/httptest"
  "testing"

  "github.com/stretchr/testify/assert"
  "yourmodule/utils" // Replace with the actual module path
)

func TestGetPaginationParams_StandardInputWithAllParametersPresent(t *testing.T) {
  req := httptest.NewRequest("GET", "/?page=2&limit=10&sortBy=created&direction=asc&search=test", nil)
  offset, limit, sortBy, direction, search := utils.GetPaginationParams(req)

  assert.Equal(t, 10, offset)
  assert.Equal(t, 10, limit)
  assert.Equal(t, "created", sortBy)
  assert.Equal(t, "asc", direction)
  assert.Equal(t, "test", search)
}

func TestGetPaginationParams_StandardInputWithDefaultParameters(t *testing.T) {
  req := httptest.NewRequest("GET", "/", nil)
  offset, limit, sortBy, direction, search := utils.GetPaginationParams(req)

  assert.Equal(t, 0, offset)
  assert.Equal(t, 1, limit)
  assert.Equal(t, "created", sortBy)
  assert.Equal(t, "desc", direction)
  assert.Equal(t, "", search)
}

func TestGetPaginationParams_EdgeCaseWithZeroPageAndLimit(t *testing.T) {
  req := httptest.NewRequest("GET", "/?page=0&limit=0", nil)
  offset, limit, sortBy, direction, search := utils.GetPaginationParams(req)

  assert.Equal(t, 0, offset)
  assert.Equal(t, 1, limit)
  assert.Equal(t, "created", sortBy)
  assert.Equal(t, "desc", direction)
  assert.Equal(t, "", search)
}

func TestGetPaginationParams_EdgeCaseWithNegativePageAndLimit(t *testing.T) {
  req := httptest.NewRequest("GET", "/?page=-1&limit=-5", nil)
  offset, limit, sortBy, direction, search := utils.GetPaginationParams(req)

  assert.Equal(t, 0, offset)
  assert.Equal(t, 1, limit)
  assert.Equal(t, "created", sortBy)
  assert.Equal(t, "desc", direction)
  assert.Equal(t, "", search)
}

func TestGetPaginationParams_EdgeCaseWithLargePageAndLimit(t *testing.T) {
  req := httptest.NewRequest("GET", "/?page=1000000&limit=1000000", nil)
  offset, limit, sortBy, direction, search := utils.GetPaginationParams(req)

  assert.Equal(t, 999999000000, offset)
  assert.Equal(t, 1000000, limit)
  assert.Equal(t, "created", sortBy)
  assert.Equal(t, "desc", direction)
  assert.Equal(t, "", search)
}

func TestGetPaginationParams_InvalidDataTypesForPageAndLimit(t *testing.T) {
  req := httptest.NewRequest("GET", "/?page=abc&limit=xyz", nil)
  offset, limit, sortBy, direction, search := utils.GetPaginationParams(req)

  assert.Equal(t, 0, offset)
  assert.Equal(t, 1, limit)
  assert.Equal(t, "created", sortBy)
  assert.Equal(t, "desc", direction)
  assert.Equal(t, "", search)
}

func TestGetPaginationParams_NullRequestObject(t *testing.T) {
  offset, limit, sortBy, direction, search := utils.GetPaginationParams(nil)

  assert.Equal(t, 0, offset)
  assert.Equal(t, 1, limit)
  assert.Equal(t, "created", sortBy)
  assert.Equal(t, "desc", direction)
  assert.Equal(t, "", search)
}

func TestGetPaginationParams_SpecialCharactersInSearchParameter(t *testing.T) {
  req := httptest.NewRequest("GET", "/?search=%20%21%40%23", nil)
  offset, limit, sortBy, direction, search := utils.GetPaginationParams(req)

  assert.Equal(t, 0, offset)
  assert.Equal(t, 1, limit)
  assert.Equal(t, "created", sortBy)
  assert.Equal(t, "desc", direction)
  assert.Equal(t, " !@#", search)
}

func TestGetPaginationParams_EmptyStringsForSortByAndDirection(t *testing.T) {
  req := httptest.NewRequest("GET", "/?sortBy=&direction=", nil)
  offset, limit, sortBy, direction, search := utils.GetPaginationParams(req)

  assert.Equal(t, 0, offset)
  assert.Equal(t, 1, limit)
  assert.Equal(t, "created", sortBy)
  assert.Equal(t, "desc", direction)
  assert.Equal(t, "", search)
}

func TestGetPaginationParams_InvalidSortByAndDirectionValues(t *testing.T) {
  req := httptest.NewRequest("GET", "/?sortBy=invalid&direction=invalid", nil)
  offset, limit, sortBy, direction, search := utils.GetPaginationParams(req)

  assert.Equal(t, 0, offset)
  assert.Equal(t, 1, limit)
  assert.Equal(t, "created", sortBy)
  assert.Equal(t, "desc", direction)
  assert.Equal(t, "", search)
}

func TestGetPaginationParams_WhitespaceInParameters(t *testing.T) {
  req := httptest.NewRequest("GET", "/?page= &limit= &sortBy= &direction= ", nil)
  offset, limit, sortBy, direction, search := utils.GetPaginationParams(req)

  assert.Equal(t, 0, offset)
  assert.Equal(t, 1, limit)
  assert.Equal(t, "created", sortBy)
  assert.Equal(t, "desc", direction)
  assert.Equal(t, "", search)
}

func TestGetPaginationParams_BoundaryValuesForPageAndLimit(t *testing.T) {
  req := httptest.NewRequest("GET", "/?page=1&limit=1", nil)
  offset, limit, sortBy, direction, search := utils.GetPaginationParams(req)

  assert.Equal(t, 0, offset)
  assert.Equal(t, 1, limit)
  assert.Equal(t, "created", sortBy)
  assert.Equal(t, "desc", direction)
  assert.Equal(t, "", search)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant