Skip to content

Commit 949cbdb

Browse files
committed
Fix unit tests
1 parent d750b30 commit 949cbdb

File tree

1 file changed

+119
-144
lines changed

1 file changed

+119
-144
lines changed

models/repo_list_test.go

Lines changed: 119 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
package models
66

77
import (
8-
"fmt"
98
"testing"
109

10+
"code.gitea.io/gitea/modules/util"
11+
1112
"github.com/stretchr/testify/assert"
1213
)
1314

@@ -16,9 +17,10 @@ func TestSearchRepositoryByName(t *testing.T) {
1617

1718
// test search public repository on explore page
1819
repos, count, err := SearchRepositoryByName(&SearchRepoOptions{
19-
Keyword: "repo_12",
20-
Page: 1,
21-
PageSize: 10,
20+
Keyword: "repo_12",
21+
Page: 1,
22+
PageSize: 10,
23+
Collaborate: util.OptionalBoolFalse,
2224
})
2325

2426
assert.NoError(t, err)
@@ -28,9 +30,10 @@ func TestSearchRepositoryByName(t *testing.T) {
2830
assert.Equal(t, int64(1), count)
2931

3032
repos, count, err = SearchRepositoryByName(&SearchRepoOptions{
31-
Keyword: "test_repo",
32-
Page: 1,
33-
PageSize: 10,
33+
Keyword: "test_repo",
34+
Page: 1,
35+
PageSize: 10,
36+
Collaborate: util.OptionalBoolFalse,
3437
})
3538

3639
assert.NoError(t, err)
@@ -39,10 +42,11 @@ func TestSearchRepositoryByName(t *testing.T) {
3942

4043
// test search private repository on explore page
4144
repos, count, err = SearchRepositoryByName(&SearchRepoOptions{
42-
Keyword: "repo_13",
43-
Page: 1,
44-
PageSize: 10,
45-
Private: true,
45+
Keyword: "repo_13",
46+
Page: 1,
47+
PageSize: 10,
48+
Private: true,
49+
Collaborate: util.OptionalBoolFalse,
4650
})
4751

4852
assert.NoError(t, err)
@@ -52,10 +56,11 @@ func TestSearchRepositoryByName(t *testing.T) {
5256
assert.Equal(t, int64(1), count)
5357

5458
repos, count, err = SearchRepositoryByName(&SearchRepoOptions{
55-
Keyword: "test_repo",
56-
Page: 1,
57-
PageSize: 10,
58-
Private: true,
59+
Keyword: "test_repo",
60+
Page: 1,
61+
PageSize: 10,
62+
Private: true,
63+
Collaborate: util.OptionalBoolFalse,
5964
})
6065

6166
assert.NoError(t, err)
@@ -69,181 +74,151 @@ func TestSearchRepositoryByName(t *testing.T) {
6974
assert.Empty(t, repos)
7075
assert.Equal(t, int64(0), count)
7176

72-
// excepted count by repo type
73-
type ec map[SearchMode]int
74-
75-
helperEC := ec{SearchModeAny: 14, SearchModeSource: 0, SearchModeFork: 0, SearchModeMirror: 4, SearchModeCollaborative: 10}
76-
helperECZero := ec{SearchModeAny: 0, SearchModeSource: 0, SearchModeFork: 0, SearchModeMirror: 0, SearchModeCollaborative: 0}
77-
7877
testCases := []struct {
7978
name string
8079
opts *SearchRepoOptions
81-
count ec
80+
count int
8281
}{
8382
{name: "PublicRepositoriesByName",
84-
opts: &SearchRepoOptions{Keyword: "big_test_", PageSize: 10},
85-
count: ec{SearchModeAny: 7, SearchModeSource: 0, SearchModeFork: 0, SearchModeMirror: 2, SearchModeCollaborative: 5}},
83+
opts: &SearchRepoOptions{Keyword: "big_test_", PageSize: 10, Collaborate: util.OptionalBoolFalse},
84+
count: 7},
8685
{name: "PublicAndPrivateRepositoriesByName",
87-
opts: &SearchRepoOptions{Keyword: "big_test_", Page: 1, PageSize: 10, Private: true},
88-
count: helperEC},
86+
opts: &SearchRepoOptions{Keyword: "big_test_", Page: 1, PageSize: 10, Private: true, Collaborate: util.OptionalBoolFalse},
87+
count: 14},
8988
{name: "PublicAndPrivateRepositoriesByNameWithPagesizeLimitFirstPage",
90-
opts: &SearchRepoOptions{Keyword: "big_test_", Page: 1, PageSize: 5, Private: true},
91-
count: helperEC},
89+
opts: &SearchRepoOptions{Keyword: "big_test_", Page: 1, PageSize: 5, Private: true, Collaborate: util.OptionalBoolFalse},
90+
count: 14},
9291
{name: "PublicAndPrivateRepositoriesByNameWithPagesizeLimitSecondPage",
93-
opts: &SearchRepoOptions{Keyword: "big_test_", Page: 2, PageSize: 5, Private: true},
94-
count: helperEC},
92+
opts: &SearchRepoOptions{Keyword: "big_test_", Page: 2, PageSize: 5, Private: true, Collaborate: util.OptionalBoolFalse},
93+
count: 14},
9594
{name: "PublicAndPrivateRepositoriesByNameWithPagesizeLimitThirdPage",
96-
opts: &SearchRepoOptions{Keyword: "big_test_", Page: 3, PageSize: 5, Private: true},
97-
count: helperEC},
95+
opts: &SearchRepoOptions{Keyword: "big_test_", Page: 3, PageSize: 5, Private: true, Collaborate: util.OptionalBoolFalse},
96+
count: 14},
9897
{name: "PublicAndPrivateRepositoriesByNameWithPagesizeLimitFourthPage",
99-
opts: &SearchRepoOptions{Keyword: "big_test_", Page: 3, PageSize: 5, Private: true},
100-
count: helperEC},
98+
opts: &SearchRepoOptions{Keyword: "big_test_", Page: 3, PageSize: 5, Private: true, Collaborate: util.OptionalBoolFalse},
99+
count: 14},
101100
{name: "PublicRepositoriesOfUser",
102-
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 15},
103-
count: ec{SearchModeAny: 2, SearchModeSource: 2, SearchModeFork: 0, SearchModeMirror: 0, SearchModeCollaborative: 0}},
101+
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 15, Collaborate: util.OptionalBoolFalse},
102+
count: 2},
104103
{name: "PublicRepositoriesOfUser2",
105-
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 18},
106-
count: helperECZero},
104+
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 18, Collaborate: util.OptionalBoolFalse},
105+
count: 0},
107106
{name: "PublicRepositoriesOfUser3",
108-
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 20},
109-
count: ec{SearchModeAny: 2, SearchModeSource: 0, SearchModeFork: 1, SearchModeMirror: 1, SearchModeCollaborative: 0}},
107+
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 20, Collaborate: util.OptionalBoolFalse},
108+
count: 2},
110109
{name: "PublicAndPrivateRepositoriesOfUser",
111-
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 15, Private: true},
112-
count: ec{SearchModeAny: 4, SearchModeSource: 4, SearchModeFork: 0, SearchModeMirror: 0, SearchModeCollaborative: 0}},
110+
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 15, Private: true, Collaborate: util.OptionalBoolFalse},
111+
count: 4},
113112
{name: "PublicAndPrivateRepositoriesOfUser2",
114-
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 18, Private: true},
115-
count: helperECZero},
113+
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 18, Private: true, Collaborate: util.OptionalBoolFalse},
114+
count: 0},
116115
{name: "PublicAndPrivateRepositoriesOfUser3",
117-
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 20, Private: true},
118-
count: ec{SearchModeAny: 4, SearchModeSource: 0, SearchModeFork: 2, SearchModeMirror: 2, SearchModeCollaborative: 0}},
116+
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 20, Private: true, Collaborate: util.OptionalBoolFalse},
117+
count: 4},
119118
{name: "PublicRepositoriesOfUserIncludingCollaborative",
120-
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 15, Collaborate: true},
121-
count: ec{SearchModeAny: 4, SearchModeSource: 2, SearchModeFork: 0, SearchModeMirror: 0, SearchModeCollaborative: 2}},
119+
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 15},
120+
count: 4},
122121
{name: "PublicRepositoriesOfUser2IncludingCollaborative",
123-
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 18, Collaborate: true},
124-
count: ec{SearchModeAny: 1, SearchModeSource: 0, SearchModeFork: 0, SearchModeMirror: 0, SearchModeCollaborative: 1}},
122+
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 18},
123+
count: 1},
125124
{name: "PublicRepositoriesOfUser3IncludingCollaborative",
126-
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 20, Collaborate: true},
127-
count: ec{SearchModeAny: 3, SearchModeSource: 0, SearchModeFork: 1, SearchModeMirror: 2, SearchModeCollaborative: 0}},
125+
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 20},
126+
count: 3},
128127
{name: "PublicAndPrivateRepositoriesOfUserIncludingCollaborative",
129-
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 15, Private: true, Collaborate: true},
130-
count: ec{SearchModeAny: 8, SearchModeSource: 4, SearchModeFork: 0, SearchModeMirror: 0, SearchModeCollaborative: 4}},
128+
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 15, Private: true},
129+
count: 8},
131130
{name: "PublicAndPrivateRepositoriesOfUser2IncludingCollaborative",
132-
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 18, Private: true, Collaborate: true},
133-
count: ec{SearchModeAny: 4, SearchModeSource: 0, SearchModeFork: 0, SearchModeMirror: 0, SearchModeCollaborative: 4}},
131+
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 18, Private: true},
132+
count: 4},
134133
{name: "PublicAndPrivateRepositoriesOfUser3IncludingCollaborative",
135-
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 20, Private: true, Collaborate: true},
136-
count: ec{SearchModeAny: 6, SearchModeSource: 0, SearchModeFork: 2, SearchModeMirror: 4, SearchModeCollaborative: 0}},
134+
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 20, Private: true},
135+
count: 6},
137136
{name: "PublicRepositoriesOfOrganization",
138-
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 17},
139-
count: ec{SearchModeAny: 1, SearchModeSource: 1, SearchModeFork: 0, SearchModeMirror: 0, SearchModeCollaborative: 0}},
137+
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 17, Collaborate: util.OptionalBoolFalse},
138+
count: 1},
140139
{name: "PublicAndPrivateRepositoriesOfOrganization",
141-
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 17, Private: true},
142-
count: ec{SearchModeAny: 2, SearchModeSource: 2, SearchModeFork: 0, SearchModeMirror: 0, SearchModeCollaborative: 0}},
140+
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 17, Private: true, Collaborate: util.OptionalBoolFalse},
141+
count: 2},
143142
{name: "AllPublic/PublicRepositoriesByName",
144-
opts: &SearchRepoOptions{Keyword: "big_test_", PageSize: 10, AllPublic: true},
145-
count: ec{SearchModeAny: 7, SearchModeSource: 0, SearchModeFork: 0, SearchModeMirror: 2, SearchModeCollaborative: 5}},
143+
opts: &SearchRepoOptions{Keyword: "big_test_", PageSize: 10, AllPublic: true, Collaborate: util.OptionalBoolFalse},
144+
count: 7},
146145
{name: "AllPublic/PublicAndPrivateRepositoriesByName",
147-
opts: &SearchRepoOptions{Keyword: "big_test_", Page: 1, PageSize: 10, Private: true, AllPublic: true},
148-
count: helperEC},
146+
opts: &SearchRepoOptions{Keyword: "big_test_", Page: 1, PageSize: 10, Private: true, AllPublic: true, Collaborate: util.OptionalBoolFalse},
147+
count: 14},
149148
{name: "AllPublic/PublicRepositoriesOfUserIncludingCollaborative",
150-
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 15, Collaborate: true, AllPublic: true},
151-
count: ec{SearchModeAny: 15, SearchModeSource: 2, SearchModeFork: 0, SearchModeMirror: 2, SearchModeCollaborative: 9}},
149+
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 15, AllPublic: true},
150+
count: 15},
152151
{name: "AllPublic/PublicAndPrivateRepositoriesOfUserIncludingCollaborative",
153-
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 15, Private: true, Collaborate: true, AllPublic: true},
154-
count: ec{SearchModeAny: 19, SearchModeSource: 4, SearchModeFork: 0, SearchModeMirror: 2, SearchModeCollaborative: 11}},
152+
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 15, Private: true, AllPublic: true},
153+
count: 19},
155154
{name: "AllPublic/PublicAndPrivateRepositoriesOfUserIncludingCollaborativeByName",
156-
opts: &SearchRepoOptions{Keyword: "test", Page: 1, PageSize: 10, OwnerID: 15, Private: true, Collaborate: true, AllPublic: true},
157-
count: ec{SearchModeAny: 13, SearchModeSource: 4, SearchModeFork: 0, SearchModeMirror: 2, SearchModeCollaborative: 7}},
155+
opts: &SearchRepoOptions{Keyword: "test", Page: 1, PageSize: 10, OwnerID: 15, Private: true, AllPublic: true},
156+
count: 13},
158157
{name: "AllPublic/PublicAndPrivateRepositoriesOfUser2IncludingCollaborativeByName",
159-
opts: &SearchRepoOptions{Keyword: "test", Page: 1, PageSize: 10, OwnerID: 18, Private: true, Collaborate: true, AllPublic: true},
160-
count: ec{SearchModeAny: 11, SearchModeSource: 0, SearchModeFork: 0, SearchModeMirror: 2, SearchModeCollaborative: 9}},
158+
opts: &SearchRepoOptions{Keyword: "test", Page: 1, PageSize: 10, OwnerID: 18, Private: true, AllPublic: true},
159+
count: 11},
161160
{name: "AllPublic/PublicRepositoriesOfOrganization",
162-
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 17, AllPublic: true},
163-
count: ec{SearchModeAny: 15, SearchModeSource: 1, SearchModeFork: 0, SearchModeMirror: 2, SearchModeCollaborative: 10}},
161+
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 17, AllPublic: true, Collaborate: util.OptionalBoolFalse},
162+
count: 15},
164163
}
165164

