Refactor topic Find functions and add more tests for pagination (#30127)

This also fixed #22238
This commit is contained in:
Lunny Xiao 2024-03-29 11:38:16 +08:00 committed by GitHub
parent dd8dde2be8
commit 8acc7aab4c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 70 additions and 32 deletions

View file

@ -26,14 +26,34 @@ func TestAPITopicSearch(t *testing.T) {
TopicNames []*api.TopicResponse `json:"topics"`
}
// search all topics
res := MakeRequest(t, NewRequest(t, "GET", searchURL.String()), http.StatusOK)
DecodeJSON(t, res, &topics)
assert.Len(t, topics.TopicNames, 6)
assert.EqualValues(t, "6", res.Header().Get("x-total-count"))
// pagination search topics first page
topics.TopicNames = nil
query := url.Values{"page": []string{"1"}, "limit": []string{"4"}}
searchURL.RawQuery = query.Encode()
res := MakeRequest(t, NewRequest(t, "GET", searchURL.String()), http.StatusOK)
res = MakeRequest(t, NewRequest(t, "GET", searchURL.String()), http.StatusOK)
DecodeJSON(t, res, &topics)
assert.Len(t, topics.TopicNames, 4)
assert.EqualValues(t, "6", res.Header().Get("x-total-count"))
// pagination search topics second page
topics.TopicNames = nil
query = url.Values{"page": []string{"2"}, "limit": []string{"4"}}
searchURL.RawQuery = query.Encode()
res = MakeRequest(t, NewRequest(t, "GET", searchURL.String()), http.StatusOK)
DecodeJSON(t, res, &topics)
assert.Len(t, topics.TopicNames, 2)
assert.EqualValues(t, "6", res.Header().Get("x-total-count"))
// add keyword search
query = url.Values{"page": []string{"1"}, "limit": []string{"4"}}
query.Add("q", "topic")
searchURL.RawQuery = query.Encode()
res = MakeRequest(t, NewRequest(t, "GET", searchURL.String()), http.StatusOK)