Fork to maintain patches against the official gitea for https://code.ceondo.com https://github.com/go-gitea/gitea

repo_watch_test.go 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. "github.com/stretchr/testify/assert"
  8. )
  9. func TestIsWatching(t *testing.T) {
  10. assert.NoError(t, PrepareTestDatabase())
  11. assert.True(t, IsWatching(1, 1))
  12. assert.True(t, IsWatching(4, 1))
  13. assert.False(t, IsWatching(1, 5))
  14. assert.False(t, IsWatching(NonexistentID, NonexistentID))
  15. }
  16. func TestWatchRepo(t *testing.T) {
  17. assert.NoError(t, PrepareTestDatabase())
  18. const repoID = 3
  19. const userID = 2
  20. assert.NoError(t, WatchRepo(userID, repoID, true))
  21. AssertExistsAndLoadBean(t, &Watch{RepoID: repoID, UserID: userID})
  22. CheckConsistencyFor(t, &Repository{ID: repoID})
  23. assert.NoError(t, WatchRepo(userID, repoID, false))
  24. AssertNotExistsBean(t, &Watch{RepoID: repoID, UserID: userID})
  25. CheckConsistencyFor(t, &Repository{ID: repoID})
  26. }
  27. func TestGetWatchers(t *testing.T) {
  28. assert.NoError(t, PrepareTestDatabase())
  29. repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
  30. watches, err := GetWatchers(repo.ID)
  31. assert.NoError(t, err)
  32. // One watchers are inactive, thus minus 1
  33. assert.Len(t, watches, repo.NumWatches-1)
  34. for _, watch := range watches {
  35. assert.EqualValues(t, repo.ID, watch.RepoID)
  36. }
  37. watches, err = GetWatchers(NonexistentID)
  38. assert.NoError(t, err)
  39. assert.Len(t, watches, 0)
  40. }
  41. func TestRepository_GetWatchers(t *testing.T) {
  42. assert.NoError(t, PrepareTestDatabase())
  43. repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
  44. watchers, err := repo.GetWatchers(1)
  45. assert.NoError(t, err)
  46. assert.Len(t, watchers, repo.NumWatches)
  47. for _, watcher := range watchers {
  48. AssertExistsAndLoadBean(t, &Watch{UserID: watcher.ID, RepoID: repo.ID})
  49. }
  50. repo = AssertExistsAndLoadBean(t, &Repository{ID: 9}).(*Repository)
  51. watchers, err = repo.GetWatchers(1)
  52. assert.NoError(t, err)
  53. assert.Len(t, watchers, 0)
  54. }
  55. func TestNotifyWatchers(t *testing.T) {
  56. assert.NoError(t, PrepareTestDatabase())
  57. action := &Action{
  58. ActUserID: 8,
  59. RepoID: 1,
  60. OpType: ActionStarRepo,
  61. }
  62. assert.NoError(t, NotifyWatchers(action))
  63. // One watchers are inactive, thus action is only created for user 8, 1, 4
  64. AssertExistsAndLoadBean(t, &Action{
  65. ActUserID: action.ActUserID,
  66. UserID: 8,
  67. RepoID: action.RepoID,
  68. OpType: action.OpType,
  69. })
  70. AssertExistsAndLoadBean(t, &Action{
  71. ActUserID: action.ActUserID,
  72. UserID: 1,
  73. RepoID: action.RepoID,
  74. OpType: action.OpType,
  75. })
  76. AssertExistsAndLoadBean(t, &Action{
  77. ActUserID: action.ActUserID,
  78. UserID: 4,
  79. RepoID: action.RepoID,
  80. OpType: action.OpType,
  81. })
  82. }