MCPcopy
hub / github.com/gofiber/fiber / Test_PaginateWithMultipleSorting

Function Test_PaginateWithMultipleSorting

middleware/paginate/paginate_test.go:688–731  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

686}
687
688func Test_PaginateWithMultipleSorting(t *testing.T) {
689 t.Parallel()
690
691 testCases := []struct {
692 name string
693 url string
694 expectedSort []SortField
695 }{
696 {"Default Sort", "/", []SortField{{Field: "id", Order: ASC}}},
697 {"Single Sort", "/?sort=name", []SortField{{Field: "name", Order: ASC}}},
698 {"Multiple Sort", "/?sort=name,-date", []SortField{{Field: "name", Order: ASC}, {Field: "date", Order: DESC}}},
699 {"Invalid Sort", "/?sort=invalid", []SortField{{Field: "id", Order: ASC}}},
700 }
701
702 for _, tc := range testCases {
703 t.Run(tc.name, func(t *testing.T) {
704 t.Parallel()
705 app := fiber.New()
706 app.Use(New(Config{
707 SortKey: "sort",
708 DefaultSort: "id",
709 AllowedSorts: []string{"id", "name", "date"},
710 }))
711
712 app.Get("/", func(c fiber.Ctx) error {
713 pageInfo, ok := FromContext(c)
714 if !ok {
715 return fiber.ErrBadRequest
716 }
717 return c.JSON(paginateResponse{
718 Sort: pageInfo.Sort,
719 })
720 })
721
722 resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, tc.url, http.NoBody))
723 require.NoError(t, err)
724 defer resp.Body.Close() //nolint:errcheck // close error not relevant in tests
725
726 var result paginateResponse
727 require.NoError(t, json.NewDecoder(resp.Body).Decode(&result))
728 require.Equal(t, tc.expectedSort, result.Sort)
729 })
730 }
731}
732
733func Test_ParseSortQuery(t *testing.T) {
734 t.Parallel()

Callers

nothing calls this directly

Calls 8

TestMethod · 0.80
NewFunction · 0.70
FromContextFunction · 0.70
NewMethod · 0.65
UseMethod · 0.65
GetMethod · 0.65
JSONMethod · 0.65
CloseMethod · 0.65

Tested by

no test coverage detected