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

star_test.go 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. if assert.Len(t, gazers, 1) {
  33. assert.Equal(t, int64(2), gazers[0].ID)
  34. }
  35. }
  36. func TestRepository_GetStargazers2(t *testing.T) {
  37. // repo with stargazers
  38. assert.NoError(t, PrepareTestDatabase())
  39. repo := AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository)
  40. gazers, err := repo.GetStargazers(0)
  41. assert.NoError(t, err)
  42. assert.Len(t, gazers, 0)
  43. }
  44. func TestUser_GetStarredRepos(t *testing.T) {
  45. // user who has starred repos
  46. assert.NoError(t, PrepareTestDatabase())
  47. user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  48. starred, err := user.GetStarredRepos(false, 1, 10, "")
  49. assert.NoError(t, err)
  50. if assert.Len(t, starred, 1) {
  51. assert.Equal(t, int64(4), starred[0].ID)
  52. }
  53. starred, err = user.GetStarredRepos(true, 1, 10, "")
  54. assert.NoError(t, err)
  55. if assert.Len(t, starred, 2) {
  56. assert.Equal(t, int64(2), starred[0].ID)
  57. assert.Equal(t, int64(4), starred[1].ID)
  58. }
  59. }
  60. func TestUser_GetStarredRepos2(t *testing.T) {
  61. // user who has no starred repos
  62. assert.NoError(t, PrepareTestDatabase())
  63. user := AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
  64. starred, err := user.GetStarredRepos(false, 1, 10, "")
  65. assert.NoError(t, err)
  66. assert.Len(t, starred, 0)
  67. starred, err = user.GetStarredRepos(true, 1, 10, "")
  68. assert.NoError(t, err)
  69. assert.Len(t, starred, 0)
  70. }
  71. func TestUserGetStarredRepoCount(t *testing.T) {
  72. assert.NoError(t, PrepareTestDatabase())
  73. user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  74. counts, err := user.GetStarredRepoCount(false)
  75. assert.NoError(t, err)
  76. assert.Equal(t, int64(1), counts)
  77. counts, err = user.GetStarredRepoCount(true)
  78. assert.NoError(t, err)
  79. assert.Equal(t, int64(2), counts)
  80. }