code.gitea.io/gitea@v1.21.7/models/issues/dependency_test.go (about)

     1  // Copyright 2018 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package issues_test
     5  
     6  import (
     7  	"testing"
     8  
     9  	"code.gitea.io/gitea/models/db"
    10  	issues_model "code.gitea.io/gitea/models/issues"
    11  	"code.gitea.io/gitea/models/unittest"
    12  	user_model "code.gitea.io/gitea/models/user"
    13  
    14  	"github.com/stretchr/testify/assert"
    15  )
    16  
    17  func TestCreateIssueDependency(t *testing.T) {
    18  	// Prepare
    19  	assert.NoError(t, unittest.PrepareTestDatabase())
    20  
    21  	user1, err := user_model.GetUserByID(db.DefaultContext, 1)
    22  	assert.NoError(t, err)
    23  
    24  	issue1, err := issues_model.GetIssueByID(db.DefaultContext, 1)
    25  	assert.NoError(t, err)
    26  
    27  	issue2, err := issues_model.GetIssueByID(db.DefaultContext, 2)
    28  	assert.NoError(t, err)
    29  
    30  	// Create a dependency and check if it was successful
    31  	err = issues_model.CreateIssueDependency(user1, issue1, issue2)
    32  	assert.NoError(t, err)
    33  
    34  	// Do it again to see if it will check if the dependency already exists
    35  	err = issues_model.CreateIssueDependency(user1, issue1, issue2)
    36  	assert.Error(t, err)
    37  	assert.True(t, issues_model.IsErrDependencyExists(err))
    38  
    39  	// Check for circular dependencies
    40  	err = issues_model.CreateIssueDependency(user1, issue2, issue1)
    41  	assert.Error(t, err)
    42  	assert.True(t, issues_model.IsErrCircularDependency(err))
    43  
    44  	_ = unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{Type: issues_model.CommentTypeAddDependency, PosterID: user1.ID, IssueID: issue1.ID})
    45  
    46  	// Check if dependencies left is correct
    47  	left, err := issues_model.IssueNoDependenciesLeft(db.DefaultContext, issue1)
    48  	assert.NoError(t, err)
    49  	assert.False(t, left)
    50  
    51  	// Close #2 and check again
    52  	_, err = issues_model.ChangeIssueStatus(db.DefaultContext, issue2, user1, true)
    53  	assert.NoError(t, err)
    54  
    55  	left, err = issues_model.IssueNoDependenciesLeft(db.DefaultContext, issue1)
    56  	assert.NoError(t, err)
    57  	assert.True(t, left)
    58  
    59  	// Test removing the dependency
    60  	err = issues_model.RemoveIssueDependency(user1, issue1, issue2, issues_model.DependencyTypeBlockedBy)
    61  	assert.NoError(t, err)
    62  }