2019-02-20 19:54:05 -05:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package issues
|
|
|
|
|
|
|
|
import (
|
2022-01-27 03:30:51 -05:00
|
|
|
"context"
|
2021-09-22 01:38:34 -04:00
|
|
|
"os"
|
2019-12-27 21:08:05 -05:00
|
|
|
"path"
|
2019-02-20 19:54:05 -05:00
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2021-11-12 09:36:47 -05:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2019-02-20 19:54:05 -05:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2020-08-11 16:05:34 -04:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2019-02-20 19:54:05 -05:00
|
|
|
|
2021-12-15 00:39:34 -05:00
|
|
|
_ "code.gitea.io/gitea/models"
|
|
|
|
|
2019-02-20 19:54:05 -05:00
|
|
|
"github.com/stretchr/testify/assert"
|
2021-11-17 07:34:35 -05:00
|
|
|
"gopkg.in/ini.v1"
|
2019-02-20 19:54:05 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
2022-04-14 09:58:21 -04:00
|
|
|
unittest.MainTest(m, &unittest.TestOptions{
|
|
|
|
GiteaRootPath: filepath.Join("..", "..", ".."),
|
|
|
|
})
|
2019-02-20 19:54:05 -05:00
|
|
|
}
|
|
|
|
|
2019-02-24 11:16:20 -05:00
|
|
|
func TestBleveSearchIssues(t *testing.T) {
|
2021-11-12 09:36:47 -05:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2020-01-07 06:23:09 -05:00
|
|
|
setting.Cfg = ini.Empty()
|
2019-02-20 19:54:05 -05:00
|
|
|
|
2021-09-22 01:38:34 -04:00
|
|
|
tmpIndexerDir, err := os.MkdirTemp("", "issues-indexer")
|
2019-12-27 21:08:05 -05:00
|
|
|
if err != nil {
|
|
|
|
assert.Fail(t, "Unable to create temporary directory: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2022-01-20 12:00:38 -05:00
|
|
|
|
|
|
|
setting.Cfg.Section("queue.issue_indexer").Key("DATADIR").MustString(path.Join(tmpIndexerDir, "issues.queue"))
|
|
|
|
|
2019-12-27 21:08:05 -05:00
|
|
|
oldIssuePath := setting.Indexer.IssuePath
|
|
|
|
setting.Indexer.IssuePath = path.Join(tmpIndexerDir, "issues.queue")
|
|
|
|
defer func() {
|
|
|
|
setting.Indexer.IssuePath = oldIssuePath
|
2020-08-11 16:05:34 -04:00
|
|
|
util.RemoveAll(tmpIndexerDir)
|
2019-12-27 21:08:05 -05:00
|
|
|
}()
|
|
|
|
|
2019-02-24 11:16:20 -05:00
|
|
|
setting.Indexer.IssueType = "bleve"
|
2020-01-07 06:23:09 -05:00
|
|
|
setting.NewQueueService()
|
2019-10-15 09:39:51 -04:00
|
|
|
InitIssueIndexer(true)
|
2019-12-27 21:08:05 -05:00
|
|
|
defer func() {
|
|
|
|
indexer := holder.get()
|
|
|
|
if bleveIndexer, ok := indexer.(*BleveIndexer); ok {
|
|
|
|
bleveIndexer.Close()
|
|
|
|
}
|
|
|
|
}()
|
2019-02-20 19:54:05 -05:00
|
|
|
|
2019-02-24 11:16:20 -05:00
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
|
2022-01-27 03:30:51 -05:00
|
|
|
ids, err := SearchIssuesByKeyword(context.TODO(), []int64{1}, "issue2")
|
2019-02-24 11:16:20 -05:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, []int64{2}, ids)
|
|
|
|
|
2022-01-27 03:30:51 -05:00
|
|
|
ids, err = SearchIssuesByKeyword(context.TODO(), []int64{1}, "first")
|
2019-02-24 11:16:20 -05:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, []int64{1}, ids)
|
|
|
|
|
2022-01-27 03:30:51 -05:00
|
|
|
ids, err = SearchIssuesByKeyword(context.TODO(), []int64{1}, "for")
|
2019-02-24 11:16:20 -05:00
|
|
|
assert.NoError(t, err)
|
2021-01-15 23:55:17 -05:00
|
|
|
assert.ElementsMatch(t, []int64{1, 2, 3, 5, 11}, ids)
|
2019-02-24 11:16:20 -05:00
|
|
|
|
2022-01-27 03:30:51 -05:00
|
|
|
ids, err = SearchIssuesByKeyword(context.TODO(), []int64{1}, "good")
|
2019-02-24 11:16:20 -05:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, []int64{1}, ids)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDBSearchIssues(t *testing.T) {
|
2021-11-12 09:36:47 -05:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2019-02-24 11:16:20 -05:00
|
|
|
|
|
|
|
setting.Indexer.IssueType = "db"
|
2019-10-15 09:39:51 -04:00
|
|
|
InitIssueIndexer(true)
|
2019-02-20 19:54:05 -05:00
|
|
|
|
2022-01-27 03:30:51 -05:00
|
|
|
ids, err := SearchIssuesByKeyword(context.TODO(), []int64{1}, "issue2")
|
2019-02-20 19:54:05 -05:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, []int64{2}, ids)
|
|
|
|
|
2022-01-27 03:30:51 -05:00
|
|
|
ids, err = SearchIssuesByKeyword(context.TODO(), []int64{1}, "first")
|
2019-02-20 19:54:05 -05:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, []int64{1}, ids)
|
|
|
|
|
2022-01-27 03:30:51 -05:00
|
|
|
ids, err = SearchIssuesByKeyword(context.TODO(), []int64{1}, "for")
|
2019-02-20 19:54:05 -05:00
|
|
|
assert.NoError(t, err)
|
2021-01-15 23:55:17 -05:00
|
|
|
assert.ElementsMatch(t, []int64{1, 2, 3, 5, 11}, ids)
|
2019-02-21 00:01:28 -05:00
|
|
|
|
2022-01-27 03:30:51 -05:00
|
|
|
ids, err = SearchIssuesByKeyword(context.TODO(), []int64{1}, "good")
|
2019-02-21 00:01:28 -05:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, []int64{1}, ids)
|
2019-02-20 19:54:05 -05:00
|
|
|
}
|