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.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright 2018 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 issues
  5. import (
  6. "io/ioutil"
  7. "os"
  8. "testing"
  9. "github.com/stretchr/testify/assert"
  10. )
  11. func TestBleveIndexAndSearch(t *testing.T) {
  12. dir, err := ioutil.TempDir("", "bleve.index")
  13. assert.NoError(t, err)
  14. if err != nil {
  15. assert.Fail(t, "Unable to create temporary directory")
  16. return
  17. }
  18. defer os.RemoveAll(dir)
  19. indexer := NewBleveIndexer(dir)
  20. defer indexer.Close()
  21. if _, err := indexer.Init(); err != nil {
  22. assert.Fail(t, "Unable to initialise bleve indexer: %v", err)
  23. return
  24. }
  25. err = indexer.Index([]*IndexerData{
  26. {
  27. ID: 1,
  28. RepoID: 2,
  29. Title: "Issue search should support Chinese",
  30. Content: "As title",
  31. Comments: []string{
  32. "test1",
  33. "test2",
  34. },
  35. },
  36. {
  37. ID: 2,
  38. RepoID: 2,
  39. Title: "CJK support could be optional",
  40. Content: "Chinese Korean and Japanese should be supported but I would like it's not enabled by default",
  41. Comments: []string{
  42. "LGTM",
  43. "Good idea",
  44. },
  45. },
  46. })
  47. assert.NoError(t, err)
  48. var (
  49. keywords = []struct {
  50. Keyword string
  51. IDs []int64
  52. }{
  53. {
  54. Keyword: "search",
  55. IDs: []int64{1},
  56. },
  57. {
  58. Keyword: "test1",
  59. IDs: []int64{1},
  60. },
  61. {
  62. Keyword: "test2",
  63. IDs: []int64{1},
  64. },
  65. {
  66. Keyword: "support",
  67. IDs: []int64{1, 2},
  68. },
  69. {
  70. Keyword: "chinese",
  71. IDs: []int64{1, 2},
  72. },
  73. {
  74. Keyword: "help",
  75. IDs: []int64{},
  76. },
  77. }
  78. )
  79. for _, kw := range keywords {
  80. res, err := indexer.Search(kw.Keyword, []int64{2}, 10, 0)
  81. assert.NoError(t, err)
  82. var ids = make([]int64, 0, len(res.Hits))
  83. for _, hit := range res.Hits {
  84. ids = append(ids, hit.ID)
  85. }
  86. assert.EqualValues(t, kw.IDs, ids)
  87. }
  88. }