diff options
Diffstat (limited to 'models')
-rw-r--r-- | models/fixtures/repository.yml | 36 | ||||
-rw-r--r-- | models/repo.go | 18 | ||||
-rw-r--r-- | models/repo_test.go | 38 |
3 files changed, 90 insertions, 2 deletions
diff --git a/models/fixtures/repository.yml b/models/fixtures/repository.yml index c45374f4f2..9f4b496010 100644 --- a/models/fixtures/repository.yml +++ b/models/fixtures/repository.yml @@ -56,3 +56,39 @@ num_pulls: 0 num_closed_pulls: 0 is_mirror: true + +- + id: 6 + owner_id: 10 + lower_name: repo6 + name: repo6 + is_private: true + num_issues: 0 + num_closed_issues: 0 + num_pulls: 0 + num_closed_pulls: 0 + is_mirror: false + +- + id: 7 + owner_id: 10 + lower_name: repo7 + name: repo7 + is_private: true + num_issues: 0 + num_closed_issues: 0 + num_pulls: 0 + num_closed_pulls: 0 + is_mirror: false + +- + id: 8 + owner_id: 10 + lower_name: repo8 + name: repo8 + is_private: false + num_issues: 0 + num_closed_issues: 0 + num_pulls: 0 + num_closed_pulls: 0 + is_mirror: false diff --git a/models/repo.go b/models/repo.go index ab10cb6651..9b1b868778 100644 --- a/models/repo.go +++ b/models/repo.go @@ -1737,11 +1737,29 @@ func getRepositoryCount(e Engine, u *User) (int64, error) { return x.Count(&Repository{OwnerID: u.ID}) } +func getPublicRepositoryCount(e Engine, u *User) (int64, error) { + return x.Where("is_private = ?", false).Count(&Repository{OwnerID: u.ID}) +} + +func getPrivateRepositoryCount(e Engine, u *User) (int64, error) { + return x.Where("is_private = ?", true).Count(&Repository{OwnerID: u.ID}) +} + // GetRepositoryCount returns the total number of repositories of user. func GetRepositoryCount(u *User) (int64, error) { return getRepositoryCount(x, u) } +// GetPublicRepositoryCount returns the total number of public repositories of user. +func GetPublicRepositoryCount(u *User) (int64, error) { + return getPublicRepositoryCount(x, u) +} + +// GetPrivateRepositoryCount returns the total number of private repositories of user. +func GetPrivateRepositoryCount(u *User) (int64, error) { + return getPrivateRepositoryCount(x, u) +} + // SearchRepoOptions holds the search options type SearchRepoOptions struct { Keyword string diff --git a/models/repo_test.go b/models/repo_test.go index 42bde62b46..499c8c83eb 100644 --- a/models/repo_test.go +++ b/models/repo_test.go @@ -1,11 +1,16 @@ -package models_test +// 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" - . "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/markdown" + . "github.com/smartystreets/goconvey/convey" + "github.com/stretchr/testify/assert" ) func TestRepo(t *testing.T) { @@ -68,3 +73,32 @@ func TestRepo(t *testing.T) { }) }) } + +func TestGetRepositoryCount(t *testing.T) { + assert.NoError(t, PrepareTestDatabase()) + + count, err1 := GetRepositoryCount(&User{ID: int64(10)}) + privateCount, err2 := GetPrivateRepositoryCount(&User{ID: int64(10)}) + publicCount, err3 := GetPublicRepositoryCount(&User{ID: int64(10)}) + assert.NoError(t, err1) + assert.NoError(t, err2) + assert.NoError(t, err3) + assert.Equal(t, int64(3), count) + assert.Equal(t, (privateCount + publicCount), count) +} + +func TestGetPublicRepositoryCount(t *testing.T) { + assert.NoError(t, PrepareTestDatabase()) + + count, err := GetPublicRepositoryCount(&User{ID: int64(10)}) + assert.NoError(t, err) + assert.Equal(t, int64(1), count) +} + +func TestGetPrivateRepositoryCount(t *testing.T) { + assert.NoError(t, PrepareTestDatabase()) + + count, err := GetPrivateRepositoryCount(&User{ID: int64(10)}) + assert.NoError(t, err) + assert.Equal(t, int64(2), count) +} |