You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

repo_watch_test.go 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package models
  5. import (
  6. "testing"
  7. "code.gitea.io/gitea/models/db"
  8. "code.gitea.io/gitea/models/unittest"
  9. "code.gitea.io/gitea/modules/setting"
  10. "github.com/stretchr/testify/assert"
  11. )
  12. func TestIsWatching(t *testing.T) {
  13. assert.NoError(t, unittest.PrepareTestDatabase())
  14. assert.True(t, IsWatching(1, 1))
  15. assert.True(t, IsWatching(4, 1))
  16. assert.True(t, IsWatching(11, 1))
  17. assert.False(t, IsWatching(1, 5))
  18. assert.False(t, IsWatching(8, 1))
  19. assert.False(t, IsWatching(db.NonexistentID, db.NonexistentID))
  20. }
  21. func TestWatchRepo(t *testing.T) {
  22. assert.NoError(t, unittest.PrepareTestDatabase())
  23. const repoID = 3
  24. const userID = 2
  25. assert.NoError(t, WatchRepo(userID, repoID, true))
  26. db.AssertExistsAndLoadBean(t, &Watch{RepoID: repoID, UserID: userID})
  27. CheckConsistencyFor(t, &Repository{ID: repoID})
  28. assert.NoError(t, WatchRepo(userID, repoID, false))
  29. db.AssertNotExistsBean(t, &Watch{RepoID: repoID, UserID: userID})
  30. CheckConsistencyFor(t, &Repository{ID: repoID})
  31. }
  32. func TestGetWatchers(t *testing.T) {
  33. assert.NoError(t, unittest.PrepareTestDatabase())
  34. repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
  35. watches, err := GetWatchers(repo.ID)
  36. assert.NoError(t, err)
  37. // One watchers are inactive, thus minus 1
  38. assert.Len(t, watches, repo.NumWatches-1)
  39. for _, watch := range watches {
  40. assert.EqualValues(t, repo.ID, watch.RepoID)
  41. }
  42. watches, err = GetWatchers(db.NonexistentID)
  43. assert.NoError(t, err)
  44. assert.Len(t, watches, 0)
  45. }
  46. func TestRepository_GetWatchers(t *testing.T) {
  47. assert.NoError(t, unittest.PrepareTestDatabase())
  48. repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
  49. watchers, err := repo.GetWatchers(db.ListOptions{Page: 1})
  50. assert.NoError(t, err)
  51. assert.Len(t, watchers, repo.NumWatches)
  52. for _, watcher := range watchers {
  53. db.AssertExistsAndLoadBean(t, &Watch{UserID: watcher.ID, RepoID: repo.ID})
  54. }
  55. repo = db.AssertExistsAndLoadBean(t, &Repository{ID: 9}).(*Repository)
  56. watchers, err = repo.GetWatchers(db.ListOptions{Page: 1})
  57. assert.NoError(t, err)
  58. assert.Len(t, watchers, 0)
  59. }
  60. func TestNotifyWatchers(t *testing.T) {
  61. assert.NoError(t, unittest.PrepareTestDatabase())
  62. action := &Action{
  63. ActUserID: 8,
  64. RepoID: 1,
  65. OpType: ActionStarRepo,
  66. }
  67. assert.NoError(t, NotifyWatchers(action))
  68. // One watchers are inactive, thus action is only created for user 8, 1, 4, 11
  69. db.AssertExistsAndLoadBean(t, &Action{
  70. ActUserID: action.ActUserID,
  71. UserID: 8,
  72. RepoID: action.RepoID,
  73. OpType: action.OpType,
  74. })
  75. db.AssertExistsAndLoadBean(t, &Action{
  76. ActUserID: action.ActUserID,
  77. UserID: 1,
  78. RepoID: action.RepoID,
  79. OpType: action.OpType,
  80. })
  81. db.AssertExistsAndLoadBean(t, &Action{
  82. ActUserID: action.ActUserID,
  83. UserID: 4,
  84. RepoID: action.RepoID,
  85. OpType: action.OpType,
  86. })
  87. db.AssertExistsAndLoadBean(t, &Action{
  88. ActUserID: action.ActUserID,
  89. UserID: 11,
  90. RepoID: action.RepoID,
  91. OpType: action.OpType,
  92. })
  93. }
  94. func TestWatchIfAuto(t *testing.T) {
  95. assert.NoError(t, unittest.PrepareTestDatabase())
  96. repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
  97. watchers, err := repo.GetWatchers(db.ListOptions{Page: 1})
  98. assert.NoError(t, err)
  99. assert.Len(t, watchers, repo.NumWatches)
  100. setting.Service.AutoWatchOnChanges = false
  101. prevCount := repo.NumWatches
  102. // Must not add watch
  103. assert.NoError(t, WatchIfAuto(8, 1, true))
  104. watchers, err = repo.GetWatchers(db.ListOptions{Page: 1})
  105. assert.NoError(t, err)
  106. assert.Len(t, watchers, prevCount)
  107. // Should not add watch
  108. assert.NoError(t, WatchIfAuto(10, 1, true))
  109. watchers, err = repo.GetWatchers(db.ListOptions{Page: 1})
  110. assert.NoError(t, err)
  111. assert.Len(t, watchers, prevCount)
  112. setting.Service.AutoWatchOnChanges = true
  113. // Must not add watch
  114. assert.NoError(t, WatchIfAuto(8, 1, true))
  115. watchers, err = repo.GetWatchers(db.ListOptions{Page: 1})
  116. assert.NoError(t, err)
  117. assert.Len(t, watchers, prevCount)
  118. // Should not add watch
  119. assert.NoError(t, WatchIfAuto(12, 1, false))
  120. watchers, err = repo.GetWatchers(db.ListOptions{Page: 1})
  121. assert.NoError(t, err)
  122. assert.Len(t, watchers, prevCount)
  123. // Should add watch
  124. assert.NoError(t, WatchIfAuto(12, 1, true))
  125. watchers, err = repo.GetWatchers(db.ListOptions{Page: 1})
  126. assert.NoError(t, err)
  127. assert.Len(t, watchers, prevCount+1)
  128. // Should remove watch, inhibit from adding auto
  129. assert.NoError(t, WatchRepo(12, 1, false))
  130. watchers, err = repo.GetWatchers(db.ListOptions{Page: 1})
  131. assert.NoError(t, err)
  132. assert.Len(t, watchers, prevCount)
  133. // Must not add watch
  134. assert.NoError(t, WatchIfAuto(12, 1, true))
  135. watchers, err = repo.GetWatchers(db.ListOptions{Page: 1})
  136. assert.NoError(t, err)
  137. assert.Len(t, watchers, prevCount)
  138. }
  139. func TestWatchRepoMode(t *testing.T) {
  140. assert.NoError(t, unittest.PrepareTestDatabase())
  141. db.AssertCount(t, &Watch{UserID: 12, RepoID: 1}, 0)
  142. assert.NoError(t, WatchRepoMode(12, 1, RepoWatchModeAuto))
  143. db.AssertCount(t, &Watch{UserID: 12, RepoID: 1}, 1)
  144. db.AssertCount(t, &Watch{UserID: 12, RepoID: 1, Mode: RepoWatchModeAuto}, 1)
  145. assert.NoError(t, WatchRepoMode(12, 1, RepoWatchModeNormal))
  146. db.AssertCount(t, &Watch{UserID: 12, RepoID: 1}, 1)
  147. db.AssertCount(t, &Watch{UserID: 12, RepoID: 1, Mode: RepoWatchModeNormal}, 1)
  148. assert.NoError(t, WatchRepoMode(12, 1, RepoWatchModeDont))
  149. db.AssertCount(t, &Watch{UserID: 12, RepoID: 1}, 1)
  150. db.AssertCount(t, &Watch{UserID: 12, RepoID: 1, Mode: RepoWatchModeDont}, 1)
  151. assert.NoError(t, WatchRepoMode(12, 1, RepoWatchModeNone))
  152. db.AssertCount(t, &Watch{UserID: 12, RepoID: 1}, 0)
  153. }