@@ -15,108 +15,11 @@ import (
1515 "github.com/stretchr/testify/assert"
1616)
1717
18- func TestSearchRepository (t * testing.T ) {
19- assert .NoError (t , unittest .PrepareTestDatabase ())
20-
21- // test search public repository on explore page
22- repos , count , err := repo_model .SearchRepositoryByName (db .DefaultContext , & repo_model.SearchRepoOptions {
23- ListOptions : db.ListOptions {
24- Page : 1 ,
25- PageSize : 10 ,
26- },
27- Keyword : "repo_12" ,
28- Collaborate : util .OptionalBoolFalse ,
29- })
30-
31- assert .NoError (t , err )
32- if assert .Len (t , repos , 1 ) {
33- assert .Equal (t , "test_repo_12" , repos [0 ].Name )
34- }
35- assert .Equal (t , int64 (1 ), count )
36-
37- repos , count , err = repo_model .SearchRepositoryByName (db .DefaultContext , & repo_model.SearchRepoOptions {
38- ListOptions : db.ListOptions {
39- Page : 1 ,
40- PageSize : 10 ,
41- },
42- Keyword : "test_repo" ,
43- Collaborate : util .OptionalBoolFalse ,
44- })
45-
46- assert .NoError (t , err )
47- assert .Equal (t , int64 (2 ), count )
48- assert .Len (t , repos , 2 )
49-
50- // test search private repository on explore page
51- repos , count , err = repo_model .SearchRepositoryByName (db .DefaultContext , & repo_model.SearchRepoOptions {
52- ListOptions : db.ListOptions {
53- Page : 1 ,
54- PageSize : 10 ,
55- },
56- Keyword : "repo_13" ,
57- Private : true ,
58- Collaborate : util .OptionalBoolFalse ,
59- })
60-
61- assert .NoError (t , err )
62- if assert .Len (t , repos , 1 ) {
63- assert .Equal (t , "test_repo_13" , repos [0 ].Name )
64- }
65- assert .Equal (t , int64 (1 ), count )
66-
67- repos , count , err = repo_model .SearchRepositoryByName (db .DefaultContext , & repo_model.SearchRepoOptions {
68- ListOptions : db.ListOptions {
69- Page : 1 ,
70- PageSize : 10 ,
71- },
72- Keyword : "test_repo" ,
73- Private : true ,
74- Collaborate : util .OptionalBoolFalse ,
75- })
76-
77- assert .NoError (t , err )
78- assert .Equal (t , int64 (3 ), count )
79- assert .Len (t , repos , 3 )
80-
81- // Test non existing owner
82- repos , count , err = repo_model .SearchRepositoryByName (db .DefaultContext , & repo_model.SearchRepoOptions {OwnerID : unittest .NonexistentID })
83-
84- assert .NoError (t , err )
85- assert .Empty (t , repos )
86- assert .Equal (t , int64 (0 ), count )
87-
88- // Test search within description
89- repos , count , err = repo_model .SearchRepository (db .DefaultContext , & repo_model.SearchRepoOptions {
90- ListOptions : db.ListOptions {
91- Page : 1 ,
92- PageSize : 10 ,
93- },
94- Keyword : "description_14" ,
95- Collaborate : util .OptionalBoolFalse ,
96- IncludeDescription : true ,
97- })
98-
99- assert .NoError (t , err )
100- if assert .Len (t , repos , 1 ) {
101- assert .Equal (t , "test_repo_14" , repos [0 ].Name )
102- }
103- assert .Equal (t , int64 (1 ), count )
104-
105- // Test NOT search within description
106- repos , count , err = repo_model .SearchRepository (db .DefaultContext , & repo_model.SearchRepoOptions {
107- ListOptions : db.ListOptions {
108- Page : 1 ,
109- PageSize : 10 ,
110- },
111- Keyword : "description_14" ,
112- Collaborate : util .OptionalBoolFalse ,
113- IncludeDescription : false ,
114- })
115-
116- assert .NoError (t , err )
117- assert .Empty (t , repos )
118- assert .Equal (t , int64 (0 ), count )
119-
18+ func getTestCases () []struct {
19+ name string
20+ opts * repo_model.SearchRepoOptions
21+ count int
22+ } {
12023 testCases := []struct {
12124 name string
12225 opts * repo_model.SearchRepoOptions
@@ -274,6 +177,113 @@ func TestSearchRepository(t *testing.T) {
274177 },
275178 }
276179
180+ return testCases
181+ }
182+
183+ func TestSearchRepository (t * testing.T ) {
184+ assert .NoError (t , unittest .PrepareTestDatabase ())
185+
186+ // test search public repository on explore page
187+ repos , count , err := repo_model .SearchRepositoryByName (db .DefaultContext , & repo_model.SearchRepoOptions {
188+ ListOptions : db.ListOptions {
189+ Page : 1 ,
190+ PageSize : 10 ,
191+ },
192+ Keyword : "repo_12" ,
193+ Collaborate : util .OptionalBoolFalse ,
194+ })
195+
196+ assert .NoError (t , err )
197+ if assert .Len (t , repos , 1 ) {
198+ assert .Equal (t , "test_repo_12" , repos [0 ].Name )
199+ }
200+ assert .Equal (t , int64 (1 ), count )
201+
202+ repos , count , err = repo_model .SearchRepositoryByName (db .DefaultContext , & repo_model.SearchRepoOptions {
203+ ListOptions : db.ListOptions {
204+ Page : 1 ,
205+ PageSize : 10 ,
206+ },
207+ Keyword : "test_repo" ,
208+ Collaborate : util .OptionalBoolFalse ,
209+ })
210+
211+ assert .NoError (t , err )
212+ assert .Equal (t , int64 (2 ), count )
213+ assert .Len (t , repos , 2 )
214+
215+ // test search private repository on explore page
216+ repos , count , err = repo_model .SearchRepositoryByName (db .DefaultContext , & repo_model.SearchRepoOptions {
217+ ListOptions : db.ListOptions {
218+ Page : 1 ,
219+ PageSize : 10 ,
220+ },
221+ Keyword : "repo_13" ,
222+ Private : true ,
223+ Collaborate : util .OptionalBoolFalse ,
224+ })
225+
226+ assert .NoError (t , err )
227+ if assert .Len (t , repos , 1 ) {
228+ assert .Equal (t , "test_repo_13" , repos [0 ].Name )
229+ }
230+ assert .Equal (t , int64 (1 ), count )
231+
232+ repos , count , err = repo_model .SearchRepositoryByName (db .DefaultContext , & repo_model.SearchRepoOptions {
233+ ListOptions : db.ListOptions {
234+ Page : 1 ,
235+ PageSize : 10 ,
236+ },
237+ Keyword : "test_repo" ,
238+ Private : true ,
239+ Collaborate : util .OptionalBoolFalse ,
240+ })
241+
242+ assert .NoError (t , err )
243+ assert .Equal (t , int64 (3 ), count )
244+ assert .Len (t , repos , 3 )
245+
246+ // Test non existing owner
247+ repos , count , err = repo_model .SearchRepositoryByName (db .DefaultContext , & repo_model.SearchRepoOptions {OwnerID : unittest .NonexistentID })
248+
249+ assert .NoError (t , err )
250+ assert .Empty (t , repos )
251+ assert .Equal (t , int64 (0 ), count )
252+
253+ // Test search within description
254+ repos , count , err = repo_model .SearchRepository (db .DefaultContext , & repo_model.SearchRepoOptions {
255+ ListOptions : db.ListOptions {
256+ Page : 1 ,
257+ PageSize : 10 ,
258+ },
259+ Keyword : "description_14" ,
260+ Collaborate : util .OptionalBoolFalse ,
261+ IncludeDescription : true ,
262+ })
263+
264+ assert .NoError (t , err )
265+ if assert .Len (t , repos , 1 ) {
266+ assert .Equal (t , "test_repo_14" , repos [0 ].Name )
267+ }
268+ assert .Equal (t , int64 (1 ), count )
269+
270+ // Test NOT search within description
271+ repos , count , err = repo_model .SearchRepository (db .DefaultContext , & repo_model.SearchRepoOptions {
272+ ListOptions : db.ListOptions {
273+ Page : 1 ,
274+ PageSize : 10 ,
275+ },
276+ Keyword : "description_14" ,
277+ Collaborate : util .OptionalBoolFalse ,
278+ IncludeDescription : false ,
279+ })
280+
281+ assert .NoError (t , err )
282+ assert .Empty (t , repos )
283+ assert .Equal (t , int64 (0 ), count )
284+
285+ testCases := getTestCases ()
286+
277287 for _ , testCase := range testCases {
278288 t .Run (testCase .name , func (t * testing.T ) {
279289 repos , count , err := repo_model .SearchRepositoryByName (db .DefaultContext , testCase .opts )
@@ -349,6 +359,21 @@ func TestSearchRepository(t *testing.T) {
349359 }
350360}
351361
362+ func TestCountRepository (t * testing.T ) {
363+ assert .NoError (t , unittest .PrepareTestDatabase ())
364+
365+ testCases := getTestCases ()
366+
367+ for _ , testCase := range testCases {
368+ t .Run (testCase .name , func (t * testing.T ) {
369+ count , err := repo_model .CountRepository (db .DefaultContext , testCase .opts )
370+
371+ assert .NoError (t , err )
372+ assert .Equal (t , int64 (testCase .count ), count )
373+ })
374+ }
375+ }
376+
352377func TestSearchRepositoryByTopicName (t * testing.T ) {
353378 assert .NoError (t , unittest .PrepareTestDatabase ())
354379
0 commit comments