Add search mode option to /api/repo/search (#2756)
* Add repo type option to /api/repo/search * Add tests and fix result of collaborative filter in specific condition * Fix/optimize search & tests * Improve integration tests * Fix lint errors * Fix unit tests * Change and improve internal implementation of repo search * Use NonexistentID * Make search api more general * Change mirror and fork search behaviour * Fix tests & typo in comment
This commit is contained in:
@ -7,6 +7,8 @@ package models
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
@ -15,9 +17,10 @@ func TestSearchRepositoryByName(t *testing.T) {
|
||||
|
||||
// test search public repository on explore page
|
||||
repos, count, err := SearchRepositoryByName(&SearchRepoOptions{
|
||||
Keyword: "repo_12",
|
||||
Page: 1,
|
||||
PageSize: 10,
|
||||
Keyword: "repo_12",
|
||||
Page: 1,
|
||||
PageSize: 10,
|
||||
Collaborate: util.OptionalBoolFalse,
|
||||
})
|
||||
|
||||
assert.NoError(t, err)
|
||||
@ -27,9 +30,10 @@ func TestSearchRepositoryByName(t *testing.T) {
|
||||
assert.Equal(t, int64(1), count)
|
||||
|
||||
repos, count, err = SearchRepositoryByName(&SearchRepoOptions{
|
||||
Keyword: "test_repo",
|
||||
Page: 1,
|
||||
PageSize: 10,
|
||||
Keyword: "test_repo",
|
||||
Page: 1,
|
||||
PageSize: 10,
|
||||
Collaborate: util.OptionalBoolFalse,
|
||||
})
|
||||
|
||||
assert.NoError(t, err)
|
||||
@ -38,10 +42,11 @@ func TestSearchRepositoryByName(t *testing.T) {
|
||||
|
||||
// test search private repository on explore page
|
||||
repos, count, err = SearchRepositoryByName(&SearchRepoOptions{
|
||||
Keyword: "repo_13",
|
||||
Page: 1,
|
||||
PageSize: 10,
|
||||
Private: true,
|
||||
Keyword: "repo_13",
|
||||
Page: 1,
|
||||
PageSize: 10,
|
||||
Private: true,
|
||||
Collaborate: util.OptionalBoolFalse,
|
||||
})
|
||||
|
||||
assert.NoError(t, err)
|
||||
@ -51,84 +56,110 @@ func TestSearchRepositoryByName(t *testing.T) {
|
||||
assert.Equal(t, int64(1), count)
|
||||
|
||||
repos, count, err = SearchRepositoryByName(&SearchRepoOptions{
|
||||
Keyword: "test_repo",
|
||||
Page: 1,
|
||||
PageSize: 10,
|
||||
Private: true,
|
||||
Keyword: "test_repo",
|
||||
Page: 1,
|
||||
PageSize: 10,
|
||||
Private: true,
|
||||
Collaborate: util.OptionalBoolFalse,
|
||||
})
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, int64(3), count)
|
||||
assert.Len(t, repos, 3)
|
||||
|
||||
// Test non existing owner
|
||||
repos, count, err = SearchRepositoryByName(&SearchRepoOptions{OwnerID: NonexistentID})
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Empty(t, repos)
|
||||
assert.Equal(t, int64(0), count)
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
opts *SearchRepoOptions
|
||||
count int
|
||||
}{
|
||||
{name: "PublicRepositoriesByName",
|
||||
opts: &SearchRepoOptions{Keyword: "big_test_", PageSize: 10},
|
||||
count: 4},
|
||||
opts: &SearchRepoOptions{Keyword: "big_test_", PageSize: 10, Collaborate: util.OptionalBoolFalse},
|
||||
count: 7},
|
||||
{name: "PublicAndPrivateRepositoriesByName",
|
||||
opts: &SearchRepoOptions{Keyword: "big_test_", Page: 1, PageSize: 10, Private: true},
|
||||
count: 8},
|
||||
opts: &SearchRepoOptions{Keyword: "big_test_", Page: 1, PageSize: 10, Private: true, Collaborate: util.OptionalBoolFalse},
|
||||
count: 14},
|
||||
{name: "PublicAndPrivateRepositoriesByNameWithPagesizeLimitFirstPage",
|
||||
opts: &SearchRepoOptions{Keyword: "big_test_", Page: 1, PageSize: 5, Private: true},
|
||||
count: 8},
|
||||
opts: &SearchRepoOptions{Keyword: "big_test_", Page: 1, PageSize: 5, Private: true, Collaborate: util.OptionalBoolFalse},
|
||||
count: 14},
|
||||
{name: "PublicAndPrivateRepositoriesByNameWithPagesizeLimitSecondPage",
|
||||
opts: &SearchRepoOptions{Keyword: "big_test_", Page: 2, PageSize: 5, Private: true},
|
||||
count: 8},
|
||||
opts: &SearchRepoOptions{Keyword: "big_test_", Page: 2, PageSize: 5, Private: true, Collaborate: util.OptionalBoolFalse},
|
||||
count: 14},
|
||||
{name: "PublicAndPrivateRepositoriesByNameWithPagesizeLimitThirdPage",
|
||||
opts: &SearchRepoOptions{Keyword: "big_test_", Page: 3, PageSize: 5, Private: true, Collaborate: util.OptionalBoolFalse},
|
||||
count: 14},
|
||||
{name: "PublicAndPrivateRepositoriesByNameWithPagesizeLimitFourthPage",
|
||||
opts: &SearchRepoOptions{Keyword: "big_test_", Page: 3, PageSize: 5, Private: true, Collaborate: util.OptionalBoolFalse},
|
||||
count: 14},
|
||||
{name: "PublicRepositoriesOfUser",
|
||||
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 15},
|
||||
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 15, Collaborate: util.OptionalBoolFalse},
|
||||
count: 2},
|
||||
{name: "PublicRepositoriesOfUser2",
|
||||
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 18},
|
||||
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 18, Collaborate: util.OptionalBoolFalse},
|
||||
count: 0},
|
||||
{name: "PublicRepositoriesOfUser3",
|
||||
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 20, Collaborate: util.OptionalBoolFalse},
|
||||
count: 2},
|
||||
{name: "PublicAndPrivateRepositoriesOfUser",
|
||||
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 15, Private: true},
|
||||
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 15, Private: true, Collaborate: util.OptionalBoolFalse},
|
||||
count: 4},
|
||||
{name: "PublicAndPrivateRepositoriesOfUser2",
|
||||
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 18, Private: true},
|
||||
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 18, Private: true, Collaborate: util.OptionalBoolFalse},
|
||||
count: 0},
|
||||
{name: "PublicAndPrivateRepositoriesOfUser3",
|
||||
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 20, Private: true, Collaborate: util.OptionalBoolFalse},
|
||||
count: 4},
|
||||
{name: "PublicRepositoriesOfUserIncludingCollaborative",
|
||||
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 15, Collaborate: true},
|
||||
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 15},
|
||||
count: 4},
|
||||
{name: "PublicRepositoriesOfUser2IncludingCollaborative",
|
||||
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 18, Collaborate: true},
|
||||
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 18},
|
||||
count: 1},
|
||||
{name: "PublicRepositoriesOfUser3IncludingCollaborative",
|
||||
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 20},
|
||||
count: 3},
|
||||
{name: "PublicAndPrivateRepositoriesOfUserIncludingCollaborative",
|
||||
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 15, Private: true, Collaborate: true},
|
||||
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 15, Private: true},
|
||||
count: 8},
|
||||
{name: "PublicAndPrivateRepositoriesOfUser2IncludingCollaborative",
|
||||
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 18, Private: true, Collaborate: true},
|
||||
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 18, Private: true},
|
||||
count: 4},
|
||||
{name: "PublicAndPrivateRepositoriesOfUser3IncludingCollaborative",
|
||||
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 20, Private: true},
|
||||
count: 6},
|
||||
{name: "PublicRepositoriesOfOrganization",
|
||||
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 17},
|
||||
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 17, Collaborate: util.OptionalBoolFalse},
|
||||
count: 1},
|
||||
{name: "PublicAndPrivateRepositoriesOfOrganization",
|
||||
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 17, Private: true},
|
||||
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 17, Private: true, Collaborate: util.OptionalBoolFalse},
|
||||
count: 2},
|
||||
{name: "AllPublic/PublicRepositoriesByName",
|
||||
opts: &SearchRepoOptions{Keyword: "big_test_", PageSize: 10, AllPublic: true},
|
||||
count: 4},
|
||||
opts: &SearchRepoOptions{Keyword: "big_test_", PageSize: 10, AllPublic: true, Collaborate: util.OptionalBoolFalse},
|
||||
count: 7},
|
||||
{name: "AllPublic/PublicAndPrivateRepositoriesByName",
|
||||
opts: &SearchRepoOptions{Keyword: "big_test_", Page: 1, PageSize: 10, Private: true, AllPublic: true},
|
||||
count: 8},
|
||||
opts: &SearchRepoOptions{Keyword: "big_test_", Page: 1, PageSize: 10, Private: true, AllPublic: true, Collaborate: util.OptionalBoolFalse},
|
||||
count: 14},
|
||||
{name: "AllPublic/PublicRepositoriesOfUserIncludingCollaborative",
|
||||
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 15, Collaborate: true, AllPublic: true},
|
||||
count: 12},
|
||||
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 15, AllPublic: true},
|
||||
count: 15},
|
||||
{name: "AllPublic/PublicAndPrivateRepositoriesOfUserIncludingCollaborative",
|
||||
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 15, Private: true, Collaborate: true, AllPublic: true},
|
||||
count: 16},
|
||||
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 15, Private: true, AllPublic: true},
|
||||
count: 19},
|
||||
{name: "AllPublic/PublicAndPrivateRepositoriesOfUserIncludingCollaborativeByName",
|
||||
opts: &SearchRepoOptions{Keyword: "test", Page: 1, PageSize: 10, OwnerID: 15, Private: true, Collaborate: true, AllPublic: true},
|
||||
count: 10},
|
||||
opts: &SearchRepoOptions{Keyword: "test", Page: 1, PageSize: 10, OwnerID: 15, Private: true, AllPublic: true},
|
||||
count: 13},
|
||||
{name: "AllPublic/PublicAndPrivateRepositoriesOfUser2IncludingCollaborativeByName",
|
||||
opts: &SearchRepoOptions{Keyword: "test", Page: 1, PageSize: 10, OwnerID: 18, Private: true, Collaborate: true, AllPublic: true},
|
||||
count: 8},
|
||||
opts: &SearchRepoOptions{Keyword: "test", Page: 1, PageSize: 10, OwnerID: 18, Private: true, AllPublic: true},
|
||||
count: 11},
|
||||
{name: "AllPublic/PublicRepositoriesOfOrganization",
|
||||
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 17, AllPublic: true},
|
||||
count: 12},
|
||||
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 17, AllPublic: true, Collaborate: util.OptionalBoolFalse},
|
||||
count: 15},
|
||||
}
|
||||
|
||||
for _, testCase := range testCases {
|
||||
@ -138,27 +169,54 @@ func TestSearchRepositoryByName(t *testing.T) {
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, int64(testCase.count), count)
|
||||
|
||||
var expectedLen int
|
||||
if testCase.opts.PageSize*testCase.opts.Page > testCase.count {
|
||||
expectedLen = testCase.count % testCase.opts.PageSize
|
||||
} else {
|
||||
expectedLen = testCase.opts.PageSize
|
||||
page := testCase.opts.Page
|
||||
if page <= 0 {
|
||||
page = 1
|
||||
}
|
||||
assert.Len(t, repos, expectedLen)
|
||||
var expectedLen = testCase.opts.PageSize
|
||||
if testCase.opts.PageSize*page > testCase.count+testCase.opts.PageSize {
|
||||
expectedLen = 0
|
||||
} else if testCase.opts.PageSize*page > testCase.count {
|
||||
expectedLen = testCase.count % testCase.opts.PageSize
|
||||
}
|
||||
if assert.Len(t, repos, expectedLen) {
|
||||
for _, repo := range repos {
|
||||
assert.NotEmpty(t, repo.Name)
|
||||
|
||||
for _, repo := range repos {
|
||||
assert.NotEmpty(t, repo.Name)
|
||||
if len(testCase.opts.Keyword) > 0 {
|
||||
assert.Contains(t, repo.Name, testCase.opts.Keyword)
|
||||
}
|
||||
|
||||
if len(testCase.opts.Keyword) > 0 {
|
||||
assert.Contains(t, repo.Name, testCase.opts.Keyword)
|
||||
}
|
||||
if !testCase.opts.Private {
|
||||
assert.False(t, repo.IsPrivate)
|
||||
}
|
||||
|
||||
if testCase.opts.OwnerID > 0 && !testCase.opts.Collaborate && !testCase.opts.AllPublic {
|
||||
assert.Equal(t, testCase.opts.OwnerID, repo.Owner.ID)
|
||||
}
|
||||
if testCase.opts.Fork == util.OptionalBoolTrue && testCase.opts.Mirror == util.OptionalBoolTrue {
|
||||
assert.True(t, repo.IsFork || repo.IsMirror)
|
||||
} else {
|
||||
switch testCase.opts.Fork {
|
||||
case util.OptionalBoolFalse:
|
||||
assert.False(t, repo.IsFork)
|
||||
case util.OptionalBoolTrue:
|
||||
assert.True(t, repo.IsFork)
|
||||
}
|
||||
|
||||
if !testCase.opts.Private {
|
||||
assert.False(t, repo.IsPrivate)
|
||||
switch testCase.opts.Mirror {
|
||||
case util.OptionalBoolFalse:
|
||||
assert.False(t, repo.IsMirror)
|
||||
case util.OptionalBoolTrue:
|
||||
assert.True(t, repo.IsMirror)
|
||||
}
|
||||
}
|
||||
|
||||
if testCase.opts.OwnerID > 0 && !testCase.opts.AllPublic {
|
||||
switch testCase.opts.Collaborate {
|
||||
case util.OptionalBoolFalse:
|
||||
assert.Equal(t, testCase.opts.OwnerID, repo.Owner.ID)
|
||||
case util.OptionalBoolTrue:
|
||||
assert.NotEqual(t, testCase.opts.OwnerID, repo.Owner.ID)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
Reference in New Issue
Block a user