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.

98 lines
2.5 KiB

  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. assert.Len(t, watches, repo.NumWatches)
  33. for _, watch := range watches {
  34. assert.EqualValues(t, repo.ID, watch.RepoID)
  35. }
  36. watches, err = GetWatchers(NonexistentID)
  37. assert.NoError(t, err)
  38. assert.Len(t, watches, 0)
  39. }
  40. func TestRepository_GetWatchers(t *testing.T) {
  41. assert.NoError(t, PrepareTestDatabase())
  42. repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
  43. watchers, err := repo.GetWatchers(1)
  44. assert.NoError(t, err)
  45. assert.Len(t, watchers, repo.NumWatches)
  46. for _, watcher := range watchers {
  47. AssertExistsAndLoadBean(t, &Watch{UserID: watcher.ID, RepoID: repo.ID})
  48. }
  49. repo = AssertExistsAndLoadBean(t, &Repository{ID: 10}).(*Repository)
  50. watchers, err = repo.GetWatchers(1)
  51. assert.NoError(t, err)
  52. assert.Len(t, watchers, 0)
  53. }
  54. func TestNotifyWatchers(t *testing.T) {
  55. assert.NoError(t, PrepareTestDatabase())
  56. action := &Action{
  57. ActUserID: 8,
  58. RepoID: 1,
  59. OpType: ActionStarRepo,
  60. }
  61. assert.NoError(t, NotifyWatchers(action))
  62. AssertExistsAndLoadBean(t, &Action{
  63. ActUserID: action.ActUserID,
  64. UserID: 1,
  65. RepoID: action.RepoID,
  66. OpType: action.OpType,
  67. })
  68. AssertExistsAndLoadBean(t, &Action{
  69. ActUserID: action.ActUserID,
  70. UserID: 4,
  71. RepoID: action.RepoID,
  72. OpType: action.OpType,
  73. })
  74. AssertExistsAndLoadBean(t, &Action{
  75. ActUserID: action.ActUserID,
  76. UserID: 8,
  77. RepoID: action.RepoID,
  78. OpType: action.OpType,
  79. })
  80. }