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

bleve_test.go 1.3 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. "os"
  7. "path/filepath"
  8. "testing"
  9. "code.gitea.io/gitea/models"
  10. "code.gitea.io/gitea/modules/log"
  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 := "./bleve.index"
  20. os.RemoveAll(dir)
  21. setting.Indexer.RepoIndexerEnabled = true
  22. idx, _, err := NewBleveIndexer(dir)
  23. if err != nil {
  24. idx.Close()
  25. log.Fatal("indexer.Init: %v", err)
  26. }
  27. err = idx.Index(1)
  28. assert.NoError(t, err)
  29. var (
  30. keywords = []struct {
  31. Keyword string
  32. IDs []int64
  33. }{
  34. {
  35. Keyword: "Description",
  36. IDs: []int64{1},
  37. },
  38. {
  39. Keyword: "repo1",
  40. IDs: []int64{1},
  41. },
  42. {
  43. Keyword: "non-exist",
  44. IDs: []int64{},
  45. },
  46. }
  47. )
  48. for _, kw := range keywords {
  49. total, res, err := idx.Search(nil, kw.Keyword, 1, 10)
  50. assert.NoError(t, err)
  51. assert.EqualValues(t, len(kw.IDs), total)
  52. var ids = make([]int64, 0, len(res))
  53. for _, hit := range res {
  54. ids = append(ids, hit.RepoID)
  55. }
  56. assert.EqualValues(t, kw.IDs, ids)
  57. }
  58. }