You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

repo_topic_test.go 2.1KB

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