8b0aaa5f86
A testing cleanup. This pull request replaces `os.MkdirTemp` with `t.TempDir`. We can use the `T.TempDir` function from the `testing` package to create temporary directory. The directory created by `T.TempDir` is automatically removed when the test and all its subtests complete. This saves us at least 2 lines (error check, and cleanup) on every instance, or in some cases adds cleanup that we forgot. Reference: https://pkg.go.dev/testing#T.TempDir ```go func TestFoo(t *testing.T) { // before tmpDir, err := os.MkdirTemp("", "") require.NoError(t, err) defer os.RemoveAll(tmpDir) // now tmpDir := t.TempDir() } ``` Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
93 lines
2.5 KiB
Go
93 lines
2.5 KiB
Go
// 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 (
|
|
"context"
|
|
"path"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
|
|
"code.gitea.io/gitea/models/unittest"
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
_ "code.gitea.io/gitea/models"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"gopkg.in/ini.v1"
|
|
)
|
|
|
|
func TestMain(m *testing.M) {
|
|
unittest.MainTest(m, &unittest.TestOptions{
|
|
GiteaRootPath: filepath.Join("..", "..", ".."),
|
|
})
|
|
}
|
|
|
|
func TestBleveSearchIssues(t *testing.T) {
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
setting.Cfg = ini.Empty()
|
|
|
|
tmpIndexerDir := t.TempDir()
|
|
|
|
setting.Cfg.Section("queue.issue_indexer").Key("DATADIR").MustString(path.Join(tmpIndexerDir, "issues.queue"))
|
|
|
|
oldIssuePath := setting.Indexer.IssuePath
|
|
setting.Indexer.IssuePath = path.Join(tmpIndexerDir, "issues.queue")
|
|
defer func() {
|
|
setting.Indexer.IssuePath = oldIssuePath
|
|
}()
|
|
|
|
setting.Indexer.IssueType = "bleve"
|
|
setting.NewQueueService()
|
|
InitIssueIndexer(true)
|
|
defer func() {
|
|
indexer := holder.get()
|
|
if bleveIndexer, ok := indexer.(*BleveIndexer); ok {
|
|
bleveIndexer.Close()
|
|
}
|
|
}()
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
ids, err := SearchIssuesByKeyword(context.TODO(), []int64{1}, "issue2")
|
|
assert.NoError(t, err)
|
|
assert.EqualValues(t, []int64{2}, ids)
|
|
|
|
ids, err = SearchIssuesByKeyword(context.TODO(), []int64{1}, "first")
|
|
assert.NoError(t, err)
|
|
assert.EqualValues(t, []int64{1}, ids)
|
|
|
|
ids, err = SearchIssuesByKeyword(context.TODO(), []int64{1}, "for")
|
|
assert.NoError(t, err)
|
|
assert.ElementsMatch(t, []int64{1, 2, 3, 5, 11}, ids)
|
|
|
|
ids, err = SearchIssuesByKeyword(context.TODO(), []int64{1}, "good")
|
|
assert.NoError(t, err)
|
|
assert.EqualValues(t, []int64{1}, ids)
|
|
}
|
|
|
|
func TestDBSearchIssues(t *testing.T) {
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
setting.Indexer.IssueType = "db"
|
|
InitIssueIndexer(true)
|
|
|
|
ids, err := SearchIssuesByKeyword(context.TODO(), []int64{1}, "issue2")
|
|
assert.NoError(t, err)
|
|
assert.EqualValues(t, []int64{2}, ids)
|
|
|
|
ids, err = SearchIssuesByKeyword(context.TODO(), []int64{1}, "first")
|
|
assert.NoError(t, err)
|
|
assert.EqualValues(t, []int64{1}, ids)
|
|
|
|
ids, err = SearchIssuesByKeyword(context.TODO(), []int64{1}, "for")
|
|
assert.NoError(t, err)
|
|
assert.ElementsMatch(t, []int64{1, 2, 3, 5, 11}, ids)
|
|
|
|
ids, err = SearchIssuesByKeyword(context.TODO(), []int64{1}, "good")
|
|
assert.NoError(t, err)
|
|
assert.EqualValues(t, []int64{1}, ids)
|
|
}
|