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.

79 lines
1.5 KiB

  1. // Copyright 2019 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. "io/ioutil"
  7. "os"
  8. "path/filepath"
  9. "testing"
  10. "code.gitea.io/gitea/models"
  11. "code.gitea.io/gitea/modules/setting"
  12. "github.com/stretchr/testify/assert"
  13. )
  14. func TestMain(m *testing.M) {
  15. models.MainTest(m, filepath.Join("..", "..", ".."))
  16. }
  17. func TestIndexAndSearch(t *testing.T) {
  18. models.PrepareTestEnv(t)
  19. dir, err := ioutil.TempDir("", "bleve.index")
  20. assert.NoError(t, err)
  21. if err != nil {
  22. assert.Fail(t, "Unable to create temporary directory")
  23. return
  24. }
  25. defer os.RemoveAll(dir)
  26. setting.Indexer.RepoIndexerEnabled = true
  27. idx, _, err := NewBleveIndexer(dir)
  28. if err != nil {
  29. assert.Fail(t, "Unable to create indexer Error: %v", err)
  30. if idx != nil {
  31. idx.Close()
  32. }
  33. return
  34. }
  35. defer idx.Close()
  36. err = idx.Index(1)
  37. assert.NoError(t, err)
  38. var (
  39. keywords = []struct {
  40. Keyword string
  41. IDs []int64
  42. }{
  43. {
  44. Keyword: "Description",
  45. IDs: []int64{1},
  46. },
  47. {
  48. Keyword: "repo1",
  49. IDs: []int64{1},
  50. },
  51. {
  52. Keyword: "non-exist",
  53. IDs: []int64{},
  54. },
  55. }
  56. )
  57. for _, kw := range keywords {
  58. total, res, err := idx.Search(nil, kw.Keyword, 1, 10)
  59. assert.NoError(t, err)
  60. assert.EqualValues(t, len(kw.IDs), total)
  61. var ids = make([]int64, 0, len(res))
  62. for _, hit := range res {
  63. ids = append(ids, hit.RepoID)
  64. }
  65. assert.EqualValues(t, kw.IDs, ids)
  66. }
  67. }