Fork to maintain patches against the official gitea for https://code.ceondo.com https://github.com/go-gitea/gitea

repo_list_test.go 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. Searcher: &User{ID: 14},
  37. })
  38. assert.NoError(t, err)
  39. if assert.Len(t, repos, 1) {
  40. assert.Equal(t, "test_repo_13", repos[0].Name)
  41. }
  42. assert.Equal(t, int64(1), count)
  43. repos, count, err = SearchRepositoryByName(&SearchRepoOptions{
  44. Keyword: "test_repo",
  45. Page: 1,
  46. PageSize: 10,
  47. Private: true,
  48. Searcher: &User{ID: 14},
  49. })
  50. assert.NoError(t, err)
  51. assert.Equal(t, int64(3), count)
  52. assert.Len(t, repos, 3)
  53. testCases := []struct {
  54. name string
  55. opts *SearchRepoOptions
  56. count int
  57. }{
  58. {name: "PublicRepositoriesByName",
  59. opts: &SearchRepoOptions{Keyword: "big_test_", PageSize: 10},
  60. count: 4},
  61. {name: "PublicAndPrivateRepositoriesByName",
  62. opts: &SearchRepoOptions{Keyword: "big_test_", Page: 1, PageSize: 10, Private: true},
  63. count: 8},
  64. {name: "PublicAndPrivateRepositoriesByNameWithPagesizeLimitFirstPage",
  65. opts: &SearchRepoOptions{Keyword: "big_test_", Page: 1, PageSize: 5, Private: true},
  66. count: 8},
  67. {name: "PublicAndPrivateRepositoriesByNameWithPagesizeLimitSecondPage",
  68. opts: &SearchRepoOptions{Keyword: "big_test_", Page: 2, PageSize: 5, Private: true},
  69. count: 8},
  70. {name: "PublicRepositoriesOfUser",
  71. opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 15},
  72. count: 3}, // FIXME: Should return 2 (only directly owned repositories), now includes 1 public repository from owned organization
  73. {name: "PublicAndPrivateRepositoriesOfUser",
  74. opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 15, Private: true},
  75. count: 6}, // FIXME: Should return 4 (only directly owned repositories), now includes 2 repositories from owned organization
  76. {name: "PublicRepositoriesOfUserIncludingCollaborative",
  77. opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 15, Collaborate: true},
  78. count: 4},
  79. {name: "PublicAndPrivateRepositoriesOfUserIncludingCollaborative",
  80. opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 15, Private: true, Collaborate: true},
  81. count: 8},
  82. {name: "PublicRepositoriesOfOrganization",
  83. opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 17},
  84. count: 1},
  85. {name: "PublicAndPrivateRepositoriesOfOrganization",
  86. opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 17, Private: true},
  87. count: 2},
  88. {name: "AllPublic/PublicRepositoriesByName",
  89. opts: &SearchRepoOptions{Keyword: "big_test_", PageSize: 10, AllPublic: true},
  90. count: 4},
  91. {name: "AllPublic/PublicAndPrivateRepositoriesByName",
  92. opts: &SearchRepoOptions{Keyword: "big_test_", Page: 1, PageSize: 10, Private: true, AllPublic: true},
  93. count: 8},
  94. {name: "AllPublic/PublicRepositoriesOfUserIncludingCollaborative",
  95. opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 15, Collaborate: true, AllPublic: true},
  96. count: 12},
  97. {name: "AllPublic/PublicAndPrivateRepositoriesOfUserIncludingCollaborative",
  98. opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 15, Private: true, Collaborate: true, AllPublic: true},
  99. count: 16},
  100. {name: "AllPublic/PublicAndPrivateRepositoriesOfUserIncludingCollaborativeByName",
  101. opts: &SearchRepoOptions{Keyword: "test", Page: 1, PageSize: 10, OwnerID: 15, Private: true, Collaborate: true, AllPublic: true},
  102. count: 10},
  103. {name: "AllPublic/PublicRepositoriesOfOrganization",
  104. opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 17, AllPublic: true},
  105. count: 12},
  106. }
  107. for _, testCase := range testCases {
  108. t.Run(testCase.name, func(t *testing.T) {
  109. if testCase.opts.OwnerID > 0 {
  110. testCase.opts.Searcher = &User{ID: testCase.opts.OwnerID}
  111. }
  112. repos, count, err := SearchRepositoryByName(testCase.opts)
  113. assert.NoError(t, err)
  114. assert.Equal(t, int64(testCase.count), count)
  115. var expectedLen int
  116. if testCase.opts.PageSize*testCase.opts.Page > testCase.count {
  117. expectedLen = testCase.count % testCase.opts.PageSize
  118. } else {
  119. expectedLen = testCase.opts.PageSize
  120. }
  121. assert.Len(t, repos, expectedLen)
  122. for _, repo := range repos {
  123. assert.NotEmpty(t, repo.Name)
  124. if len(testCase.opts.Keyword) > 0 {
  125. assert.Contains(t, repo.Name, testCase.opts.Keyword)
  126. }
  127. // FIXME: Can't check, need to fix current behaviour (see previous FIXME comments in test cases)
  128. /*if testCase.opts.OwnerID > 0 && !testCase.opts.Collaborate && !AllPublic {
  129. assert.Equal(t, testCase.opts.OwnerID, repo.Owner.ID)
  130. }*/
  131. if !testCase.opts.Private {
  132. assert.False(t, repo.IsPrivate)
  133. }
  134. }
  135. })
  136. }
  137. }