code.gitea.io/gitea@v1.22.3/tests/integration/repo_topic_test.go (about) 1 // Copyright 2022 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package integration 5 6 import ( 7 "net/http" 8 "net/url" 9 "testing" 10 11 api "code.gitea.io/gitea/modules/structs" 12 "code.gitea.io/gitea/tests" 13 14 "github.com/stretchr/testify/assert" 15 ) 16 17 func TestTopicSearch(t *testing.T) { 18 defer tests.PrepareTestEnv(t)() 19 searchURL, _ := url.Parse("/explore/topics/search") 20 var topics struct { 21 TopicNames []*api.TopicResponse `json:"topics"` 22 } 23 24 // search all topics 25 res := MakeRequest(t, NewRequest(t, "GET", searchURL.String()), http.StatusOK) 26 DecodeJSON(t, res, &topics) 27 assert.Len(t, topics.TopicNames, 6) 28 assert.EqualValues(t, "6", res.Header().Get("x-total-count")) 29 30 // pagination search topics 31 topics.TopicNames = nil 32 query := url.Values{"page": []string{"1"}, "limit": []string{"4"}} 33 34 searchURL.RawQuery = query.Encode() 35 res = MakeRequest(t, NewRequest(t, "GET", searchURL.String()), http.StatusOK) 36 DecodeJSON(t, res, &topics) 37 assert.Len(t, topics.TopicNames, 4) 38 assert.EqualValues(t, "6", res.Header().Get("x-total-count")) 39 40 // second page 41 topics.TopicNames = nil 42 query = url.Values{"page": []string{"2"}, "limit": []string{"4"}} 43 44 searchURL.RawQuery = query.Encode() 45 res = MakeRequest(t, NewRequest(t, "GET", searchURL.String()), http.StatusOK) 46 DecodeJSON(t, res, &topics) 47 assert.Len(t, topics.TopicNames, 2) 48 assert.EqualValues(t, "6", res.Header().Get("x-total-count")) 49 50 // add keyword search 51 topics.TopicNames = nil 52 query = url.Values{"page": []string{"1"}, "limit": []string{"4"}} 53 query.Add("q", "topic") 54 searchURL.RawQuery = query.Encode() 55 res = MakeRequest(t, NewRequest(t, "GET", searchURL.String()), http.StatusOK) 56 DecodeJSON(t, res, &topics) 57 assert.Len(t, topics.TopicNames, 2) 58 59 topics.TopicNames = nil 60 query.Set("q", "database") 61 searchURL.RawQuery = query.Encode() 62 res = MakeRequest(t, NewRequest(t, "GET", searchURL.String()), http.StatusOK) 63 DecodeJSON(t, res, &topics) 64 if assert.Len(t, topics.TopicNames, 1) { 65 assert.EqualValues(t, 2, topics.TopicNames[0].ID) 66 assert.EqualValues(t, "database", topics.TopicNames[0].Name) 67 assert.EqualValues(t, 1, topics.TopicNames[0].RepoCount) 68 } 69 }