2019-02-21 06:24:05 +05:30
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2019-02-21 06:24:05 +05:30
|
|
|
|
|
|
|
package issues
|
|
|
|
|
|
|
|
import (
|
2022-01-27 14:00:51 +05:30
|
|
|
"context"
|
2019-12-28 07:38:05 +05:30
|
|
|
"path"
|
2019-02-21 06:24:05 +05:30
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2021-11-12 20:06:47 +05:30
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2019-02-21 06:24:05 +05:30
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
|
2021-12-15 11:09:34 +05:30
|
|
|
_ "code.gitea.io/gitea/models"
|
|
|
|
|
2019-02-21 06:24:05 +05:30
|
|
|
"github.com/stretchr/testify/assert"
|
2021-11-17 18:04:35 +05:30
|
|
|
"gopkg.in/ini.v1"
|
2019-02-21 06:24:05 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
2022-04-14 19:28:21 +05:30
|
|
|
unittest.MainTest(m, &unittest.TestOptions{
|
|
|
|
GiteaRootPath: filepath.Join("..", "..", ".."),
|
|
|
|
})
|
2019-02-21 06:24:05 +05:30
|
|
|
}
|
|
|
|
|
2019-02-24 21:46:20 +05:30
|
|
|
func TestBleveSearchIssues(t *testing.T) {
|
2021-11-12 20:06:47 +05:30
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2023-02-19 21:42:01 +05:30
|
|
|
setting.CfgProvider = ini.Empty()
|
2019-02-21 06:24:05 +05:30
|
|
|
|
2022-09-04 20:44:53 +05:30
|
|
|
tmpIndexerDir := t.TempDir()
|
2022-01-20 22:30:38 +05:30
|
|
|
|
2023-02-19 21:42:01 +05:30
|
|
|
setting.CfgProvider.Section("queue.issue_indexer").Key("DATADIR").MustString(path.Join(tmpIndexerDir, "issues.queue"))
|
2022-01-20 22:30:38 +05:30
|
|
|
|
2019-12-28 07:38:05 +05:30
|
|
|
oldIssuePath := setting.Indexer.IssuePath
|
|
|
|
setting.Indexer.IssuePath = path.Join(tmpIndexerDir, "issues.queue")
|
|
|
|
defer func() {
|
|
|
|
setting.Indexer.IssuePath = oldIssuePath
|
|
|
|
}()
|
|
|
|
|
2019-02-24 21:46:20 +05:30
|
|
|
setting.Indexer.IssueType = "bleve"
|
2023-02-19 21:42:01 +05:30
|
|
|
setting.LoadQueueSettings()
|
2019-10-15 19:09:51 +05:30
|
|
|
InitIssueIndexer(true)
|
2019-12-28 07:38:05 +05:30
|
|
|
defer func() {
|
|
|
|
indexer := holder.get()
|
|
|
|
if bleveIndexer, ok := indexer.(*BleveIndexer); ok {
|
|
|
|
bleveIndexer.Close()
|
|
|
|
}
|
|
|
|
}()
|
2019-02-21 06:24:05 +05:30
|
|
|
|
2019-02-24 21:46:20 +05:30
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
|
2022-01-27 14:00:51 +05:30
|
|
|
ids, err := SearchIssuesByKeyword(context.TODO(), []int64{1}, "issue2")
|
2019-02-24 21:46:20 +05:30
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, []int64{2}, ids)
|
|
|
|
|
2022-01-27 14:00:51 +05:30
|
|
|
ids, err = SearchIssuesByKeyword(context.TODO(), []int64{1}, "first")
|
2019-02-24 21:46:20 +05:30
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, []int64{1}, ids)
|
|
|
|
|
2022-01-27 14:00:51 +05:30
|
|
|
ids, err = SearchIssuesByKeyword(context.TODO(), []int64{1}, "for")
|
2019-02-24 21:46:20 +05:30
|
|
|
assert.NoError(t, err)
|
2021-01-16 10:25:17 +05:30
|
|
|
assert.ElementsMatch(t, []int64{1, 2, 3, 5, 11}, ids)
|
2019-02-24 21:46:20 +05:30
|
|
|
|
2022-01-27 14:00:51 +05:30
|
|
|
ids, err = SearchIssuesByKeyword(context.TODO(), []int64{1}, "good")
|
2019-02-24 21:46:20 +05:30
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, []int64{1}, ids)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDBSearchIssues(t *testing.T) {
|
2021-11-12 20:06:47 +05:30
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2019-02-24 21:46:20 +05:30
|
|
|
|
|
|
|
setting.Indexer.IssueType = "db"
|
2019-10-15 19:09:51 +05:30
|
|
|
InitIssueIndexer(true)
|
2019-02-21 06:24:05 +05:30
|
|
|
|
2022-01-27 14:00:51 +05:30
|
|
|
ids, err := SearchIssuesByKeyword(context.TODO(), []int64{1}, "issue2")
|
2019-02-21 06:24:05 +05:30
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, []int64{2}, ids)
|
|
|
|
|
2022-01-27 14:00:51 +05:30
|
|
|
ids, err = SearchIssuesByKeyword(context.TODO(), []int64{1}, "first")
|
2019-02-21 06:24:05 +05:30
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, []int64{1}, ids)
|
|
|
|
|
2022-01-27 14:00:51 +05:30
|
|
|
ids, err = SearchIssuesByKeyword(context.TODO(), []int64{1}, "for")
|
2019-02-21 06:24:05 +05:30
|
|
|
assert.NoError(t, err)
|
2021-01-16 10:25:17 +05:30
|
|
|
assert.ElementsMatch(t, []int64{1, 2, 3, 5, 11}, ids)
|
2019-02-21 10:31:28 +05:30
|
|
|
|
2022-01-27 14:00:51 +05:30
|
|
|
ids, err = SearchIssuesByKeyword(context.TODO(), []int64{1}, "good")
|
2019-02-21 10:31:28 +05:30
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, []int64{1}, ids)
|
2019-02-21 06:24:05 +05:30
|
|
|
}
|