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.

indexer_test.go 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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/unittest"
  9. _ "code.gitea.io/gitea/models"
  10. "github.com/stretchr/testify/assert"
  11. )
  12. func TestMain(m *testing.M) {
  13. unittest.MainTest(m, filepath.Join("..", "..", ".."))
  14. }
  15. func testIndexer(name string, t *testing.T, indexer Indexer) {
  16. t.Run(name, func(t *testing.T) {
  17. var repoID int64 = 1
  18. err := index(indexer, repoID)
  19. assert.NoError(t, err)
  20. var (
  21. keywords = []struct {
  22. RepoIDs []int64
  23. Keyword string
  24. IDs []int64
  25. Langs int
  26. }{
  27. {
  28. RepoIDs: nil,
  29. Keyword: "Description",
  30. IDs: []int64{repoID},
  31. Langs: 1,
  32. },
  33. {
  34. RepoIDs: []int64{2},
  35. Keyword: "Description",
  36. IDs: []int64{},
  37. Langs: 0,
  38. },
  39. {
  40. RepoIDs: nil,
  41. Keyword: "repo1",
  42. IDs: []int64{repoID},
  43. Langs: 1,
  44. },
  45. {
  46. RepoIDs: []int64{2},
  47. Keyword: "repo1",
  48. IDs: []int64{},
  49. Langs: 0,
  50. },
  51. {
  52. RepoIDs: nil,
  53. Keyword: "non-exist",
  54. IDs: []int64{},
  55. Langs: 0,
  56. },
  57. }
  58. )
  59. for _, kw := range keywords {
  60. t.Run(kw.Keyword, func(t *testing.T) {
  61. total, res, langs, err := indexer.Search(kw.RepoIDs, "", kw.Keyword, 1, 10, false)
  62. assert.NoError(t, err)
  63. assert.EqualValues(t, len(kw.IDs), total)
  64. assert.Len(t, langs, kw.Langs)
  65. var ids = make([]int64, 0, len(res))
  66. for _, hit := range res {
  67. ids = append(ids, hit.RepoID)
  68. assert.EqualValues(t, "# repo1\n\nDescription for repo1", hit.Content)
  69. }
  70. assert.EqualValues(t, kw.IDs, ids)
  71. })
  72. }
  73. assert.NoError(t, indexer.Delete(repoID))
  74. })
  75. }