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.

watch_test.go 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 repo
  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(unittest.NonexistentID, unittest.NonexistentID))
  20. }
  21. func TestGetWatchers(t *testing.T) {
  22. assert.NoError(t, unittest.PrepareTestDatabase())
  23. repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
  24. watches, err := GetWatchers(db.DefaultContext, repo.ID)
  25. assert.NoError(t, err)
  26. // One watchers are inactive, thus minus 1
  27. assert.Len(t, watches, repo.NumWatches-1)
  28. for _, watch := range watches {
  29. assert.EqualValues(t, repo.ID, watch.RepoID)
  30. }
  31. watches, err = GetWatchers(db.DefaultContext, unittest.NonexistentID)
  32. assert.NoError(t, err)
  33. assert.Len(t, watches, 0)
  34. }
  35. func TestRepository_GetWatchers(t *testing.T) {
  36. assert.NoError(t, unittest.PrepareTestDatabase())
  37. repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
  38. watchers, err := GetRepoWatchers(repo.ID, db.ListOptions{Page: 1})
  39. assert.NoError(t, err)
  40. assert.Len(t, watchers, repo.NumWatches)
  41. for _, watcher := range watchers {
  42. unittest.AssertExistsAndLoadBean(t, &Watch{UserID: watcher.ID, RepoID: repo.ID})
  43. }
  44. repo = unittest.AssertExistsAndLoadBean(t, &Repository{ID: 9}).(*Repository)
  45. watchers, err = GetRepoWatchers(repo.ID, db.ListOptions{Page: 1})
  46. assert.NoError(t, err)
  47. assert.Len(t, watchers, 0)
  48. }
  49. func TestWatchIfAuto(t *testing.T) {
  50. assert.NoError(t, unittest.PrepareTestDatabase())
  51. repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
  52. watchers, err := GetRepoWatchers(repo.ID, db.ListOptions{Page: 1})
  53. assert.NoError(t, err)
  54. assert.Len(t, watchers, repo.NumWatches)
  55. setting.Service.AutoWatchOnChanges = false
  56. prevCount := repo.NumWatches
  57. // Must not add watch
  58. assert.NoError(t, WatchIfAuto(db.DefaultContext, 8, 1, true))
  59. watchers, err = GetRepoWatchers(repo.ID, db.ListOptions{Page: 1})
  60. assert.NoError(t, err)
  61. assert.Len(t, watchers, prevCount)
  62. // Should not add watch
  63. assert.NoError(t, WatchIfAuto(db.DefaultContext, 10, 1, true))
  64. watchers, err = GetRepoWatchers(repo.ID, db.ListOptions{Page: 1})
  65. assert.NoError(t, err)
  66. assert.Len(t, watchers, prevCount)
  67. setting.Service.AutoWatchOnChanges = true
  68. // Must not add watch
  69. assert.NoError(t, WatchIfAuto(db.DefaultContext, 8, 1, true))
  70. watchers, err = GetRepoWatchers(repo.ID, db.ListOptions{Page: 1})
  71. assert.NoError(t, err)
  72. assert.Len(t, watchers, prevCount)
  73. // Should not add watch
  74. assert.NoError(t, WatchIfAuto(db.DefaultContext, 12, 1, false))
  75. watchers, err = GetRepoWatchers(repo.ID, db.ListOptions{Page: 1})
  76. assert.NoError(t, err)
  77. assert.Len(t, watchers, prevCount)
  78. // Should add watch
  79. assert.NoError(t, WatchIfAuto(db.DefaultContext, 12, 1, true))
  80. watchers, err = GetRepoWatchers(repo.ID, db.ListOptions{Page: 1})
  81. assert.NoError(t, err)
  82. assert.Len(t, watchers, prevCount+1)
  83. // Should remove watch, inhibit from adding auto
  84. assert.NoError(t, WatchRepo(db.DefaultContext, 12, 1, false))
  85. watchers, err = GetRepoWatchers(repo.ID, db.ListOptions{Page: 1})
  86. assert.NoError(t, err)
  87. assert.Len(t, watchers, prevCount)
  88. // Must not add watch
  89. assert.NoError(t, WatchIfAuto(db.DefaultContext, 12, 1, true))
  90. watchers, err = GetRepoWatchers(repo.ID, db.ListOptions{Page: 1})
  91. assert.NoError(t, err)
  92. assert.Len(t, watchers, prevCount)
  93. }
  94. func TestWatchRepoMode(t *testing.T) {
  95. assert.NoError(t, unittest.PrepareTestDatabase())
  96. unittest.AssertCount(t, &Watch{UserID: 12, RepoID: 1}, 0)
  97. assert.NoError(t, WatchRepoMode(12, 1, WatchModeAuto))
  98. unittest.AssertCount(t, &Watch{UserID: 12, RepoID: 1}, 1)
  99. unittest.AssertCount(t, &Watch{UserID: 12, RepoID: 1, Mode: WatchModeAuto}, 1)
  100. assert.NoError(t, WatchRepoMode(12, 1, WatchModeNormal))
  101. unittest.AssertCount(t, &Watch{UserID: 12, RepoID: 1}, 1)
  102. unittest.AssertCount(t, &Watch{UserID: 12, RepoID: 1, Mode: WatchModeNormal}, 1)
  103. assert.NoError(t, WatchRepoMode(12, 1, WatchModeDont))
  104. unittest.AssertCount(t, &Watch{UserID: 12, RepoID: 1}, 1)
  105. unittest.AssertCount(t, &Watch{UserID: 12, RepoID: 1, Mode: WatchModeDont}, 1)
  106. assert.NoError(t, WatchRepoMode(12, 1, WatchModeNone))
  107. unittest.AssertCount(t, &Watch{UserID: 12, RepoID: 1}, 0)
  108. }