1
1
mirror of https://github.com/go-gitea/gitea synced 2025-07-22 18:28:37 +00:00

Move db related basic functions to models/db (#17075)

* Move db related basic functions to models/db

* Fix lint

* Fix lint

* Fix test

* Fix lint

* Fix lint

* revert unnecessary change

* Fix test

* Fix wrong replace string

* Use *Context

* Correct committer spelling and fix wrong replaced words

Co-authored-by: zeripath <art27@cantab.net>
This commit is contained in:
Lunny Xiao
2021-09-19 19:49:59 +08:00
committed by GitHub
parent 462306e263
commit a4bfef265d
335 changed files with 4191 additions and 3654 deletions

View File

@@ -7,13 +7,14 @@ package models
import (
"testing"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/setting"
"github.com/stretchr/testify/assert"
)
func TestIsWatching(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
assert.NoError(t, db.PrepareTestDatabase())
assert.True(t, IsWatching(1, 1))
assert.True(t, IsWatching(4, 1))
@@ -21,27 +22,27 @@ func TestIsWatching(t *testing.T) {
assert.False(t, IsWatching(1, 5))
assert.False(t, IsWatching(8, 1))
assert.False(t, IsWatching(NonexistentID, NonexistentID))
assert.False(t, IsWatching(db.NonexistentID, db.NonexistentID))
}
func TestWatchRepo(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
assert.NoError(t, db.PrepareTestDatabase())
const repoID = 3
const userID = 2
assert.NoError(t, WatchRepo(userID, repoID, true))
AssertExistsAndLoadBean(t, &Watch{RepoID: repoID, UserID: userID})
db.AssertExistsAndLoadBean(t, &Watch{RepoID: repoID, UserID: userID})
CheckConsistencyFor(t, &Repository{ID: repoID})
assert.NoError(t, WatchRepo(userID, repoID, false))
AssertNotExistsBean(t, &Watch{RepoID: repoID, UserID: userID})
db.AssertNotExistsBean(t, &Watch{RepoID: repoID, UserID: userID})
CheckConsistencyFor(t, &Repository{ID: repoID})
}
func TestGetWatchers(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
assert.NoError(t, db.PrepareTestDatabase())
repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
watches, err := GetWatchers(repo.ID)
assert.NoError(t, err)
// One watchers are inactive, thus minus 1
@@ -50,30 +51,30 @@ func TestGetWatchers(t *testing.T) {
assert.EqualValues(t, repo.ID, watch.RepoID)
}
watches, err = GetWatchers(NonexistentID)
watches, err = GetWatchers(db.NonexistentID)
assert.NoError(t, err)
assert.Len(t, watches, 0)
}
func TestRepository_GetWatchers(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
assert.NoError(t, db.PrepareTestDatabase())
repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
watchers, err := repo.GetWatchers(ListOptions{Page: 1})
assert.NoError(t, err)
assert.Len(t, watchers, repo.NumWatches)
for _, watcher := range watchers {
AssertExistsAndLoadBean(t, &Watch{UserID: watcher.ID, RepoID: repo.ID})
db.AssertExistsAndLoadBean(t, &Watch{UserID: watcher.ID, RepoID: repo.ID})
}
repo = AssertExistsAndLoadBean(t, &Repository{ID: 9}).(*Repository)
repo = db.AssertExistsAndLoadBean(t, &Repository{ID: 9}).(*Repository)
watchers, err = repo.GetWatchers(ListOptions{Page: 1})
assert.NoError(t, err)
assert.Len(t, watchers, 0)
}
func TestNotifyWatchers(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
assert.NoError(t, db.PrepareTestDatabase())
action := &Action{
ActUserID: 8,
@@ -83,25 +84,25 @@ func TestNotifyWatchers(t *testing.T) {
assert.NoError(t, NotifyWatchers(action))
// One watchers are inactive, thus action is only created for user 8, 1, 4, 11
AssertExistsAndLoadBean(t, &Action{
db.AssertExistsAndLoadBean(t, &Action{
ActUserID: action.ActUserID,
UserID: 8,
RepoID: action.RepoID,
OpType: action.OpType,
})
AssertExistsAndLoadBean(t, &Action{
db.AssertExistsAndLoadBean(t, &Action{
ActUserID: action.ActUserID,
UserID: 1,
RepoID: action.RepoID,
OpType: action.OpType,
})
AssertExistsAndLoadBean(t, &Action{
db.AssertExistsAndLoadBean(t, &Action{
ActUserID: action.ActUserID,
UserID: 4,
RepoID: action.RepoID,
OpType: action.OpType,
})
AssertExistsAndLoadBean(t, &Action{
db.AssertExistsAndLoadBean(t, &Action{
ActUserID: action.ActUserID,
UserID: 11,
RepoID: action.RepoID,
@@ -110,9 +111,9 @@ func TestNotifyWatchers(t *testing.T) {
}
func TestWatchIfAuto(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
assert.NoError(t, db.PrepareTestDatabase())
repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
watchers, err := repo.GetWatchers(ListOptions{Page: 1})
assert.NoError(t, err)
assert.Len(t, watchers, repo.NumWatches)
@@ -167,22 +168,22 @@ func TestWatchIfAuto(t *testing.T) {
}
func TestWatchRepoMode(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
assert.NoError(t, db.PrepareTestDatabase())
AssertCount(t, &Watch{UserID: 12, RepoID: 1}, 0)
db.AssertCount(t, &Watch{UserID: 12, RepoID: 1}, 0)
assert.NoError(t, WatchRepoMode(12, 1, RepoWatchModeAuto))
AssertCount(t, &Watch{UserID: 12, RepoID: 1}, 1)
AssertCount(t, &Watch{UserID: 12, RepoID: 1, Mode: RepoWatchModeAuto}, 1)
db.AssertCount(t, &Watch{UserID: 12, RepoID: 1}, 1)
db.AssertCount(t, &Watch{UserID: 12, RepoID: 1, Mode: RepoWatchModeAuto}, 1)
assert.NoError(t, WatchRepoMode(12, 1, RepoWatchModeNormal))
AssertCount(t, &Watch{UserID: 12, RepoID: 1}, 1)
AssertCount(t, &Watch{UserID: 12, RepoID: 1, Mode: RepoWatchModeNormal}, 1)
db.AssertCount(t, &Watch{UserID: 12, RepoID: 1}, 1)
db.AssertCount(t, &Watch{UserID: 12, RepoID: 1, Mode: RepoWatchModeNormal}, 1)
assert.NoError(t, WatchRepoMode(12, 1, RepoWatchModeDont))
AssertCount(t, &Watch{UserID: 12, RepoID: 1}, 1)
AssertCount(t, &Watch{UserID: 12, RepoID: 1, Mode: RepoWatchModeDont}, 1)
db.AssertCount(t, &Watch{UserID: 12, RepoID: 1}, 1)
db.AssertCount(t, &Watch{UserID: 12, RepoID: 1, Mode: RepoWatchModeDont}, 1)
assert.NoError(t, WatchRepoMode(12, 1, RepoWatchModeNone))
AssertCount(t, &Watch{UserID: 12, RepoID: 1}, 0)
db.AssertCount(t, &Watch{UserID: 12, RepoID: 1}, 0)
}