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.

issue_comment_test.go 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. "time"
  8. "code.gitea.io/gitea/models/unittest"
  9. "github.com/stretchr/testify/assert"
  10. )
  11. func TestCreateComment(t *testing.T) {
  12. assert.NoError(t, unittest.PrepareTestDatabase())
  13. issue := unittest.AssertExistsAndLoadBean(t, &Issue{}).(*Issue)
  14. repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: issue.RepoID}).(*Repository)
  15. doer := unittest.AssertExistsAndLoadBean(t, &User{ID: repo.OwnerID}).(*User)
  16. now := time.Now().Unix()
  17. comment, err := CreateComment(&CreateCommentOptions{
  18. Type: CommentTypeComment,
  19. Doer: doer,
  20. Repo: repo,
  21. Issue: issue,
  22. Content: "Hello",
  23. })
  24. assert.NoError(t, err)
  25. then := time.Now().Unix()
  26. assert.EqualValues(t, CommentTypeComment, comment.Type)
  27. assert.EqualValues(t, "Hello", comment.Content)
  28. assert.EqualValues(t, issue.ID, comment.IssueID)
  29. assert.EqualValues(t, doer.ID, comment.PosterID)
  30. unittest.AssertInt64InRange(t, now, then, int64(comment.CreatedUnix))
  31. unittest.AssertExistsAndLoadBean(t, comment) // assert actually added to DB
  32. updatedIssue := unittest.AssertExistsAndLoadBean(t, &Issue{ID: issue.ID}).(*Issue)
  33. unittest.AssertInt64InRange(t, now, then, int64(updatedIssue.UpdatedUnix))
  34. }
  35. func TestFetchCodeComments(t *testing.T) {
  36. assert.NoError(t, unittest.PrepareTestDatabase())
  37. issue := unittest.AssertExistsAndLoadBean(t, &Issue{ID: 2}).(*Issue)
  38. user := unittest.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
  39. res, err := FetchCodeComments(issue, user)
  40. assert.NoError(t, err)
  41. assert.Contains(t, res, "README.md")
  42. assert.Contains(t, res["README.md"], int64(4))
  43. assert.Len(t, res["README.md"][4], 1)
  44. assert.Equal(t, int64(4), res["README.md"][4][0].ID)
  45. user2 := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  46. res, err = FetchCodeComments(issue, user2)
  47. assert.NoError(t, err)
  48. assert.Len(t, res, 1)
  49. }