本站源代码
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.

138 lines
4.1KB

  1. package models
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. )
  6. func TestGetReviewByID(t *testing.T) {
  7. assert.NoError(t, PrepareTestDatabase())
  8. review, err := GetReviewByID(1)
  9. assert.NoError(t, err)
  10. assert.Equal(t, "Demo Review", review.Content)
  11. assert.Equal(t, ReviewTypeApprove, review.Type)
  12. _, err = GetReviewByID(23892)
  13. assert.Error(t, err)
  14. assert.True(t, IsErrReviewNotExist(err), "IsErrReviewNotExist")
  15. }
  16. func TestReview_LoadAttributes(t *testing.T) {
  17. assert.NoError(t, PrepareTestDatabase())
  18. review := AssertExistsAndLoadBean(t, &Review{ID: 1}).(*Review)
  19. assert.NoError(t, review.LoadAttributes())
  20. assert.NotNil(t, review.Issue)
  21. assert.NotNil(t, review.Reviewer)
  22. invalidReview1 := AssertExistsAndLoadBean(t, &Review{ID: 2}).(*Review)
  23. assert.Error(t, invalidReview1.LoadAttributes())
  24. invalidReview2 := AssertExistsAndLoadBean(t, &Review{ID: 3}).(*Review)
  25. assert.Error(t, invalidReview2.LoadAttributes())
  26. }
  27. func TestReview_LoadCodeComments(t *testing.T) {
  28. assert.NoError(t, PrepareTestDatabase())
  29. review := AssertExistsAndLoadBean(t, &Review{ID: 4}).(*Review)
  30. assert.NoError(t, review.LoadAttributes())
  31. assert.NoError(t, review.LoadCodeComments())
  32. assert.Len(t, review.CodeComments, 1)
  33. assert.Equal(t, int64(4), review.CodeComments["README.md"][int64(4)][0].Line)
  34. }
  35. func TestReviewType_Icon(t *testing.T) {
  36. assert.Equal(t, "eye", ReviewTypeApprove.Icon())
  37. assert.Equal(t, "x", ReviewTypeReject.Icon())
  38. assert.Equal(t, "comment", ReviewTypeComment.Icon())
  39. assert.Equal(t, "comment", ReviewTypeUnknown.Icon())
  40. assert.Equal(t, "comment", ReviewType(4).Icon())
  41. }
  42. func TestFindReviews(t *testing.T) {
  43. assert.NoError(t, PrepareTestDatabase())
  44. reviews, err := FindReviews(FindReviewOptions{
  45. Type: ReviewTypeApprove,
  46. IssueID: 2,
  47. ReviewerID: 1,
  48. })
  49. assert.NoError(t, err)
  50. assert.Len(t, reviews, 1)
  51. assert.Equal(t, "Demo Review", reviews[0].Content)
  52. }
  53. func TestGetCurrentReview(t *testing.T) {
  54. assert.NoError(t, PrepareTestDatabase())
  55. issue := AssertExistsAndLoadBean(t, &Issue{ID: 2}).(*Issue)
  56. user := AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
  57. review, err := GetCurrentReview(user, issue)
  58. assert.NoError(t, err)
  59. assert.NotNil(t, review)
  60. assert.Equal(t, ReviewTypePending, review.Type)
  61. assert.Equal(t, "Pending Review", review.Content)
  62. user2 := AssertExistsAndLoadBean(t, &User{ID: 7}).(*User)
  63. review2, err := GetCurrentReview(user2, issue)
  64. assert.Error(t, err)
  65. assert.True(t, IsErrReviewNotExist(err))
  66. assert.Nil(t, review2)
  67. }
  68. func TestCreateReview(t *testing.T) {
  69. assert.NoError(t, PrepareTestDatabase())
  70. issue := AssertExistsAndLoadBean(t, &Issue{ID: 2}).(*Issue)
  71. user := AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
  72. review, err := CreateReview(CreateReviewOptions{
  73. Content: "New Review",
  74. Type: ReviewTypePending,
  75. Issue: issue,
  76. Reviewer: user,
  77. })
  78. assert.NoError(t, err)
  79. assert.Equal(t, "New Review", review.Content)
  80. AssertExistsAndLoadBean(t, &Review{Content: "New Review"})
  81. }
  82. func TestUpdateReview(t *testing.T) {
  83. assert.NoError(t, PrepareTestDatabase())
  84. review := AssertExistsAndLoadBean(t, &Review{ID: 1}).(*Review)
  85. review.Content = "Updated Review"
  86. assert.NoError(t, UpdateReview(review))
  87. AssertExistsAndLoadBean(t, &Review{ID: 1, Content: "Updated Review"})
  88. }
  89. func TestGetReviewersByPullID(t *testing.T) {
  90. assert.NoError(t, PrepareTestDatabase())
  91. issue := AssertExistsAndLoadBean(t, &Issue{ID: 3}).(*Issue)
  92. user2 := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  93. user3 := AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
  94. user4 := AssertExistsAndLoadBean(t, &User{ID: 4}).(*User)
  95. expectedReviews := []*PullReviewersWithType{}
  96. expectedReviews = append(expectedReviews, &PullReviewersWithType{
  97. User: *user2,
  98. Type: ReviewTypeReject,
  99. ReviewUpdatedUnix: 946684810,
  100. },
  101. &PullReviewersWithType{
  102. User: *user3,
  103. Type: ReviewTypeReject,
  104. ReviewUpdatedUnix: 946684810,
  105. },
  106. &PullReviewersWithType{
  107. User: *user4,
  108. Type: ReviewTypeApprove,
  109. ReviewUpdatedUnix: 946684810,
  110. })
  111. allReviews, err := GetReviewersByPullID(issue.ID)
  112. assert.NoError(t, err)
  113. assert.Equal(t, expectedReviews, allReviews)
  114. }
上海开阖软件有限公司 沪ICP备12045867号-1