2020-08-30 21:38:01 +05:30
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2020-08-30 21:38:01 +05:30
|
|
|
|
|
|
|
package code
|
|
|
|
|
|
|
|
import (
|
2022-01-27 14:00:51 +05:30
|
|
|
"context"
|
2020-08-30 21:38:01 +05:30
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
2021-11-12 20:06:47 +05:30
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2022-01-20 04:56:57 +05:30
|
|
|
"code.gitea.io/gitea/modules/git"
|
2021-11-12 20:06:47 +05:30
|
|
|
|
2021-12-15 11:09:34 +05:30
|
|
|
_ "code.gitea.io/gitea/models"
|
|
|
|
|
2020-08-30 21:38:01 +05:30
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
2022-04-14 19:28:21 +05:30
|
|
|
unittest.MainTest(m, &unittest.TestOptions{
|
|
|
|
GiteaRootPath: filepath.Join("..", "..", ".."),
|
|
|
|
})
|
2020-08-30 21:38:01 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func testIndexer(name string, t *testing.T, indexer Indexer) {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
var repoID int64 = 1
|
2022-01-20 04:56:57 +05:30
|
|
|
err := index(git.DefaultContext, indexer, repoID)
|
2020-08-30 21:38:01 +05:30
|
|
|
assert.NoError(t, err)
|
2022-01-20 23:16:10 +05:30
|
|
|
keywords := []struct {
|
|
|
|
RepoIDs []int64
|
|
|
|
Keyword string
|
|
|
|
IDs []int64
|
|
|
|
Langs int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
RepoIDs: nil,
|
|
|
|
Keyword: "Description",
|
|
|
|
IDs: []int64{repoID},
|
|
|
|
Langs: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
RepoIDs: []int64{2},
|
|
|
|
Keyword: "Description",
|
|
|
|
IDs: []int64{},
|
|
|
|
Langs: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
RepoIDs: nil,
|
|
|
|
Keyword: "repo1",
|
|
|
|
IDs: []int64{repoID},
|
|
|
|
Langs: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
RepoIDs: []int64{2},
|
|
|
|
Keyword: "repo1",
|
|
|
|
IDs: []int64{},
|
|
|
|
Langs: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
RepoIDs: nil,
|
|
|
|
Keyword: "non-exist",
|
|
|
|
IDs: []int64{},
|
|
|
|
Langs: 0,
|
|
|
|
},
|
|
|
|
}
|
2020-08-30 21:38:01 +05:30
|
|
|
|
|
|
|
for _, kw := range keywords {
|
|
|
|
t.Run(kw.Keyword, func(t *testing.T) {
|
2022-01-27 14:00:51 +05:30
|
|
|
total, res, langs, err := indexer.Search(context.TODO(), kw.RepoIDs, "", kw.Keyword, 1, 10, false)
|
2020-08-30 21:38:01 +05:30
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, len(kw.IDs), total)
|
2021-06-07 10:57:09 +05:30
|
|
|
assert.Len(t, langs, kw.Langs)
|
2020-08-30 21:38:01 +05:30
|
|
|
|
2022-01-20 23:16:10 +05:30
|
|
|
ids := make([]int64, 0, len(res))
|
2020-08-30 21:38:01 +05:30
|
|
|
for _, hit := range res {
|
|
|
|
ids = append(ids, hit.RepoID)
|
|
|
|
assert.EqualValues(t, "# repo1\n\nDescription for repo1", hit.Content)
|
|
|
|
}
|
|
|
|
assert.EqualValues(t, kw.IDs, ids)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.NoError(t, indexer.Delete(repoID))
|
|
|
|
})
|
|
|
|
}
|