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.

86 lines
2.6 KiB

  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. "sort"
  7. "testing"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. func TestIssue_ReplaceLabels(t *testing.T) {
  11. assert.NoError(t, PrepareTestDatabase())
  12. testSuccess := func(issueID int64, labelIDs []int64) {
  13. issue := AssertExistsAndLoadBean(t, &Issue{ID: issueID}).(*Issue)
  14. repo := AssertExistsAndLoadBean(t, &Repository{ID: issue.RepoID}).(*Repository)
  15. doer := AssertExistsAndLoadBean(t, &User{ID: repo.OwnerID}).(*User)
  16. labels := make([]*Label, len(labelIDs))
  17. for i, labelID := range labelIDs {
  18. labels[i] = AssertExistsAndLoadBean(t, &Label{ID: labelID, RepoID: repo.ID}).(*Label)
  19. }
  20. assert.NoError(t, issue.ReplaceLabels(labels, doer))
  21. AssertCount(t, &IssueLabel{IssueID: issueID}, len(labelIDs))
  22. for _, labelID := range labelIDs {
  23. AssertExistsAndLoadBean(t, &IssueLabel{IssueID: issueID, LabelID: labelID})
  24. }
  25. }
  26. testSuccess(1, []int64{2})
  27. testSuccess(1, []int64{1, 2})
  28. testSuccess(1, []int64{})
  29. }
  30. func TestIssueAPIURL(t *testing.T) {
  31. assert.NoError(t, PrepareTestDatabase())
  32. issue := AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue)
  33. err := issue.LoadAttributes()
  34. assert.NoError(t, err)
  35. assert.Equal(t, "https://try.gitea.io/api/v1/repos/user2/repo1/issues/1", issue.APIURL())
  36. }
  37. func TestGetIssuesByIDs(t *testing.T) {
  38. assert.NoError(t, PrepareTestDatabase())
  39. testSuccess := func(expectedIssueIDs []int64, nonExistentIssueIDs []int64) {
  40. issues, err := GetIssuesByIDs(append(expectedIssueIDs, nonExistentIssueIDs...))
  41. assert.NoError(t, err)
  42. actualIssueIDs := make([]int64, len(issues))
  43. for i, issue := range issues {
  44. actualIssueIDs[i] = issue.ID
  45. }
  46. assert.Equal(t, expectedIssueIDs, actualIssueIDs)
  47. }
  48. testSuccess([]int64{1, 2, 3}, []int64{})
  49. testSuccess([]int64{1, 2, 3}, []int64{NonexistentID})
  50. }
  51. func TestGetParticipantsByIssueID(t *testing.T) {
  52. assert.NoError(t, PrepareTestDatabase())
  53. checkPartecipants := func(issueID int64, userIDs []int) {
  54. partecipants, err := GetParticipantsByIssueID(issueID)
  55. if assert.NoError(t, err) {
  56. partecipantsIDs := make([]int, len(partecipants))
  57. for i, u := range partecipants {
  58. partecipantsIDs[i] = int(u.ID)
  59. }
  60. sort.Ints(partecipantsIDs)
  61. sort.Ints(userIDs)
  62. assert.Equal(t, userIDs, partecipantsIDs)
  63. }
  64. }
  65. // User 1 is issue1 poster (see fixtures/issue.yml)
  66. // User 2 only labeled issue1 (see fixtures/comment.yml)
  67. // Users 3 and 5 made actual comments (see fixtures/comment.yml)
  68. checkPartecipants(1, []int{3, 5})
  69. }