2018-07-17 21:23:58 +00:00
|
|
|
// Copyright 2018 The Gitea Authors. All rights reserved.
|
2022-11-27 18:20:29 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2018-07-17 21:23:58 +00:00
|
|
|
|
2022-06-13 09:37:59 +00:00
|
|
|
package issues_test
|
2018-07-17 21:23:58 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2022-05-03 19:46:28 +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
|
|
|
|
2018-07-17 21:23:58 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2024-07-30 19:41:27 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-07-17 21:23:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCreateIssueDependency(t *testing.T) {
|
|
|
|
// Prepare
|
2024-07-30 19:41:27 +00:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2018-07-17 21:23:58 +00:00
|
|
|
|
2022-12-03 02:48:26 +00:00
|
|
|
user1, err := user_model.GetUserByID(db.DefaultContext, 1)
|
2024-07-30 19:41:27 +00:00
|
|
|
require.NoError(t, err)
|
2018-07-17 21:23:58 +00:00
|
|
|
|
2022-06-13 09:37:59 +00:00
|
|
|
issue1, err := issues_model.GetIssueByID(db.DefaultContext, 1)
|
2024-07-30 19:41:27 +00:00
|
|
|
require.NoError(t, err)
|
2018-12-13 15:55:43 +00:00
|
|
|
|
2022-06-13 09:37:59 +00:00
|
|
|
issue2, err := issues_model.GetIssueByID(db.DefaultContext, 2)
|
2024-07-30 19:41:27 +00:00
|
|
|
require.NoError(t, err)
|
2018-07-17 21:23:58 +00:00
|
|
|
|
|
|
|
// Create a dependency and check if it was successful
|
2023-10-11 04:24:07 +00:00
|
|
|
err = issues_model.CreateIssueDependency(db.DefaultContext, user1, issue1, issue2)
|
2024-07-30 19:41:27 +00:00
|
|
|
require.NoError(t, err)
|
2018-07-17 21:23:58 +00:00
|
|
|
|
|
|
|
// Do it again to see if it will check if the dependency already exists
|
2023-10-11 04:24:07 +00:00
|
|
|
err = issues_model.CreateIssueDependency(db.DefaultContext, user1, issue1, issue2)
|
2024-07-30 19:41:27 +00:00
|
|
|
require.Error(t, err)
|
2022-06-13 09:37:59 +00:00
|
|
|
assert.True(t, issues_model.IsErrDependencyExists(err))
|
2018-07-17 21:23:58 +00:00
|
|
|
|
|
|
|
// Check for circular dependencies
|
2023-10-11 04:24:07 +00:00
|
|
|
err = issues_model.CreateIssueDependency(db.DefaultContext, user1, issue2, issue1)
|
2024-07-30 19:41:27 +00:00
|
|
|
require.Error(t, err)
|
2022-06-13 09:37:59 +00:00
|
|
|
assert.True(t, issues_model.IsErrCircularDependency(err))
|
2018-07-17 21:23:58 +00:00
|
|
|
|
2022-06-13 09:37:59 +00:00
|
|
|
_ = unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{Type: issues_model.CommentTypeAddDependency, PosterID: user1.ID, IssueID: issue1.ID})
|
2018-07-17 21:23:58 +00:00
|
|
|
|
|
|
|
// Check if dependencies left is correct
|
2022-06-13 09:37:59 +00:00
|
|
|
left, err := issues_model.IssueNoDependenciesLeft(db.DefaultContext, issue1)
|
2024-07-30 19:41:27 +00:00
|
|
|
require.NoError(t, err)
|
2018-07-17 21:23:58 +00:00
|
|
|
assert.False(t, left)
|
|
|
|
|
|
|
|
// Close #2 and check again
|
2022-06-13 09:37:59 +00:00
|
|
|
_, err = issues_model.ChangeIssueStatus(db.DefaultContext, issue2, user1, true)
|
2024-07-30 19:41:27 +00:00
|
|
|
require.NoError(t, err)
|
2018-07-17 21:23:58 +00:00
|
|
|
|
2022-06-13 09:37:59 +00:00
|
|
|
left, err = issues_model.IssueNoDependenciesLeft(db.DefaultContext, issue1)
|
2024-07-30 19:41:27 +00:00
|
|
|
require.NoError(t, err)
|
2018-07-17 21:23:58 +00:00
|
|
|
assert.True(t, left)
|
|
|
|
|
|
|
|
// Test removing the dependency
|
2023-10-11 04:24:07 +00:00
|
|
|
err = issues_model.RemoveIssueDependency(db.DefaultContext, user1, issue1, issue2, issues_model.DependencyTypeBlockedBy)
|
2024-07-30 19:41:27 +00:00
|
|
|
require.NoError(t, err)
|
2018-07-17 21:23:58 +00:00
|
|
|
}
|