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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. {name: "PublicRepositoriesByName",
  103. opts: &SearchRepoOptions{Keyword: "big_test_", ListOptions: ListOptions{PageSize: 10}, Collaborate: util.OptionalBoolFalse},
  104. count: 7},
  105. {name: "PublicAndPrivateRepositoriesByName",
  106. opts: &SearchRepoOptions{Keyword: "big_test_", ListOptions: ListOptions{Page: 1, PageSize: 10}, Private: true, Collaborate: util.OptionalBoolFalse},
  107. count: 14},
  108. {name: "PublicAndPrivateRepositoriesByNameWithPagesizeLimitFirstPage",
  109. opts: &SearchRepoOptions{Keyword: "big_test_", ListOptions: ListOptions{Page: 1, PageSize: 5}, Private: true, Collaborate: util.OptionalBoolFalse},
  110. count: 14},
  111. {name: "PublicAndPrivateRepositoriesByNameWithPagesizeLimitSecondPage",
  112. opts: &SearchRepoOptions{Keyword: "big_test_", ListOptions: ListOptions{Page: 2, PageSize: 5}, Private: true, Collaborate: util.OptionalBoolFalse},
  113. count: 14},
  114. {name: "PublicAndPrivateRepositoriesByNameWithPagesizeLimitThirdPage",
  115. opts: &SearchRepoOptions{Keyword: "big_test_", ListOptions: ListOptions{Page: 3, PageSize: 5}, Private: true, Collaborate: util.OptionalBoolFalse},
  116. count: 14},
  117. {name: "PublicAndPrivateRepositoriesByNameWithPagesizeLimitFourthPage",
  118. opts: &SearchRepoOptions{Keyword: "big_test_", ListOptions: ListOptions{Page: 3, PageSize: 5}, Private: true, Collaborate: util.OptionalBoolFalse},
  119. count: 14},
  120. {name: "PublicRepositoriesOfUser",
  121. opts: &SearchRepoOptions{ListOptions: ListOptions{Page: 1, PageSize: 10}, OwnerID: 15, Collaborate: util.OptionalBoolFalse},
  122. count: 2},
  123. {name: "PublicRepositoriesOfUser2",
  124. opts: &SearchRepoOptions{ListOptions: ListOptions{Page: 1, PageSize: 10}, OwnerID: 18, Collaborate: util.OptionalBoolFalse},
  125. count: 0},
  126. {name: "PublicRepositoriesOfUser3",
  127. opts: &SearchRepoOptions{ListOptions: ListOptions{Page: 1, PageSize: 10}, OwnerID: 20, Collaborate: util.OptionalBoolFalse},
  128. count: 2},
  129. {name: "PublicAndPrivateRepositoriesOfUser",
  130. opts: &SearchRepoOptions{ListOptions: ListOptions{Page: 1, PageSize: 10}, OwnerID: 15, Private: true, Collaborate: util.OptionalBoolFalse},
  131. count: 4},
  132. {name: "PublicAndPrivateRepositoriesOfUser2",
  133. opts: &SearchRepoOptions{ListOptions: ListOptions{Page: 1, PageSize: 10}, OwnerID: 18, Private: true, Collaborate: util.OptionalBoolFalse},
  134. count: 0},
  135. {name: "PublicAndPrivateRepositoriesOfUser3",
  136. opts: &SearchRepoOptions{ListOptions: ListOptions{Page: 1, PageSize: 10}, OwnerID: 20, Private: true, Collaborate: util.OptionalBoolFalse},
  137. count: 4},
  138. {name: "PublicRepositoriesOfUserIncludingCollaborative",
  139. opts: &SearchRepoOptions{ListOptions: ListOptions{Page: 1, PageSize: 10}, OwnerID: 15},
  140. count: 5},
  141. {name: "PublicRepositoriesOfUser2IncludingCollaborative",
  142. opts: &SearchRepoOptions{ListOptions: ListOptions{Page: 1, PageSize: 10}, OwnerID: 18},
  143. count: 1},
  144. {name: "PublicRepositoriesOfUser3IncludingCollaborative",
  145. opts: &SearchRepoOptions{ListOptions: ListOptions{Page: 1, PageSize: 10}, OwnerID: 20},
  146. count: 3},
  147. {name: "PublicAndPrivateRepositoriesOfUserIncludingCollaborative",
  148. opts: &SearchRepoOptions{ListOptions: ListOptions{Page: 1, PageSize: 10}, OwnerID: 15, Private: true},
  149. count: 9},
  150. {name: "PublicAndPrivateRepositoriesOfUser2IncludingCollaborative",
  151. opts: &SearchRepoOptions{ListOptions: ListOptions{Page: 1, PageSize: 10}, OwnerID: 18, Private: true},
  152. count: 4},
  153. {name: "PublicAndPrivateRepositoriesOfUser3IncludingCollaborative",
  154. opts: &SearchRepoOptions{ListOptions: ListOptions{Page: 1, PageSize: 10}, OwnerID: 20, Private: true},
  155. count: 7},
  156. {name: "PublicRepositoriesOfOrganization",
  157. opts: &SearchRepoOptions{ListOptions: ListOptions{Page: 1, PageSize: 10}, OwnerID: 17, Collaborate: util.OptionalBoolFalse},
  158. count: 1},
  159. {name: "PublicAndPrivateRepositoriesOfOrganization",
  160. opts: &SearchRepoOptions{ListOptions: ListOptions{Page: 1, PageSize: 10}, OwnerID: 17, Private: true, Collaborate: util.OptionalBoolFalse},
  161. count: 2},
  162. {name: "AllPublic/PublicRepositoriesByName",
  163. opts: &SearchRepoOptions{Keyword: "big_test_", ListOptions: ListOptions{PageSize: 10}, AllPublic: true, Collaborate: util.OptionalBoolFalse},
  164. count: 7},
  165. {name: "AllPublic/PublicAndPrivateRepositoriesByName",
  166. opts: &SearchRepoOptions{Keyword: "big_test_", ListOptions: ListOptions{Page: 1, PageSize: 10}, Private: true, AllPublic: true, Collaborate: util.OptionalBoolFalse},
  167. count: 14},
  168. {name: "AllPublic/PublicRepositoriesOfUserIncludingCollaborative",
  169. opts: &SearchRepoOptions{ListOptions: ListOptions{Page: 1, PageSize: 10}, OwnerID: 15, AllPublic: true, Template: util.OptionalBoolFalse},
  170. count: 25},
  171. {name: "AllPublic/PublicAndPrivateRepositoriesOfUserIncludingCollaborative",
  172. opts: &SearchRepoOptions{ListOptions: ListOptions{Page: 1, PageSize: 10}, OwnerID: 15, Private: true, AllPublic: true, AllLimited: true, Template: util.OptionalBoolFalse},
  173. count: 30},
  174. {name: "AllPublic/PublicAndPrivateRepositoriesOfUserIncludingCollaborativeByName",
  175. opts: &SearchRepoOptions{Keyword: "test", ListOptions: ListOptions{Page: 1, PageSize: 10}, OwnerID: 15, Private: true, AllPublic: true},
  176. count: 15},
  177. {name: "AllPublic/PublicAndPrivateRepositoriesOfUser2IncludingCollaborativeByName",
  178. opts: &SearchRepoOptions{Keyword: "test", ListOptions: ListOptions{Page: 1, PageSize: 10}, OwnerID: 18, Private: true, AllPublic: true},
  179. count: 13},
  180. {name: "AllPublic/PublicRepositoriesOfOrganization",
  181. opts: &SearchRepoOptions{ListOptions: ListOptions{Page: 1, PageSize: 10}, OwnerID: 17, AllPublic: true, Collaborate: util.OptionalBoolFalse, Template: util.OptionalBoolFalse},
  182. count: 25},
  183. {name: "AllTemplates",
  184. opts: &SearchRepoOptions{ListOptions: ListOptions{Page: 1, PageSize: 10}, Template: util.OptionalBoolTrue},
  185. count: 2},
  186. }
  187. for _, testCase := range testCases {
  188. t.Run(testCase.name, func(t *testing.T) {
  189. repos, count, err := SearchRepositoryByName(testCase.opts)
  190. assert.NoError(t, err)
  191. assert.Equal(t, int64(testCase.count), count)
  192. page := testCase.opts.Page
  193. if page <= 0 {
  194. page = 1
  195. }
  196. var expectedLen = testCase.opts.PageSize
  197. if testCase.opts.PageSize*page > testCase.count+testCase.opts.PageSize {
  198. expectedLen = 0
  199. } else if testCase.opts.PageSize*page > testCase.count {
  200. expectedLen = testCase.count % testCase.opts.PageSize
  201. }
  202. if assert.Len(t, repos, expectedLen) {
  203. for _, repo := range repos {
  204. assert.NotEmpty(t, repo.Name)
  205. if len(testCase.opts.Keyword) > 0 {
  206. assert.Contains(t, repo.Name, testCase.opts.Keyword)
  207. }
  208. if !testCase.opts.Private {
  209. assert.False(t, repo.IsPrivate)
  210. }
  211. if testCase.opts.Fork == util.OptionalBoolTrue && testCase.opts.Mirror == util.OptionalBoolTrue {
  212. assert.True(t, repo.IsFork || repo.IsMirror)
  213. } else {
  214. switch testCase.opts.Fork {
  215. case util.OptionalBoolFalse:
  216. assert.False(t, repo.IsFork)
  217. case util.OptionalBoolTrue:
  218. assert.True(t, repo.IsFork)
  219. }
  220. switch testCase.opts.Mirror {
  221. case util.OptionalBoolFalse:
  222. assert.False(t, repo.IsMirror)
  223. case util.OptionalBoolTrue:
  224. assert.True(t, repo.IsMirror)
  225. }
  226. }
  227. if testCase.opts.OwnerID > 0 && !testCase.opts.AllPublic {
  228. switch testCase.opts.Collaborate {
  229. case util.OptionalBoolFalse:
  230. assert.Equal(t, testCase.opts.OwnerID, repo.Owner.ID)
  231. case util.OptionalBoolTrue:
  232. assert.NotEqual(t, testCase.opts.OwnerID, repo.Owner.ID)
  233. }
  234. }
  235. }
  236. }
  237. })
  238. }
  239. }
  240. func TestSearchRepositoryByTopicName(t *testing.T) {
  241. assert.NoError(t, PrepareTestDatabase())
  242. testCases := []struct {
  243. name string
  244. opts *SearchRepoOptions
  245. count int
  246. }{
  247. {name: "AllPublic/SearchPublicRepositoriesFromTopicAndName",
  248. opts: &SearchRepoOptions{OwnerID: 21, AllPublic: true, Keyword: "graphql"},
  249. count: 2},
  250. {name: "AllPublic/OnlySearchPublicRepositoriesFromTopic",
  251. opts: &SearchRepoOptions{OwnerID: 21, AllPublic: true, Keyword: "graphql", TopicOnly: true},
  252. count: 1},
  253. {name: "AllPublic/OnlySearchMultipleKeywordPublicRepositoriesFromTopic",
  254. opts: &SearchRepoOptions{OwnerID: 21, AllPublic: true, Keyword: "graphql,golang", TopicOnly: true},
  255. count: 2},
  256. }
  257. for _, testCase := range testCases {
  258. t.Run(testCase.name, func(t *testing.T) {
  259. _, count, err := SearchRepositoryByName(testCase.opts)
  260. assert.NoError(t, err)
  261. assert.Equal(t, int64(testCase.count), count)
  262. })
  263. }
  264. }