code.gitea.io/gitea@v1.21.7/models/repo/watch_test.go (about) 1 // Copyright 2017 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package repo_test 5 6 import ( 7 "testing" 8 9 "code.gitea.io/gitea/models/db" 10 repo_model "code.gitea.io/gitea/models/repo" 11 "code.gitea.io/gitea/models/unittest" 12 "code.gitea.io/gitea/modules/setting" 13 14 "github.com/stretchr/testify/assert" 15 ) 16 17 func TestIsWatching(t *testing.T) { 18 assert.NoError(t, unittest.PrepareTestDatabase()) 19 20 assert.True(t, repo_model.IsWatching(db.DefaultContext, 1, 1)) 21 assert.True(t, repo_model.IsWatching(db.DefaultContext, 4, 1)) 22 assert.True(t, repo_model.IsWatching(db.DefaultContext, 11, 1)) 23 24 assert.False(t, repo_model.IsWatching(db.DefaultContext, 1, 5)) 25 assert.False(t, repo_model.IsWatching(db.DefaultContext, 8, 1)) 26 assert.False(t, repo_model.IsWatching(db.DefaultContext, unittest.NonexistentID, unittest.NonexistentID)) 27 } 28 29 func TestGetWatchers(t *testing.T) { 30 assert.NoError(t, unittest.PrepareTestDatabase()) 31 32 repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) 33 watches, err := repo_model.GetWatchers(db.DefaultContext, repo.ID) 34 assert.NoError(t, err) 35 // One watchers are inactive, thus minus 1 36 assert.Len(t, watches, repo.NumWatches-1) 37 for _, watch := range watches { 38 assert.EqualValues(t, repo.ID, watch.RepoID) 39 } 40 41 watches, err = repo_model.GetWatchers(db.DefaultContext, unittest.NonexistentID) 42 assert.NoError(t, err) 43 assert.Len(t, watches, 0) 44 } 45 46 func TestRepository_GetWatchers(t *testing.T) { 47 assert.NoError(t, unittest.PrepareTestDatabase()) 48 49 repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) 50 watchers, err := repo_model.GetRepoWatchers(db.DefaultContext, repo.ID, db.ListOptions{Page: 1}) 51 assert.NoError(t, err) 52 assert.Len(t, watchers, repo.NumWatches) 53 for _, watcher := range watchers { 54 unittest.AssertExistsAndLoadBean(t, &repo_model.Watch{UserID: watcher.ID, RepoID: repo.ID}) 55 } 56 57 repo = unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 9}) 58 watchers, err = repo_model.GetRepoWatchers(db.DefaultContext, repo.ID, db.ListOptions{Page: 1}) 59 assert.NoError(t, err) 60 assert.Len(t, watchers, 0) 61 } 62 63 func TestWatchIfAuto(t *testing.T) { 64 assert.NoError(t, unittest.PrepareTestDatabase()) 65 66 repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) 67 watchers, err := repo_model.GetRepoWatchers(db.DefaultContext, repo.ID, db.ListOptions{Page: 1}) 68 assert.NoError(t, err) 69 assert.Len(t, watchers, repo.NumWatches) 70 71 setting.Service.AutoWatchOnChanges = false 72 73 prevCount := repo.NumWatches 74 75 // Must not add watch 76 assert.NoError(t, repo_model.WatchIfAuto(db.DefaultContext, 8, 1, true)) 77 watchers, err = repo_model.GetRepoWatchers(db.DefaultContext, repo.ID, db.ListOptions{Page: 1}) 78 assert.NoError(t, err) 79 assert.Len(t, watchers, prevCount) 80 81 // Should not add watch 82 assert.NoError(t, repo_model.WatchIfAuto(db.DefaultContext, 10, 1, true)) 83 watchers, err = repo_model.GetRepoWatchers(db.DefaultContext, repo.ID, db.ListOptions{Page: 1}) 84 assert.NoError(t, err) 85 assert.Len(t, watchers, prevCount) 86 87 setting.Service.AutoWatchOnChanges = true 88 89 // Must not add watch 90 assert.NoError(t, repo_model.WatchIfAuto(db.DefaultContext, 8, 1, true)) 91 watchers, err = repo_model.GetRepoWatchers(db.DefaultContext, repo.ID, db.ListOptions{Page: 1}) 92 assert.NoError(t, err) 93 assert.Len(t, watchers, prevCount) 94 95 // Should not add watch 96 assert.NoError(t, repo_model.WatchIfAuto(db.DefaultContext, 12, 1, false)) 97 watchers, err = repo_model.GetRepoWatchers(db.DefaultContext, repo.ID, db.ListOptions{Page: 1}) 98 assert.NoError(t, err) 99 assert.Len(t, watchers, prevCount) 100 101 // Should add watch 102 assert.NoError(t, repo_model.WatchIfAuto(db.DefaultContext, 12, 1, true)) 103 watchers, err = repo_model.GetRepoWatchers(db.DefaultContext, repo.ID, db.ListOptions{Page: 1}) 104 assert.NoError(t, err) 105 assert.Len(t, watchers, prevCount+1) 106 107 // Should remove watch, inhibit from adding auto 108 assert.NoError(t, repo_model.WatchRepo(db.DefaultContext, 12, 1, false)) 109 watchers, err = repo_model.GetRepoWatchers(db.DefaultContext, repo.ID, db.ListOptions{Page: 1}) 110 assert.NoError(t, err) 111 assert.Len(t, watchers, prevCount) 112 113 // Must not add watch 114 assert.NoError(t, repo_model.WatchIfAuto(db.DefaultContext, 12, 1, true)) 115 watchers, err = repo_model.GetRepoWatchers(db.DefaultContext, repo.ID, db.ListOptions{Page: 1}) 116 assert.NoError(t, err) 117 assert.Len(t, watchers, prevCount) 118 } 119 120 func TestWatchRepoMode(t *testing.T) { 121 assert.NoError(t, unittest.PrepareTestDatabase()) 122 123 unittest.AssertCount(t, &repo_model.Watch{UserID: 12, RepoID: 1}, 0) 124 125 assert.NoError(t, repo_model.WatchRepoMode(12, 1, repo_model.WatchModeAuto)) 126 unittest.AssertCount(t, &repo_model.Watch{UserID: 12, RepoID: 1}, 1) 127 unittest.AssertCount(t, &repo_model.Watch{UserID: 12, RepoID: 1, Mode: repo_model.WatchModeAuto}, 1) 128 129 assert.NoError(t, repo_model.WatchRepoMode(12, 1, repo_model.WatchModeNormal)) 130 unittest.AssertCount(t, &repo_model.Watch{UserID: 12, RepoID: 1}, 1) 131 unittest.AssertCount(t, &repo_model.Watch{UserID: 12, RepoID: 1, Mode: repo_model.WatchModeNormal}, 1) 132 133 assert.NoError(t, repo_model.WatchRepoMode(12, 1, repo_model.WatchModeDont)) 134 unittest.AssertCount(t, &repo_model.Watch{UserID: 12, RepoID: 1}, 1) 135 unittest.AssertCount(t, &repo_model.Watch{UserID: 12, RepoID: 1, Mode: repo_model.WatchModeDont}, 1) 136 137 assert.NoError(t, repo_model.WatchRepoMode(12, 1, repo_model.WatchModeNone)) 138 unittest.AssertCount(t, &repo_model.Watch{UserID: 12, RepoID: 1}, 0) 139 }