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.2KB

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