You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
563 lines
18 KiB
563 lines
18 KiB
8 years ago
|
// Copyright 2017 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.
|
||
|
|
||
2 years ago
|
package issues_test
|
||
8 years ago
|
|
||
|
import (
|
||
3 years ago
|
"context"
|
||
3 years ago
|
"fmt"
|
||
8 years ago
|
"sort"
|
||
3 years ago
|
"strconv"
|
||
3 years ago
|
"sync"
|
||
8 years ago
|
"testing"
|
||
7 years ago
|
"time"
|
||
8 years ago
|
|
||
3 years ago
|
"code.gitea.io/gitea/models/db"
|
||
3 years ago
|
"code.gitea.io/gitea/models/foreignreference"
|
||
3 years ago
|
issues_model "code.gitea.io/gitea/models/issues"
|
||
3 years ago
|
"code.gitea.io/gitea/models/organization"
|
||
3 years ago
|
repo_model "code.gitea.io/gitea/models/repo"
|
||
3 years ago
|
"code.gitea.io/gitea/models/unittest"
|
||
3 years ago
|
user_model "code.gitea.io/gitea/models/user"
|
||
3 years ago
|
|
||
8 years ago
|
"github.com/stretchr/testify/assert"
|
||
3 years ago
|
"xorm.io/builder"
|
||
8 years ago
|
)
|
||
|
|
||
|
func TestIssue_ReplaceLabels(t *testing.T) {
|
||
3 years ago
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||
8 years ago
|
|
||
|
testSuccess := func(issueID int64, labelIDs []int64) {
|
||
2 years ago
|
issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: issueID}).(*issues_model.Issue)
|
||
3 years ago
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: issue.RepoID}).(*repo_model.Repository)
|
||
3 years ago
|
doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID}).(*user_model.User)
|
||
8 years ago
|
|
||
2 years ago
|
labels := make([]*issues_model.Label, len(labelIDs))
|
||
8 years ago
|
for i, labelID := range labelIDs {
|
||
2 years ago
|
labels[i] = unittest.AssertExistsAndLoadBean(t, &issues_model.Label{ID: labelID, RepoID: repo.ID}).(*issues_model.Label)
|
||
8 years ago
|
}
|
||
2 years ago
|
assert.NoError(t, issues_model.ReplaceIssueLabels(issue, labels, doer))
|
||
|
unittest.AssertCount(t, &issues_model.IssueLabel{IssueID: issueID}, len(labelIDs))
|
||
8 years ago
|
for _, labelID := range labelIDs {
|
||
2 years ago
|
unittest.AssertExistsAndLoadBean(t, &issues_model.IssueLabel{IssueID: issueID, LabelID: labelID})
|
||
8 years ago
|
}
|
||
|
}
|
||
|
|
||
|
testSuccess(1, []int64{2})
|
||
|
testSuccess(1, []int64{1, 2})
|
||
|
testSuccess(1, []int64{})
|
||
|
}
|
||
8 years ago
|
|
||
3 years ago
|
func Test_GetIssueIDsByRepoID(t *testing.T) {
|
||
3 years ago
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||
3 years ago
|
|
||
2 years ago
|
ids, err := issues_model.GetIssueIDsByRepoID(db.DefaultContext, 1)
|
||
3 years ago
|
assert.NoError(t, err)
|
||
|
assert.Len(t, ids, 5)
|
||
|
}
|
||
|
|
||
8 years ago
|
func TestIssueAPIURL(t *testing.T) {
|
||
3 years ago
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||
2 years ago
|
issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 1}).(*issues_model.Issue)
|
||
|
err := issue.LoadAttributes(db.DefaultContext)
|
||
8 years ago
|
|
||
|
assert.NoError(t, err)
|
||
|
assert.Equal(t, "https://try.gitea.io/api/v1/repos/user2/repo1/issues/1", issue.APIURL())
|
||
|
}
|
||
8 years ago
|
|
||
|
func TestGetIssuesByIDs(t *testing.T) {
|
||
3 years ago
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||
4 years ago
|
testSuccess := func(expectedIssueIDs, nonExistentIssueIDs []int64) {
|
||
2 years ago
|
issues, err := issues_model.GetIssuesByIDs(db.DefaultContext, append(expectedIssueIDs, nonExistentIssueIDs...))
|
||
8 years ago
|
assert.NoError(t, err)
|
||
|
actualIssueIDs := make([]int64, len(issues))
|
||
|
for i, issue := range issues {
|
||
|
actualIssueIDs[i] = issue.ID
|
||
|
}
|
||
|
assert.Equal(t, expectedIssueIDs, actualIssueIDs)
|
||
|
}
|
||
|
testSuccess([]int64{1, 2, 3}, []int64{})
|
||
3 years ago
|
testSuccess([]int64{1, 2, 3}, []int64{unittest.NonexistentID})
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
5 years ago
|
func TestGetParticipantIDsByIssue(t *testing.T) {
|
||
3 years ago
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||
8 years ago
|
|
||
8 years ago
|
checkParticipants := func(issueID int64, userIDs []int) {
|
||
2 years ago
|
issue, err := issues_model.GetIssueByID(db.DefaultContext, issueID)
|
||
5 years ago
|
assert.NoError(t, err)
|
||
2 years ago
|
participants, err := issue.GetParticipantIDsByIssue(db.DefaultContext)
|
||
8 years ago
|
if assert.NoError(t, err) {
|
||
8 years ago
|
participantsIDs := make([]int, len(participants))
|
||
5 years ago
|
for i, uid := range participants {
|
||
|
participantsIDs[i] = int(uid)
|
||
8 years ago
|
}
|
||
8 years ago
|
sort.Ints(participantsIDs)
|
||
8 years ago
|
sort.Ints(userIDs)
|
||
8 years ago
|
assert.Equal(t, userIDs, participantsIDs)
|
||
8 years ago
|
}
|
||
|
}
|
||
|
|
||
|
// User 1 is issue1 poster (see fixtures/issue.yml)
|
||
|
// User 2 only labeled issue1 (see fixtures/comment.yml)
|
||
|
// Users 3 and 5 made actual comments (see fixtures/comment.yml)
|
||
7 years ago
|
// User 3 is inactive, thus not active participant
|
||
5 years ago
|
checkParticipants(1, []int{1, 5})
|
||
8 years ago
|
}
|
||
7 years ago
|
|
||
|
func TestIssue_ClearLabels(t *testing.T) {
|
||
4 years ago
|
tests := []struct {
|
||
7 years ago
|
issueID int64
|
||
|
doerID int64
|
||
|
}{
|
||
|
{1, 2}, // non-pull-request, has labels
|
||
|
{2, 2}, // pull-request, has labels
|
||
|
{3, 2}, // pull-request, has no labels
|
||
|
}
|
||
|
for _, test := range tests {
|
||
3 years ago
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||
2 years ago
|
issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: test.issueID}).(*issues_model.Issue)
|
||
3 years ago
|
doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: test.doerID}).(*user_model.User)
|
||
2 years ago
|
assert.NoError(t, issues_model.ClearIssueLabels(issue, doer))
|
||
|
unittest.AssertNotExistsBean(t, &issues_model.IssueLabel{IssueID: test.issueID})
|
||
7 years ago
|
}
|
||
|
}
|
||
7 years ago
|
|
||
|
func TestUpdateIssueCols(t *testing.T) {
|
||
3 years ago
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||
2 years ago
|
issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{}).(*issues_model.Issue)
|
||
7 years ago
|
|
||
|
const newTitle = "New Title for unit test"
|
||
|
issue.Title = newTitle
|
||
|
|
||
|
prevContent := issue.Content
|
||
|
issue.Content = "This should have no effect"
|
||
|
|
||
|
now := time.Now().Unix()
|
||
2 years ago
|
assert.NoError(t, issues_model.UpdateIssueCols(db.DefaultContext, issue, "name"))
|
||
7 years ago
|
then := time.Now().Unix()
|
||
|
|
||
2 years ago
|
updatedIssue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: issue.ID}).(*issues_model.Issue)
|
||
7 years ago
|
assert.EqualValues(t, newTitle, updatedIssue.Title)
|
||
|
assert.EqualValues(t, prevContent, updatedIssue.Content)
|
||
3 years ago
|
unittest.AssertInt64InRange(t, now, then, int64(updatedIssue.UpdatedUnix))
|
||
7 years ago
|
}
|
||
7 years ago
|
|
||
|
func TestIssues(t *testing.T) {
|
||
3 years ago
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||
7 years ago
|
for _, test := range []struct {
|
||
2 years ago
|
Opts issues_model.IssuesOptions
|
||
7 years ago
|
ExpectedIssueIDs []int64
|
||
|
}{
|
||
|
{
|
||
2 years ago
|
issues_model.IssuesOptions{
|
||
7 years ago
|
AssigneeID: 1,
|
||
|
SortType: "oldest",
|
||
|
},
|
||
|
[]int64{1, 6},
|
||
|
},
|
||
|
{
|
||
2 years ago
|
issues_model.IssuesOptions{
|
||
3 years ago
|
RepoCond: builder.In("repo_id", 1, 3),
|
||
7 years ago
|
SortType: "oldest",
|
||
3 years ago
|
ListOptions: db.ListOptions{
|
||
5 years ago
|
Page: 1,
|
||
|
PageSize: 4,
|
||
|
},
|
||
7 years ago
|
},
|
||
|
[]int64{1, 2, 3, 5},
|
||
|
},
|
||
|
{
|
||
2 years ago
|
issues_model.IssuesOptions{
|
||
6 years ago
|
LabelIDs: []int64{1},
|
||
3 years ago
|
ListOptions: db.ListOptions{
|
||
5 years ago
|
Page: 1,
|
||
|
PageSize: 4,
|
||
|
},
|
||
7 years ago
|
},
|
||
6 years ago
|
[]int64{2, 1},
|
||
|
},
|
||
|
{
|
||
2 years ago
|
issues_model.IssuesOptions{
|
||
6 years ago
|
LabelIDs: []int64{1, 2},
|
||
3 years ago
|
ListOptions: db.ListOptions{
|
||
5 years ago
|
Page: 1,
|
||
|
PageSize: 4,
|
||
|
},
|
||
6 years ago
|
},
|
||
|
[]int64{}, // issues with **both** label 1 and 2, none of these issues matches, TODO: add more tests
|
||
7 years ago
|
},
|
||
|
} {
|
||
2 years ago
|
issues, err := issues_model.Issues(&test.Opts)
|
||
7 years ago
|
assert.NoError(t, err)
|
||
|
if assert.Len(t, issues, len(test.ExpectedIssueIDs)) {
|
||
|
for i, issue := range issues {
|
||
|
assert.EqualValues(t, test.ExpectedIssueIDs[i], issue.ID)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestGetUserIssueStats(t *testing.T) {
|
||
3 years ago
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||
7 years ago
|
for _, test := range []struct {
|
||
2 years ago
|
Opts issues_model.UserIssueStatsOptions
|
||
|
ExpectedIssueStats issues_model.IssueStats
|
||
7 years ago
|
}{
|
||
|
{
|
||
2 years ago
|
issues_model.UserIssueStatsOptions{
|
||
7 years ago
|
UserID: 1,
|
||
5 years ago
|
RepoIDs: []int64{1},
|
||
2 years ago
|
FilterMode: issues_model.FilterModeAll,
|
||
7 years ago
|
},
|
||
2 years ago
|
issues_model.IssueStats{
|
||
3 years ago
|
YourRepositoriesCount: 1, // 6
|
||
|
AssignCount: 1, // 6
|
||
|
CreateCount: 1, // 6
|
||
|
OpenCount: 1, // 6
|
||
|
ClosedCount: 1, // 1
|
||
7 years ago
|
},
|
||
|
},
|
||
|
{
|
||
2 years ago
|
issues_model.UserIssueStatsOptions{
|
||
7 years ago
|
UserID: 1,
|
||
3 years ago
|
RepoIDs: []int64{1},
|
||
2 years ago
|
FilterMode: issues_model.FilterModeAll,
|
||
3 years ago
|
IsClosed: true,
|
||
7 years ago
|
},
|
||
2 years ago
|
issues_model.IssueStats{
|
||
3 years ago
|
YourRepositoriesCount: 1, // 6
|
||
|
AssignCount: 0,
|
||
|
CreateCount: 0,
|
||
|
OpenCount: 1, // 6
|
||
|
ClosedCount: 1, // 1
|
||
7 years ago
|
},
|
||
|
},
|
||
|
{
|
||
2 years ago
|
issues_model.UserIssueStatsOptions{
|
||
7 years ago
|
UserID: 1,
|
||
2 years ago
|
FilterMode: issues_model.FilterModeAssign,
|
||
7 years ago
|
},
|
||
2 years ago
|
issues_model.IssueStats{
|
||
3 years ago
|
YourRepositoriesCount: 1, // 6
|
||
|
AssignCount: 1, // 6
|
||
|
CreateCount: 1, // 6
|
||
|
OpenCount: 1, // 6
|
||
7 years ago
|
ClosedCount: 0,
|
||
|
},
|
||
|
},
|
||
|
{
|
||
2 years ago
|
issues_model.UserIssueStatsOptions{
|
||
3 years ago
|
UserID: 1,
|
||
2 years ago
|
FilterMode: issues_model.FilterModeCreate,
|
||
7 years ago
|
},
|
||
2 years ago
|
issues_model.IssueStats{
|
||
3 years ago
|
YourRepositoriesCount: 1, // 6
|
||
|
AssignCount: 1, // 6
|
||
|
CreateCount: 1, // 6
|
||
|
OpenCount: 1, // 6
|
||
|
ClosedCount: 0,
|
||
7 years ago
|
},
|
||
|
},
|
||
5 years ago
|
{
|
||
2 years ago
|
issues_model.UserIssueStatsOptions{
|
||
5 years ago
|
UserID: 1,
|
||
2 years ago
|
FilterMode: issues_model.FilterModeMention,
|
||
5 years ago
|
},
|
||
2 years ago
|
issues_model.IssueStats{
|
||
3 years ago
|
YourRepositoriesCount: 1, // 6
|
||
|
AssignCount: 1, // 6
|
||
|
CreateCount: 1, // 6
|
||
|
MentionCount: 0,
|
||
5 years ago
|
OpenCount: 0,
|
||
|
ClosedCount: 0,
|
||
|
},
|
||
|
},
|
||
5 years ago
|
{
|
||
2 years ago
|
issues_model.UserIssueStatsOptions{
|
||
5 years ago
|
UserID: 1,
|
||
2 years ago
|
FilterMode: issues_model.FilterModeCreate,
|
||
5 years ago
|
IssueIDs: []int64{1},
|
||
|
},
|
||
2 years ago
|
issues_model.IssueStats{
|
||
3 years ago
|
YourRepositoriesCount: 1, // 1
|
||
|
AssignCount: 1, // 1
|
||
|
CreateCount: 1, // 1
|
||
|
OpenCount: 1, // 1
|
||
5 years ago
|
ClosedCount: 0,
|
||
|
},
|
||
|
},
|
||
3 years ago
|
{
|
||
2 years ago
|
issues_model.UserIssueStatsOptions{
|
||
3 years ago
|
UserID: 2,
|
||
|
Org: unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: 3}).(*organization.Organization),
|
||
|
Team: unittest.AssertExistsAndLoadBean(t, &organization.Team{ID: 7}).(*organization.Team),
|
||
2 years ago
|
FilterMode: issues_model.FilterModeAll,
|
||
3 years ago
|
},
|
||
2 years ago
|
issues_model.IssueStats{
|
||
3 years ago
|
YourRepositoriesCount: 2,
|
||
|
AssignCount: 1,
|
||
|
CreateCount: 1,
|
||
|
OpenCount: 2,
|
||
|
},
|
||
|
},
|
||
7 years ago
|
} {
|
||
3 years ago
|
t.Run(fmt.Sprintf("%#v", test.Opts), func(t *testing.T) {
|
||
2 years ago
|
stats, err := issues_model.GetUserIssueStats(test.Opts)
|
||
3 years ago
|
if !assert.NoError(t, err) {
|
||
|
return
|
||
|
}
|
||
|
assert.Equal(t, test.ExpectedIssueStats, *stats)
|
||
|
})
|
||
7 years ago
|
}
|
||
|
}
|
||
7 years ago
|
|
||
|
func TestIssue_loadTotalTimes(t *testing.T) {
|
||
3 years ago
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||
2 years ago
|
ms, err := issues_model.GetIssueByID(db.DefaultContext, 2)
|
||
7 years ago
|
assert.NoError(t, err)
|
||
2 years ago
|
assert.NoError(t, ms.LoadTotalTimes(db.DefaultContext))
|
||
5 years ago
|
assert.Equal(t, int64(3682), ms.TotalTrackedTime)
|
||
7 years ago
|
}
|
||
6 years ago
|
|
||
|
func TestIssue_SearchIssueIDsByKeyword(t *testing.T) {
|
||
3 years ago
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||
2 years ago
|
total, ids, err := issues_model.SearchIssueIDsByKeyword(context.TODO(), "issue2", []int64{1}, 10, 0)
|
||
6 years ago
|
assert.NoError(t, err)
|
||
|
assert.EqualValues(t, 1, total)
|
||
|
assert.EqualValues(t, []int64{2}, ids)
|
||
|
|
||
2 years ago
|
total, ids, err = issues_model.SearchIssueIDsByKeyword(context.TODO(), "first", []int64{1}, 10, 0)
|
||
6 years ago
|
assert.NoError(t, err)
|
||
|
assert.EqualValues(t, 1, total)
|
||
|
assert.EqualValues(t, []int64{1}, ids)
|
||
|
|
||
2 years ago
|
total, ids, err = issues_model.SearchIssueIDsByKeyword(context.TODO(), "for", []int64{1}, 10, 0)
|
||
6 years ago
|
assert.NoError(t, err)
|
||
5 years ago
|
assert.EqualValues(t, 5, total)
|
||
4 years ago
|
assert.ElementsMatch(t, []int64{1, 2, 3, 5, 11}, ids)
|
||
6 years ago
|
|
||
|
// issue1's comment id 2
|
||
2 years ago
|
total, ids, err = issues_model.SearchIssueIDsByKeyword(context.TODO(), "good", []int64{1}, 10, 0)
|
||
6 years ago
|
assert.NoError(t, err)
|
||
|
assert.EqualValues(t, 1, total)
|
||
|
assert.EqualValues(t, []int64{1}, ids)
|
||
|
}
|
||
5 years ago
|
|
||
5 years ago
|
func TestGetRepoIDsForIssuesOptions(t *testing.T) {
|
||
3 years ago
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||
3 years ago
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}).(*user_model.User)
|
||
5 years ago
|
for _, test := range []struct {
|
||
2 years ago
|
Opts issues_model.IssuesOptions
|
||
5 years ago
|
ExpectedRepoIDs []int64
|
||
|
}{
|
||
|
{
|
||
2 years ago
|
issues_model.IssuesOptions{
|
||
5 years ago
|
AssigneeID: 2,
|
||
|
},
|
||
3 years ago
|
[]int64{3, 32},
|
||
5 years ago
|
},
|
||
|
{
|
||
2 years ago
|
issues_model.IssuesOptions{
|
||
3 years ago
|
RepoCond: builder.In("repo_id", 1, 2),
|
||
5 years ago
|
},
|
||
|
[]int64{1, 2},
|
||
|
},
|
||
|
} {
|
||
2 years ago
|
repoIDs, err := issues_model.GetRepoIDsForIssuesOptions(&test.Opts, user)
|
||
5 years ago
|
assert.NoError(t, err)
|
||
|
if assert.Len(t, repoIDs, len(test.ExpectedRepoIDs)) {
|
||
|
for i, repoID := range repoIDs {
|
||
|
assert.EqualValues(t, test.ExpectedRepoIDs[i], repoID)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
2 years ago
|
func testInsertIssue(t *testing.T, title, content string, expectIndex int64) *issues_model.Issue {
|
||
|
var newIssue issues_model.Issue
|
||
3 years ago
|
t.Run(title, func(t *testing.T) {
|
||
3 years ago
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}).(*repo_model.Repository)
|
||
3 years ago
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}).(*user_model.User)
|
||
3 years ago
|
|
||
2 years ago
|
issue := issues_model.Issue{
|
||
3 years ago
|
RepoID: repo.ID,
|
||
|
PosterID: user.ID,
|
||
3 years ago
|
Poster: user,
|
||
3 years ago
|
Title: title,
|
||
|
Content: content,
|
||
|
}
|
||
2 years ago
|
err := issues_model.NewIssue(repo, &issue, nil, nil)
|
||
3 years ago
|
assert.NoError(t, err)
|
||
5 years ago
|
|
||
3 years ago
|
has, err := db.GetEngine(db.DefaultContext).ID(issue.ID).Get(&newIssue)
|
||
3 years ago
|
assert.NoError(t, err)
|
||
|
assert.True(t, has)
|
||
|
assert.EqualValues(t, issue.Title, newIssue.Title)
|
||
|
assert.EqualValues(t, issue.Content, newIssue.Content)
|
||
|
if expectIndex > 0 {
|
||
|
assert.EqualValues(t, expectIndex, newIssue.Index)
|
||
|
}
|
||
|
})
|
||
|
return &newIssue
|
||
5 years ago
|
}
|
||
|
|
||
|
func TestIssue_InsertIssue(t *testing.T) {
|
||
3 years ago
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||
5 years ago
|
|
||
3 years ago
|
// there are 5 issues and max index is 5 on repository 1, so this one should 6
|
||
|
issue := testInsertIssue(t, "my issue1", "special issue's comments?", 6)
|
||
2 years ago
|
_, err := db.GetEngine(db.DefaultContext).ID(issue.ID).Delete(new(issues_model.Issue))
|
||
3 years ago
|
assert.NoError(t, err)
|
||
|
|
||
|
issue = testInsertIssue(t, `my issue2, this is my son's love \n \r \ `, "special issue's '' comments?", 7)
|
||
2 years ago
|
_, err = db.GetEngine(db.DefaultContext).ID(issue.ID).Delete(new(issues_model.Issue))
|
||
3 years ago
|
assert.NoError(t, err)
|
||
5 years ago
|
}
|
||
5 years ago
|
|
||
|
func TestIssue_ResolveMentions(t *testing.T) {
|
||
3 years ago
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||
5 years ago
|
|
||
|
testSuccess := func(owner, repo, doer string, mentions []string, expected []int64) {
|
||
3 years ago
|
o := unittest.AssertExistsAndLoadBean(t, &user_model.User{LowerName: owner}).(*user_model.User)
|
||
3 years ago
|
r := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerID: o.ID, LowerName: repo}).(*repo_model.Repository)
|
||
2 years ago
|
issue := &issues_model.Issue{RepoID: r.ID}
|
||
3 years ago
|
d := unittest.AssertExistsAndLoadBean(t, &user_model.User{LowerName: doer}).(*user_model.User)
|
||
2 years ago
|
resolved, err := issues_model.ResolveIssueMentionsByVisibility(db.DefaultContext, issue, d, mentions)
|
||
5 years ago
|
assert.NoError(t, err)
|
||
|
ids := make([]int64, len(resolved))
|
||
|
for i, user := range resolved {
|
||
|
ids[i] = user.ID
|
||
|
}
|
||
|
sort.Slice(ids, func(i, j int) bool { return ids[i] < ids[j] })
|
||
|
assert.EqualValues(t, expected, ids)
|
||
|
}
|
||
|
|
||
|
// Public repo, existing user
|
||
|
testSuccess("user2", "repo1", "user1", []string{"user5"}, []int64{5})
|
||
|
// Public repo, non-existing user
|
||
|
testSuccess("user2", "repo1", "user1", []string{"nonexisting"}, []int64{})
|
||
|
// Public repo, doer
|
||
|
testSuccess("user2", "repo1", "user1", []string{"user1"}, []int64{})
|
||
|
// Private repo, team member
|
||
|
testSuccess("user17", "big_test_private_4", "user20", []string{"user2"}, []int64{2})
|
||
|
// Private repo, not a team member
|
||
|
testSuccess("user17", "big_test_private_4", "user20", []string{"user5"}, []int64{})
|
||
|
// Private repo, whole team
|
||
4 years ago
|
testSuccess("user17", "big_test_private_4", "user15", []string{"user17/owners"}, []int64{18})
|
||
5 years ago
|
}
|
||
3 years ago
|
|
||
|
func TestResourceIndex(t *testing.T) {
|
||
3 years ago
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||
3 years ago
|
|
||
|
var wg sync.WaitGroup
|
||
|
for i := 0; i < 100; i++ {
|
||
|
wg.Add(1)
|
||
|
go func(i int) {
|
||
|
testInsertIssue(t, fmt.Sprintf("issue %d", i+1), "my issue", 0)
|
||
|
wg.Done()
|
||
|
}(i)
|
||
|
}
|
||
|
wg.Wait()
|
||
|
}
|
||
3 years ago
|
|
||
|
func TestCorrectIssueStats(t *testing.T) {
|
||
3 years ago
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||
3 years ago
|
|
||
|
// Because the condition is to have chunked database look-ups,
|
||
|
// We have to more issues than `maxQueryParameters`, we will insert.
|
||
|
// maxQueryParameters + 10 issues into the testDatabase.
|
||
|
// Each new issues will have a constant description "Bugs are nasty"
|
||
|
// Which will be used later on.
|
||
|
|
||
2 years ago
|
issueAmount := issues_model.MaxQueryParameters + 10
|
||
3 years ago
|
|
||
|
var wg sync.WaitGroup
|
||
|
for i := 0; i < issueAmount; i++ {
|
||
|
wg.Add(1)
|
||
|
go func(i int) {
|
||
|
testInsertIssue(t, fmt.Sprintf("Issue %d", i+1), "Bugs are nasty", 0)
|
||
|
wg.Done()
|
||
|
}(i)
|
||
|
}
|
||
|
wg.Wait()
|
||
|
|
||
|
// Now we will get all issueID's that match the "Bugs are nasty" query.
|
||
2 years ago
|
total, ids, err := issues_model.SearchIssueIDsByKeyword(context.TODO(), "Bugs are nasty", []int64{1}, issueAmount, 0)
|
||
3 years ago
|
|
||
|
// Just to be sure.
|
||
|
assert.NoError(t, err)
|
||
|
assert.EqualValues(t, issueAmount, total)
|
||
|
|
||
|
// Now we will call the GetIssueStats with these IDs and if working,
|
||
|
// get the correct stats back.
|
||
2 years ago
|
issueStats, err := issues_model.GetIssueStats(&issues_model.IssueStatsOptions{
|
||
3 years ago
|
RepoID: 1,
|
||
|
IssueIDs: ids,
|
||
|
})
|
||
|
|
||
|
// Now check the values.
|
||
|
assert.NoError(t, err)
|
||
|
assert.EqualValues(t, issueStats.OpenCount, issueAmount)
|
||
|
}
|
||
3 years ago
|
|
||
|
func TestIssueForeignReference(t *testing.T) {
|
||
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||
2 years ago
|
issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 4}).(*issues_model.Issue)
|
||
3 years ago
|
assert.NotEqualValues(t, issue.Index, issue.ID) // make sure they are different to avoid false positive
|
||
|
|
||
|
// it is fine for an issue to not have a foreign reference
|
||
2 years ago
|
err := issue.LoadAttributes(db.DefaultContext)
|
||
3 years ago
|
assert.NoError(t, err)
|
||
|
assert.Nil(t, issue.ForeignReference)
|
||
|
|
||
|
var foreignIndex int64 = 12345
|
||
2 years ago
|
_, err = issues_model.GetIssueByForeignIndex(context.Background(), issue.RepoID, foreignIndex)
|
||
3 years ago
|
assert.True(t, foreignreference.IsErrLocalIndexNotExist(err))
|
||
|
|
||
2 years ago
|
err = db.Insert(db.DefaultContext, &foreignreference.ForeignReference{
|
||
3 years ago
|
LocalIndex: issue.Index,
|
||
|
ForeignIndex: strconv.FormatInt(foreignIndex, 10),
|
||
|
RepoID: issue.RepoID,
|
||
|
Type: foreignreference.TypeIssue,
|
||
|
})
|
||
|
assert.NoError(t, err)
|
||
|
|
||
2 years ago
|
err = issue.LoadAttributes(db.DefaultContext)
|
||
3 years ago
|
assert.NoError(t, err)
|
||
|
|
||
|
assert.EqualValues(t, issue.ForeignReference.ForeignIndex, strconv.FormatInt(foreignIndex, 10))
|
||
|
|
||
2 years ago
|
found, err := issues_model.GetIssueByForeignIndex(context.Background(), issue.RepoID, foreignIndex)
|
||
3 years ago
|
assert.NoError(t, err)
|
||
|
assert.EqualValues(t, found.Index, issue.Index)
|
||
|
}
|
||
3 years ago
|
|
||
|
func TestMilestoneList_LoadTotalTrackedTimes(t *testing.T) {
|
||
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||
|
miles := issues_model.MilestoneList{
|
||
|
unittest.AssertExistsAndLoadBean(t, &issues_model.Milestone{ID: 1}).(*issues_model.Milestone),
|
||
|
}
|
||
|
|
||
|
assert.NoError(t, miles.LoadTotalTrackedTimes())
|
||
|
|
||
|
assert.Equal(t, int64(3682), miles[0].TotalTrackedTime)
|
||
|
}
|
||
|
|
||
|
func TestLoadTotalTrackedTime(t *testing.T) {
|
||
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||
|
milestone := unittest.AssertExistsAndLoadBean(t, &issues_model.Milestone{ID: 1}).(*issues_model.Milestone)
|
||
|
|
||
|
assert.NoError(t, milestone.LoadTotalTrackedTime())
|
||
|
|
||
|
assert.Equal(t, int64(3682), milestone.TotalTrackedTime)
|
||
|
}
|
||
3 years ago
|
|
||
|
func TestCountIssues(t *testing.T) {
|
||
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||
2 years ago
|
count, err := issues_model.CountIssues(&issues_model.IssuesOptions{})
|
||
3 years ago
|
assert.NoError(t, err)
|
||
3 years ago
|
assert.EqualValues(t, 17, count)
|
||
3 years ago
|
}
|