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.

59 lines
2.0 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 issue
  5. import (
  6. "testing"
  7. "code.gitea.io/gitea/models"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. func TestIssue_AddLabels(t *testing.T) {
  11. var tests = []struct {
  12. issueID int64
  13. labelIDs []int64
  14. doerID int64
  15. }{
  16. {1, []int64{1, 2}, 2}, // non-pull-request
  17. {1, []int64{}, 2}, // non-pull-request, empty
  18. {2, []int64{1, 2}, 2}, // pull-request
  19. {2, []int64{}, 1}, // pull-request, empty
  20. }
  21. for _, test := range tests {
  22. assert.NoError(t, models.PrepareTestDatabase())
  23. issue := models.AssertExistsAndLoadBean(t, &models.Issue{ID: test.issueID}).(*models.Issue)
  24. labels := make([]*models.Label, len(test.labelIDs))
  25. for i, labelID := range test.labelIDs {
  26. labels[i] = models.AssertExistsAndLoadBean(t, &models.Label{ID: labelID}).(*models.Label)
  27. }
  28. doer := models.AssertExistsAndLoadBean(t, &models.User{ID: test.doerID}).(*models.User)
  29. assert.NoError(t, AddLabels(issue, doer, labels))
  30. for _, labelID := range test.labelIDs {
  31. models.AssertExistsAndLoadBean(t, &models.IssueLabel{IssueID: test.issueID, LabelID: labelID})
  32. }
  33. }
  34. }
  35. func TestIssue_AddLabel(t *testing.T) {
  36. var tests = []struct {
  37. issueID int64
  38. labelID int64
  39. doerID int64
  40. }{
  41. {1, 2, 2}, // non-pull-request, not-already-added label
  42. {1, 1, 2}, // non-pull-request, already-added label
  43. {2, 2, 2}, // pull-request, not-already-added label
  44. {2, 1, 2}, // pull-request, already-added label
  45. }
  46. for _, test := range tests {
  47. assert.NoError(t, models.PrepareTestDatabase())
  48. issue := models.AssertExistsAndLoadBean(t, &models.Issue{ID: test.issueID}).(*models.Issue)
  49. label := models.AssertExistsAndLoadBean(t, &models.Label{ID: test.labelID}).(*models.Label)
  50. doer := models.AssertExistsAndLoadBean(t, &models.User{ID: test.doerID}).(*models.User)
  51. assert.NoError(t, AddLabel(issue, doer, label))
  52. models.AssertExistsAndLoadBean(t, &models.IssueLabel{IssueID: test.issueID, LabelID: test.labelID})
  53. }
  54. }