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.2KB

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