Add unit tests for Review

Signed-off-by: Jonas Franz <info@jonasfranz.software>
This commit is contained in:
Jonas Franz 2018-05-11 14:36:08 +02:00
parent 36d6631957
commit 9c6bb4b1e9
No known key found for this signature in database
GPG Key ID: 506AEEBE80BEDECD
4 changed files with 149 additions and 1 deletions

View File

@ -20,3 +20,13 @@
issue_id: 1 # in repo_id 1
content: "meh..."
created_unix: 946684812
-
id: 4
type: 19 # code comment
poster_id: 1
issue_id: 2
content: "meh..."
review_id: 1
line: 4
tree_path: "README.md"
created_unix: 946684812

View File

@ -0,0 +1,32 @@
-
id: 1
type: 1
reviewer_id: 1
issue_id: 2
content: "Demo Review"
updated_unix: 946684810
created_unix: 946684810
-
id: 2
type: 1
reviewer_id: 534543
issue_id: 534543
content: "Invalid Review #1"
updated_unix: 946684810
created_unix: 946684810
-
id: 3
type: 1
reviewer_id: 1
issue_id: 343545
content: "Invalid Review #2"
updated_unix: 946684810
created_unix: 946684810
-
id: 4
type: 0 # Pending review
reviewer_id: 1
issue_id: 2
content: "Pending Review"
updated_unix: 946684810
created_unix: 946684810

View File

@ -60,7 +60,7 @@ type Review struct {
}
func (r *Review) loadCodeComments(e Engine) (err error) {
r.CodeComments, err = findComments(e, FindCommentsOptions{IssueID: r.IssueID, ReviewID: r.ID})
r.CodeComments, err = findComments(e, FindCommentsOptions{IssueID: r.IssueID, ReviewID: r.ID, Type: CommentTypeCode})
return
}

106
models/review_test.go Normal file
View File

@ -0,0 +1,106 @@
package models
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestGetReviewByID(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
review, err := GetReviewByID(1)
assert.NoError(t, err)
assert.Equal(t, "Demo Review", review.Content)
assert.Equal(t, ReviewTypeApprove, review.Type)
_, err = GetReviewByID(23892)
assert.Error(t, err)
assert.True(t, IsErrReviewNotExist(err), "IsErrReviewNotExist")
}
func TestReview_LoadAttributes(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
review := AssertExistsAndLoadBean(t, &Review{ID: 1}).(*Review)
assert.NoError(t, review.LoadAttributes())
assert.NotNil(t, review.Issue)
assert.NotNil(t, review.Reviewer)
invalidReview1 := AssertExistsAndLoadBean(t, &Review{ID: 2}).(*Review)
assert.Error(t, invalidReview1.LoadAttributes())
invalidReview2 := AssertExistsAndLoadBean(t, &Review{ID: 3}).(*Review)
assert.Error(t, invalidReview2.LoadAttributes())
}
func TestReview_LoadCodeComments(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
review := AssertExistsAndLoadBean(t, &Review{ID: 1}).(*Review)
assert.NoError(t, review.LoadCodeComments())
assert.Len(t, review.CodeComments, 1)
assert.Equal(t, int64(4), review.CodeComments[0].Line)
}
func TestReviewType_Icon(t *testing.T) {
assert.Equal(t, "eye", ReviewTypeApprove.Icon())
assert.Equal(t, "x", ReviewTypeReject.Icon())
assert.Equal(t, "comment", ReviewTypeComment.Icon())
assert.Equal(t, "comment", ReviewTypeUnknown.Icon())
assert.Equal(t, "comment", ReviewType(4).Icon())
}
func TestFindReviews(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
reviews, err := FindReviews(FindReviewOptions{
Type: ReviewTypeApprove,
IssueID: 2,
ReviewerID: 1,
})
assert.NoError(t, err)
assert.Len(t, reviews, 1)
assert.Equal(t, "Demo Review", reviews[0].Content)
}
func TestGetCurrentReview(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
issue := AssertExistsAndLoadBean(t, &Issue{ID: 2}).(*Issue)
user := AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
review, err := GetCurrentReview(user, issue)
assert.NoError(t, err)
assert.NotNil(t, review)
assert.Equal(t, ReviewTypePending, review.Type)
assert.Equal(t, "Pending Review", review.Content )
user2 := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
review2, err := GetCurrentReview(user2, issue)
assert.Error(t, err)
assert.True(t, IsErrReviewNotExist(err))
assert.Nil(t, review2)
}
func TestCreateReview(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
issue := AssertExistsAndLoadBean(t, &Issue{ID: 2}).(*Issue)
user := AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
review, err := CreateReview(CreateReviewOptions{
Content: "New Review",
Type: ReviewTypePending,
Issue: issue,
Reviewer: user,
})
assert.NoError(t, err)
assert.Equal(t, "New Review", review.Content)
AssertExistsAndLoadBean(t, &Review{Content: "New Review"})
}
func TestUpdateReview(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
review := AssertExistsAndLoadBean(t, &Review{ID: 1}).(*Review)
review.Content = "Updated Review"
assert.NoError(t, UpdateReview(review))
AssertExistsAndLoadBean(t, &Review{ID: 1, Content: "Updated Review"})
}