您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

repo_list_test.go 11KB

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