123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384 |
- // 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 (
- "strings"
- "testing"
-
- "code.gitea.io/gitea/models/db"
- "code.gitea.io/gitea/models/unittest"
- "code.gitea.io/gitea/modules/util"
-
- "github.com/stretchr/testify/assert"
- )
-
- func TestSearchRepository(t *testing.T) {
- assert.NoError(t, unittest.PrepareTestDatabase())
-
- // test search public repository on explore page
- repos, count, err := SearchRepositoryByName(&SearchRepoOptions{
- ListOptions: db.ListOptions{
- Page: 1,
- PageSize: 10,
- },
- Keyword: "repo_12",
- Collaborate: util.OptionalBoolFalse,
- })
-
- assert.NoError(t, err)
- if assert.Len(t, repos, 1) {
- assert.Equal(t, "test_repo_12", repos[0].Name)
- }
- assert.Equal(t, int64(1), count)
-
- repos, count, err = SearchRepositoryByName(&SearchRepoOptions{
- ListOptions: db.ListOptions{
- Page: 1,
- PageSize: 10,
- },
- Keyword: "test_repo",
- Collaborate: util.OptionalBoolFalse,
- })
-
- assert.NoError(t, err)
- assert.Equal(t, int64(2), count)
- assert.Len(t, repos, 2)
-
- // test search private repository on explore page
- repos, count, err = SearchRepositoryByName(&SearchRepoOptions{
- ListOptions: db.ListOptions{
- Page: 1,
- PageSize: 10,
- },
- Keyword: "repo_13",
- Private: true,
- Collaborate: util.OptionalBoolFalse,
- })
-
- assert.NoError(t, err)
- if assert.Len(t, repos, 1) {
- assert.Equal(t, "test_repo_13", repos[0].Name)
- }
- assert.Equal(t, int64(1), count)
-
- repos, count, err = SearchRepositoryByName(&SearchRepoOptions{
- ListOptions: db.ListOptions{
- Page: 1,
- PageSize: 10,
- },
- Keyword: "test_repo",
- 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: unittest.NonexistentID})
-
- assert.NoError(t, err)
- assert.Empty(t, repos)
- assert.Equal(t, int64(0), count)
-
- // Test search within description
- repos, count, err = SearchRepository(&SearchRepoOptions{
- ListOptions: db.ListOptions{
- Page: 1,
- PageSize: 10,
- },
- Keyword: "description_14",
- 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{
- ListOptions: db.ListOptions{
- Page: 1,
- PageSize: 10,
- },
- Keyword: "description_14",
- Collaborate: util.OptionalBoolFalse,
- IncludeDescription: false,
- })
-
- 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_", ListOptions: db.ListOptions{PageSize: 10}, Collaborate: util.OptionalBoolFalse},
- count: 7,
- },
- {
- name: "PublicAndPrivateRepositoriesByName",
- opts: &SearchRepoOptions{Keyword: "big_test_", ListOptions: db.ListOptions{Page: 1, PageSize: 10}, Private: true, Collaborate: util.OptionalBoolFalse},
- count: 14,
- },
- {
- name: "PublicAndPrivateRepositoriesByNameWithPagesizeLimitFirstPage",
- opts: &SearchRepoOptions{Keyword: "big_test_", ListOptions: db.ListOptions{Page: 1, PageSize: 5}, Private: true, Collaborate: util.OptionalBoolFalse},
- count: 14,
- },
- {
- name: "PublicAndPrivateRepositoriesByNameWithPagesizeLimitSecondPage",
- opts: &SearchRepoOptions{Keyword: "big_test_", ListOptions: db.ListOptions{Page: 2, PageSize: 5}, Private: true, Collaborate: util.OptionalBoolFalse},
- count: 14,
- },
- {
- name: "PublicAndPrivateRepositoriesByNameWithPagesizeLimitThirdPage",
- opts: &SearchRepoOptions{Keyword: "big_test_", ListOptions: db.ListOptions{Page: 3, PageSize: 5}, Private: true, Collaborate: util.OptionalBoolFalse},
- count: 14,
- },
- {
- name: "PublicAndPrivateRepositoriesByNameWithPagesizeLimitFourthPage",
- opts: &SearchRepoOptions{Keyword: "big_test_", ListOptions: db.ListOptions{Page: 3, PageSize: 5}, Private: true, Collaborate: util.OptionalBoolFalse},
- count: 14,
- },
- {
- name: "PublicRepositoriesOfUser",
- opts: &SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 15, Collaborate: util.OptionalBoolFalse},
- count: 2,
- },
- {
- name: "PublicRepositoriesOfUser2",
- opts: &SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 18, Collaborate: util.OptionalBoolFalse},
- count: 0,
- },
- {
- name: "PublicRepositoriesOfUser3",
- opts: &SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 20, Collaborate: util.OptionalBoolFalse},
- count: 2,
- },
- {
- name: "PublicAndPrivateRepositoriesOfUser",
- opts: &SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 15, Private: true, Collaborate: util.OptionalBoolFalse},
- count: 4,
- },
- {
- name: "PublicAndPrivateRepositoriesOfUser2",
- opts: &SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 18, Private: true, Collaborate: util.OptionalBoolFalse},
- count: 0,
- },
- {
- name: "PublicAndPrivateRepositoriesOfUser3",
- opts: &SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 20, Private: true, Collaborate: util.OptionalBoolFalse},
- count: 4,
- },
- {
- name: "PublicRepositoriesOfUserIncludingCollaborative",
- opts: &SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 15},
- count: 5,
- },
- {
- name: "PublicRepositoriesOfUser2IncludingCollaborative",
- opts: &SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 18},
- count: 1,
- },
- {
- name: "PublicRepositoriesOfUser3IncludingCollaborative",
- opts: &SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 20},
- count: 3,
- },
- {
- name: "PublicAndPrivateRepositoriesOfUserIncludingCollaborative",
- opts: &SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 15, Private: true},
- count: 9,
- },
- {
- name: "PublicAndPrivateRepositoriesOfUser2IncludingCollaborative",
- opts: &SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 18, Private: true},
- count: 4,
- },
- {
- name: "PublicAndPrivateRepositoriesOfUser3IncludingCollaborative",
- opts: &SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 20, Private: true},
- count: 7,
- },
- {
- name: "PublicRepositoriesOfOrganization",
- opts: &SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 17, Collaborate: util.OptionalBoolFalse},
- count: 1,
- },
- {
- name: "PublicAndPrivateRepositoriesOfOrganization",
- opts: &SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 17, Private: true, Collaborate: util.OptionalBoolFalse},
- count: 2,
- },
- {
- name: "AllPublic/PublicRepositoriesByName",
- opts: &SearchRepoOptions{Keyword: "big_test_", ListOptions: db.ListOptions{PageSize: 10}, AllPublic: true, Collaborate: util.OptionalBoolFalse},
- count: 7,
- },
- {
- name: "AllPublic/PublicAndPrivateRepositoriesByName",
- opts: &SearchRepoOptions{Keyword: "big_test_", ListOptions: db.ListOptions{Page: 1, PageSize: 10}, Private: true, AllPublic: true, Collaborate: util.OptionalBoolFalse},
- count: 14,
- },
- {
- name: "AllPublic/PublicRepositoriesOfUserIncludingCollaborative",
- opts: &SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 15, AllPublic: true, Template: util.OptionalBoolFalse},
- count: 28,
- },
- {
- name: "AllPublic/PublicAndPrivateRepositoriesOfUserIncludingCollaborative",
- opts: &SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 15, Private: true, AllPublic: true, AllLimited: true, Template: util.OptionalBoolFalse},
- count: 33,
- },
- {
- name: "AllPublic/PublicAndPrivateRepositoriesOfUserIncludingCollaborativeByName",
- opts: &SearchRepoOptions{Keyword: "test", ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 15, Private: true, AllPublic: true},
- count: 15,
- },
- {
- name: "AllPublic/PublicAndPrivateRepositoriesOfUser2IncludingCollaborativeByName",
- opts: &SearchRepoOptions{Keyword: "test", ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 18, Private: true, AllPublic: true},
- count: 13,
- },
- {
- name: "AllPublic/PublicRepositoriesOfOrganization",
- opts: &SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 17, AllPublic: true, Collaborate: util.OptionalBoolFalse, Template: util.OptionalBoolFalse},
- count: 28,
- },
- {
- name: "AllTemplates",
- opts: &SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, Template: util.OptionalBoolTrue},
- count: 2,
- },
- {
- name: "OwnerSlashRepoSearch",
- opts: &SearchRepoOptions{Keyword: "user/repo2", ListOptions: db.ListOptions{Page: 1, PageSize: 10}, Private: true, OwnerID: 0},
- count: 3,
- },
- {
- name: "OwnerSlashSearch",
- opts: &SearchRepoOptions{Keyword: "user20/", ListOptions: db.ListOptions{Page: 1, PageSize: 10}, Private: true, OwnerID: 0},
- count: 4,
- },
- }
-
- 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)
-
- page := testCase.opts.Page
- if page <= 0 {
- page = 1
- }
- 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)
-
- if len(testCase.opts.Keyword) > 0 {
- // Keyword match condition is different for search terms of form "owner/repo"
- if strings.Count(testCase.opts.Keyword, "/") == 1 {
- // May still match as a whole...
- wholeMatch := strings.Contains(repo.Name, testCase.opts.Keyword)
-
- pieces := strings.Split(testCase.opts.Keyword, "/")
- ownerName := pieces[0]
- repoName := pieces[1]
- // ... or match in parts
- splitMatch := strings.Contains(repo.OwnerName, ownerName) && strings.Contains(repo.Name, repoName)
-
- assert.True(t, wholeMatch || splitMatch, "Keyword '%s' does not match repo '%s/%s'", testCase.opts.Keyword, repo.Owner.Name, repo.Name)
- } else {
- 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)
- }
- }
- }
- }
- })
- }
- }
-
- func TestSearchRepositoryByTopicName(t *testing.T) {
- assert.NoError(t, unittest.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,
- },
- {
- name: "AllPublic/OnlySearchMultipleKeywordPublicRepositoriesFromTopic",
- opts: &SearchRepoOptions{OwnerID: 21, AllPublic: true, Keyword: "graphql,golang", TopicOnly: true},
- count: 2,
- },
- }
-
- 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)
- })
- }
- }
|