aboutsummaryrefslogtreecommitdiffstats
path: root/modules/indexer/issues/indexer_test.go
blob: d6812f714e0864d6653a39b104f379168deec382 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright 2019 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package issues

import (
	"context"
	"path"
	"path/filepath"
	"testing"
	"time"

	"code.gitea.io/gitea/models/unittest"
	"code.gitea.io/gitea/modules/indexer/issues/bleve"
	"code.gitea.io/gitea/modules/setting"

	_ "code.gitea.io/gitea/models"

	"github.com/stretchr/testify/assert"
)

func TestMain(m *testing.M) {
	unittest.MainTest(m, &unittest.TestOptions{
		GiteaRootPath: filepath.Join("..", "..", ".."),
	})
}

func TestBleveSearchIssues(t *testing.T) {
	assert.NoError(t, unittest.PrepareTestDatabase())
	setting.CfgProvider, _ = setting.NewConfigProviderFromData("")

	tmpIndexerDir := t.TempDir()

	setting.CfgProvider.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.LoadQueueSettings()
	InitIssueIndexer(true)
	defer func() {
		if bleveIndexer, ok := (*globalIndexer.Load()).(*bleve.Indexer); ok {
			bleveIndexer.Close()
		}
	}()

	time.Sleep(5 * time.Second)

	t.Run("issue2", func(t *testing.T) {
		ids, _, err := SearchIssues(context.TODO(), &SearchOptions{
			Keyword: "issue2",
			RepoIDs: []int64{1},
		})
		assert.NoError(t, err)
		assert.EqualValues(t, []int64{2}, ids)
	})

	t.Run("first", func(t *testing.T) {
		ids, _, err := SearchIssues(context.TODO(), &SearchOptions{
			Keyword: "first",
			RepoIDs: []int64{1},
		})
		assert.NoError(t, err)
		assert.EqualValues(t, []int64{1}, ids)
	})

	t.Run("for", func(t *testing.T) {
		ids, _, err := SearchIssues(context.TODO(), &SearchOptions{
			Keyword: "for",
			RepoIDs: []int64{1},
		})
		assert.NoError(t, err)
		assert.ElementsMatch(t, []int64{1, 2, 3, 5, 11}, ids)
	})

	t.Run("good", func(t *testing.T) {
		ids, _, err := SearchIssues(context.TODO(), &SearchOptions{
			Keyword: "good",
			RepoIDs: []int64{1},
		})
		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)

	t.Run("issue2", func(t *testing.T) {
		ids, _, err := SearchIssues(context.TODO(), &SearchOptions{
			Keyword: "issue2",
			RepoIDs: []int64{1},
		})
		assert.NoError(t, err)
		assert.EqualValues(t, []int64{2}, ids)
	})

	t.Run("first", func(t *testing.T) {
		ids, _, err := SearchIssues(context.TODO(), &SearchOptions{
			Keyword: "first",
			RepoIDs: []int64{1},
		})
		assert.NoError(t, err)
		assert.EqualValues(t, []int64{1}, ids)
	})

	t.Run("for", func(t *testing.T) {
		ids, _, err := SearchIssues(context.TODO(), &SearchOptions{
			Keyword: "for",
			RepoIDs: []int64{1},
		})
		assert.NoError(t, err)
		assert.ElementsMatch(t, []int64{1, 2, 3, 5, 11}, ids)
	})

	t.Run("good", func(t *testing.T) {
		ids, _, err := SearchIssues(context.TODO(), &SearchOptions{
			Keyword: "good",
			RepoIDs: []int64{1},
		})
		assert.NoError(t, err)
		assert.EqualValues(t, []int64{1}, ids)
	})
}