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.

bleve_test.go 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. Langs int
  43. }{
  44. {
  45. Keyword: "Description",
  46. IDs: []int64{1},
  47. Langs: 1,
  48. },
  49. {
  50. Keyword: "repo1",
  51. IDs: []int64{1},
  52. Langs: 1,
  53. },
  54. {
  55. Keyword: "non-exist",
  56. IDs: []int64{},
  57. Langs: 0,
  58. },
  59. }
  60. )
  61. for _, kw := range keywords {
  62. total, res, langs, err := idx.Search(nil, "", kw.Keyword, 1, 10)
  63. assert.NoError(t, err)
  64. assert.EqualValues(t, len(kw.IDs), total)
  65. assert.NotNil(t, langs)
  66. assert.Len(t, langs, kw.Langs)
  67. var ids = make([]int64, 0, len(res))
  68. for _, hit := range res {
  69. ids = append(ids, hit.RepoID)
  70. }
  71. assert.EqualValues(t, kw.IDs, ids)
  72. }
  73. }