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

repo_watch_test.go 2.5 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. // Two watchers are inactive, thus minus 2
  33. assert.Len(t, watches, repo.NumWatches-2)
  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: 10}).(*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. // Two watchers are inactive, thus action is only created for user 8, 10
  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: 10,
  73. RepoID: action.RepoID,
  74. OpType: action.OpType,
  75. })
  76. }