summaryrefslogtreecommitdiffstats
path: root/models/issue_dependency_test.go
blob: 60fa58738983825572bea72bb5de1ba8cc9ac58a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright 2018 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package models

import (
	"testing"

	"code.gitea.io/gitea/models/db"
	"code.gitea.io/gitea/models/unittest"
	"github.com/stretchr/testify/assert"
)

func TestCreateIssueDependency(t *testing.T) {
	// Prepare
	assert.NoError(t, unittest.PrepareTestDatabase())

	user1, err := GetUserByID(1)
	assert.NoError(t, err)

	issue1, err := GetIssueByID(1)
	assert.NoError(t, err)

	issue2, err := GetIssueByID(2)
	assert.NoError(t, err)

	// Create a dependency and check if it was successful
	err = CreateIssueDependency(user1, issue1, issue2)
	assert.NoError(t, err)

	// Do it again to see if it will check if the dependency already exists
	err = CreateIssueDependency(user1, issue1, issue2)
	assert.Error(t, err)
	assert.True(t, IsErrDependencyExists(err))

	// Check for circular dependencies
	err = CreateIssueDependency(user1, issue2, issue1)
	assert.Error(t, err)
	assert.True(t, IsErrCircularDependency(err))

	_ = db.AssertExistsAndLoadBean(t, &Comment{Type: CommentTypeAddDependency, PosterID: user1.ID, IssueID: issue1.ID})

	// Check if dependencies left is correct
	left, err := IssueNoDependenciesLeft(issue1)
	assert.NoError(t, err)
	assert.False(t, left)

	// Close #2 and check again
	_, err = issue2.ChangeStatus(user1, true)
	assert.NoError(t, err)

	left, err = IssueNoDependenciesLeft(issue1)
	assert.NoError(t, err)
	assert.True(t, left)

	// Test removing the dependency
	err = RemoveIssueDependency(user1, issue1, issue2, DependencyTypeBlockedBy)
	assert.NoError(t, err)
}