2019-10-19 09:48:29 +00:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
2022-11-27 18:20:29 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2019-10-19 09:48:29 +00:00
|
|
|
|
|
|
|
package issue
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2023-09-16 14:39:12 +00:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-06-13 09:37:59 +00:00
|
|
|
issues_model "code.gitea.io/gitea/models/issues"
|
2021-11-12 14:36:47 +00:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2021-11-24 09:49:20 +00:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2021-11-17 12:34:35 +00:00
|
|
|
|
2024-07-30 19:41:10 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-10-19 09:48:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestIssue_AddLabels(t *testing.T) {
|
2022-01-20 17:46:10 +00:00
|
|
|
tests := []struct {
|
2019-10-19 09:48:29 +00:00
|
|
|
issueID int64
|
|
|
|
labelIDs []int64
|
|
|
|
doerID int64
|
|
|
|
}{
|
|
|
|
{1, []int64{1, 2}, 2}, // non-pull-request
|
|
|
|
{1, []int64{}, 2}, // non-pull-request, empty
|
|
|
|
{2, []int64{1, 2}, 2}, // pull-request
|
|
|
|
{2, []int64{}, 1}, // pull-request, empty
|
|
|
|
}
|
|
|
|
for _, test := range tests {
|
2024-07-30 19:41:10 +00:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-08-16 02:22:25 +00:00
|
|
|
issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: test.issueID})
|
2022-06-13 09:37:59 +00:00
|
|
|
labels := make([]*issues_model.Label, len(test.labelIDs))
|
2019-10-19 09:48:29 +00:00
|
|
|
for i, labelID := range test.labelIDs {
|
2022-08-16 02:22:25 +00:00
|
|
|
labels[i] = unittest.AssertExistsAndLoadBean(t, &issues_model.Label{ID: labelID})
|
2019-10-19 09:48:29 +00:00
|
|
|
}
|
2022-08-16 02:22:25 +00:00
|
|
|
doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: test.doerID})
|
2024-07-30 19:41:10 +00:00
|
|
|
require.NoError(t, AddLabels(db.DefaultContext, issue, doer, labels))
|
2019-10-19 09:48:29 +00:00
|
|
|
for _, labelID := range test.labelIDs {
|
2022-06-13 09:37:59 +00:00
|
|
|
unittest.AssertExistsAndLoadBean(t, &issues_model.IssueLabel{IssueID: test.issueID, LabelID: labelID})
|
2019-10-19 09:48:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIssue_AddLabel(t *testing.T) {
|
2022-01-20 17:46:10 +00:00
|
|
|
tests := []struct {
|
2019-10-19 09:48:29 +00:00
|
|
|
issueID int64
|
|
|
|
labelID int64
|
|
|
|
doerID int64
|
|
|
|
}{
|
|
|
|
{1, 2, 2}, // non-pull-request, not-already-added label
|
|
|
|
{1, 1, 2}, // non-pull-request, already-added label
|
|
|
|
{2, 2, 2}, // pull-request, not-already-added label
|
|
|
|
{2, 1, 2}, // pull-request, already-added label
|
|
|
|
}
|
|
|
|
for _, test := range tests {
|
2024-07-30 19:41:10 +00:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-08-16 02:22:25 +00:00
|
|
|
issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: test.issueID})
|
|
|
|
label := unittest.AssertExistsAndLoadBean(t, &issues_model.Label{ID: test.labelID})
|
|
|
|
doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: test.doerID})
|
2024-07-30 19:41:10 +00:00
|
|
|
require.NoError(t, AddLabel(db.DefaultContext, issue, doer, label))
|
2022-06-13 09:37:59 +00:00
|
|
|
unittest.AssertExistsAndLoadBean(t, &issues_model.IssueLabel{IssueID: test.issueID, LabelID: test.labelID})
|
2019-10-19 09:48:29 +00:00
|
|
|
}
|
|
|
|
}
|