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.

83 lines
1.7 KiB

  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package code
  5. import (
  6. "path/filepath"
  7. "testing"
  8. "code.gitea.io/gitea/models"
  9. "github.com/stretchr/testify/assert"
  10. )
  11. func TestMain(m *testing.M) {
  12. models.MainTest(m, filepath.Join("..", "..", ".."))
  13. }
  14. func testIndexer(name string, t *testing.T, indexer Indexer) {
  15. t.Run(name, func(t *testing.T) {
  16. var repoID int64 = 1
  17. err := index(indexer, repoID)
  18. assert.NoError(t, err)
  19. var (
  20. keywords = []struct {
  21. RepoIDs []int64
  22. Keyword string
  23. IDs []int64
  24. Langs int
  25. }{
  26. {
  27. RepoIDs: nil,
  28. Keyword: "Description",
  29. IDs: []int64{repoID},
  30. Langs: 1,
  31. },
  32. {
  33. RepoIDs: []int64{2},
  34. Keyword: "Description",
  35. IDs: []int64{},
  36. Langs: 0,
  37. },
  38. {
  39. RepoIDs: nil,
  40. Keyword: "repo1",
  41. IDs: []int64{repoID},
  42. Langs: 1,
  43. },
  44. {
  45. RepoIDs: []int64{2},
  46. Keyword: "repo1",
  47. IDs: []int64{},
  48. Langs: 0,
  49. },
  50. {
  51. RepoIDs: nil,
  52. Keyword: "non-exist",
  53. IDs: []int64{},
  54. Langs: 0,
  55. },
  56. }
  57. )
  58. for _, kw := range keywords {
  59. t.Run(kw.Keyword, func(t *testing.T) {
  60. total, res, langs, err := indexer.Search(kw.RepoIDs, "", kw.Keyword, 1, 10)
  61. assert.NoError(t, err)
  62. assert.EqualValues(t, len(kw.IDs), total)
  63. assert.EqualValues(t, kw.Langs, len(langs))
  64. var ids = make([]int64, 0, len(res))
  65. for _, hit := range res {
  66. ids = append(ids, hit.RepoID)
  67. assert.EqualValues(t, "# repo1\n\nDescription for repo1", hit.Content)
  68. }
  69. assert.EqualValues(t, kw.IDs, ids)
  70. })
  71. }
  72. assert.NoError(t, indexer.Delete(repoID))
  73. })
  74. }