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

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