You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

repo_list_test.go 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package models
  5. import (
  6. "testing"
  7. "code.gitea.io/gitea/models/db"
  8. "code.gitea.io/gitea/models/unittest"
  9. "code.gitea.io/gitea/modules/util"
  10. "github.com/stretchr/testify/assert"
  11. )
  12. func TestSearchRepository(t *testing.T) {
  13. assert.NoError(t, unittest.PrepareTestDatabase())
  14. // test search public repository on explore page
  15. repos, count, err := SearchRepositoryByName(&SearchRepoOptions{
  16. ListOptions: db.ListOptions{
  17. Page: 1,
  18. PageSize: 10,
  19. },
  20. Keyword: "repo_12",
  21. Collaborate: util.OptionalBoolFalse,
  22. })
  23. assert.NoError(t, err)
  24. if assert.Len(t, repos, 1) {
  25. assert.Equal(t, "test_repo_12", repos[0].Name)
  26. }
  27. assert.Equal(t, int64(1), count)
  28. repos, count, err = SearchRepositoryByName(&SearchRepoOptions{
  29. ListOptions: db.ListOptions{
  30. Page: 1,
  31. PageSize: 10,
  32. },
  33. Keyword: "test_repo",
  34. Collaborate: util.OptionalBoolFalse,
  35. })
  36. assert.NoError(t, err)
  37. assert.Equal(t, int64(2), count)
  38. assert.Len(t, repos, 2)
  39. // test search private repository on explore page
  40. repos, count, err = SearchRepositoryByName(&SearchRepoOptions{
  41. ListOptions: db.ListOptions{
  42. Page: 1,
  43. PageSize: 10,
  44. },
  45. Keyword: "repo_13",
  46. Private: true,
  47. Collaborate: util.OptionalBoolFalse,
  48. })
  49. assert.NoError(t, err)
  50. if assert.Len(t, repos, 1) {
  51. assert.Equal(t, "test_repo_13", repos[0].Name)
  52. }
  53. assert.Equal(t, int64(1), count)
  54. repos, count, err = SearchRepositoryByName(&SearchRepoOptions{
  55. ListOptions: db.ListOptions{
  56. Page: 1,
  57. PageSize: 10,
  58. },
  59. Keyword: "test_repo",
  60. Private: true,
  61. Collaborate: util.OptionalBoolFalse,
  62. })
  63. assert.NoError(t, err)
  64. assert.Equal(t, int64(3), count)
  65. assert.Len(t, repos, 3)
  66. // Test non existing owner
  67. repos, count, err = SearchRepositoryByName(&SearchRepoOptions{OwnerID: unittest.NonexistentID})
  68. assert.NoError(t, err)
  69. assert.Empty(t, repos)
  70. assert.Equal(t, int64(0), count)
  71. // Test search within description
  72. repos, count, err = SearchRepository(&SearchRepoOptions{
  73. ListOptions: db.ListOptions{
  74. Page: 1,
  75. PageSize: 10,
  76. },
  77. Keyword: "description_14",
  78. Collaborate: util.OptionalBoolFalse,
  79. IncludeDescription: true,
  80. })
  81. assert.NoError(t, err)
  82. if assert.Len(t, repos, 1) {
  83. assert.Equal(t, "test_repo_14", repos[0].Name)
  84. }
  85. assert.Equal(t, int64(1), count)
  86. // Test NOT search within description
  87. repos, count, err = SearchRepository(&SearchRepoOptions{
  88. ListOptions: db.ListOptions{
  89. Page: 1,
  90. PageSize: 10,
  91. },
  92. Keyword: "description_14",
  93. Collaborate: util.OptionalBoolFalse,
  94. IncludeDescription: false,
  95. })
  96. assert.NoError(t, err)
  97. assert.Empty(t, repos)
  98. assert.Equal(t, int64(0), count)
  99. testCases := []struct {
  100. name string
  101. opts *SearchRepoOptions
  102. count int
  103. }{
  104. {
  105. name: "PublicRepositoriesByName",
  106. opts: &SearchRepoOptions{Keyword: "big_test_", ListOptions: db.ListOptions{PageSize: 10}, Collaborate: util.OptionalBoolFalse},
  107. count: 7,
  108. },
  109. {
  110. name: "PublicAndPrivateRepositoriesByName",
  111. opts: &SearchRepoOptions{Keyword: "big_test_", ListOptions: db.ListOptions{Page: 1, PageSize: 10}, Private: true, Collaborate: util.OptionalBoolFalse},
  112. count: 14,
  113. },
  114. {
  115. name: "PublicAndPrivateRepositoriesByNameWithPagesizeLimitFirstPage",
  116. opts: &SearchRepoOptions{Keyword: "big_test_", ListOptions: db.ListOptions{Page: 1, PageSize: 5}, Private: true, Collaborate: util.OptionalBoolFalse},
  117. count: 14,
  118. },
  119. {
  120. name: "PublicAndPrivateRepositoriesByNameWithPagesizeLimitSecondPage",
  121. opts: &SearchRepoOptions{Keyword: "big_test_", ListOptions: db.ListOptions{Page: 2, PageSize: 5}, Private: true, Collaborate: util.OptionalBoolFalse},
  122. count: 14,
  123. },
  124. {
  125. name: "PublicAndPrivateRepositoriesByNameWithPagesizeLimitThirdPage",
  126. opts: &SearchRepoOptions{Keyword: "big_test_", ListOptions: db.ListOptions{Page: 3, PageSize: 5}, Private: true, Collaborate: util.OptionalBoolFalse},
  127. count: 14,
  128. },
  129. {
  130. name: "PublicAndPrivateRepositoriesByNameWithPagesizeLimitFourthPage",
  131. opts: &SearchRepoOptions{Keyword: "big_test_", ListOptions: db.ListOptions{Page: 3, PageSize: 5}, Private: true, Collaborate: util.OptionalBoolFalse},
  132. count: 14,
  133. },
  134. {
  135. name: "PublicRepositoriesOfUser",
  136. opts: &SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 15, Collaborate: util.OptionalBoolFalse},
  137. count: 2,
  138. },
  139. {
  140. name: "PublicRepositoriesOfUser2",
  141. opts: &SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 18, Collaborate: util.OptionalBoolFalse},
  142. count: 0,
  143. },
  144. {
  145. name: "PublicRepositoriesOfUser3",
  146. opts: &SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 20, Collaborate: util.OptionalBoolFalse},
  147. count: 2,
  148. },
  149. {
  150. name: "PublicAndPrivateRepositoriesOfUser",
  151. opts: &SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 15, Private: true, Collaborate: util.OptionalBoolFalse},
  152. count: 4,
  153. },
  154. {
  155. name: "PublicAndPrivateRepositoriesOfUser2",
  156. opts: &SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 18, Private: true, Collaborate: util.OptionalBoolFalse},
  157. count: 0,
  158. },
  159. {
  160. name: "PublicAndPrivateRepositoriesOfUser3",
  161. opts: &SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 20, Private: true, Collaborate: util.OptionalBoolFalse},
  162. count: 4,
  163. },
  164. {
  165. name: "PublicRepositoriesOfUserIncludingCollaborative",
  166. opts: &SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 15},
  167. count: 5,
  168. },
  169. {
  170. name: "PublicRepositoriesOfUser2IncludingCollaborative",
  171. opts: &SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 18},
  172. count: 1,
  173. },
  174. {
  175. name: "PublicRepositoriesOfUser3IncludingCollaborative",
  176. opts: &SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 20},
  177. count: 3,
  178. },
  179. {
  180. name: "PublicAndPrivateRepositoriesOfUserIncludingCollaborative",
  181. opts: &SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 15, Private: true},
  182. count: 9,
  183. },
  184. {
  185. name: "PublicAndPrivateRepositoriesOfUser2IncludingCollaborative",
  186. opts: &SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 18, Private: true},
  187. count: 4,
  188. },
  189. {
  190. name: "PublicAndPrivateRepositoriesOfUser3IncludingCollaborative",
  191. opts: &SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 20, Private: true},
  192. count: 7,
  193. },
  194. {
  195. name: "PublicRepositoriesOfOrganization",
  196. opts: &SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 17, Collaborate: util.OptionalBoolFalse},
  197. count: 1,
  198. },
  199. {
  200. name: "PublicAndPrivateRepositoriesOfOrganization",
  201. opts: &SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 17, Private: true, Collaborate: util.OptionalBoolFalse},
  202. count: 2,
  203. },
  204. {
  205. name: "AllPublic/PublicRepositoriesByName",
  206. opts: &SearchRepoOptions{Keyword: "big_test_", ListOptions: db.ListOptions{PageSize: 10}, AllPublic: true, Collaborate: util.OptionalBoolFalse},
  207. count: 7,
  208. },
  209. {
  210. name: "AllPublic/PublicAndPrivateRepositoriesByName",
  211. opts: &SearchRepoOptions{Keyword: "big_test_", ListOptions: db.ListOptions{Page: 1, PageSize: 10}, Private: true, AllPublic: true, Collaborate: util.OptionalBoolFalse},
  212. count: 14,
  213. },
  214. {
  215. name: "AllPublic/PublicRepositoriesOfUserIncludingCollaborative",
  216. opts: &SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 15, AllPublic: true, Template: util.OptionalBoolFalse},
  217. count: 28,
  218. },
  219. {
  220. name: "AllPublic/PublicAndPrivateRepositoriesOfUserIncludingCollaborative",
  221. opts: &SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 15, Private: true, AllPublic: true, AllLimited: true, Template: util.OptionalBoolFalse},
  222. count: 33,
  223. },
  224. {
  225. name: "AllPublic/PublicAndPrivateRepositoriesOfUserIncludingCollaborativeByName",
  226. opts: &SearchRepoOptions{Keyword: "test", ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 15, Private: true, AllPublic: true},
  227. count: 15,
  228. },
  229. {
  230. name: "AllPublic/PublicAndPrivateRepositoriesOfUser2IncludingCollaborativeByName",
  231. opts: &SearchRepoOptions{Keyword: "test", ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 18, Private: true, AllPublic: true},
  232. count: 13,
  233. },
  234. {
  235. name: "AllPublic/PublicRepositoriesOfOrganization",
  236. opts: &SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 17, AllPublic: true, Collaborate: util.OptionalBoolFalse, Template: util.OptionalBoolFalse},
  237. count: 28,
  238. },
  239. {
  240. name: "AllTemplates",
  241. opts: &SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, Template: util.OptionalBoolTrue},
  242. count: 2,
  243. },
  244. }
  245. for _, testCase := range testCases {
  246. t.Run(testCase.name, func(t *testing.T) {
  247. repos, count, err := SearchRepositoryByName(testCase.opts)
  248. assert.NoError(t, err)
  249. assert.Equal(t, int64(testCase.count), count)
  250. page := testCase.opts.Page
  251. if page <= 0 {
  252. page = 1
  253. }
  254. expectedLen := testCase.opts.PageSize
  255. if testCase.opts.PageSize*page > testCase.count+testCase.opts.PageSize {
  256. expectedLen = 0
  257. } else if testCase.opts.PageSize*page > testCase.count {
  258. expectedLen = testCase.count % testCase.opts.PageSize
  259. }
  260. if assert.Len(t, repos, expectedLen) {
  261. for _, repo := range repos {
  262. assert.NotEmpty(t, repo.Name)
  263. if len(testCase.opts.Keyword) > 0 {
  264. assert.Contains(t, repo.Name, testCase.opts.Keyword)
  265. }
  266. if !testCase.opts.Private {
  267. assert.False(t, repo.IsPrivate)
  268. }
  269. if testCase.opts.Fork == util.OptionalBoolTrue && testCase.opts.Mirror == util.OptionalBoolTrue {
  270. assert.True(t, repo.IsFork || repo.IsMirror)
  271. } else {
  272. switch testCase.opts.Fork {
  273. case util.OptionalBoolFalse:
  274. assert.False(t, repo.IsFork)
  275. case util.OptionalBoolTrue:
  276. assert.True(t, repo.IsFork)
  277. }
  278. switch testCase.opts.Mirror {
  279. case util.OptionalBoolFalse:
  280. assert.False(t, repo.IsMirror)
  281. case util.OptionalBoolTrue:
  282. assert.True(t, repo.IsMirror)
  283. }
  284. }
  285. if testCase.opts.OwnerID > 0 && !testCase.opts.AllPublic {
  286. switch testCase.opts.Collaborate {
  287. case util.OptionalBoolFalse:
  288. assert.Equal(t, testCase.opts.OwnerID, repo.Owner.ID)
  289. case util.OptionalBoolTrue:
  290. assert.NotEqual(t, testCase.opts.OwnerID, repo.Owner.ID)
  291. }
  292. }
  293. }
  294. }
  295. })
  296. }
  297. }
  298. func TestSearchRepositoryByTopicName(t *testing.T) {
  299. assert.NoError(t, unittest.PrepareTestDatabase())
  300. testCases := []struct {
  301. name string
  302. opts *SearchRepoOptions
  303. count int
  304. }{
  305. {
  306. name: "AllPublic/SearchPublicRepositoriesFromTopicAndName",
  307. opts: &SearchRepoOptions{OwnerID: 21, AllPublic: true, Keyword: "graphql"},
  308. count: 2,
  309. },
  310. {
  311. name: "AllPublic/OnlySearchPublicRepositoriesFromTopic",
  312. opts: &SearchRepoOptions{OwnerID: 21, AllPublic: true, Keyword: "graphql", TopicOnly: true},
  313. count: 1,
  314. },
  315. {
  316. name: "AllPublic/OnlySearchMultipleKeywordPublicRepositoriesFromTopic",
  317. opts: &SearchRepoOptions{OwnerID: 21, AllPublic: true, Keyword: "graphql,golang", TopicOnly: true},
  318. count: 2,
  319. },
  320. }
  321. for _, testCase := range testCases {
  322. t.Run(testCase.name, func(t *testing.T) {
  323. _, count, err := SearchRepositoryByName(testCase.opts)
  324. assert.NoError(t, err)
  325. assert.Equal(t, int64(testCase.count), count)
  326. })
  327. }
  328. }