2017-02-17 13:32:11 +05:30
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2017-02-17 13:32:11 +05:30
|
|
|
|
2022-06-06 13:31:49 +05:30
|
|
|
package repo_test
|
2017-02-17 13:32:11 +05:30
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2021-09-19 17:19:59 +05:30
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-06-06 13:31:49 +05:30
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-12 20:06:47 +05:30
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2019-11-10 14:52:19 +05:30
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
|
2017-02-17 13:32:11 +05:30
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestIsWatching(t *testing.T) {
|
2021-11-12 20:06:47 +05:30
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-02-17 13:32:11 +05:30
|
|
|
|
2023-09-15 11:43:19 +05:30
|
|
|
assert.True(t, repo_model.IsWatching(db.DefaultContext, 1, 1))
|
|
|
|
assert.True(t, repo_model.IsWatching(db.DefaultContext, 4, 1))
|
|
|
|
assert.True(t, repo_model.IsWatching(db.DefaultContext, 11, 1))
|
2017-02-17 13:32:11 +05:30
|
|
|
|
2023-09-15 11:43:19 +05:30
|
|
|
assert.False(t, repo_model.IsWatching(db.DefaultContext, 1, 5))
|
|
|
|
assert.False(t, repo_model.IsWatching(db.DefaultContext, 8, 1))
|
|
|
|
assert.False(t, repo_model.IsWatching(db.DefaultContext, unittest.NonexistentID, unittest.NonexistentID))
|
2017-02-17 13:32:11 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetWatchers(t *testing.T) {
|
2021-11-12 20:06:47 +05:30
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-02-17 13:32:11 +05:30
|
|
|
|
2022-08-16 07:52:25 +05:30
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
2022-06-06 13:31:49 +05:30
|
|
|
watches, err := repo_model.GetWatchers(db.DefaultContext, repo.ID)
|
2017-02-17 13:32:11 +05:30
|
|
|
assert.NoError(t, err)
|
2017-10-24 23:06:19 +05:30
|
|
|
// One watchers are inactive, thus minus 1
|
|
|
|
assert.Len(t, watches, repo.NumWatches-1)
|
2017-02-17 13:32:11 +05:30
|
|
|
for _, watch := range watches {
|
|
|
|
assert.EqualValues(t, repo.ID, watch.RepoID)
|
|
|
|
}
|
|
|
|
|
2022-06-06 13:31:49 +05:30
|
|
|
watches, err = repo_model.GetWatchers(db.DefaultContext, unittest.NonexistentID)
|
2017-02-17 13:32:11 +05:30
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, watches, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRepository_GetWatchers(t *testing.T) {
|
2021-11-12 20:06:47 +05:30
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-02-17 13:32:11 +05:30
|
|
|
|
2022-08-16 07:52:25 +05:30
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
2023-09-15 11:43:19 +05:30
|
|
|
watchers, err := repo_model.GetRepoWatchers(db.DefaultContext, repo.ID, db.ListOptions{Page: 1})
|
2017-02-17 13:32:11 +05:30
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, watchers, repo.NumWatches)
|
|
|
|
for _, watcher := range watchers {
|
2022-06-06 13:31:49 +05:30
|
|
|
unittest.AssertExistsAndLoadBean(t, &repo_model.Watch{UserID: watcher.ID, RepoID: repo.ID})
|
2017-02-17 13:32:11 +05:30
|
|
|
}
|
|
|
|
|
2022-08-16 07:52:25 +05:30
|
|
|
repo = unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 9})
|
2023-09-15 11:43:19 +05:30
|
|
|
watchers, err = repo_model.GetRepoWatchers(db.DefaultContext, repo.ID, db.ListOptions{Page: 1})
|
2017-02-17 13:32:11 +05:30
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, watchers, 0)
|
|
|
|
}
|
|
|
|
|
2019-11-10 14:52:19 +05:30
|
|
|
func TestWatchIfAuto(t *testing.T) {
|
2021-11-12 20:06:47 +05:30
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2019-11-10 14:52:19 +05:30
|
|
|
|
2022-08-16 07:52:25 +05:30
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
2023-09-15 11:43:19 +05:30
|
|
|
watchers, err := repo_model.GetRepoWatchers(db.DefaultContext, repo.ID, db.ListOptions{Page: 1})
|
2019-11-10 14:52:19 +05:30
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, watchers, repo.NumWatches)
|
|
|
|
|
|
|
|
setting.Service.AutoWatchOnChanges = false
|
|
|
|
|
|
|
|
prevCount := repo.NumWatches
|
|
|
|
|
|
|
|
// Must not add watch
|
2022-06-06 13:31:49 +05:30
|
|
|
assert.NoError(t, repo_model.WatchIfAuto(db.DefaultContext, 8, 1, true))
|
2023-09-15 11:43:19 +05:30
|
|
|
watchers, err = repo_model.GetRepoWatchers(db.DefaultContext, repo.ID, db.ListOptions{Page: 1})
|
2019-11-10 14:52:19 +05:30
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, watchers, prevCount)
|
|
|
|
|
|
|
|
// Should not add watch
|
2022-06-06 13:31:49 +05:30
|
|
|
assert.NoError(t, repo_model.WatchIfAuto(db.DefaultContext, 10, 1, true))
|
2023-09-15 11:43:19 +05:30
|
|
|
watchers, err = repo_model.GetRepoWatchers(db.DefaultContext, repo.ID, db.ListOptions{Page: 1})
|
2019-11-10 14:52:19 +05:30
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, watchers, prevCount)
|
|
|
|
|
|
|
|
setting.Service.AutoWatchOnChanges = true
|
|
|
|
|
|
|
|
// Must not add watch
|
2022-06-06 13:31:49 +05:30
|
|
|
assert.NoError(t, repo_model.WatchIfAuto(db.DefaultContext, 8, 1, true))
|
2023-09-15 11:43:19 +05:30
|
|
|
watchers, err = repo_model.GetRepoWatchers(db.DefaultContext, repo.ID, db.ListOptions{Page: 1})
|
2019-11-10 14:52:19 +05:30
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, watchers, prevCount)
|
|
|
|
|
|
|
|
// Should not add watch
|
2022-06-06 13:31:49 +05:30
|
|
|
assert.NoError(t, repo_model.WatchIfAuto(db.DefaultContext, 12, 1, false))
|
2023-09-15 11:43:19 +05:30
|
|
|
watchers, err = repo_model.GetRepoWatchers(db.DefaultContext, repo.ID, db.ListOptions{Page: 1})
|
2019-11-10 14:52:19 +05:30
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, watchers, prevCount)
|
|
|
|
|
|
|
|
// Should add watch
|
2022-06-06 13:31:49 +05:30
|
|
|
assert.NoError(t, repo_model.WatchIfAuto(db.DefaultContext, 12, 1, true))
|
2023-09-15 11:43:19 +05:30
|
|
|
watchers, err = repo_model.GetRepoWatchers(db.DefaultContext, repo.ID, db.ListOptions{Page: 1})
|
2019-11-10 14:52:19 +05:30
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, watchers, prevCount+1)
|
|
|
|
|
|
|
|
// Should remove watch, inhibit from adding auto
|
2022-06-06 13:31:49 +05:30
|
|
|
assert.NoError(t, repo_model.WatchRepo(db.DefaultContext, 12, 1, false))
|
2023-09-15 11:43:19 +05:30
|
|
|
watchers, err = repo_model.GetRepoWatchers(db.DefaultContext, repo.ID, db.ListOptions{Page: 1})
|
2019-11-10 14:52:19 +05:30
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, watchers, prevCount)
|
|
|
|
|
|
|
|
// Must not add watch
|
2022-06-06 13:31:49 +05:30
|
|
|
assert.NoError(t, repo_model.WatchIfAuto(db.DefaultContext, 12, 1, true))
|
2023-09-15 11:43:19 +05:30
|
|
|
watchers, err = repo_model.GetRepoWatchers(db.DefaultContext, repo.ID, db.ListOptions{Page: 1})
|
2019-11-10 14:52:19 +05:30
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, watchers, prevCount)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWatchRepoMode(t *testing.T) {
|
2021-11-12 20:06:47 +05:30
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2019-11-10 14:52:19 +05:30
|
|
|
|
2022-06-06 13:31:49 +05:30
|
|
|
unittest.AssertCount(t, &repo_model.Watch{UserID: 12, RepoID: 1}, 0)
|
2019-11-10 14:52:19 +05:30
|
|
|
|
2022-06-06 13:31:49 +05:30
|
|
|
assert.NoError(t, repo_model.WatchRepoMode(12, 1, repo_model.WatchModeAuto))
|
|
|
|
unittest.AssertCount(t, &repo_model.Watch{UserID: 12, RepoID: 1}, 1)
|
|
|
|
unittest.AssertCount(t, &repo_model.Watch{UserID: 12, RepoID: 1, Mode: repo_model.WatchModeAuto}, 1)
|
2019-11-10 14:52:19 +05:30
|
|
|
|
2022-06-06 13:31:49 +05:30
|
|
|
assert.NoError(t, repo_model.WatchRepoMode(12, 1, repo_model.WatchModeNormal))
|
|
|
|
unittest.AssertCount(t, &repo_model.Watch{UserID: 12, RepoID: 1}, 1)
|
|
|
|
unittest.AssertCount(t, &repo_model.Watch{UserID: 12, RepoID: 1, Mode: repo_model.WatchModeNormal}, 1)
|
2019-11-10 14:52:19 +05:30
|
|
|
|
2022-06-06 13:31:49 +05:30
|
|
|
assert.NoError(t, repo_model.WatchRepoMode(12, 1, repo_model.WatchModeDont))
|
|
|
|
unittest.AssertCount(t, &repo_model.Watch{UserID: 12, RepoID: 1}, 1)
|
|
|
|
unittest.AssertCount(t, &repo_model.Watch{UserID: 12, RepoID: 1, Mode: repo_model.WatchModeDont}, 1)
|
2019-11-10 14:52:19 +05:30
|
|
|
|
2022-06-06 13:31:49 +05:30
|
|
|
assert.NoError(t, repo_model.WatchRepoMode(12, 1, repo_model.WatchModeNone))
|
|
|
|
unittest.AssertCount(t, &repo_model.Watch{UserID: 12, RepoID: 1}, 0)
|
2017-02-17 13:32:11 +05:30
|
|
|
}
|