2020-05-15 00:55:43 +02:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-27 19:20:29 +01:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-05-15 00:55:43 +02:00
|
|
|
|
|
|
|
package issue
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2022-06-13 11:37:59 +02:00
|
|
|
"code.gitea.io/gitea/models/db"
|
|
|
|
issues_model "code.gitea.io/gitea/models/issues"
|
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
|
|
|
"code.gitea.io/gitea/models/unittest"
|
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2020-05-15 00:55:43 +02:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2024-07-30 21:41:10 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-05-15 00:55:43 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestGetRefEndNamesAndURLs(t *testing.T) {
|
2022-06-13 11:37:59 +02:00
|
|
|
issues := []*issues_model.Issue{
|
2020-05-15 00:55:43 +02:00
|
|
|
{ID: 1, Ref: "refs/heads/branch1"},
|
|
|
|
{ID: 2, Ref: "refs/tags/tag1"},
|
|
|
|
{ID: 3, Ref: "c0ffee"},
|
|
|
|
}
|
|
|
|
repoLink := "/foo/bar"
|
|
|
|
|
|
|
|
endNames, urls := GetRefEndNamesAndURLs(issues, repoLink)
|
|
|
|
assert.EqualValues(t, map[int64]string{1: "branch1", 2: "tag1", 3: "c0ffee"}, endNames)
|
|
|
|
assert.EqualValues(t, map[int64]string{
|
|
|
|
1: repoLink + "/src/branch/branch1",
|
|
|
|
2: repoLink + "/src/tag/tag1",
|
|
|
|
3: repoLink + "/src/commit/c0ffee",
|
|
|
|
}, urls)
|
|
|
|
}
|
2022-06-13 11:37:59 +02:00
|
|
|
|
|
|
|
func TestIssue_DeleteIssue(t *testing.T) {
|
2024-07-30 21:41:10 +02:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-06-13 11:37:59 +02:00
|
|
|
|
|
|
|
issueIDs, err := issues_model.GetIssueIDsByRepoID(db.DefaultContext, 1)
|
2024-07-30 21:41:10 +02:00
|
|
|
require.NoError(t, err)
|
2023-04-22 23:56:27 +02:00
|
|
|
assert.Len(t, issueIDs, 5)
|
2022-06-13 11:37:59 +02:00
|
|
|
|
|
|
|
issue := &issues_model.Issue{
|
|
|
|
RepoID: 1,
|
|
|
|
ID: issueIDs[2],
|
|
|
|
}
|
|
|
|
|
2023-04-14 20:18:28 +02:00
|
|
|
err = deleteIssue(db.DefaultContext, issue)
|
2024-07-30 21:41:10 +02:00
|
|
|
require.NoError(t, err)
|
2022-06-13 11:37:59 +02:00
|
|
|
issueIDs, err = issues_model.GetIssueIDsByRepoID(db.DefaultContext, 1)
|
2024-07-30 21:41:10 +02:00
|
|
|
require.NoError(t, err)
|
2023-04-22 23:56:27 +02:00
|
|
|
assert.Len(t, issueIDs, 4)
|
2022-06-13 11:37:59 +02:00
|
|
|
|
|
|
|
// check attachment removal
|
|
|
|
attachments, err := repo_model.GetAttachmentsByIssueID(db.DefaultContext, 4)
|
2024-07-30 21:41:10 +02:00
|
|
|
require.NoError(t, err)
|
2022-06-13 11:37:59 +02:00
|
|
|
issue, err = issues_model.GetIssueByID(db.DefaultContext, 4)
|
2024-07-30 21:41:10 +02:00
|
|
|
require.NoError(t, err)
|
2023-04-14 20:18:28 +02:00
|
|
|
err = deleteIssue(db.DefaultContext, issue)
|
2024-07-30 21:41:10 +02:00
|
|
|
require.NoError(t, err)
|
2023-04-22 23:56:27 +02:00
|
|
|
assert.Len(t, attachments, 2)
|
2022-06-13 11:37:59 +02:00
|
|
|
for i := range attachments {
|
|
|
|
attachment, err := repo_model.GetAttachmentByUUID(db.DefaultContext, attachments[i].UUID)
|
2024-07-30 21:41:10 +02:00
|
|
|
require.Error(t, err)
|
2022-06-13 11:37:59 +02:00
|
|
|
assert.True(t, repo_model.IsErrAttachmentNotExist(err))
|
|
|
|
assert.Nil(t, attachment)
|
|
|
|
}
|
|
|
|
|
|
|
|
// check issue dependencies
|
2022-12-03 03:48:26 +01:00
|
|
|
user, err := user_model.GetUserByID(db.DefaultContext, 1)
|
2024-07-30 21:41:10 +02:00
|
|
|
require.NoError(t, err)
|
2022-06-13 11:37:59 +02:00
|
|
|
issue1, err := issues_model.GetIssueByID(db.DefaultContext, 1)
|
2024-07-30 21:41:10 +02:00
|
|
|
require.NoError(t, err)
|
2022-06-13 11:37:59 +02:00
|
|
|
issue2, err := issues_model.GetIssueByID(db.DefaultContext, 2)
|
2024-07-30 21:41:10 +02:00
|
|
|
require.NoError(t, err)
|
2023-10-11 06:24:07 +02:00
|
|
|
err = issues_model.CreateIssueDependency(db.DefaultContext, user, issue1, issue2)
|
2024-07-30 21:41:10 +02:00
|
|
|
require.NoError(t, err)
|
2022-06-13 11:37:59 +02:00
|
|
|
left, err := issues_model.IssueNoDependenciesLeft(db.DefaultContext, issue1)
|
2024-07-30 21:41:10 +02:00
|
|
|
require.NoError(t, err)
|
2022-06-13 11:37:59 +02:00
|
|
|
assert.False(t, left)
|
|
|
|
|
2023-04-14 20:18:28 +02:00
|
|
|
err = deleteIssue(db.DefaultContext, issue2)
|
2024-07-30 21:41:10 +02:00
|
|
|
require.NoError(t, err)
|
2022-06-13 11:37:59 +02:00
|
|
|
left, err = issues_model.IssueNoDependenciesLeft(db.DefaultContext, issue1)
|
2024-07-30 21:41:10 +02:00
|
|
|
require.NoError(t, err)
|
2022-06-13 11:37:59 +02:00
|
|
|
assert.True(t, left)
|
|
|
|
}
|