2017-02-28 17:58:50 +08:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2017-10-27 05:16:13 +08:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
|
|
|
|
2017-02-28 17:58:50 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2019-08-26 01:06:36 +08:00
|
|
|
func TestSearchRepository(t *testing.T) {
|
2017-02-28 17:58:50 +08:00
|
|
|
assert.NoError(t, PrepareTestDatabase())
|
|
|
|
|
|
|
|
// test search public repository on explore page
|
|
|
|
repos, count, err := SearchRepositoryByName(&SearchRepoOptions{
|
2017-10-27 05:16:13 +08:00
|
|
|
Keyword: "repo_12",
|
|
|
|
Page: 1,
|
|
|
|
PageSize: 10,
|
|
|
|
Collaborate: util.OptionalBoolFalse,
|
2017-02-28 17:58:50 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
2017-08-28 09:33:38 +08:00
|
|
|
if assert.Len(t, repos, 1) {
|
|
|
|
assert.Equal(t, "test_repo_12", repos[0].Name)
|
|
|
|
}
|
2017-02-28 17:58:50 +08:00
|
|
|
assert.Equal(t, int64(1), count)
|
|
|
|
|
|
|
|
repos, count, err = SearchRepositoryByName(&SearchRepoOptions{
|
2017-10-27 05:16:13 +08:00
|
|
|
Keyword: "test_repo",
|
|
|
|
Page: 1,
|
|
|
|
PageSize: 10,
|
|
|
|
Collaborate: util.OptionalBoolFalse,
|
2017-02-28 17:58:50 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, int64(2), count)
|
2017-10-10 09:23:29 +08:00
|
|
|
assert.Len(t, repos, 2)
|
2017-02-28 17:58:50 +08:00
|
|
|
|
|
|
|
// test search private repository on explore page
|
|
|
|
repos, count, err = SearchRepositoryByName(&SearchRepoOptions{
|
2017-10-27 05:16:13 +08:00
|
|
|
Keyword: "repo_13",
|
|
|
|
Page: 1,
|
|
|
|
PageSize: 10,
|
|
|
|
Private: true,
|
|
|
|
Collaborate: util.OptionalBoolFalse,
|
2017-02-28 17:58:50 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
2017-08-28 09:33:38 +08:00
|
|
|
if assert.Len(t, repos, 1) {
|
|
|
|
assert.Equal(t, "test_repo_13", repos[0].Name)
|
|
|
|
}
|
2017-02-28 17:58:50 +08:00
|
|
|
assert.Equal(t, int64(1), count)
|
|
|
|
|
|
|
|
repos, count, err = SearchRepositoryByName(&SearchRepoOptions{
|
2017-10-27 05:16:13 +08:00
|
|
|
Keyword: "test_repo",
|
|
|
|
Page: 1,
|
|
|
|
PageSize: 10,
|
|
|
|
Private: true,
|
|
|
|
Collaborate: util.OptionalBoolFalse,
|
2017-02-28 17:58:50 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, int64(3), count)
|
2017-10-10 09:23:29 +08:00
|
|
|
assert.Len(t, repos, 3)
|
|
|
|
|
2017-10-27 05:16:13 +08:00
|
|
|
// 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)
|
|
|
|
|
2019-08-26 01:06:36 +08:00
|
|
|
// Test search within description
|
|
|
|
repos, count, err = SearchRepository(&SearchRepoOptions{
|
|
|
|
Keyword: "description_14",
|
|
|
|
Page: 1,
|
|
|
|
PageSize: 10,
|
|
|
|
Collaborate: util.OptionalBoolFalse,
|
|
|
|
IncludeDescription: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if assert.Len(t, repos, 1) {
|
|
|
|
assert.Equal(t, "test_repo_14", repos[0].Name)
|
|
|
|
}
|
|
|
|
assert.Equal(t, int64(1), count)
|
|
|
|
|
|
|
|
// Test NOT search within description
|
|
|
|
repos, count, err = SearchRepository(&SearchRepoOptions{
|
|
|
|
Keyword: "description_14",
|
|
|
|
Page: 1,
|
|
|
|
PageSize: 10,
|
|
|
|
Collaborate: util.OptionalBoolFalse,
|
|
|
|
IncludeDescription: false,
|
|
|
|
})
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Empty(t, repos)
|
|
|
|
assert.Equal(t, int64(0), count)
|
|
|
|
|
2017-10-10 09:23:29 +08:00
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
opts *SearchRepoOptions
|
|
|
|
count int
|
|
|
|
}{
|
|
|
|
{name: "PublicRepositoriesByName",
|
2017-10-27 05:16:13 +08:00
|
|
|
opts: &SearchRepoOptions{Keyword: "big_test_", PageSize: 10, Collaborate: util.OptionalBoolFalse},
|
|
|
|
count: 7},
|
2017-10-10 09:23:29 +08:00
|
|
|
{name: "PublicAndPrivateRepositoriesByName",
|
2017-10-27 05:16:13 +08:00
|
|
|
opts: &SearchRepoOptions{Keyword: "big_test_", Page: 1, PageSize: 10, Private: true, Collaborate: util.OptionalBoolFalse},
|
|
|
|
count: 14},
|
2017-10-10 09:23:29 +08:00
|
|
|
{name: "PublicAndPrivateRepositoriesByNameWithPagesizeLimitFirstPage",
|
2017-10-27 05:16:13 +08:00
|
|
|
opts: &SearchRepoOptions{Keyword: "big_test_", Page: 1, PageSize: 5, Private: true, Collaborate: util.OptionalBoolFalse},
|
|
|
|
count: 14},
|
2017-10-10 09:23:29 +08:00
|
|
|
{name: "PublicAndPrivateRepositoriesByNameWithPagesizeLimitSecondPage",
|
2017-10-27 05:16:13 +08:00
|
|
|
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},
|
2017-10-10 09:23:29 +08:00
|
|
|
{name: "PublicRepositoriesOfUser",
|
2017-10-27 05:16:13 +08:00
|
|
|
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 15, Collaborate: util.OptionalBoolFalse},
|
2017-10-17 23:20:22 +08:00
|
|
|
count: 2},
|
|
|
|
{name: "PublicRepositoriesOfUser2",
|
2017-10-27 05:16:13 +08:00
|
|
|
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 18, Collaborate: util.OptionalBoolFalse},
|
2017-10-17 23:20:22 +08:00
|
|
|
count: 0},
|
2017-10-27 05:16:13 +08:00
|
|
|
{name: "PublicRepositoriesOfUser3",
|
|
|
|
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 20, Collaborate: util.OptionalBoolFalse},
|
|
|
|
count: 2},
|
2017-10-10 09:23:29 +08:00
|
|
|
{name: "PublicAndPrivateRepositoriesOfUser",
|
2017-10-27 05:16:13 +08:00
|
|
|
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 15, Private: true, Collaborate: util.OptionalBoolFalse},
|
2017-10-17 23:20:22 +08:00
|
|
|
count: 4},
|
|
|
|
{name: "PublicAndPrivateRepositoriesOfUser2",
|
2017-10-27 05:16:13 +08:00
|
|
|
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 18, Private: true, Collaborate: util.OptionalBoolFalse},
|
2017-10-17 23:20:22 +08:00
|
|
|
count: 0},
|
2017-10-27 05:16:13 +08:00
|
|
|
{name: "PublicAndPrivateRepositoriesOfUser3",
|
|
|
|
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 20, Private: true, Collaborate: util.OptionalBoolFalse},
|
|
|
|
count: 4},
|
2017-10-10 09:23:29 +08:00
|
|
|
{name: "PublicRepositoriesOfUserIncludingCollaborative",
|
2017-10-27 05:16:13 +08:00
|
|
|
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 15},
|
2019-05-15 23:24:39 +08:00
|
|
|
count: 5},
|
2017-10-17 23:20:22 +08:00
|
|
|
{name: "PublicRepositoriesOfUser2IncludingCollaborative",
|
2017-10-27 05:16:13 +08:00
|
|
|
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 18},
|
2017-10-17 23:20:22 +08:00
|
|
|
count: 1},
|
2017-10-27 05:16:13 +08:00
|
|
|
{name: "PublicRepositoriesOfUser3IncludingCollaborative",
|
|
|
|
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 20},
|
|
|
|
count: 3},
|
2017-10-10 09:23:29 +08:00
|
|
|
{name: "PublicAndPrivateRepositoriesOfUserIncludingCollaborative",
|
2017-10-27 05:16:13 +08:00
|
|
|
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 15, Private: true},
|
2019-05-15 23:24:39 +08:00
|
|
|
count: 9},
|
2017-10-17 23:20:22 +08:00
|
|
|
{name: "PublicAndPrivateRepositoriesOfUser2IncludingCollaborative",
|
2017-10-27 05:16:13 +08:00
|
|
|
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 18, Private: true},
|
2017-10-17 23:20:22 +08:00
|
|
|
count: 4},
|
2017-10-27 05:16:13 +08:00
|
|
|
{name: "PublicAndPrivateRepositoriesOfUser3IncludingCollaborative",
|
|
|
|
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 20, Private: true},
|
2019-05-15 23:24:39 +08:00
|
|
|
count: 7},
|
2017-10-10 09:23:29 +08:00
|
|
|
{name: "PublicRepositoriesOfOrganization",
|
2017-10-27 05:16:13 +08:00
|
|
|
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 17, Collaborate: util.OptionalBoolFalse},
|
2017-10-10 09:23:29 +08:00
|
|
|
count: 1},
|
|
|
|
{name: "PublicAndPrivateRepositoriesOfOrganization",
|
2017-10-27 05:16:13 +08:00
|
|
|
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 17, Private: true, Collaborate: util.OptionalBoolFalse},
|
2017-10-10 09:23:29 +08:00
|
|
|
count: 2},
|
2017-10-11 04:37:18 +08:00
|
|
|
{name: "AllPublic/PublicRepositoriesByName",
|
2017-10-27 05:16:13 +08:00
|
|
|
opts: &SearchRepoOptions{Keyword: "big_test_", PageSize: 10, AllPublic: true, Collaborate: util.OptionalBoolFalse},
|
|
|
|
count: 7},
|
2017-10-11 04:37:18 +08:00
|
|
|
{name: "AllPublic/PublicAndPrivateRepositoriesByName",
|
2017-10-27 05:16:13 +08:00
|
|
|
opts: &SearchRepoOptions{Keyword: "big_test_", Page: 1, PageSize: 10, Private: true, AllPublic: true, Collaborate: util.OptionalBoolFalse},
|
|
|
|
count: 14},
|
2017-10-11 04:37:18 +08:00
|
|
|
{name: "AllPublic/PublicRepositoriesOfUserIncludingCollaborative",
|
2017-10-27 05:16:13 +08:00
|
|
|
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 15, AllPublic: true},
|
2019-09-12 01:26:28 +08:00
|
|
|
count: 22},
|
2017-10-11 04:37:18 +08:00
|
|
|
{name: "AllPublic/PublicAndPrivateRepositoriesOfUserIncludingCollaborative",
|
2020-01-06 10:12:55 +08:00
|
|
|
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 15, Private: true, AllPublic: true, AllLimited: true},
|
|
|
|
count: 27},
|
2017-10-11 04:37:18 +08:00
|
|
|
{name: "AllPublic/PublicAndPrivateRepositoriesOfUserIncludingCollaborativeByName",
|
2017-10-27 05:16:13 +08:00
|
|
|
opts: &SearchRepoOptions{Keyword: "test", Page: 1, PageSize: 10, OwnerID: 15, Private: true, AllPublic: true},
|
2019-04-17 13:31:08 +08:00
|
|
|
count: 15},
|
2017-10-17 23:20:22 +08:00
|
|
|
{name: "AllPublic/PublicAndPrivateRepositoriesOfUser2IncludingCollaborativeByName",
|
2017-10-27 05:16:13 +08:00
|
|
|
opts: &SearchRepoOptions{Keyword: "test", Page: 1, PageSize: 10, OwnerID: 18, Private: true, AllPublic: true},
|
2019-04-17 13:31:08 +08:00
|
|
|
count: 13},
|
2017-10-11 04:37:18 +08:00
|
|
|
{name: "AllPublic/PublicRepositoriesOfOrganization",
|
2017-10-27 05:16:13 +08:00
|
|
|
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 17, AllPublic: true, Collaborate: util.OptionalBoolFalse},
|
2019-09-12 01:26:28 +08:00
|
|
|
count: 22},
|
2017-10-10 09:23:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, testCase := range testCases {
|
|
|
|
t.Run(testCase.name, func(t *testing.T) {
|
|
|
|
repos, count, err := SearchRepositoryByName(testCase.opts)
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, int64(testCase.count), count)
|
|
|
|
|
2017-10-27 05:16:13 +08:00
|
|
|
page := testCase.opts.Page
|
|
|
|
if page <= 0 {
|
|
|
|
page = 1
|
|
|
|
}
|
|
|
|
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 {
|
2017-10-10 09:23:29 +08:00
|
|
|
expectedLen = testCase.count % testCase.opts.PageSize
|
|
|
|
}
|
2017-10-27 05:16:13 +08:00
|
|
|
if assert.Len(t, repos, expectedLen) {
|
|
|
|
for _, repo := range repos {
|
|
|
|
assert.NotEmpty(t, repo.Name)
|
|
|
|
|
|
|
|
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.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)
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2017-10-10 09:23:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2017-02-28 17:58:50 +08:00
|
|
|
}
|
2018-09-13 10:33:48 +08:00
|
|
|
|
|
|
|
func TestSearchRepositoryByTopicName(t *testing.T) {
|
|
|
|
assert.NoError(t, PrepareTestDatabase())
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
opts *SearchRepoOptions
|
|
|
|
count int
|
|
|
|
}{
|
|
|
|
{name: "AllPublic/SearchPublicRepositoriesFromTopicAndName",
|
|
|
|
opts: &SearchRepoOptions{OwnerID: 21, AllPublic: true, Keyword: "graphql"},
|
|
|
|
count: 2},
|
|
|
|
{name: "AllPublic/OnlySearchPublicRepositoriesFromTopic",
|
|
|
|
opts: &SearchRepoOptions{OwnerID: 21, AllPublic: true, Keyword: "graphql", TopicOnly: true},
|
|
|
|
count: 1},
|
2018-10-18 11:14:28 +08:00
|
|
|
{name: "AllPublic/OnlySearchMultipleKeywordPublicRepositoriesFromTopic",
|
|
|
|
opts: &SearchRepoOptions{OwnerID: 21, AllPublic: true, Keyword: "graphql,golang", TopicOnly: true},
|
2018-10-31 05:48:37 +08:00
|
|
|
count: 2},
|
2018-09-13 10:33:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, testCase := range testCases {
|
|
|
|
t.Run(testCase.name, func(t *testing.T) {
|
|
|
|
_, count, err := SearchRepositoryByName(testCase.opts)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, int64(testCase.count), count)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|