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

60 lines
1.8KB

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