Files
new-api/routers/api/v1/utils/sort_test.go
T
2026-05-30 22:47:36 +08:00

45 lines
1.1 KiB
Go

// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package utils
import (
"net/http"
"testing"
"gitea.dev/models/db"
"gitea.dev/services/contexttest"
"github.com/stretchr/testify/assert"
)
func TestResolveSortOrder(t *testing.T) {
m := map[string]map[string]db.SearchOrderBy{
"asc": {"id": "id ASC"},
"desc": {"id": "id DESC"},
}
defaultOrder := db.SearchOrderBy("default")
cases := []struct {
path string
wantOK bool
wantOrder db.SearchOrderBy
wantStatus int
}{
{"GET /", true, defaultOrder, 0},
{"GET /?sort=id", true, "id ASC", 0},
{"GET /?sort=id&order=desc", true, "id DESC", 0},
{"GET /?sort=bogus", false, "", http.StatusUnprocessableEntity},
{"GET /?sort=id&order=bogus", false, "", http.StatusUnprocessableEntity},
}
for _, tc := range cases {
t.Run(tc.path, func(t *testing.T) {
ctx, _ := contexttest.MockAPIContext(t, tc.path)
got, ok := ResolveSortOrder(ctx, m, defaultOrder)
assert.Equal(t, tc.wantOK, ok)
assert.Equal(t, tc.wantOrder, got)
assert.Equal(t, tc.wantStatus, ctx.Resp.WrittenStatus())
})
}
}