166165
for _, testCase := range testCases {
167166
t.Run(testCase.name, func(t *testing.T) {
168-
for searchMode, expectedCount := range testCase.count {
169-
testName := "SearchModeAny"
170-
testCase.opts.SearchMode = searchMode
171-
if len(searchMode) > 0 {
172-
testName = fmt.Sprintf("SearchMode%s", searchMode)
173-
}
174-
175-
page := testCase.opts.Page
176-
if page <= 0 {
177-
page = 1
178-
}
167+
repos, count, err := SearchRepositoryByName(testCase.opts)
179168

180-
t.Run(testName, func(t *testing.T) {
181-
repos, count, err := SearchRepositoryByName(testCase.opts)
169+
assert.NoError(t, err)
170+
assert.Equal(t, int64(testCase.count), count)
182171

183-
assert.NoError(t, err)
184-
assert.Equal(t, int64(expectedCount), count)
172+
page := testCase.opts.Page
173+
if page <= 0 {
174+
page = 1
175+
}
176+
var expectedLen = testCase.opts.PageSize
177+
if testCase.opts.PageSize*page > testCase.count+testCase.opts.PageSize {
178+
expectedLen = 0
179+
} else if testCase.opts.PageSize*page > testCase.count {
180+
expectedLen = testCase.count % testCase.opts.PageSize
181+
}
182+
if assert.Len(t, repos, expectedLen) {
183+
for _, repo := range repos {
184+
assert.NotEmpty(t, repo.Name)
185185

186-
var expectedLen int
187-
if testCase.opts.PageSize*page > expectedCount+testCase.opts.PageSize {
188-
expectedLen = 0
189-
} else if testCase.opts.PageSize*page > expectedCount {
190-
expectedLen = expectedCount % testCase.opts.PageSize
191-
} else {
192-
expectedLen = testCase.opts.PageSize
186+
if len(testCase.opts.Keyword) > 0 {
187+
assert.Contains(t, repo.Name, testCase.opts.Keyword)
193188
}
194-
if assert.Len(t, repos, expectedLen) {
195-
var tester func(t *testing.T, ownerID int64, repo *Repository)
196-
197-
switch searchMode {
198-
case SearchModeSource:
199-
tester = SearchModeSourceTester
200-
case SearchModeFork:
201-
tester = SearchModeForkTester
202-
case SearchModeMirror:
203-
tester = SearchModeMirrorTester
204-
case SearchModeCollaborative:
205-
tester = SearchModeCollaborativeTester
206-
}
207189

208-
for _, repo := range repos {
209-
assert.NotEmpty(t, repo.Name)
190+
if !testCase.opts.Private {
191+
assert.False(t, repo.IsPrivate)
192+
}
210193

211-
if len(testCase.opts.Keyword) > 0 {
212-
assert.Contains(t, repo.Name, testCase.opts.Keyword)
213-
}
194+
if testCase.opts.Fork == util.OptionalBoolTrue && testCase.opts.Mirror == util.OptionalBoolTrue {
195+
assert.True(t, repo.IsFork || repo.IsMirror)
196+
} else {
197+
switch testCase.opts.Fork {
198+
case util.OptionalBoolFalse:
199+
assert.False(t, repo.IsFork)
200+
case util.OptionalBoolTrue:
201+
assert.True(t, repo.IsFork)
202+
}
214203

215-
if !testCase.opts.Private {
216-
assert.False(t, repo.IsPrivate)
217-
}
204+
switch testCase.opts.Mirror {
205+
case util.OptionalBoolFalse:
206+
assert.False(t, repo.IsMirror)
207+
case util.OptionalBoolTrue:
208+
assert.True(t, repo.IsMirror)
209+
}
210+
}
218211

219-
if tester != nil {
220-
tester(t, testCase.opts.OwnerID, repo)
221-
}
212+
if testCase.opts.OwnerID > 0 && !testCase.opts.AllPublic {
213+
switch testCase.opts.Collaborate {
214+
case util.OptionalBoolFalse:
215+
assert.Equal(t, testCase.opts.OwnerID, repo.Owner.ID)
216+
case util.OptionalBoolTrue:
217+
assert.NotEqual(t, testCase.opts.OwnerID, repo.Owner.ID)
222218
}
223219
}
224-
})
220+
}
225221
}
226222
})
227223
}
228224
}
229-
230-
func SearchModeSourceTester(t *testing.T, ownerID int64, repo *Repository) {
231-
assert.False(t, repo.IsFork)
232-
assert.False(t, repo.IsMirror)
233-
assert.Equal(t, ownerID, repo.Owner.ID)
234-
}
235-
236-
func SearchModeForkTester(t *testing.T, ownerID int64, repo *Repository) {
237-
assert.True(t, repo.IsFork)
238-
assert.False(t, repo.IsMirror)
239-
assert.Equal(t, ownerID, repo.Owner.ID)
240-
}
241-
242-
func SearchModeMirrorTester(t *testing.T, ownerID int64, repo *Repository) {
243-
assert.True(t, repo.IsMirror)
244-
}
245-
246-
func SearchModeCollaborativeTester(t *testing.T, ownerID int64, repo *Repository) {
247-
assert.False(t, repo.IsMirror)
248-
assert.NotEqual(t, ownerID, repo.Owner.ID)
249-
}

0 commit comments

Comments
 (0)