diff options
author | JakobDev <jakobdev@gmx.de> | 2023-08-11 19:08:05 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-08-11 13:08:05 -0400 |
commit | f3fbb7c67d21c01a58653c7ef9ae0e80dbb4becd (patch) | |
tree | 8989d303f376193c50db1eaec57caa1b89e33fff /models | |
parent | 7e382a55558d4a8719e8493de5455883a2003759 (diff) | |
download | gitea-f3fbb7c67d21c01a58653c7ef9ae0e80dbb4becd.tar.gz gitea-f3fbb7c67d21c01a58653c7ef9ae0e80dbb4becd.zip |
Count only visible repos on profile (#25928)
Fixes #25914
Diffstat (limited to 'models')
-rw-r--r-- | models/repo/repo_list.go | 5 | ||||
-rw-r--r-- | models/repo/repo_list_test.go | 229 |
2 files changed, 132 insertions, 102 deletions
diff --git a/models/repo/repo_list.go b/models/repo/repo_list.go index 83ba02e316..b8427bec4e 100644 --- a/models/repo/repo_list.go +++ b/models/repo/repo_list.go @@ -522,6 +522,11 @@ func SearchRepository(ctx context.Context, opts *SearchRepoOptions) (RepositoryL return SearchRepositoryByCondition(ctx, opts, cond, true) } +// CountRepository counts repositories based on search options, +func CountRepository(ctx context.Context, opts *SearchRepoOptions) (int64, error) { + return db.GetEngine(ctx).Where(SearchRepositoryCondition(opts)).Count(new(Repository)) +} + // SearchRepositoryByCondition search repositories by condition func SearchRepositoryByCondition(ctx context.Context, opts *SearchRepoOptions, cond builder.Cond, loadAttributes bool) (RepositoryList, int64, error) { sess, count, err := searchRepositoryByCondition(ctx, opts, cond) diff --git a/models/repo/repo_list_test.go b/models/repo/repo_list_test.go index e013953c68..7097b6ea14 100644 --- a/models/repo/repo_list_test.go +++ b/models/repo/repo_list_test.go @@ -15,108 +15,11 @@ import ( "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 := repo_model.SearchRepositoryByName(db.DefaultContext, &repo_model.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 = repo_model.SearchRepositoryByName(db.DefaultContext, &repo_model.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 = repo_model.SearchRepositoryByName(db.DefaultContext, &repo_model.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 = repo_model.SearchRepositoryByName(db.DefaultContext, &repo_model.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 = repo_model.SearchRepositoryByName(db.DefaultContext, &repo_model.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 = repo_model.SearchRepository(db.DefaultContext, &repo_model.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 = repo_model.SearchRepository(db.DefaultContext, &repo_model.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) - +func getTestCases() []struct { + name string + opts *repo_model.SearchRepoOptions + count int +} { testCases := []struct { name string opts *repo_model.SearchRepoOptions @@ -274,6 +177,113 @@ func TestSearchRepository(t *testing.T) { }, } + return testCases +} + +func TestSearchRepository(t *testing.T) { + assert.NoError(t, unittest.PrepareTestDatabase()) + + // test search public repository on explore page + repos, count, err := repo_model.SearchRepositoryByName(db.DefaultContext, &repo_model.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 = repo_model.SearchRepositoryByName(db.DefaultContext, &repo_model.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 = repo_model.SearchRepositoryByName(db.DefaultContext, &repo_model.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 = repo_model.SearchRepositoryByName(db.DefaultContext, &repo_model.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 = repo_model.SearchRepositoryByName(db.DefaultContext, &repo_model.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 = repo_model.SearchRepository(db.DefaultContext, &repo_model.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 = repo_model.SearchRepository(db.DefaultContext, &repo_model.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 := getTestCases() + for _, testCase := range testCases { t.Run(testCase.name, func(t *testing.T) { repos, count, err := repo_model.SearchRepositoryByName(db.DefaultContext, testCase.opts) @@ -349,6 +359,21 @@ func TestSearchRepository(t *testing.T) { } } +func TestCountRepository(t *testing.T) { + assert.NoError(t, unittest.PrepareTestDatabase()) + + testCases := getTestCases() + + for _, testCase := range testCases { + t.Run(testCase.name, func(t *testing.T) { + count, err := repo_model.CountRepository(db.DefaultContext, testCase.opts) + + assert.NoError(t, err) + assert.Equal(t, int64(testCase.count), count) + }) + } +} + func TestSearchRepositoryByTopicName(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) |