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 11KB

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