5
5
package models
6
6
7
7
import (
8
- "fmt"
9
8
"testing"
10
9
10
+ "code.gitea.io/gitea/modules/util"
11
+
11
12
"github.com/stretchr/testify/assert"
12
13
)
13
14
@@ -16,9 +17,10 @@ func TestSearchRepositoryByName(t *testing.T) {
16
17
17
18
// test search public repository on explore page
18
19
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 ,
22
24
})
23
25
24
26
assert .NoError (t , err )
@@ -28,9 +30,10 @@ func TestSearchRepositoryByName(t *testing.T) {
28
30
assert .Equal (t , int64 (1 ), count )
29
31
30
32
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 ,
34
37
})
35
38
36
39
assert .NoError (t , err )
@@ -39,10 +42,11 @@ func TestSearchRepositoryByName(t *testing.T) {
39
42
40
43
// test search private repository on explore page
41
44
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 ,
46
50
})
47
51
48
52
assert .NoError (t , err )
@@ -52,10 +56,11 @@ func TestSearchRepositoryByName(t *testing.T) {
52
56
assert .Equal (t , int64 (1 ), count )
53
57
54
58
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 ,
59
64
})
60
65
61
66
assert .NoError (t , err )
@@ -69,181 +74,151 @@ func TestSearchRepositoryByName(t *testing.T) {
69
74
assert .Empty (t , repos )
70
75
assert .Equal (t , int64 (0 ), count )
71
76
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
-
78
77
testCases := []struct {
79
78
name string
80
79
opts * SearchRepoOptions
81
- count ec
80
+ count int
82
81
}{
83
82
{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 },
86
85
{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 },
89
88
{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 },
92
91
{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 },
95
94
{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 },
98
97
{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 },
101
100
{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 },
104
103
{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 },
107
106
{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 },
110
109
{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 },
113
112
{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 },
116
115
{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 },
119
118
{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 },
122
121
{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 },
125
124
{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 },
128
127
{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 },
131
130
{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 },
134
133
{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 },
137
136
{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 },
140
139
{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 },
143
142
{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 },
146
145
{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 },
149
148
{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 },
152
151
{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 },
155
154
{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 },
158
157
{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 },
161
160
{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 },
164
163
}
165
164
166
165
for _ , testCase := range testCases {
167
166
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 )
179
168
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 )
182
171
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 )
185
185
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 )
193
188
}
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
- }
207
189
208
- for _ , repo := range repos {
209
- assert .NotEmpty (t , repo .Name )
190
+ if ! testCase .opts .Private {
191
+ assert .False (t , repo .IsPrivate )
192
+ }
210
193
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
+ }
214
203
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
+ }
218
211
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 )
222
218
}
223
219
}
224
- })
220
+ }
225
221
}
226
222
})
227
223
}
228
224
}
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