mirror of
https://github.com/go-gitea/gitea.git
synced 2026-10-26 06:16:56 +09:00
Consolidate all Go tests. Coverage is identical to before. Go tests are around 5% faster while being 7% smaller. No non-test code is touched.
181 lines
7.7 KiB
Go
181 lines
7.7 KiB
Go
// Copyright 2017 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package repo_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"gitea.dev/models/db"
|
|
repo_model "gitea.dev/models/repo"
|
|
"gitea.dev/models/unittest"
|
|
user_model "gitea.dev/models/user"
|
|
"gitea.dev/modules/setting"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestIsWatching(t *testing.T) {
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
assert.True(t, repo_model.IsWatchingRepo(t.Context(), 1, 1))
|
|
assert.True(t, repo_model.IsWatchingRepo(t.Context(), 4, 1))
|
|
assert.True(t, repo_model.IsWatchingRepo(t.Context(), 11, 1))
|
|
|
|
assert.False(t, repo_model.IsWatchingRepo(t.Context(), 1, 5))
|
|
assert.False(t, repo_model.IsWatchingRepo(t.Context(), 8, 1))
|
|
assert.False(t, repo_model.IsWatchingRepo(t.Context(), unittest.NonexistentID, unittest.NonexistentID))
|
|
}
|
|
|
|
func TestGetWatchers(t *testing.T) {
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
|
watches, err := repo_model.GetWatchers(t.Context(), repo.ID)
|
|
assert.NoError(t, err)
|
|
// One watcher is inactive, thus minus 1
|
|
assert.Len(t, watches, repo.NumWatches-1)
|
|
for _, watch := range watches {
|
|
assert.Equal(t, repo.ID, watch.RepoID)
|
|
}
|
|
|
|
watches, err = repo_model.GetWatchers(t.Context(), unittest.NonexistentID)
|
|
assert.NoError(t, err)
|
|
assert.Empty(t, watches)
|
|
}
|
|
|
|
func TestRepository_GetWatchers(t *testing.T) {
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
|
watchers, err := repo_model.GetRepoWatchers(t.Context(), repo.ID, db.ListOptions{Page: 1})
|
|
assert.NoError(t, err)
|
|
assert.Len(t, watchers, repo.NumWatches)
|
|
for _, watcher := range watchers {
|
|
unittest.AssertExistsAndLoadBean(t, &repo_model.Watch{UserID: watcher.ID, RepoID: repo.ID})
|
|
}
|
|
|
|
repo = unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 9})
|
|
watchers, err = repo_model.GetRepoWatchers(t.Context(), repo.ID, db.ListOptions{Page: 1})
|
|
assert.NoError(t, err)
|
|
assert.Empty(t, watchers)
|
|
}
|
|
|
|
func TestWatchIfAuto(t *testing.T) {
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
|
user12 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 12})
|
|
|
|
watchers, err := repo_model.GetRepoWatchers(t.Context(), repo.ID, db.ListOptions{Page: 1})
|
|
assert.NoError(t, err)
|
|
assert.Len(t, watchers, repo.NumWatches)
|
|
|
|
prevCount := repo.NumWatches
|
|
for _, test := range []struct {
|
|
autoWatch bool
|
|
userID int64
|
|
isWrite bool
|
|
count int
|
|
}{
|
|
{false, 8, true, prevCount}, // Must not add watch
|
|
{false, 10, true, prevCount}, // Should not add watch
|
|
{true, 8, true, prevCount}, // Must not add watch (user 8 has "don't watch" set)
|
|
{true, 12, false, prevCount}, // Should not add watch (read operation)
|
|
{true, 12, true, prevCount + 1}, // Should add watch (write operation)
|
|
{true, user_model.ActionsUserID, true, prevCount + 1}, // Should not add watch (system user should not be able to watch)
|
|
} {
|
|
setting.Service.AutoWatchOnChanges = test.autoWatch
|
|
assert.NoError(t, repo_model.WatchIfAuto(t.Context(), test.userID, 1, test.isWrite))
|
|
watchers, err = repo_model.GetRepoWatchers(t.Context(), repo.ID, db.ListOptions{Page: 1})
|
|
assert.NoError(t, err)
|
|
assert.Len(t, watchers, test.count)
|
|
}
|
|
|
|
// Should remove watch, inhibit from adding auto
|
|
assert.NoError(t, repo_model.WatchRepoAuto(t.Context(), user12, repo, false))
|
|
watchers, err = repo_model.GetRepoWatchers(t.Context(), repo.ID, db.ListOptions{Page: 1})
|
|
assert.NoError(t, err)
|
|
assert.Len(t, watchers, prevCount)
|
|
|
|
// Must not add watch
|
|
assert.NoError(t, repo_model.WatchIfAuto(t.Context(), 12, 1, true))
|
|
watchers, err = repo_model.GetRepoWatchers(t.Context(), repo.ID, db.ListOptions{Page: 1})
|
|
assert.NoError(t, err)
|
|
assert.Len(t, watchers, prevCount)
|
|
}
|
|
|
|
func TestClearRepoWatches(t *testing.T) {
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
const repoID int64 = 1
|
|
watchers, err := repo_model.GetRepoWatchers(t.Context(), repoID, db.ListOptions{Page: 1})
|
|
require.NoError(t, err)
|
|
require.NotEmpty(t, watchers)
|
|
|
|
assert.NoError(t, repo_model.ClearRepoWatches(t.Context(), repoID))
|
|
|
|
watchers, err = repo_model.GetRepoWatchers(t.Context(), repoID, db.ListOptions{Page: 1})
|
|
assert.NoError(t, err)
|
|
assert.Empty(t, watchers)
|
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: repoID})
|
|
assert.Zero(t, repo.NumWatches)
|
|
}
|
|
|
|
func TestWatchOptions(t *testing.T) {
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
// repo 1 is watched by users 1, 4, 9 and 11, all with every event enabled
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
|
|
assert.NoError(t, repo_model.WatchRepoWithOptions(t.Context(), user, repo, repo_model.WatchOptions{Mode: repo_model.WatchModeNormal, WatchPullRequests: true})) // custom watches were stored like this before WatchModeCustom
|
|
|
|
for watchType, expected := range map[repo_model.WatchType][]int64{
|
|
repo_model.WatchPullRequests: {1, 4, 9, 11},
|
|
repo_model.WatchIssues: {4, 9, 11},
|
|
repo_model.WatchReleases: {4, 9, 11},
|
|
} {
|
|
ids, err := repo_model.GetRepoWatchersIDs(t.Context(), repo.ID, watchType)
|
|
assert.NoError(t, err)
|
|
assert.ElementsMatch(t, expected, ids, watchType)
|
|
}
|
|
|
|
// a custom watch picking every event differs from all activity, which can be set up automatically
|
|
customUser := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 4})
|
|
assert.NoError(t, repo_model.WatchRepoWithOptions(t.Context(), customUser, repo, repo_model.WatchOptions{Mode: repo_model.WatchModeCustom, WatchPullRequests: true, WatchIssues: true, WatchReleases: true}))
|
|
assert.NoError(t, repo_model.WatchRepoAuto(t.Context(), customUser, repo, true)) // must keep the custom watch
|
|
for watchType, expected := range map[repo_model.WatchType][]int64{
|
|
repo_model.WatchPullRequests: {1, 4},
|
|
repo_model.WatchIssues: {4},
|
|
repo_model.WatchReleases: {4},
|
|
} {
|
|
ids, err := repo_model.GetRepoCustomWatchersIDs(t.Context(), repo.ID, watchType)
|
|
assert.NoError(t, err)
|
|
assert.ElementsMatch(t, expected, ids, watchType)
|
|
}
|
|
|
|
// the options of one user must not show up for another
|
|
watches, err := repo_model.GetUserWatches(t.Context(), 4, []int64{repo.ID})
|
|
assert.NoError(t, err)
|
|
assert.True(t, watches[repo.ID].IncludeIssues)
|
|
|
|
// watching again resets a custom selection
|
|
assert.NoError(t, repo_model.WatchRepoAuto(t.Context(), user, repo, false))
|
|
assert.NoError(t, repo_model.WatchRepoAuto(t.Context(), user, repo, true))
|
|
watch, err := repo_model.GetWatch(t.Context(), user.ID, repo.ID)
|
|
assert.NoError(t, err)
|
|
assert.True(t, watch.IsWatchingAll())
|
|
}
|
|
|
|
func TestWatchSelectedMode(t *testing.T) {
|
|
// a user without a watch row gets the dummy record, whose flags are the column defaults
|
|
assert.Equal(t, "participate", (&repo_model.Watch{Mode: repo_model.WatchModeNone, IncludePullRequests: true, IncludeIssues: true, IncludeReleases: true}).SelectedMode())
|
|
assert.Equal(t, "participate", (&repo_model.Watch{Mode: repo_model.WatchModeNormal}).SelectedMode())
|
|
assert.Equal(t, "ignore", (&repo_model.Watch{Mode: repo_model.WatchModeDont}).SelectedMode())
|
|
assert.Equal(t, "custom", (&repo_model.Watch{Mode: repo_model.WatchModeNormal, IncludeIssues: true}).SelectedMode())
|
|
assert.Equal(t, "custom", (&repo_model.Watch{Mode: repo_model.WatchModeCustom, IncludePullRequests: true, IncludeIssues: true, IncludeReleases: true}).SelectedMode())
|
|
assert.Equal(t, "all", (&repo_model.Watch{Mode: repo_model.WatchModeAuto, IncludePullRequests: true, IncludeIssues: true, IncludeReleases: true}).SelectedMode())
|
|
}
|