mirror of
https://gitee.com/gitea/gitea
synced 2025-12-10 10:10:26 +08:00
Move repository model into models/repo (#17933)
* Some refactors related repository model * Move more methods out of repository * Move repository into models/repo * Fix test * Fix test * some improvements * Remove unnecessary function
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"code.gitea.io/gitea/models/db"
|
||||
repo_model "code.gitea.io/gitea/models/repo"
|
||||
"code.gitea.io/gitea/models/unittest"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
|
||||
@@ -33,17 +34,17 @@ func TestWatchRepo(t *testing.T) {
|
||||
|
||||
assert.NoError(t, WatchRepo(userID, repoID, true))
|
||||
unittest.AssertExistsAndLoadBean(t, &Watch{RepoID: repoID, UserID: userID})
|
||||
unittest.CheckConsistencyFor(t, &Repository{ID: repoID})
|
||||
unittest.CheckConsistencyFor(t, &repo_model.Repository{ID: repoID})
|
||||
|
||||
assert.NoError(t, WatchRepo(userID, repoID, false))
|
||||
unittest.AssertNotExistsBean(t, &Watch{RepoID: repoID, UserID: userID})
|
||||
unittest.CheckConsistencyFor(t, &Repository{ID: repoID})
|
||||
unittest.CheckConsistencyFor(t, &repo_model.Repository{ID: repoID})
|
||||
}
|
||||
|
||||
func TestGetWatchers(t *testing.T) {
|
||||
assert.NoError(t, unittest.PrepareTestDatabase())
|
||||
|
||||
repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
|
||||
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}).(*repo_model.Repository)
|
||||
watches, err := GetWatchers(repo.ID)
|
||||
assert.NoError(t, err)
|
||||
// One watchers are inactive, thus minus 1
|
||||
@@ -60,16 +61,16 @@ func TestGetWatchers(t *testing.T) {
|
||||
func TestRepository_GetWatchers(t *testing.T) {
|
||||
assert.NoError(t, unittest.PrepareTestDatabase())
|
||||
|
||||
repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
|
||||
watchers, err := repo.GetWatchers(db.ListOptions{Page: 1})
|
||||
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}).(*repo_model.Repository)
|
||||
watchers, err := GetRepoWatchers(repo.ID, db.ListOptions{Page: 1})
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, watchers, repo.NumWatches)
|
||||
for _, watcher := range watchers {
|
||||
unittest.AssertExistsAndLoadBean(t, &Watch{UserID: watcher.ID, RepoID: repo.ID})
|
||||
}
|
||||
|
||||
repo = unittest.AssertExistsAndLoadBean(t, &Repository{ID: 9}).(*Repository)
|
||||
watchers, err = repo.GetWatchers(db.ListOptions{Page: 1})
|
||||
repo = unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 9}).(*repo_model.Repository)
|
||||
watchers, err = GetRepoWatchers(repo.ID, db.ListOptions{Page: 1})
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, watchers, 0)
|
||||
}
|
||||
@@ -114,8 +115,8 @@ func TestNotifyWatchers(t *testing.T) {
|
||||
func TestWatchIfAuto(t *testing.T) {
|
||||
assert.NoError(t, unittest.PrepareTestDatabase())
|
||||
|
||||
repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
|
||||
watchers, err := repo.GetWatchers(db.ListOptions{Page: 1})
|
||||
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}).(*repo_model.Repository)
|
||||
watchers, err := GetRepoWatchers(repo.ID, db.ListOptions{Page: 1})
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, watchers, repo.NumWatches)
|
||||
|
||||
@@ -125,13 +126,13 @@ func TestWatchIfAuto(t *testing.T) {
|
||||
|
||||
// Must not add watch
|
||||
assert.NoError(t, WatchIfAuto(8, 1, true))
|
||||
watchers, err = repo.GetWatchers(db.ListOptions{Page: 1})
|
||||
watchers, err = GetRepoWatchers(repo.ID, db.ListOptions{Page: 1})
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, watchers, prevCount)
|
||||
|
||||
// Should not add watch
|
||||
assert.NoError(t, WatchIfAuto(10, 1, true))
|
||||
watchers, err = repo.GetWatchers(db.ListOptions{Page: 1})
|
||||
watchers, err = GetRepoWatchers(repo.ID, db.ListOptions{Page: 1})
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, watchers, prevCount)
|
||||
|
||||
@@ -139,31 +140,31 @@ func TestWatchIfAuto(t *testing.T) {
|
||||
|
||||
// Must not add watch
|
||||
assert.NoError(t, WatchIfAuto(8, 1, true))
|
||||
watchers, err = repo.GetWatchers(db.ListOptions{Page: 1})
|
||||
watchers, err = GetRepoWatchers(repo.ID, db.ListOptions{Page: 1})
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, watchers, prevCount)
|
||||
|
||||
// Should not add watch
|
||||
assert.NoError(t, WatchIfAuto(12, 1, false))
|
||||
watchers, err = repo.GetWatchers(db.ListOptions{Page: 1})
|
||||
watchers, err = GetRepoWatchers(repo.ID, db.ListOptions{Page: 1})
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, watchers, prevCount)
|
||||
|
||||
// Should add watch
|
||||
assert.NoError(t, WatchIfAuto(12, 1, true))
|
||||
watchers, err = repo.GetWatchers(db.ListOptions{Page: 1})
|
||||
watchers, err = GetRepoWatchers(repo.ID, db.ListOptions{Page: 1})
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, watchers, prevCount+1)
|
||||
|
||||
// Should remove watch, inhibit from adding auto
|
||||
assert.NoError(t, WatchRepo(12, 1, false))
|
||||
watchers, err = repo.GetWatchers(db.ListOptions{Page: 1})
|
||||
watchers, err = GetRepoWatchers(repo.ID, db.ListOptions{Page: 1})
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, watchers, prevCount)
|
||||
|
||||
// Must not add watch
|
||||
assert.NoError(t, WatchIfAuto(12, 1, true))
|
||||
watchers, err = repo.GetWatchers(db.ListOptions{Page: 1})
|
||||
watchers, err = GetRepoWatchers(repo.ID, db.ListOptions{Page: 1})
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, watchers, prevCount)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user