本站源代码
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.

281 lines
10.0KB

  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. Keyword: "repo_12",
  15. Page: 1,
  16. PageSize: 10,
  17. Collaborate: util.OptionalBoolFalse,
  18. })
  19. assert.NoError(t, err)
  20. if assert.Len(t, repos, 1) {
  21. assert.Equal(t, "test_repo_12", repos[0].Name)
  22. }
  23. assert.Equal(t, int64(1), count)
  24. repos, count, err = SearchRepositoryByName(&SearchRepoOptions{
  25. Keyword: "test_repo",
  26. Page: 1,
  27. PageSize: 10,
  28. Collaborate: util.OptionalBoolFalse,
  29. })
  30. assert.NoError(t, err)
  31. assert.Equal(t, int64(2), count)
  32. assert.Len(t, repos, 2)
  33. // test search private repository on explore page
  34. repos, count, err = SearchRepositoryByName(&SearchRepoOptions{
  35. Keyword: "repo_13",
  36. Page: 1,
  37. PageSize: 10,
  38. Private: true,
  39. Collaborate: util.OptionalBoolFalse,
  40. })
  41. assert.NoError(t, err)
  42. if assert.Len(t, repos, 1) {
  43. assert.Equal(t, "test_repo_13", repos[0].Name)
  44. }
  45. assert.Equal(t, int64(1), count)
  46. repos, count, err = SearchRepositoryByName(&SearchRepoOptions{
  47. Keyword: "test_repo",
  48. Page: 1,
  49. PageSize: 10,
  50. Private: true,
  51. Collaborate: util.OptionalBoolFalse,
  52. })
  53. assert.NoError(t, err)
  54. assert.Equal(t, int64(3), count)
  55. assert.Len(t, repos, 3)
  56. // Test non existing owner
  57. repos, count, err = SearchRepositoryByName(&SearchRepoOptions{OwnerID: NonexistentID})
  58. assert.NoError(t, err)
  59. assert.Empty(t, repos)
  60. assert.Equal(t, int64(0), count)
  61. // Test search within description
  62. repos, count, err = SearchRepository(&SearchRepoOptions{
  63. Keyword: "description_14",
  64. Page: 1,
  65. PageSize: 10,
  66. Collaborate: util.OptionalBoolFalse,
  67. IncludeDescription: true,
  68. })
  69. assert.NoError(t, err)
  70. if assert.Len(t, repos, 1) {
  71. assert.Equal(t, "test_repo_14", repos[0].Name)
  72. }
  73. assert.Equal(t, int64(1), count)
  74. // Test NOT search within description
  75. repos, count, err = SearchRepository(&SearchRepoOptions{
  76. Keyword: "description_14",
  77. Page: 1,
  78. PageSize: 10,
  79. Collaborate: util.OptionalBoolFalse,
  80. IncludeDescription: false,
  81. })
  82. assert.NoError(t, err)
  83. assert.Empty(t, repos)
  84. assert.Equal(t, int64(0), count)
  85. testCases := []struct {
  86. name string
  87. opts *SearchRepoOptions
  88. count int
  89. }{
  90. {name: "PublicRepositoriesByName",
  91. opts: &SearchRepoOptions{Keyword: "big_test_", PageSize: 10, Collaborate: util.OptionalBoolFalse},
  92. count: 7},
  93. {name: "PublicAndPrivateRepositoriesByName",
  94. opts: &SearchRepoOptions{Keyword: "big_test_", Page: 1, PageSize: 10, Private: true, Collaborate: util.OptionalBoolFalse},
  95. count: 14},
  96. {name: "PublicAndPrivateRepositoriesByNameWithPagesizeLimitFirstPage",
  97. opts: &SearchRepoOptions{Keyword: "big_test_", Page: 1, PageSize: 5, Private: true, Collaborate: util.OptionalBoolFalse},
  98. count: 14},
  99. {name: "PublicAndPrivateRepositoriesByNameWithPagesizeLimitSecondPage",
  100. opts: &SearchRepoOptions{Keyword: "big_test_", Page: 2, PageSize: 5, Private: true, Collaborate: util.OptionalBoolFalse},
  101. count: 14},
  102. {name: "PublicAndPrivateRepositoriesByNameWithPagesizeLimitThirdPage",
  103. opts: &SearchRepoOptions{Keyword: "big_test_", Page: 3, PageSize: 5, Private: true, Collaborate: util.OptionalBoolFalse},
  104. count: 14},
  105. {name: "PublicAndPrivateRepositoriesByNameWithPagesizeLimitFourthPage",
  106. opts: &SearchRepoOptions{Keyword: "big_test_", Page: 3, PageSize: 5, Private: true, Collaborate: util.OptionalBoolFalse},
  107. count: 14},
  108. {name: "PublicRepositoriesOfUser",
  109. opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 15, Collaborate: util.OptionalBoolFalse},
  110. count: 2},
  111. {name: "PublicRepositoriesOfUser2",
  112. opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 18, Collaborate: util.OptionalBoolFalse},
  113. count: 0},
  114. {name: "PublicRepositoriesOfUser3",
  115. opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 20, Collaborate: util.OptionalBoolFalse},
  116. count: 2},
  117. {name: "PublicAndPrivateRepositoriesOfUser",
  118. opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 15, Private: true, Collaborate: util.OptionalBoolFalse},
  119. count: 4},
  120. {name: "PublicAndPrivateRepositoriesOfUser2",
  121. opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 18, Private: true, Collaborate: util.OptionalBoolFalse},
  122. count: 0},
  123. {name: "PublicAndPrivateRepositoriesOfUser3",
  124. opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 20, Private: true, Collaborate: util.OptionalBoolFalse},
  125. count: 4},
  126. {name: "PublicRepositoriesOfUserIncludingCollaborative",
  127. opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 15},
  128. count: 5},
  129. {name: "PublicRepositoriesOfUser2IncludingCollaborative",
  130. opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 18},
  131. count: 1},
  132. {name: "PublicRepositoriesOfUser3IncludingCollaborative",
  133. opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 20},
  134. count: 3},
  135. {name: "PublicAndPrivateRepositoriesOfUserIncludingCollaborative",
  136. opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 15, Private: true},
  137. count: 9},
  138. {name: "PublicAndPrivateRepositoriesOfUser2IncludingCollaborative",
  139. opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 18, Private: true},
  140. count: 4},
  141. {name: "PublicAndPrivateRepositoriesOfUser3IncludingCollaborative",
  142. opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 20, Private: true},
  143. count: 7},
  144. {name: "PublicRepositoriesOfOrganization",
  145. opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 17, Collaborate: util.OptionalBoolFalse},
  146. count: 1},
  147. {name: "PublicAndPrivateRepositoriesOfOrganization",
  148. opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 17, Private: true, Collaborate: util.OptionalBoolFalse},
  149. count: 2},
  150. {name: "AllPublic/PublicRepositoriesByName",
  151. opts: &SearchRepoOptions{Keyword: "big_test_", PageSize: 10, AllPublic: true, Collaborate: util.OptionalBoolFalse},
  152. count: 7},
  153. {name: "AllPublic/PublicAndPrivateRepositoriesByName",
  154. opts: &SearchRepoOptions{Keyword: "big_test_", Page: 1, PageSize: 10, Private: true, AllPublic: true, Collaborate: util.OptionalBoolFalse},
  155. count: 14},
  156. {name: "AllPublic/PublicRepositoriesOfUserIncludingCollaborative",
  157. opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 15, AllPublic: true},
  158. count: 22},
  159. {name: "AllPublic/PublicAndPrivateRepositoriesOfUserIncludingCollaborative",
  160. opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 15, Private: true, AllPublic: true},
  161. count: 28},
  162. {name: "AllPublic/PublicAndPrivateRepositoriesOfUserIncludingCollaborativeByName",
  163. opts: &SearchRepoOptions{Keyword: "test", Page: 1, PageSize: 10, OwnerID: 15, Private: true, AllPublic: true},
  164. count: 15},
  165. {name: "AllPublic/PublicAndPrivateRepositoriesOfUser2IncludingCollaborativeByName",
  166. opts: &SearchRepoOptions{Keyword: "test", Page: 1, PageSize: 10, OwnerID: 18, Private: true, AllPublic: true},
  167. count: 13},
  168. {name: "AllPublic/PublicRepositoriesOfOrganization",
  169. opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 17, AllPublic: true, Collaborate: util.OptionalBoolFalse},
  170. count: 22},
  171. }
  172. for _, testCase := range testCases {
  173. t.Run(testCase.name, func(t *testing.T) {
  174. repos, count, err := SearchRepositoryByName(testCase.opts)
  175. assert.NoError(t, err)
  176. assert.Equal(t, int64(testCase.count), count)
  177. page := testCase.opts.Page
  178. if page <= 0 {
  179. page = 1
  180. }
  181. var expectedLen = testCase.opts.PageSize
  182. if testCase.opts.PageSize*page > testCase.count+testCase.opts.PageSize {
  183. expectedLen = 0
  184. } else if testCase.opts.PageSize*page > testCase.count {
  185. expectedLen = testCase.count % testCase.opts.PageSize
  186. }
  187. if assert.Len(t, repos, expectedLen) {
  188. for _, repo := range repos {
  189. assert.NotEmpty(t, repo.Name)
  190. if len(testCase.opts.Keyword) > 0 {
  191. assert.Contains(t, repo.Name, testCase.opts.Keyword)
  192. }
  193. if !testCase.opts.Private {
  194. assert.False(t, repo.IsPrivate)
  195. }
  196. if testCase.opts.Fork == util.OptionalBoolTrue && testCase.opts.Mirror == util.OptionalBoolTrue {
  197. assert.True(t, repo.IsFork || repo.IsMirror)
  198. } else {
  199. switch testCase.opts.Fork {
  200. case util.OptionalBoolFalse:
  201. assert.False(t, repo.IsFork)
  202. case util.OptionalBoolTrue:
  203. assert.True(t, repo.IsFork)
  204. }
  205. switch testCase.opts.Mirror {
  206. case util.OptionalBoolFalse:
  207. assert.False(t, repo.IsMirror)
  208. case util.OptionalBoolTrue:
  209. assert.True(t, repo.IsMirror)
  210. }
  211. }
  212. if testCase.opts.OwnerID > 0 && !testCase.opts.AllPublic {
  213. switch testCase.opts.Collaborate {
  214. case util.OptionalBoolFalse:
  215. assert.Equal(t, testCase.opts.OwnerID, repo.Owner.ID)
  216. case util.OptionalBoolTrue:
  217. assert.NotEqual(t, testCase.opts.OwnerID, repo.Owner.ID)
  218. }
  219. }
  220. }
  221. }
  222. })
  223. }
  224. }
  225. func TestSearchRepositoryByTopicName(t *testing.T) {
  226. assert.NoError(t, PrepareTestDatabase())
  227. testCases := []struct {
  228. name string
  229. opts *SearchRepoOptions
  230. count int
  231. }{
  232. {name: "AllPublic/SearchPublicRepositoriesFromTopicAndName",
  233. opts: &SearchRepoOptions{OwnerID: 21, AllPublic: true, Keyword: "graphql"},
  234. count: 2},
  235. {name: "AllPublic/OnlySearchPublicRepositoriesFromTopic",
  236. opts: &SearchRepoOptions{OwnerID: 21, AllPublic: true, Keyword: "graphql", TopicOnly: true},
  237. count: 1},
  238. {name: "AllPublic/OnlySearchMultipleKeywordPublicRepositoriesFromTopic",
  239. opts: &SearchRepoOptions{OwnerID: 21, AllPublic: true, Keyword: "graphql,golang", TopicOnly: true},
  240. count: 2},
  241. }
  242. for _, testCase := range testCases {
  243. t.Run(testCase.name, func(t *testing.T) {
  244. _, count, err := SearchRepositoryByName(testCase.opts)
  245. assert.NoError(t, err)
  246. assert.Equal(t, int64(testCase.count), count)
  247. })
  248. }
  249. }
上海开阖软件有限公司 沪ICP备12045867号-1