You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

repo_list_test.go 5.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. "github.com/stretchr/testify/assert"
  8. )
  9. func TestSearchRepositoryByName(t *testing.T) {
  10. assert.NoError(t, PrepareTestDatabase())
  11. // test search public repository on explore page
  12. repos, count, err := SearchRepositoryByName(&SearchRepoOptions{
  13. Keyword: "repo_12",
  14. Page: 1,
  15. PageSize: 10,
  16. })
  17. assert.NoError(t, err)
  18. if assert.Len(t, repos, 1) {
  19. assert.Equal(t, "test_repo_12", repos[0].Name)
  20. }
  21. assert.Equal(t, int64(1), count)
  22. repos, count, err = SearchRepositoryByName(&SearchRepoOptions{
  23. Keyword: "test_repo",
  24. Page: 1,
  25. PageSize: 10,
  26. })
  27. assert.NoError(t, err)
  28. assert.Equal(t, int64(2), count)
  29. assert.Len(t, repos, 2)
  30. // test search private repository on explore page
  31. repos, count, err = SearchRepositoryByName(&SearchRepoOptions{
  32. Keyword: "repo_13",
  33. Page: 1,
  34. PageSize: 10,
  35. Private: true,
  36. })
  37. assert.NoError(t, err)
  38. if assert.Len(t, repos, 1) {
  39. assert.Equal(t, "test_repo_13", repos[0].Name)
  40. }
  41. assert.Equal(t, int64(1), count)
  42. repos, count, err = SearchRepositoryByName(&SearchRepoOptions{
  43. Keyword: "test_repo",
  44. Page: 1,
  45. PageSize: 10,
  46. Private: true,
  47. })
  48. assert.NoError(t, err)
  49. assert.Equal(t, int64(3), count)
  50. assert.Len(t, repos, 3)
  51. testCases := []struct {
  52. name string
  53. opts *SearchRepoOptions
  54. count int
  55. }{
  56. {name: "PublicRepositoriesByName",
  57. opts: &SearchRepoOptions{Keyword: "big_test_", PageSize: 10},
  58. count: 4},
  59. {name: "PublicAndPrivateRepositoriesByName",
  60. opts: &SearchRepoOptions{Keyword: "big_test_", Page: 1, PageSize: 10, Private: true},
  61. count: 8},
  62. {name: "PublicAndPrivateRepositoriesByNameWithPagesizeLimitFirstPage",
  63. opts: &SearchRepoOptions{Keyword: "big_test_", Page: 1, PageSize: 5, Private: true},
  64. count: 8},
  65. {name: "PublicAndPrivateRepositoriesByNameWithPagesizeLimitSecondPage",
  66. opts: &SearchRepoOptions{Keyword: "big_test_", Page: 2, PageSize: 5, Private: true},
  67. count: 8},
  68. {name: "PublicRepositoriesOfUser",
  69. opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 15},
  70. count: 2},
  71. {name: "PublicRepositoriesOfUser2",
  72. opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 18},
  73. count: 0},
  74. {name: "PublicAndPrivateRepositoriesOfUser",
  75. opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 15, Private: true},
  76. count: 4},
  77. {name: "PublicAndPrivateRepositoriesOfUser2",
  78. opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 18, Private: true},
  79. count: 0},
  80. {name: "PublicRepositoriesOfUserIncludingCollaborative",
  81. opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 15, Collaborate: true},
  82. count: 4},
  83. {name: "PublicRepositoriesOfUser2IncludingCollaborative",
  84. opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 18, Collaborate: true},
  85. count: 1},
  86. {name: "PublicAndPrivateRepositoriesOfUserIncludingCollaborative",
  87. opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 15, Private: true, Collaborate: true},
  88. count: 8},
  89. {name: "PublicAndPrivateRepositoriesOfUser2IncludingCollaborative",
  90. opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 18, Private: true, Collaborate: true},
  91. count: 4},
  92. {name: "PublicRepositoriesOfOrganization",
  93. opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 17},
  94. count: 1},
  95. {name: "PublicAndPrivateRepositoriesOfOrganization",
  96. opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 17, Private: true},
  97. count: 2},
  98. {name: "AllPublic/PublicRepositoriesByName",
  99. opts: &SearchRepoOptions{Keyword: "big_test_", PageSize: 10, AllPublic: true},
  100. count: 4},
  101. {name: "AllPublic/PublicAndPrivateRepositoriesByName",
  102. opts: &SearchRepoOptions{Keyword: "big_test_", Page: 1, PageSize: 10, Private: true, AllPublic: true},
  103. count: 8},
  104. {name: "AllPublic/PublicRepositoriesOfUserIncludingCollaborative",
  105. opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 15, Collaborate: true, AllPublic: true},
  106. count: 12},
  107. {name: "AllPublic/PublicAndPrivateRepositoriesOfUserIncludingCollaborative",
  108. opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 15, Private: true, Collaborate: true, AllPublic: true},
  109. count: 16},
  110. {name: "AllPublic/PublicAndPrivateRepositoriesOfUserIncludingCollaborativeByName",
  111. opts: &SearchRepoOptions{Keyword: "test", Page: 1, PageSize: 10, OwnerID: 15, Private: true, Collaborate: true, AllPublic: true},
  112. count: 10},
  113. {name: "AllPublic/PublicAndPrivateRepositoriesOfUser2IncludingCollaborativeByName",
  114. opts: &SearchRepoOptions{Keyword: "test", Page: 1, PageSize: 10, OwnerID: 18, Private: true, Collaborate: true, AllPublic: true},
  115. count: 8},
  116. {name: "AllPublic/PublicRepositoriesOfOrganization",
  117. opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 17, AllPublic: true},
  118. count: 12},
  119. }
  120. for _, testCase := range testCases {
  121. t.Run(testCase.name, func(t *testing.T) {
  122. repos, count, err := SearchRepositoryByName(testCase.opts)
  123. assert.NoError(t, err)
  124. assert.Equal(t, int64(testCase.count), count)
  125. var expectedLen int
  126. if testCase.opts.PageSize*testCase.opts.Page > testCase.count {
  127. expectedLen = testCase.count % testCase.opts.PageSize
  128. } else {
  129. expectedLen = testCase.opts.PageSize
  130. }
  131. assert.Len(t, repos, expectedLen)
  132. for _, repo := range repos {
  133. assert.NotEmpty(t, repo.Name)
  134. if len(testCase.opts.Keyword) > 0 {
  135. assert.Contains(t, repo.Name, testCase.opts.Keyword)
  136. }
  137. if testCase.opts.OwnerID > 0 && !testCase.opts.Collaborate && !testCase.opts.AllPublic {
  138. assert.Equal(t, testCase.opts.OwnerID, repo.Owner.ID)
  139. }
  140. if !testCase.opts.Private {
  141. assert.False(t, repo.IsPrivate)
  142. }
  143. }
  144. })
  145. }
  146. }