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.

252 lines
9.2 KiB

  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 TestSearchRepositoryByName(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. testCases := []struct {
  62. name string
  63. opts *SearchRepoOptions
  64. count int
  65. }{
  66. {name: "PublicRepositoriesByName",
  67. opts: &SearchRepoOptions{Keyword: "big_test_", PageSize: 10, Collaborate: util.OptionalBoolFalse},
  68. count: 7},
  69. {name: "PublicAndPrivateRepositoriesByName",
  70. opts: &SearchRepoOptions{Keyword: "big_test_", Page: 1, PageSize: 10, Private: true, Collaborate: util.OptionalBoolFalse},
  71. count: 14},
  72. {name: "PublicAndPrivateRepositoriesByNameWithPagesizeLimitFirstPage",
  73. opts: &SearchRepoOptions{Keyword: "big_test_", Page: 1, PageSize: 5, Private: true, Collaborate: util.OptionalBoolFalse},
  74. count: 14},
  75. {name: "PublicAndPrivateRepositoriesByNameWithPagesizeLimitSecondPage",
  76. opts: &SearchRepoOptions{Keyword: "big_test_", Page: 2, PageSize: 5, Private: true, Collaborate: util.OptionalBoolFalse},
  77. count: 14},
  78. {name: "PublicAndPrivateRepositoriesByNameWithPagesizeLimitThirdPage",
  79. opts: &SearchRepoOptions{Keyword: "big_test_", Page: 3, PageSize: 5, Private: true, Collaborate: util.OptionalBoolFalse},
  80. count: 14},
  81. {name: "PublicAndPrivateRepositoriesByNameWithPagesizeLimitFourthPage",
  82. opts: &SearchRepoOptions{Keyword: "big_test_", Page: 3, PageSize: 5, Private: true, Collaborate: util.OptionalBoolFalse},
  83. count: 14},
  84. {name: "PublicRepositoriesOfUser",
  85. opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 15, Collaborate: util.OptionalBoolFalse},
  86. count: 2},
  87. {name: "PublicRepositoriesOfUser2",
  88. opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 18, Collaborate: util.OptionalBoolFalse},
  89. count: 0},
  90. {name: "PublicRepositoriesOfUser3",
  91. opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 20, Collaborate: util.OptionalBoolFalse},
  92. count: 2},
  93. {name: "PublicAndPrivateRepositoriesOfUser",
  94. opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 15, Private: true, Collaborate: util.OptionalBoolFalse},
  95. count: 4},
  96. {name: "PublicAndPrivateRepositoriesOfUser2",
  97. opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 18, Private: true, Collaborate: util.OptionalBoolFalse},
  98. count: 0},
  99. {name: "PublicAndPrivateRepositoriesOfUser3",
  100. opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 20, Private: true, Collaborate: util.OptionalBoolFalse},
  101. count: 4},
  102. {name: "PublicRepositoriesOfUserIncludingCollaborative",
  103. opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 15},
  104. count: 4},
  105. {name: "PublicRepositoriesOfUser2IncludingCollaborative",
  106. opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 18},
  107. count: 1},
  108. {name: "PublicRepositoriesOfUser3IncludingCollaborative",
  109. opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 20},
  110. count: 3},
  111. {name: "PublicAndPrivateRepositoriesOfUserIncludingCollaborative",
  112. opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 15, Private: true},
  113. count: 8},
  114. {name: "PublicAndPrivateRepositoriesOfUser2IncludingCollaborative",
  115. opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 18, Private: true},
  116. count: 4},
  117. {name: "PublicAndPrivateRepositoriesOfUser3IncludingCollaborative",
  118. opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 20, Private: true},
  119. count: 6},
  120. {name: "PublicRepositoriesOfOrganization",
  121. opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 17, Collaborate: util.OptionalBoolFalse},
  122. count: 1},
  123. {name: "PublicAndPrivateRepositoriesOfOrganization",
  124. opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 17, Private: true, Collaborate: util.OptionalBoolFalse},
  125. count: 2},
  126. {name: "AllPublic/PublicRepositoriesByName",
  127. opts: &SearchRepoOptions{Keyword: "big_test_", PageSize: 10, AllPublic: true, Collaborate: util.OptionalBoolFalse},
  128. count: 7},
  129. {name: "AllPublic/PublicAndPrivateRepositoriesByName",
  130. opts: &SearchRepoOptions{Keyword: "big_test_", Page: 1, PageSize: 10, Private: true, AllPublic: true, Collaborate: util.OptionalBoolFalse},
  131. count: 14},
  132. {name: "AllPublic/PublicRepositoriesOfUserIncludingCollaborative",
  133. opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 15, AllPublic: true},
  134. count: 19},
  135. {name: "AllPublic/PublicAndPrivateRepositoriesOfUserIncludingCollaborative",
  136. opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 15, Private: true, AllPublic: true},
  137. count: 23},
  138. {name: "AllPublic/PublicAndPrivateRepositoriesOfUserIncludingCollaborativeByName",
  139. opts: &SearchRepoOptions{Keyword: "test", Page: 1, PageSize: 10, OwnerID: 15, Private: true, AllPublic: true},
  140. count: 13},
  141. {name: "AllPublic/PublicAndPrivateRepositoriesOfUser2IncludingCollaborativeByName",
  142. opts: &SearchRepoOptions{Keyword: "test", Page: 1, PageSize: 10, OwnerID: 18, Private: true, AllPublic: true},
  143. count: 11},
  144. {name: "AllPublic/PublicRepositoriesOfOrganization",
  145. opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 17, AllPublic: true, Collaborate: util.OptionalBoolFalse},
  146. count: 19},
  147. }
  148. for _, testCase := range testCases {
  149. t.Run(testCase.name, func(t *testing.T) {
  150. repos, count, err := SearchRepositoryByName(testCase.opts)
  151. assert.NoError(t, err)
  152. assert.Equal(t, int64(testCase.count), count)
  153. page := testCase.opts.Page
  154. if page <= 0 {
  155. page = 1
  156. }
  157. var expectedLen = testCase.opts.PageSize
  158. if testCase.opts.PageSize*page > testCase.count+testCase.opts.PageSize {
  159. expectedLen = 0
  160. } else if testCase.opts.PageSize*page > testCase.count {
  161. expectedLen = testCase.count % testCase.opts.PageSize
  162. }
  163. if assert.Len(t, repos, expectedLen) {
  164. for _, repo := range repos {
  165. assert.NotEmpty(t, repo.Name)
  166. if len(testCase.opts.Keyword) > 0 {
  167. assert.Contains(t, repo.Name, testCase.opts.Keyword)
  168. }
  169. if !testCase.opts.Private {
  170. assert.False(t, repo.IsPrivate)
  171. }
  172. if testCase.opts.Fork == util.OptionalBoolTrue && testCase.opts.Mirror == util.OptionalBoolTrue {
  173. assert.True(t, repo.IsFork || repo.IsMirror)
  174. } else {
  175. switch testCase.opts.Fork {
  176. case util.OptionalBoolFalse:
  177. assert.False(t, repo.IsFork)
  178. case util.OptionalBoolTrue:
  179. assert.True(t, repo.IsFork)
  180. }
  181. switch testCase.opts.Mirror {
  182. case util.OptionalBoolFalse:
  183. assert.False(t, repo.IsMirror)
  184. case util.OptionalBoolTrue:
  185. assert.True(t, repo.IsMirror)
  186. }
  187. }
  188. if testCase.opts.OwnerID > 0 && !testCase.opts.AllPublic {
  189. switch testCase.opts.Collaborate {
  190. case util.OptionalBoolFalse:
  191. assert.Equal(t, testCase.opts.OwnerID, repo.Owner.ID)
  192. case util.OptionalBoolTrue:
  193. assert.NotEqual(t, testCase.opts.OwnerID, repo.Owner.ID)
  194. }
  195. }
  196. }
  197. }
  198. })
  199. }
  200. }
  201. func TestSearchRepositoryByTopicName(t *testing.T) {
  202. assert.NoError(t, PrepareTestDatabase())
  203. testCases := []struct {
  204. name string
  205. opts *SearchRepoOptions
  206. count int
  207. }{
  208. {name: "AllPublic/SearchPublicRepositoriesFromTopicAndName",
  209. opts: &SearchRepoOptions{OwnerID: 21, AllPublic: true, Keyword: "graphql"},
  210. count: 2},
  211. {name: "AllPublic/OnlySearchPublicRepositoriesFromTopic",
  212. opts: &SearchRepoOptions{OwnerID: 21, AllPublic: true, Keyword: "graphql", TopicOnly: true},
  213. count: 1},
  214. {name: "AllPublic/OnlySearchMultipleKeywordPublicRepositoriesFromTopic",
  215. opts: &SearchRepoOptions{OwnerID: 21, AllPublic: true, Keyword: "graphql,golang", TopicOnly: true},
  216. count: 2},
  217. }
  218. for _, testCase := range testCases {
  219. t.Run(testCase.name, func(t *testing.T) {
  220. _, count, err := SearchRepositoryByName(testCase.opts)
  221. assert.NoError(t, err)
  222. assert.Equal(t, int64(testCase.count), count)
  223. })
  224. }
  225. }