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.

93 lines
2.7 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 TestStarRepo(t *testing.T) {
  10. assert.NoError(t, PrepareTestDatabase())
  11. const userID = 2
  12. const repoID = 1
  13. AssertNotExistsBean(t, &Star{UID: userID, RepoID: repoID})
  14. assert.NoError(t, StarRepo(userID, repoID, true))
  15. AssertExistsAndLoadBean(t, &Star{UID: userID, RepoID: repoID})
  16. assert.NoError(t, StarRepo(userID, repoID, true))
  17. AssertExistsAndLoadBean(t, &Star{UID: userID, RepoID: repoID})
  18. assert.NoError(t, StarRepo(userID, repoID, false))
  19. AssertNotExistsBean(t, &Star{UID: userID, RepoID: repoID})
  20. }
  21. func TestIsStaring(t *testing.T) {
  22. assert.NoError(t, PrepareTestDatabase())
  23. assert.True(t, IsStaring(2, 4))
  24. assert.False(t, IsStaring(3, 4))
  25. }
  26. func TestRepository_GetStargazers(t *testing.T) {
  27. // repo with stargazers
  28. assert.NoError(t, PrepareTestDatabase())
  29. repo := AssertExistsAndLoadBean(t, &Repository{ID: 4}).(*Repository)
  30. gazers, err := repo.GetStargazers(0)
  31. assert.NoError(t, err)
  32. assert.Len(t, gazers, 1)
  33. assert.Equal(t, int64(2), gazers[0].ID)
  34. }
  35. func TestRepository_GetStargazers2(t *testing.T) {
  36. // repo with stargazers
  37. assert.NoError(t, PrepareTestDatabase())
  38. repo := AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository)
  39. gazers, err := repo.GetStargazers(0)
  40. assert.NoError(t, err)
  41. assert.Len(t, gazers, 0)
  42. }
  43. func TestUser_GetStarredRepos(t *testing.T) {
  44. // user who has starred repos
  45. assert.NoError(t, PrepareTestDatabase())
  46. user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  47. starred, err := user.GetStarredRepos(false, 1, 10, "")
  48. assert.NoError(t, err)
  49. assert.Len(t, starred, 1)
  50. assert.Equal(t, int64(4), starred[0].ID)
  51. starred, err = user.GetStarredRepos(true, 1, 10, "")
  52. assert.NoError(t, err)
  53. assert.Len(t, starred, 2)
  54. assert.Equal(t, int64(2), starred[0].ID)
  55. assert.Equal(t, int64(4), starred[1].ID)
  56. }
  57. func TestUser_GetStarredRepos2(t *testing.T) {
  58. // user who has no starred repos
  59. assert.NoError(t, PrepareTestDatabase())
  60. user := AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
  61. starred, err := user.GetStarredRepos(false, 1, 10, "")
  62. assert.NoError(t, err)
  63. assert.Len(t, starred, 0)
  64. starred, err = user.GetStarredRepos(true, 1, 10, "")
  65. assert.NoError(t, err)
  66. assert.Len(t, starred, 0)
  67. }
  68. func TestUserGetStarredRepoCount(t *testing.T) {
  69. assert.NoError(t, PrepareTestDatabase())
  70. user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  71. counts, err := user.GetStarredRepoCount(false)
  72. assert.NoError(t, err)
  73. assert.Equal(t, int64(1), counts)
  74. counts, err = user.GetStarredRepoCount(true)
  75. assert.NoError(t, err)
  76. assert.Equal(t, int64(2), counts)
  77. }