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