Move issues related files into models/issues (#19931)
* Move access and repo permission to models/perm/access * fix test * fix git test * Move functions sequence * Some improvements per @KN4CK3R and @delvh * Move issues related code to models/issues * Move some issues related sub package * Merge * Fix test * Fix test * Fix test * Fix test * Rename some filestokarchuk/v1.17
parent
3708ca8e28
commit
1a9821f57a
@ -1,80 +0,0 @@ |
|||||||
// Copyright 2022 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 ( |
|
||||||
"context" |
|
||||||
|
|
||||||
"code.gitea.io/gitea/models/db" |
|
||||||
git_model "code.gitea.io/gitea/models/git" |
|
||||||
"code.gitea.io/gitea/modules/log" |
|
||||||
) |
|
||||||
|
|
||||||
// HasEnoughApprovals returns true if pr has enough granted approvals.
|
|
||||||
func HasEnoughApprovals(ctx context.Context, protectBranch *git_model.ProtectedBranch, pr *PullRequest) bool { |
|
||||||
if protectBranch.RequiredApprovals == 0 { |
|
||||||
return true |
|
||||||
} |
|
||||||
return GetGrantedApprovalsCount(ctx, protectBranch, pr) >= protectBranch.RequiredApprovals |
|
||||||
} |
|
||||||
|
|
||||||
// GetGrantedApprovalsCount returns the number of granted approvals for pr. A granted approval must be authored by a user in an approval whitelist.
|
|
||||||
func GetGrantedApprovalsCount(ctx context.Context, protectBranch *git_model.ProtectedBranch, pr *PullRequest) int64 { |
|
||||||
sess := db.GetEngine(ctx).Where("issue_id = ?", pr.IssueID). |
|
||||||
And("type = ?", ReviewTypeApprove). |
|
||||||
And("official = ?", true). |
|
||||||
And("dismissed = ?", false) |
|
||||||
if protectBranch.DismissStaleApprovals { |
|
||||||
sess = sess.And("stale = ?", false) |
|
||||||
} |
|
||||||
approvals, err := sess.Count(new(Review)) |
|
||||||
if err != nil { |
|
||||||
log.Error("GetGrantedApprovalsCount: %v", err) |
|
||||||
return 0 |
|
||||||
} |
|
||||||
|
|
||||||
return approvals |
|
||||||
} |
|
||||||
|
|
||||||
// MergeBlockedByRejectedReview returns true if merge is blocked by rejected reviews
|
|
||||||
func MergeBlockedByRejectedReview(ctx context.Context, protectBranch *git_model.ProtectedBranch, pr *PullRequest) bool { |
|
||||||
if !protectBranch.BlockOnRejectedReviews { |
|
||||||
return false |
|
||||||
} |
|
||||||
rejectExist, err := db.GetEngine(ctx).Where("issue_id = ?", pr.IssueID). |
|
||||||
And("type = ?", ReviewTypeReject). |
|
||||||
And("official = ?", true). |
|
||||||
And("dismissed = ?", false). |
|
||||||
Exist(new(Review)) |
|
||||||
if err != nil { |
|
||||||
log.Error("MergeBlockedByRejectedReview: %v", err) |
|
||||||
return true |
|
||||||
} |
|
||||||
|
|
||||||
return rejectExist |
|
||||||
} |
|
||||||
|
|
||||||
// MergeBlockedByOfficialReviewRequests block merge because of some review request to official reviewer
|
|
||||||
// of from official review
|
|
||||||
func MergeBlockedByOfficialReviewRequests(ctx context.Context, protectBranch *git_model.ProtectedBranch, pr *PullRequest) bool { |
|
||||||
if !protectBranch.BlockOnOfficialReviewRequests { |
|
||||||
return false |
|
||||||
} |
|
||||||
has, err := db.GetEngine(ctx).Where("issue_id = ?", pr.IssueID). |
|
||||||
And("type = ?", ReviewTypeRequest). |
|
||||||
And("official = ?", true). |
|
||||||
Exist(new(Review)) |
|
||||||
if err != nil { |
|
||||||
log.Error("MergeBlockedByOfficialReviewRequests: %v", err) |
|
||||||
return true |
|
||||||
} |
|
||||||
|
|
||||||
return has |
|
||||||
} |
|
||||||
|
|
||||||
// MergeBlockedByOutdatedBranch returns true if merge is blocked by an outdated head branch
|
|
||||||
func MergeBlockedByOutdatedBranch(protectBranch *git_model.ProtectedBranch, pr *PullRequest) bool { |
|
||||||
return protectBranch.BlockOnOutdatedBranch && pr.CommitsBehind > 0 |
|
||||||
} |
|
@ -1,149 +0,0 @@ |
|||||||
// Copyright 2021 Gitea. 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" |
|
||||||
issues_model "code.gitea.io/gitea/models/issues" |
|
||||||
repo_model "code.gitea.io/gitea/models/repo" |
|
||||||
"code.gitea.io/gitea/models/unittest" |
|
||||||
"code.gitea.io/gitea/modules/setting" |
|
||||||
"code.gitea.io/gitea/modules/timeutil" |
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert" |
|
||||||
) |
|
||||||
|
|
||||||
func TestDeleteOrphanedObjects(t *testing.T) { |
|
||||||
assert.NoError(t, unittest.PrepareTestDatabase()) |
|
||||||
|
|
||||||
countBefore, err := db.GetEngine(db.DefaultContext).Count(&PullRequest{}) |
|
||||||
assert.NoError(t, err) |
|
||||||
|
|
||||||
_, err = db.GetEngine(db.DefaultContext).Insert(&PullRequest{IssueID: 1000}, &PullRequest{IssueID: 1001}, &PullRequest{IssueID: 1003}) |
|
||||||
assert.NoError(t, err) |
|
||||||
|
|
||||||
orphaned, err := CountOrphanedObjects("pull_request", "issue", "pull_request.issue_id=issue.id") |
|
||||||
assert.NoError(t, err) |
|
||||||
assert.EqualValues(t, 3, orphaned) |
|
||||||
|
|
||||||
err = DeleteOrphanedObjects("pull_request", "issue", "pull_request.issue_id=issue.id") |
|
||||||
assert.NoError(t, err) |
|
||||||
|
|
||||||
countAfter, err := db.GetEngine(db.DefaultContext).Count(&PullRequest{}) |
|
||||||
assert.NoError(t, err) |
|
||||||
assert.EqualValues(t, countBefore, countAfter) |
|
||||||
} |
|
||||||
|
|
||||||
func TestNewMilestone(t *testing.T) { |
|
||||||
assert.NoError(t, unittest.PrepareTestDatabase()) |
|
||||||
milestone := &issues_model.Milestone{ |
|
||||||
RepoID: 1, |
|
||||||
Name: "milestoneName", |
|
||||||
Content: "milestoneContent", |
|
||||||
} |
|
||||||
|
|
||||||
assert.NoError(t, issues_model.NewMilestone(milestone)) |
|
||||||
unittest.AssertExistsAndLoadBean(t, milestone) |
|
||||||
unittest.CheckConsistencyFor(t, &repo_model.Repository{ID: milestone.RepoID}, &issues_model.Milestone{}) |
|
||||||
} |
|
||||||
|
|
||||||
func TestChangeMilestoneStatus(t *testing.T) { |
|
||||||
assert.NoError(t, unittest.PrepareTestDatabase()) |
|
||||||
milestone := unittest.AssertExistsAndLoadBean(t, &issues_model.Milestone{ID: 1}).(*issues_model.Milestone) |
|
||||||
|
|
||||||
assert.NoError(t, issues_model.ChangeMilestoneStatus(milestone, true)) |
|
||||||
unittest.AssertExistsAndLoadBean(t, &issues_model.Milestone{ID: 1}, "is_closed=1") |
|
||||||
unittest.CheckConsistencyFor(t, &repo_model.Repository{ID: milestone.RepoID}, &issues_model.Milestone{}) |
|
||||||
|
|
||||||
assert.NoError(t, issues_model.ChangeMilestoneStatus(milestone, false)) |
|
||||||
unittest.AssertExistsAndLoadBean(t, &issues_model.Milestone{ID: 1}, "is_closed=0") |
|
||||||
unittest.CheckConsistencyFor(t, &repo_model.Repository{ID: milestone.RepoID}, &issues_model.Milestone{}) |
|
||||||
} |
|
||||||
|
|
||||||
func TestDeleteMilestoneByRepoID(t *testing.T) { |
|
||||||
assert.NoError(t, unittest.PrepareTestDatabase()) |
|
||||||
assert.NoError(t, issues_model.DeleteMilestoneByRepoID(1, 1)) |
|
||||||
unittest.AssertNotExistsBean(t, &issues_model.Milestone{ID: 1}) |
|
||||||
unittest.CheckConsistencyFor(t, &repo_model.Repository{ID: 1}) |
|
||||||
|
|
||||||
assert.NoError(t, issues_model.DeleteMilestoneByRepoID(unittest.NonexistentID, unittest.NonexistentID)) |
|
||||||
} |
|
||||||
|
|
||||||
func TestUpdateMilestone(t *testing.T) { |
|
||||||
assert.NoError(t, unittest.PrepareTestDatabase()) |
|
||||||
|
|
||||||
milestone := unittest.AssertExistsAndLoadBean(t, &issues_model.Milestone{ID: 1}).(*issues_model.Milestone) |
|
||||||
milestone.Name = " newMilestoneName " |
|
||||||
milestone.Content = "newMilestoneContent" |
|
||||||
assert.NoError(t, issues_model.UpdateMilestone(milestone, milestone.IsClosed)) |
|
||||||
milestone = unittest.AssertExistsAndLoadBean(t, &issues_model.Milestone{ID: 1}).(*issues_model.Milestone) |
|
||||||
assert.EqualValues(t, "newMilestoneName", milestone.Name) |
|
||||||
unittest.CheckConsistencyFor(t, &issues_model.Milestone{}) |
|
||||||
} |
|
||||||
|
|
||||||
func TestUpdateMilestoneCounters(t *testing.T) { |
|
||||||
assert.NoError(t, unittest.PrepareTestDatabase()) |
|
||||||
issue := unittest.AssertExistsAndLoadBean(t, &Issue{MilestoneID: 1}, |
|
||||||
"is_closed=0").(*Issue) |
|
||||||
|
|
||||||
issue.IsClosed = true |
|
||||||
issue.ClosedUnix = timeutil.TimeStampNow() |
|
||||||
_, err := db.GetEngine(db.DefaultContext).ID(issue.ID).Cols("is_closed", "closed_unix").Update(issue) |
|
||||||
assert.NoError(t, err) |
|
||||||
assert.NoError(t, issues_model.UpdateMilestoneCounters(db.DefaultContext, issue.MilestoneID)) |
|
||||||
unittest.CheckConsistencyFor(t, &issues_model.Milestone{}) |
|
||||||
|
|
||||||
issue.IsClosed = false |
|
||||||
issue.ClosedUnix = 0 |
|
||||||
_, err = db.GetEngine(db.DefaultContext).ID(issue.ID).Cols("is_closed", "closed_unix").Update(issue) |
|
||||||
assert.NoError(t, err) |
|
||||||
assert.NoError(t, issues_model.UpdateMilestoneCounters(db.DefaultContext, issue.MilestoneID)) |
|
||||||
unittest.CheckConsistencyFor(t, &issues_model.Milestone{}) |
|
||||||
} |
|
||||||
|
|
||||||
func TestConsistencyUpdateAction(t *testing.T) { |
|
||||||
if !setting.Database.UseSQLite3 { |
|
||||||
t.Skip("Test is only for SQLite database.") |
|
||||||
} |
|
||||||
assert.NoError(t, unittest.PrepareTestDatabase()) |
|
||||||
id := 8 |
|
||||||
unittest.AssertExistsAndLoadBean(t, &Action{ |
|
||||||
ID: int64(id), |
|
||||||
}) |
|
||||||
_, err := db.GetEngine(db.DefaultContext).Exec(`UPDATE action SET created_unix = "" WHERE id = ?`, id) |
|
||||||
assert.NoError(t, err) |
|
||||||
actions := make([]*Action, 0, 1) |
|
||||||
//
|
|
||||||
// XORM returns an error when created_unix is a string
|
|
||||||
//
|
|
||||||
err = db.GetEngine(db.DefaultContext).Where("id = ?", id).Find(&actions) |
|
||||||
if assert.Error(t, err) { |
|
||||||
assert.Contains(t, err.Error(), "type string to a int64: invalid syntax") |
|
||||||
} |
|
||||||
//
|
|
||||||
// Get rid of incorrectly set created_unix
|
|
||||||
//
|
|
||||||
count, err := CountActionCreatedUnixString() |
|
||||||
assert.NoError(t, err) |
|
||||||
assert.EqualValues(t, 1, count) |
|
||||||
count, err = FixActionCreatedUnixString() |
|
||||||
assert.NoError(t, err) |
|
||||||
assert.EqualValues(t, 1, count) |
|
||||||
|
|
||||||
count, err = CountActionCreatedUnixString() |
|
||||||
assert.NoError(t, err) |
|
||||||
assert.EqualValues(t, 0, count) |
|
||||||
count, err = FixActionCreatedUnixString() |
|
||||||
assert.NoError(t, err) |
|
||||||
assert.EqualValues(t, 0, count) |
|
||||||
|
|
||||||
//
|
|
||||||
// XORM must be happy now
|
|
||||||
//
|
|
||||||
assert.NoError(t, db.GetEngine(db.DefaultContext).Where("id = ?", id).Find(&actions)) |
|
||||||
unittest.CheckConsistencyFor(t, &Action{}) |
|
||||||
} |
|
@ -0,0 +1,27 @@ |
|||||||
|
// Copyright 2022 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 db |
||||||
|
|
||||||
|
import "xorm.io/builder" |
||||||
|
|
||||||
|
// CountOrphanedObjects count subjects with have no existing refobject anymore
|
||||||
|
func CountOrphanedObjects(subject, refobject, joinCond string) (int64, error) { |
||||||
|
return GetEngine(DefaultContext).Table("`"+subject+"`"). |
||||||
|
Join("LEFT", "`"+refobject+"`", joinCond). |
||||||
|
Where(builder.IsNull{"`" + refobject + "`.id"}). |
||||||
|
Select("COUNT(`" + subject + "`.`id`)"). |
||||||
|
Count() |
||||||
|
} |
||||||
|
|
||||||
|
// DeleteOrphanedObjects delete subjects with have no existing refobject anymore
|
||||||
|
func DeleteOrphanedObjects(subject, refobject, joinCond string) error { |
||||||
|
subQuery := builder.Select("`"+subject+"`.id"). |
||||||
|
From("`"+subject+"`"). |
||||||
|
Join("LEFT", "`"+refobject+"`", joinCond). |
||||||
|
Where(builder.IsNull{"`" + refobject + "`.id"}) |
||||||
|
b := builder.Delete(builder.In("id", subQuery)).From("`" + subject + "`") |
||||||
|
_, err := GetEngine(DefaultContext).Exec(b) |
||||||
|
return err |
||||||
|
} |
@ -1,394 +0,0 @@ |
|||||||
// 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.
|
|
||||||
|
|
||||||
package models |
|
||||||
|
|
||||||
import ( |
|
||||||
"html/template" |
|
||||||
"testing" |
|
||||||
|
|
||||||
"code.gitea.io/gitea/models/db" |
|
||||||
repo_model "code.gitea.io/gitea/models/repo" |
|
||||||
"code.gitea.io/gitea/models/unittest" |
|
||||||
user_model "code.gitea.io/gitea/models/user" |
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert" |
|
||||||
) |
|
||||||
|
|
||||||
// TODO TestGetLabelTemplateFile
|
|
||||||
|
|
||||||
func TestLabel_CalOpenIssues(t *testing.T) { |
|
||||||
assert.NoError(t, unittest.PrepareTestDatabase()) |
|
||||||
label := unittest.AssertExistsAndLoadBean(t, &Label{ID: 1}).(*Label) |
|
||||||
label.CalOpenIssues() |
|
||||||
assert.EqualValues(t, 2, label.NumOpenIssues) |
|
||||||
} |
|
||||||
|
|
||||||
func TestLabel_ForegroundColor(t *testing.T) { |
|
||||||
assert.NoError(t, unittest.PrepareTestDatabase()) |
|
||||||
label := unittest.AssertExistsAndLoadBean(t, &Label{ID: 1}).(*Label) |
|
||||||
assert.Equal(t, template.CSS("#000"), label.ForegroundColor()) |
|
||||||
|
|
||||||
label = unittest.AssertExistsAndLoadBean(t, &Label{ID: 2}).(*Label) |
|
||||||
assert.Equal(t, template.CSS("#fff"), label.ForegroundColor()) |
|
||||||
} |
|
||||||
|
|
||||||
func TestNewLabels(t *testing.T) { |
|
||||||
assert.NoError(t, unittest.PrepareTestDatabase()) |
|
||||||
labels := []*Label{ |
|
||||||
{RepoID: 2, Name: "labelName2", Color: "#123456"}, |
|
||||||
{RepoID: 3, Name: "labelName3", Color: "#123"}, |
|
||||||
{RepoID: 4, Name: "labelName4", Color: "ABCDEF"}, |
|
||||||
{RepoID: 5, Name: "labelName5", Color: "DEF"}, |
|
||||||
} |
|
||||||
assert.Error(t, NewLabel(db.DefaultContext, &Label{RepoID: 3, Name: "invalid Color", Color: ""})) |
|
||||||
assert.Error(t, NewLabel(db.DefaultContext, &Label{RepoID: 3, Name: "invalid Color", Color: "#45G"})) |
|
||||||
assert.Error(t, NewLabel(db.DefaultContext, &Label{RepoID: 3, Name: "invalid Color", Color: "#12345G"})) |
|
||||||
assert.Error(t, NewLabel(db.DefaultContext, &Label{RepoID: 3, Name: "invalid Color", Color: "45G"})) |
|
||||||
assert.Error(t, NewLabel(db.DefaultContext, &Label{RepoID: 3, Name: "invalid Color", Color: "12345G"})) |
|
||||||
for _, label := range labels { |
|
||||||
unittest.AssertNotExistsBean(t, label) |
|
||||||
} |
|
||||||
assert.NoError(t, NewLabels(labels...)) |
|
||||||
for _, label := range labels { |
|
||||||
unittest.AssertExistsAndLoadBean(t, label, unittest.Cond("id = ?", label.ID)) |
|
||||||
} |
|
||||||
unittest.CheckConsistencyFor(t, &Label{}, &repo_model.Repository{}) |
|
||||||
} |
|
||||||
|
|
||||||
func TestGetLabelByID(t *testing.T) { |
|
||||||
assert.NoError(t, unittest.PrepareTestDatabase()) |
|
||||||
label, err := GetLabelByID(db.DefaultContext, 1) |
|
||||||
assert.NoError(t, err) |
|
||||||
assert.EqualValues(t, 1, label.ID) |
|
||||||
|
|
||||||
_, err = GetLabelByID(db.DefaultContext, unittest.NonexistentID) |
|
||||||
assert.True(t, IsErrLabelNotExist(err)) |
|
||||||
} |
|
||||||
|
|
||||||
func TestGetLabelInRepoByName(t *testing.T) { |
|
||||||
assert.NoError(t, unittest.PrepareTestDatabase()) |
|
||||||
label, err := GetLabelInRepoByName(db.DefaultContext, 1, "label1") |
|
||||||
assert.NoError(t, err) |
|
||||||
assert.EqualValues(t, 1, label.ID) |
|
||||||
assert.Equal(t, "label1", label.Name) |
|
||||||
|
|
||||||
_, err = GetLabelInRepoByName(db.DefaultContext, 1, "") |
|
||||||
assert.True(t, IsErrRepoLabelNotExist(err)) |
|
||||||
|
|
||||||
_, err = GetLabelInRepoByName(db.DefaultContext, unittest.NonexistentID, "nonexistent") |
|
||||||
assert.True(t, IsErrRepoLabelNotExist(err)) |
|
||||||
} |
|
||||||
|
|
||||||
func TestGetLabelInRepoByNames(t *testing.T) { |
|
||||||
assert.NoError(t, unittest.PrepareTestDatabase()) |
|
||||||
labelIDs, err := GetLabelIDsInRepoByNames(1, []string{"label1", "label2"}) |
|
||||||
assert.NoError(t, err) |
|
||||||
|
|
||||||
assert.Len(t, labelIDs, 2) |
|
||||||
|
|
||||||
assert.Equal(t, int64(1), labelIDs[0]) |
|
||||||
assert.Equal(t, int64(2), labelIDs[1]) |
|
||||||
} |
|
||||||
|
|
||||||
func TestGetLabelInRepoByNamesDiscardsNonExistentLabels(t *testing.T) { |
|
||||||
assert.NoError(t, unittest.PrepareTestDatabase()) |
|
||||||
// label3 doesn't exists.. See labels.yml
|
|
||||||
labelIDs, err := GetLabelIDsInRepoByNames(1, []string{"label1", "label2", "label3"}) |
|
||||||
assert.NoError(t, err) |
|
||||||
|
|
||||||
assert.Len(t, labelIDs, 2) |
|
||||||
|
|
||||||
assert.Equal(t, int64(1), labelIDs[0]) |
|
||||||
assert.Equal(t, int64(2), labelIDs[1]) |
|
||||||
assert.NoError(t, err) |
|
||||||
} |
|
||||||
|
|
||||||
func TestGetLabelInRepoByID(t *testing.T) { |
|
||||||
assert.NoError(t, unittest.PrepareTestDatabase()) |
|
||||||
label, err := GetLabelInRepoByID(db.DefaultContext, 1, 1) |
|
||||||
assert.NoError(t, err) |
|
||||||
assert.EqualValues(t, 1, label.ID) |
|
||||||
|
|
||||||
_, err = GetLabelInRepoByID(db.DefaultContext, 1, -1) |
|
||||||
assert.True(t, IsErrRepoLabelNotExist(err)) |
|
||||||
|
|
||||||
_, err = GetLabelInRepoByID(db.DefaultContext, unittest.NonexistentID, unittest.NonexistentID) |
|
||||||
assert.True(t, IsErrRepoLabelNotExist(err)) |
|
||||||
} |
|
||||||
|
|
||||||
func TestGetLabelsInRepoByIDs(t *testing.T) { |
|
||||||
assert.NoError(t, unittest.PrepareTestDatabase()) |
|
||||||
labels, err := GetLabelsInRepoByIDs(1, []int64{1, 2, unittest.NonexistentID}) |
|
||||||
assert.NoError(t, err) |
|
||||||
if assert.Len(t, labels, 2) { |
|
||||||
assert.EqualValues(t, 1, labels[0].ID) |
|
||||||
assert.EqualValues(t, 2, labels[1].ID) |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
func TestGetLabelsByRepoID(t *testing.T) { |
|
||||||
assert.NoError(t, unittest.PrepareTestDatabase()) |
|
||||||
testSuccess := func(repoID int64, sortType string, expectedIssueIDs []int64) { |
|
||||||
labels, err := GetLabelsByRepoID(db.DefaultContext, repoID, sortType, db.ListOptions{}) |
|
||||||
assert.NoError(t, err) |
|
||||||
assert.Len(t, labels, len(expectedIssueIDs)) |
|
||||||
for i, label := range labels { |
|
||||||
assert.EqualValues(t, expectedIssueIDs[i], label.ID) |
|
||||||
} |
|
||||||
} |
|
||||||
testSuccess(1, "leastissues", []int64{2, 1}) |
|
||||||
testSuccess(1, "mostissues", []int64{1, 2}) |
|
||||||
testSuccess(1, "reversealphabetically", []int64{2, 1}) |
|
||||||
testSuccess(1, "default", []int64{1, 2}) |
|
||||||
} |
|
||||||
|
|
||||||
// Org versions
|
|
||||||
|
|
||||||
func TestGetLabelInOrgByName(t *testing.T) { |
|
||||||
assert.NoError(t, unittest.PrepareTestDatabase()) |
|
||||||
label, err := GetLabelInOrgByName(db.DefaultContext, 3, "orglabel3") |
|
||||||
assert.NoError(t, err) |
|
||||||
assert.EqualValues(t, 3, label.ID) |
|
||||||
assert.Equal(t, "orglabel3", label.Name) |
|
||||||
|
|
||||||
_, err = GetLabelInOrgByName(db.DefaultContext, 3, "") |
|
||||||
assert.True(t, IsErrOrgLabelNotExist(err)) |
|
||||||
|
|
||||||
_, err = GetLabelInOrgByName(db.DefaultContext, 0, "orglabel3") |
|
||||||
assert.True(t, IsErrOrgLabelNotExist(err)) |
|
||||||
|
|
||||||
_, err = GetLabelInOrgByName(db.DefaultContext, -1, "orglabel3") |
|
||||||
assert.True(t, IsErrOrgLabelNotExist(err)) |
|
||||||
|
|
||||||
_, err = GetLabelInOrgByName(db.DefaultContext, unittest.NonexistentID, "nonexistent") |
|
||||||
assert.True(t, IsErrOrgLabelNotExist(err)) |
|
||||||
} |
|
||||||
|
|
||||||
func TestGetLabelInOrgByNames(t *testing.T) { |
|
||||||
assert.NoError(t, unittest.PrepareTestDatabase()) |
|
||||||
labelIDs, err := GetLabelIDsInOrgByNames(3, []string{"orglabel3", "orglabel4"}) |
|
||||||
assert.NoError(t, err) |
|
||||||
|
|
||||||
assert.Len(t, labelIDs, 2) |
|
||||||
|
|
||||||
assert.Equal(t, int64(3), labelIDs[0]) |
|
||||||
assert.Equal(t, int64(4), labelIDs[1]) |
|
||||||
} |
|
||||||
|
|
||||||
func TestGetLabelInOrgByNamesDiscardsNonExistentLabels(t *testing.T) { |
|
||||||
assert.NoError(t, unittest.PrepareTestDatabase()) |
|
||||||
// orglabel99 doesn't exists.. See labels.yml
|
|
||||||
labelIDs, err := GetLabelIDsInOrgByNames(3, []string{"orglabel3", "orglabel4", "orglabel99"}) |
|
||||||
assert.NoError(t, err) |
|
||||||
|
|
||||||
assert.Len(t, labelIDs, 2) |
|
||||||
|
|
||||||
assert.Equal(t, int64(3), labelIDs[0]) |
|
||||||
assert.Equal(t, int64(4), labelIDs[1]) |
|
||||||
assert.NoError(t, err) |
|
||||||
} |
|
||||||
|
|
||||||
func TestGetLabelInOrgByID(t *testing.T) { |
|
||||||
assert.NoError(t, unittest.PrepareTestDatabase()) |
|
||||||
label, err := GetLabelInOrgByID(db.DefaultContext, 3, 3) |
|
||||||
assert.NoError(t, err) |
|
||||||
assert.EqualValues(t, 3, label.ID) |
|
||||||
|
|
||||||
_, err = GetLabelInOrgByID(db.DefaultContext, 3, -1) |
|
||||||
assert.True(t, IsErrOrgLabelNotExist(err)) |
|
||||||
|
|
||||||
_, err = GetLabelInOrgByID(db.DefaultContext, 0, 3) |
|
||||||
assert.True(t, IsErrOrgLabelNotExist(err)) |
|
||||||
|
|
||||||
_, err = GetLabelInOrgByID(db.DefaultContext, -1, 3) |
|
||||||
assert.True(t, IsErrOrgLabelNotExist(err)) |
|
||||||
|
|
||||||
_, err = GetLabelInOrgByID(db.DefaultContext, unittest.NonexistentID, unittest.NonexistentID) |
|
||||||
assert.True(t, IsErrOrgLabelNotExist(err)) |
|
||||||
} |
|
||||||
|
|
||||||
func TestGetLabelsInOrgByIDs(t *testing.T) { |
|
||||||
assert.NoError(t, unittest.PrepareTestDatabase()) |
|
||||||
labels, err := GetLabelsInOrgByIDs(3, []int64{3, 4, unittest.NonexistentID}) |
|
||||||
assert.NoError(t, err) |
|
||||||
if assert.Len(t, labels, 2) { |
|
||||||
assert.EqualValues(t, 3, labels[0].ID) |
|
||||||
assert.EqualValues(t, 4, labels[1].ID) |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
func TestGetLabelsByOrgID(t *testing.T) { |
|
||||||
assert.NoError(t, unittest.PrepareTestDatabase()) |
|
||||||
testSuccess := func(orgID int64, sortType string, expectedIssueIDs []int64) { |
|
||||||
labels, err := GetLabelsByOrgID(db.DefaultContext, orgID, sortType, db.ListOptions{}) |
|
||||||
assert.NoError(t, err) |
|
||||||
assert.Len(t, labels, len(expectedIssueIDs)) |
|
||||||
for i, label := range labels { |
|
||||||
assert.EqualValues(t, expectedIssueIDs[i], label.ID) |
|
||||||
} |
|
||||||
} |
|
||||||
testSuccess(3, "leastissues", []int64{3, 4}) |
|
||||||
testSuccess(3, "mostissues", []int64{4, 3}) |
|
||||||
testSuccess(3, "reversealphabetically", []int64{4, 3}) |
|
||||||
testSuccess(3, "default", []int64{3, 4}) |
|
||||||
|
|
||||||
var err error |
|
||||||
_, err = GetLabelsByOrgID(db.DefaultContext, 0, "leastissues", db.ListOptions{}) |
|
||||||
assert.True(t, IsErrOrgLabelNotExist(err)) |
|
||||||
|
|
||||||
_, err = GetLabelsByOrgID(db.DefaultContext, -1, "leastissues", db.ListOptions{}) |
|
||||||
assert.True(t, IsErrOrgLabelNotExist(err)) |
|
||||||
} |
|
||||||
|
|
||||||
//
|
|
||||||
|
|
||||||
func TestGetLabelsByIssueID(t *testing.T) { |
|
||||||
assert.NoError(t, unittest.PrepareTestDatabase()) |
|
||||||
labels, err := GetLabelsByIssueID(db.DefaultContext, 1) |
|
||||||
assert.NoError(t, err) |
|
||||||
if assert.Len(t, labels, 1) { |
|
||||||
assert.EqualValues(t, 1, labels[0].ID) |
|
||||||
} |
|
||||||
|
|
||||||
labels, err = GetLabelsByIssueID(db.DefaultContext, unittest.NonexistentID) |
|
||||||
assert.NoError(t, err) |
|
||||||
assert.Len(t, labels, 0) |
|
||||||
} |
|
||||||
|
|
||||||
func TestUpdateLabel(t *testing.T) { |
|
||||||
assert.NoError(t, unittest.PrepareTestDatabase()) |
|
||||||
label := unittest.AssertExistsAndLoadBean(t, &Label{ID: 1}).(*Label) |
|
||||||
// make sure update wont overwrite it
|
|
||||||
update := &Label{ |
|
||||||
ID: label.ID, |
|
||||||
Color: "#ffff00", |
|
||||||
Name: "newLabelName", |
|
||||||
Description: label.Description, |
|
||||||
} |
|
||||||
label.Color = update.Color |
|
||||||
label.Name = update.Name |
|
||||||
assert.NoError(t, UpdateLabel(update)) |
|
||||||
newLabel := unittest.AssertExistsAndLoadBean(t, &Label{ID: 1}).(*Label) |
|
||||||
assert.EqualValues(t, label.ID, newLabel.ID) |
|
||||||
assert.EqualValues(t, label.Color, newLabel.Color) |
|
||||||
assert.EqualValues(t, label.Name, newLabel.Name) |
|
||||||
assert.EqualValues(t, label.Description, newLabel.Description) |
|
||||||
unittest.CheckConsistencyFor(t, &Label{}, &repo_model.Repository{}) |
|
||||||
} |
|
||||||
|
|
||||||
func TestDeleteLabel(t *testing.T) { |
|
||||||
assert.NoError(t, unittest.PrepareTestDatabase()) |
|
||||||
label := unittest.AssertExistsAndLoadBean(t, &Label{ID: 1}).(*Label) |
|
||||||
assert.NoError(t, DeleteLabel(label.RepoID, label.ID)) |
|
||||||
unittest.AssertNotExistsBean(t, &Label{ID: label.ID, RepoID: label.RepoID}) |
|
||||||
|
|
||||||
assert.NoError(t, DeleteLabel(label.RepoID, label.ID)) |
|
||||||
unittest.AssertNotExistsBean(t, &Label{ID: label.ID}) |
|
||||||
|
|
||||||
assert.NoError(t, DeleteLabel(unittest.NonexistentID, unittest.NonexistentID)) |
|
||||||
unittest.CheckConsistencyFor(t, &Label{}, &repo_model.Repository{}) |
|
||||||
} |
|
||||||
|
|
||||||
func TestHasIssueLabel(t *testing.T) { |
|
||||||
assert.NoError(t, unittest.PrepareTestDatabase()) |
|
||||||
assert.True(t, HasIssueLabel(db.DefaultContext, 1, 1)) |
|
||||||
assert.False(t, HasIssueLabel(db.DefaultContext, 1, 2)) |
|
||||||
assert.False(t, HasIssueLabel(db.DefaultContext, unittest.NonexistentID, unittest.NonexistentID)) |
|
||||||
} |
|
||||||
|
|
||||||
func TestNewIssueLabel(t *testing.T) { |
|
||||||
assert.NoError(t, unittest.PrepareTestDatabase()) |
|
||||||
label := unittest.AssertExistsAndLoadBean(t, &Label{ID: 2}).(*Label) |
|
||||||
issue := unittest.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue) |
|
||||||
doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}).(*user_model.User) |
|
||||||
|
|
||||||
// add new IssueLabel
|
|
||||||
prevNumIssues := label.NumIssues |
|
||||||
assert.NoError(t, NewIssueLabel(issue, label, doer)) |
|
||||||
unittest.AssertExistsAndLoadBean(t, &IssueLabel{IssueID: issue.ID, LabelID: label.ID}) |
|
||||||
unittest.AssertExistsAndLoadBean(t, &Comment{ |
|
||||||
Type: CommentTypeLabel, |
|
||||||
PosterID: doer.ID, |
|
||||||
IssueID: issue.ID, |
|
||||||
LabelID: label.ID, |
|
||||||
Content: "1", |
|
||||||
}) |
|
||||||
label = unittest.AssertExistsAndLoadBean(t, &Label{ID: 2}).(*Label) |
|
||||||
assert.EqualValues(t, prevNumIssues+1, label.NumIssues) |
|
||||||
|
|
||||||
// re-add existing IssueLabel
|
|
||||||
assert.NoError(t, NewIssueLabel(issue, label, doer)) |
|
||||||
unittest.CheckConsistencyFor(t, &Issue{}, &Label{}) |
|
||||||
} |
|
||||||
|
|
||||||
func TestNewIssueLabels(t *testing.T) { |
|
||||||
assert.NoError(t, unittest.PrepareTestDatabase()) |
|
||||||
label1 := unittest.AssertExistsAndLoadBean(t, &Label{ID: 1}).(*Label) |
|
||||||
label2 := unittest.AssertExistsAndLoadBean(t, &Label{ID: 2}).(*Label) |
|
||||||
issue := unittest.AssertExistsAndLoadBean(t, &Issue{ID: 5}).(*Issue) |
|
||||||
doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}).(*user_model.User) |
|
||||||
|
|
||||||
assert.NoError(t, NewIssueLabels(issue, []*Label{label1, label2}, doer)) |
|
||||||
unittest.AssertExistsAndLoadBean(t, &IssueLabel{IssueID: issue.ID, LabelID: label1.ID}) |
|
||||||
unittest.AssertExistsAndLoadBean(t, &Comment{ |
|
||||||
Type: CommentTypeLabel, |
|
||||||
PosterID: doer.ID, |
|
||||||
IssueID: issue.ID, |
|
||||||
LabelID: label1.ID, |
|
||||||
Content: "1", |
|
||||||
}) |
|
||||||
unittest.AssertExistsAndLoadBean(t, &IssueLabel{IssueID: issue.ID, LabelID: label1.ID}) |
|
||||||
label1 = unittest.AssertExistsAndLoadBean(t, &Label{ID: 1}).(*Label) |
|
||||||
assert.EqualValues(t, 3, label1.NumIssues) |
|
||||||
assert.EqualValues(t, 1, label1.NumClosedIssues) |
|
||||||
label2 = unittest.AssertExistsAndLoadBean(t, &Label{ID: 2}).(*Label) |
|
||||||
assert.EqualValues(t, 1, label2.NumIssues) |
|
||||||
assert.EqualValues(t, 1, label2.NumClosedIssues) |
|
||||||
|
|
||||||
// corner case: test empty slice
|
|
||||||
assert.NoError(t, NewIssueLabels(issue, []*Label{}, doer)) |
|
||||||
|
|
||||||
unittest.CheckConsistencyFor(t, &Issue{}, &Label{}) |
|
||||||
} |
|
||||||
|
|
||||||
func TestDeleteIssueLabel(t *testing.T) { |
|
||||||
assert.NoError(t, unittest.PrepareTestDatabase()) |
|
||||||
testSuccess := func(labelID, issueID, doerID int64) { |
|
||||||
label := unittest.AssertExistsAndLoadBean(t, &Label{ID: labelID}).(*Label) |
|
||||||
issue := unittest.AssertExistsAndLoadBean(t, &Issue{ID: issueID}).(*Issue) |
|
||||||
doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: doerID}).(*user_model.User) |
|
||||||
|
|
||||||
expectedNumIssues := label.NumIssues |
|
||||||
expectedNumClosedIssues := label.NumClosedIssues |
|
||||||
if unittest.BeanExists(t, &IssueLabel{IssueID: issueID, LabelID: labelID}) { |
|
||||||
expectedNumIssues-- |
|
||||||
if issue.IsClosed { |
|
||||||
expectedNumClosedIssues-- |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
ctx, committer, err := db.TxContext() |
|
||||||
defer committer.Close() |
|
||||||
assert.NoError(t, err) |
|
||||||
assert.NoError(t, DeleteIssueLabel(ctx, issue, label, doer)) |
|
||||||
assert.NoError(t, committer.Commit()) |
|
||||||
|
|
||||||
unittest.AssertNotExistsBean(t, &IssueLabel{IssueID: issueID, LabelID: labelID}) |
|
||||||
unittest.AssertExistsAndLoadBean(t, &Comment{ |
|
||||||
Type: CommentTypeLabel, |
|
||||||
PosterID: doerID, |
|
||||||
IssueID: issueID, |
|
||||||
LabelID: labelID, |
|
||||||
}, `content=""`) |
|
||||||
label = unittest.AssertExistsAndLoadBean(t, &Label{ID: labelID}).(*Label) |
|
||||||
assert.EqualValues(t, expectedNumIssues, label.NumIssues) |
|
||||||
assert.EqualValues(t, expectedNumClosedIssues, label.NumClosedIssues) |
|
||||||
} |
|
||||||
testSuccess(1, 1, 2) |
|
||||||
testSuccess(2, 5, 2) |
|
||||||
testSuccess(1, 1, 2) // delete non-existent IssueLabel
|
|
||||||
|
|
||||||
unittest.CheckConsistencyFor(t, &Issue{}, &Label{}) |
|
||||||
} |
|
@ -1,78 +0,0 @@ |
|||||||
// Copyright 2020 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" |
|
||||||
user_model "code.gitea.io/gitea/models/user" |
|
||||||
"code.gitea.io/gitea/modules/timeutil" |
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert" |
|
||||||
) |
|
||||||
|
|
||||||
func TestCancelStopwatch(t *testing.T) { |
|
||||||
assert.NoError(t, unittest.PrepareTestDatabase()) |
|
||||||
|
|
||||||
user1, err := user_model.GetUserByID(1) |
|
||||||
assert.NoError(t, err) |
|
||||||
|
|
||||||
issue1, err := GetIssueByID(1) |
|
||||||
assert.NoError(t, err) |
|
||||||
issue2, err := GetIssueByID(2) |
|
||||||
assert.NoError(t, err) |
|
||||||
|
|
||||||
err = CancelStopwatch(user1, issue1) |
|
||||||
assert.NoError(t, err) |
|
||||||
unittest.AssertNotExistsBean(t, &Stopwatch{UserID: user1.ID, IssueID: issue1.ID}) |
|
||||||
|
|
||||||
_ = unittest.AssertExistsAndLoadBean(t, &Comment{Type: CommentTypeCancelTracking, PosterID: user1.ID, IssueID: issue1.ID}) |
|
||||||
|
|
||||||
assert.Nil(t, CancelStopwatch(user1, issue2)) |
|
||||||
} |
|
||||||
|
|
||||||
func TestStopwatchExists(t *testing.T) { |
|
||||||
assert.NoError(t, unittest.PrepareTestDatabase()) |
|
||||||
|
|
||||||
assert.True(t, StopwatchExists(1, 1)) |
|
||||||
assert.False(t, StopwatchExists(1, 2)) |
|
||||||
} |
|
||||||
|
|
||||||
func TestHasUserStopwatch(t *testing.T) { |
|
||||||
assert.NoError(t, unittest.PrepareTestDatabase()) |
|
||||||
|
|
||||||
exists, sw, err := HasUserStopwatch(db.DefaultContext, 1) |
|
||||||
assert.NoError(t, err) |
|
||||||
assert.True(t, exists) |
|
||||||
assert.Equal(t, int64(1), sw.ID) |
|
||||||
|
|
||||||
exists, _, err = HasUserStopwatch(db.DefaultContext, 3) |
|
||||||
assert.NoError(t, err) |
|
||||||
assert.False(t, exists) |
|
||||||
} |
|
||||||
|
|
||||||
func TestCreateOrStopIssueStopwatch(t *testing.T) { |
|
||||||
assert.NoError(t, unittest.PrepareTestDatabase()) |
|
||||||
|
|
||||||
user2, err := user_model.GetUserByID(2) |
|
||||||
assert.NoError(t, err) |
|
||||||
user3, err := user_model.GetUserByID(3) |
|
||||||
assert.NoError(t, err) |
|
||||||
|
|
||||||
issue1, err := GetIssueByID(1) |
|
||||||
assert.NoError(t, err) |
|
||||||
issue2, err := GetIssueByID(2) |
|
||||||
assert.NoError(t, err) |
|
||||||
|
|
||||||
assert.NoError(t, CreateOrStopIssueStopwatch(user3, issue1)) |
|
||||||
sw := unittest.AssertExistsAndLoadBean(t, &Stopwatch{UserID: 3, IssueID: 1}).(*Stopwatch) |
|
||||||
assert.LessOrEqual(t, sw.CreatedUnix, timeutil.TimeStampNow()) |
|
||||||
|
|
||||||
assert.NoError(t, CreateOrStopIssueStopwatch(user2, issue2)) |
|
||||||
unittest.AssertNotExistsBean(t, &Stopwatch{UserID: 2, IssueID: 2}) |
|
||||||
unittest.AssertExistsAndLoadBean(t, &TrackedTime{UserID: 2, IssueID: 2}) |
|
||||||
} |
|
@ -1,61 +0,0 @@ |
|||||||
// Copyright 2017 The Gogs 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" |
|
||||||
repo_model "code.gitea.io/gitea/models/repo" |
|
||||||
"code.gitea.io/gitea/models/unittest" |
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert" |
|
||||||
) |
|
||||||
|
|
||||||
func Test_newIssueUsers(t *testing.T) { |
|
||||||
assert.NoError(t, unittest.PrepareTestDatabase()) |
|
||||||
|
|
||||||
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}).(*repo_model.Repository) |
|
||||||
newIssue := &Issue{ |
|
||||||
RepoID: repo.ID, |
|
||||||
PosterID: 4, |
|
||||||
Index: 6, |
|
||||||
Title: "newTestIssueTitle", |
|
||||||
Content: "newTestIssueContent", |
|
||||||
} |
|
||||||
|
|
||||||
// artificially insert new issue
|
|
||||||
unittest.AssertSuccessfulInsert(t, newIssue) |
|
||||||
|
|
||||||
assert.NoError(t, newIssueUsers(db.DefaultContext, repo, newIssue)) |
|
||||||
|
|
||||||
// issue_user table should now have entries for new issue
|
|
||||||
unittest.AssertExistsAndLoadBean(t, &IssueUser{IssueID: newIssue.ID, UID: newIssue.PosterID}) |
|
||||||
unittest.AssertExistsAndLoadBean(t, &IssueUser{IssueID: newIssue.ID, UID: repo.OwnerID}) |
|
||||||
} |
|
||||||
|
|
||||||
func TestUpdateIssueUserByRead(t *testing.T) { |
|
||||||
assert.NoError(t, unittest.PrepareTestDatabase()) |
|
||||||
issue := unittest.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue) |
|
||||||
|
|
||||||
assert.NoError(t, UpdateIssueUserByRead(4, issue.ID)) |
|
||||||
unittest.AssertExistsAndLoadBean(t, &IssueUser{IssueID: issue.ID, UID: 4}, "is_read=1") |
|
||||||
|
|
||||||
assert.NoError(t, UpdateIssueUserByRead(4, issue.ID)) |
|
||||||
unittest.AssertExistsAndLoadBean(t, &IssueUser{IssueID: issue.ID, UID: 4}, "is_read=1") |
|
||||||
|
|
||||||
assert.NoError(t, UpdateIssueUserByRead(unittest.NonexistentID, unittest.NonexistentID)) |
|
||||||
} |
|
||||||
|
|
||||||
func TestUpdateIssueUsersByMentions(t *testing.T) { |
|
||||||
assert.NoError(t, unittest.PrepareTestDatabase()) |
|
||||||
issue := unittest.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue) |
|
||||||
|
|
||||||
uids := []int64{2, 5} |
|
||||||
assert.NoError(t, UpdateIssueUsersByMentions(db.DefaultContext, issue.ID, uids)) |
|
||||||
for _, uid := range uids { |
|
||||||
unittest.AssertExistsAndLoadBean(t, &IssueUser{IssueID: issue.ID, UID: uid}, "is_mentioned=1") |
|
||||||
} |
|
||||||
} |
|
@ -0,0 +1,32 @@ |
|||||||
|
// 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.
|
||||||
|
|
||||||
|
package issues |
||||||
|
|
||||||
|
import "code.gitea.io/gitea/models/db" |
||||||
|
|
||||||
|
// RecalculateIssueIndexForRepo create issue_index for repo if not exist and
|
||||||
|
// update it based on highest index of existing issues assigned to a repo
|
||||||
|
func RecalculateIssueIndexForRepo(repoID int64) error { |
||||||
|
ctx, committer, err := db.TxContext() |
||||||
|
if err != nil { |
||||||
|
return err |
||||||
|
} |
||||||
|
defer committer.Close() |
||||||
|
|
||||||
|
if err := db.UpsertResourceIndex(ctx, "issue_index", repoID); err != nil { |
||||||
|
return err |
||||||
|
} |
||||||
|
|
||||||
|
var max int64 |
||||||
|
if _, err := db.GetEngine(ctx).Select(" MAX(`index`)").Table("issue").Where("repo_id=?", repoID).Get(&max); err != nil { |
||||||
|
return err |
||||||
|
} |
||||||
|
|
||||||
|
if _, err := db.GetEngine(ctx).Exec("UPDATE `issue_index` SET max_index=? WHERE group_id=?", max, repoID); err != nil { |
||||||
|
return err |
||||||
|
} |
||||||
|
|
||||||
|
return committer.Commit() |
||||||
|
} |
@ -0,0 +1,62 @@ |
|||||||
|
// Copyright 2017 The Gogs 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 issues_test |
||||||
|
|
||||||
|
import ( |
||||||
|
"testing" |
||||||
|
|
||||||
|
"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" |
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert" |
||||||
|
) |
||||||
|
|
||||||
|
func Test_NewIssueUsers(t *testing.T) { |
||||||
|
assert.NoError(t, unittest.PrepareTestDatabase()) |
||||||
|
|
||||||
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}).(*repo_model.Repository) |
||||||
|
newIssue := &issues_model.Issue{ |
||||||
|
RepoID: repo.ID, |
||||||
|
PosterID: 4, |
||||||
|
Index: 6, |
||||||
|
Title: "newTestIssueTitle", |
||||||
|
Content: "newTestIssueContent", |
||||||
|
} |
||||||
|
|
||||||
|
// artificially insert new issue
|
||||||
|
unittest.AssertSuccessfulInsert(t, newIssue) |
||||||
|
|
||||||
|
assert.NoError(t, issues_model.NewIssueUsers(db.DefaultContext, repo, newIssue)) |
||||||
|
|
||||||
|
// issue_user table should now have entries for new issue
|
||||||
|
unittest.AssertExistsAndLoadBean(t, &issues_model.IssueUser{IssueID: newIssue.ID, UID: newIssue.PosterID}) |
||||||
|
unittest.AssertExistsAndLoadBean(t, &issues_model.IssueUser{IssueID: newIssue.ID, UID: repo.OwnerID}) |
||||||
|
} |
||||||
|
|
||||||
|
func TestUpdateIssueUserByRead(t *testing.T) { |
||||||
|
assert.NoError(t, unittest.PrepareTestDatabase()) |
||||||
|
issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 1}).(*issues_model.Issue) |
||||||
|
|
||||||
|
assert.NoError(t, issues_model.UpdateIssueUserByRead(4, issue.ID)) |
||||||
|
unittest.AssertExistsAndLoadBean(t, &issues_model.IssueUser{IssueID: issue.ID, UID: 4}, "is_read=1") |
||||||
|
|
||||||
|
assert.NoError(t, issues_model.UpdateIssueUserByRead(4, issue.ID)) |
||||||
|
unittest.AssertExistsAndLoadBean(t, &issues_model.IssueUser{IssueID: issue.ID, UID: 4}, "is_read=1") |
||||||
|
|
||||||
|
assert.NoError(t, issues_model.UpdateIssueUserByRead(unittest.NonexistentID, unittest.NonexistentID)) |
||||||
|
} |
||||||
|
|
||||||
|
func TestUpdateIssueUsersByMentions(t *testing.T) { |
||||||
|
assert.NoError(t, unittest.PrepareTestDatabase()) |
||||||
|
issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 1}).(*issues_model.Issue) |
||||||
|
|
||||||
|
uids := []int64{2, 5} |
||||||
|
assert.NoError(t, issues_model.UpdateIssueUsersByMentions(db.DefaultContext, issue.ID, uids)) |
||||||
|
for _, uid := range uids { |
||||||
|
unittest.AssertExistsAndLoadBean(t, &issues_model.IssueUser{IssueID: issue.ID, UID: uid}, "is_mentioned=1") |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,395 @@ |
|||||||
|
// 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.
|
||||||
|
|
||||||
|
package issues_test |
||||||
|
|
||||||
|
import ( |
||||||
|
"html/template" |
||||||
|
"testing" |
||||||
|
|
||||||
|
"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" |
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert" |
||||||
|
) |
||||||
|
|
||||||
|
// TODO TestGetLabelTemplateFile
|
||||||
|
|
||||||
|
func TestLabel_CalOpenIssues(t *testing.T) { |
||||||
|
assert.NoError(t, unittest.PrepareTestDatabase()) |
||||||
|
label := unittest.AssertExistsAndLoadBean(t, &issues_model.Label{ID: 1}).(*issues_model.Label) |
||||||
|
label.CalOpenIssues() |
||||||
|
assert.EqualValues(t, 2, label.NumOpenIssues) |
||||||
|
} |
||||||
|
|
||||||
|
func TestLabel_ForegroundColor(t *testing.T) { |
||||||
|
assert.NoError(t, unittest.PrepareTestDatabase()) |
||||||
|
label := unittest.AssertExistsAndLoadBean(t, &issues_model.Label{ID: 1}).(*issues_model.Label) |
||||||
|
assert.Equal(t, template.CSS("#000"), label.ForegroundColor()) |
||||||
|
|
||||||
|
label = unittest.AssertExistsAndLoadBean(t, &issues_model.Label{ID: 2}).(*issues_model.Label) |
||||||
|
assert.Equal(t, template.CSS("#fff"), label.ForegroundColor()) |
||||||
|
} |
||||||
|
|
||||||
|
func TestNewLabels(t *testing.T) { |
||||||
|
assert.NoError(t, unittest.PrepareTestDatabase()) |
||||||
|
labels := []*issues_model.Label{ |
||||||
|
{RepoID: 2, Name: "labelName2", Color: "#123456"}, |
||||||
|
{RepoID: 3, Name: "labelName3", Color: "#123"}, |
||||||
|
{RepoID: 4, Name: "labelName4", Color: "ABCDEF"}, |
||||||
|
{RepoID: 5, Name: "labelName5", Color: "DEF"}, |
||||||
|
} |
||||||
|
assert.Error(t, issues_model.NewLabel(db.DefaultContext, &issues_model.Label{RepoID: 3, Name: "invalid Color", Color: ""})) |
||||||
|
assert.Error(t, issues_model.NewLabel(db.DefaultContext, &issues_model.Label{RepoID: 3, Name: "invalid Color", Color: "#45G"})) |
||||||
|
assert.Error(t, issues_model.NewLabel(db.DefaultContext, &issues_model.Label{RepoID: 3, Name: "invalid Color", Color: "#12345G"})) |
||||||
|
assert.Error(t, issues_model.NewLabel(db.DefaultContext, &issues_model.Label{RepoID: 3, Name: "invalid Color", Color: "45G"})) |
||||||
|
assert.Error(t, issues_model.NewLabel(db.DefaultContext, &issues_model.Label{RepoID: 3, Name: "invalid Color", Color: "12345G"})) |
||||||
|
for _, label := range labels { |
||||||
|
unittest.AssertNotExistsBean(t, label) |
||||||
|
} |
||||||
|
assert.NoError(t, issues_model.NewLabels(labels...)) |
||||||
|
for _, label := range labels { |
||||||
|
unittest.AssertExistsAndLoadBean(t, label, unittest.Cond("id = ?", label.ID)) |
||||||
|
} |
||||||
|
unittest.CheckConsistencyFor(t, &issues_model.Label{}, &repo_model.Repository{}) |
||||||
|
} |
||||||
|
|
||||||
|
func TestGetLabelByID(t *testing.T) { |
||||||
|
assert.NoError(t, unittest.PrepareTestDatabase()) |
||||||
|
label, err := issues_model.GetLabelByID(db.DefaultContext, 1) |
||||||
|
assert.NoError(t, err) |
||||||
|
assert.EqualValues(t, 1, label.ID) |
||||||
|
|
||||||
|
_, err = issues_model.GetLabelByID(db.DefaultContext, unittest.NonexistentID) |
||||||
|
assert.True(t, issues_model.IsErrLabelNotExist(err)) |
||||||
|
} |
||||||
|
|
||||||
|
func TestGetLabelInRepoByName(t *testing.T) { |
||||||
|
assert.NoError(t, unittest.PrepareTestDatabase()) |
||||||
|
label, err := issues_model.GetLabelInRepoByName(db.DefaultContext, 1, "label1") |
||||||
|
assert.NoError(t, err) |
||||||
|
assert.EqualValues(t, 1, label.ID) |
||||||
|
assert.Equal(t, "label1", label.Name) |
||||||
|
|
||||||
|
_, err = issues_model.GetLabelInRepoByName(db.DefaultContext, 1, "") |
||||||
|
assert.True(t, issues_model.IsErrRepoLabelNotExist(err)) |
||||||
|
|
||||||
|
_, err = issues_model.GetLabelInRepoByName(db.DefaultContext, unittest.NonexistentID, "nonexistent") |
||||||
|
assert.True(t, issues_model.IsErrRepoLabelNotExist(err)) |
||||||
|
} |
||||||
|
|
||||||
|
func TestGetLabelInRepoByNames(t *testing.T) { |
||||||
|
assert.NoError(t, unittest.PrepareTestDatabase()) |
||||||
|
labelIDs, err := issues_model.GetLabelIDsInRepoByNames(1, []string{"label1", "label2"}) |
||||||
|
assert.NoError(t, err) |
||||||
|
|
||||||
|
assert.Len(t, labelIDs, 2) |
||||||
|
|
||||||
|
assert.Equal(t, int64(1), labelIDs[0]) |
||||||
|
assert.Equal(t, int64(2), labelIDs[1]) |
||||||
|
} |
||||||
|
|
||||||
|
func TestGetLabelInRepoByNamesDiscardsNonExistentLabels(t *testing.T) { |
||||||
|
assert.NoError(t, unittest.PrepareTestDatabase()) |
||||||
|
// label3 doesn't exists.. See labels.yml
|
||||||
|
labelIDs, err := issues_model.GetLabelIDsInRepoByNames(1, []string{"label1", "label2", "label3"}) |
||||||
|
assert.NoError(t, err) |
||||||
|
|
||||||
|
assert.Len(t, labelIDs, 2) |
||||||
|
|
||||||
|
assert.Equal(t, int64(1), labelIDs[0]) |
||||||
|
assert.Equal(t, int64(2), labelIDs[1]) |
||||||
|
assert.NoError(t, err) |
||||||
|
} |
||||||
|
|
||||||
|
func TestGetLabelInRepoByID(t *testing.T) { |
||||||
|
assert.NoError(t, unittest.PrepareTestDatabase()) |
||||||
|
label, err := issues_model.GetLabelInRepoByID(db.DefaultContext, 1, 1) |
||||||
|
assert.NoError(t, err) |
||||||
|
assert.EqualValues(t, 1, label.ID) |
||||||
|
|
||||||
|
_, err = issues_model.GetLabelInRepoByID(db.DefaultContext, 1, -1) |
||||||
|
assert.True(t, issues_model.IsErrRepoLabelNotExist(err)) |
||||||
|
|
||||||
|
_, err = issues_model.GetLabelInRepoByID(db.DefaultContext, unittest.NonexistentID, unittest.NonexistentID) |
||||||
|
assert.True(t, issues_model.IsErrRepoLabelNotExist(err)) |
||||||
|
} |
||||||
|
|
||||||
|
func TestGetLabelsInRepoByIDs(t *testing.T) { |
||||||
|
assert.NoError(t, unittest.PrepareTestDatabase()) |
||||||
|
labels, err := issues_model.GetLabelsInRepoByIDs(1, []int64{1, 2, unittest.NonexistentID}) |
||||||
|
assert.NoError(t, err) |
||||||
|
if assert.Len(t, labels, 2) { |
||||||
|
assert.EqualValues(t, 1, labels[0].ID) |
||||||
|
assert.EqualValues(t, 2, labels[1].ID) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
func TestGetLabelsByRepoID(t *testing.T) { |
||||||
|
assert.NoError(t, unittest.PrepareTestDatabase()) |
||||||
|
testSuccess := func(repoID int64, sortType string, expectedIssueIDs []int64) { |
||||||
|
labels, err := issues_model.GetLabelsByRepoID(db.DefaultContext, repoID, sortType, db.ListOptions{}) |
||||||
|
assert.NoError(t, err) |
||||||
|
assert.Len(t, labels, len(expectedIssueIDs)) |
||||||
|
for i, label := range labels { |
||||||
|
assert.EqualValues(t, expectedIssueIDs[i], label.ID) |
||||||
|
} |
||||||
|
} |
||||||
|
testSuccess(1, "leastissues", []int64{2, 1}) |
||||||
|
testSuccess(1, "mostissues", []int64{1, 2}) |
||||||
|
testSuccess(1, "reversealphabetically", []int64{2, 1}) |
||||||
|
testSuccess(1, "default", []int64{1, 2}) |
||||||
|
} |
||||||
|
|
||||||
|
// Org versions
|
||||||
|
|
||||||
|
func TestGetLabelInOrgByName(t *testing.T) { |
||||||
|
assert.NoError(t, unittest.PrepareTestDatabase()) |
||||||
|
label, err := issues_model.GetLabelInOrgByName(db.DefaultContext, 3, "orglabel3") |
||||||
|
assert.NoError(t, err) |
||||||
|
assert.EqualValues(t, 3, label.ID) |
||||||
|
assert.Equal(t, "orglabel3", label.Name) |
||||||
|
|
||||||
|
_, err = issues_model.GetLabelInOrgByName(db.DefaultContext, 3, "") |
||||||
|
assert.True(t, issues_model.IsErrOrgLabelNotExist(err)) |
||||||
|
|
||||||
|
_, err = issues_model.GetLabelInOrgByName(db.DefaultContext, 0, "orglabel3") |
||||||
|
assert.True(t, issues_model.IsErrOrgLabelNotExist(err)) |
||||||
|
|
||||||
|
_, err = issues_model.GetLabelInOrgByName(db.DefaultContext, -1, "orglabel3") |
||||||
|
assert.True(t, issues_model.IsErrOrgLabelNotExist(err)) |
||||||
|
|
||||||
|
_, err = issues_model.GetLabelInOrgByName(db.DefaultContext, unittest.NonexistentID, "nonexistent") |
||||||
|
assert.True(t, issues_model.IsErrOrgLabelNotExist(err)) |
||||||
|
} |
||||||
|
|
||||||
|
func TestGetLabelInOrgByNames(t *testing.T) { |
||||||
|
assert.NoError(t, unittest.PrepareTestDatabase()) |
||||||
|
labelIDs, err := issues_model.GetLabelIDsInOrgByNames(3, []string{"orglabel3", "orglabel4"}) |
||||||
|
assert.NoError(t, err) |
||||||
|
|
||||||
|
assert.Len(t, labelIDs, 2) |
||||||
|
|
||||||
|
assert.Equal(t, int64(3), labelIDs[0]) |
||||||
|
assert.Equal(t, int64(4), labelIDs[1]) |
||||||
|
} |
||||||
|
|
||||||
|
func TestGetLabelInOrgByNamesDiscardsNonExistentLabels(t *testing.T) { |
||||||
|
assert.NoError(t, unittest.PrepareTestDatabase()) |
||||||
|
// orglabel99 doesn't exists.. See labels.yml
|
||||||
|
labelIDs, err := issues_model.GetLabelIDsInOrgByNames(3, []string{"orglabel3", "orglabel4", "orglabel99"}) |
||||||
|
assert.NoError(t, err) |
||||||
|
|
||||||
|
assert.Len(t, labelIDs, 2) |
||||||
|
|
||||||
|
assert.Equal(t, int64(3), labelIDs[0]) |
||||||
|
assert.Equal(t, int64(4), labelIDs[1]) |
||||||
|
assert.NoError(t, err) |
||||||
|
} |
||||||
|
|
||||||
|
func TestGetLabelInOrgByID(t *testing.T) { |
||||||
|
assert.NoError(t, unittest.PrepareTestDatabase()) |
||||||
|
label, err := issues_model.GetLabelInOrgByID(db.DefaultContext, 3, 3) |
||||||
|
assert.NoError(t, err) |
||||||
|
assert.EqualValues(t, 3, label.ID) |
||||||
|
|
||||||
|
_, err = issues_model.GetLabelInOrgByID(db.DefaultContext, 3, -1) |
||||||
|
assert.True(t, issues_model.IsErrOrgLabelNotExist(err)) |
||||||
|
|
||||||
|
_, err = issues_model.GetLabelInOrgByID(db.DefaultContext, 0, 3) |
||||||
|
assert.True(t, issues_model.IsErrOrgLabelNotExist(err)) |
||||||
|
|
||||||
|
_, err = issues_model.GetLabelInOrgByID(db.DefaultContext, -1, 3) |
||||||
|
assert.True(t, issues_model.IsErrOrgLabelNotExist(err)) |
||||||
|
|
||||||
|
_, err = issues_model.GetLabelInOrgByID(db.DefaultContext, unittest.NonexistentID, unittest.NonexistentID) |
||||||
|
assert.True(t, issues_model.IsErrOrgLabelNotExist(err)) |
||||||
|
} |
||||||
|
|
||||||
|
func TestGetLabelsInOrgByIDs(t *testing.T) { |
||||||
|
assert.NoError(t, unittest.PrepareTestDatabase()) |
||||||
|
labels, err := issues_model.GetLabelsInOrgByIDs(3, []int64{3, 4, unittest.NonexistentID}) |
||||||
|
assert.NoError(t, err) |
||||||
|
if assert.Len(t, labels, 2) { |
||||||
|
assert.EqualValues(t, 3, labels[0].ID) |
||||||
|
assert.EqualValues(t, 4, labels[1].ID) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
func TestGetLabelsByOrgID(t *testing.T) { |
||||||
|
assert.NoError(t, unittest.PrepareTestDatabase()) |
||||||
|
testSuccess := func(orgID int64, sortType string, expectedIssueIDs []int64) { |
||||||
|
labels, err := issues_model.GetLabelsByOrgID(db.DefaultContext, orgID, sortType, db.ListOptions{}) |
||||||
|
assert.NoError(t, err) |
||||||
|
assert.Len(t, labels, len(expectedIssueIDs)) |
||||||
|
for i, label := range labels { |
||||||
|
assert.EqualValues(t, expectedIssueIDs[i], label.ID) |
||||||
|
} |
||||||
|
} |
||||||
|
testSuccess(3, "leastissues", []int64{3, 4}) |
||||||
|
testSuccess(3, "mostissues", []int64{4, 3}) |
||||||
|
testSuccess(3, "reversealphabetically", []int64{4, 3}) |
||||||
|
testSuccess(3, "default", []int64{3, 4}) |
||||||
|
|
||||||
|
var err error |
||||||
|
_, err = issues_model.GetLabelsByOrgID(db.DefaultContext, 0, "leastissues", db.ListOptions{}) |
||||||
|
assert.True(t, issues_model.IsErrOrgLabelNotExist(err)) |
||||||
|
|
||||||
|
_, err = issues_model.GetLabelsByOrgID(db.DefaultContext, -1, "leastissues", db.ListOptions{}) |
||||||
|
assert.True(t, issues_model.IsErrOrgLabelNotExist(err)) |
||||||
|
} |
||||||
|
|
||||||
|
//
|
||||||
|
|
||||||
|
func TestGetLabelsByIssueID(t *testing.T) { |
||||||
|
assert.NoError(t, unittest.PrepareTestDatabase()) |
||||||
|
labels, err := issues_model.GetLabelsByIssueID(db.DefaultContext, 1) |
||||||
|
assert.NoError(t, err) |
||||||
|
if assert.Len(t, labels, 1) { |
||||||
|
assert.EqualValues(t, 1, labels[0].ID) |
||||||
|
} |
||||||
|
|
||||||
|
labels, err = issues_model.GetLabelsByIssueID(db.DefaultContext, unittest.NonexistentID) |
||||||
|
assert.NoError(t, err) |
||||||
|
assert.Len(t, labels, 0) |
||||||
|
} |
||||||
|
|
||||||
|
func TestUpdateLabel(t *testing.T) { |
||||||
|
assert.NoError(t, unittest.PrepareTestDatabase()) |
||||||
|
label := unittest.AssertExistsAndLoadBean(t, &issues_model.Label{ID: 1}).(*issues_model.Label) |
||||||
|
// make sure update wont overwrite it
|
||||||
|
update := &issues_model.Label{ |
||||||
|
ID: label.ID, |
||||||
|
Color: "#ffff00", |
||||||
|
Name: "newLabelName", |
||||||
|
Description: label.Description, |
||||||
|
} |
||||||
|
label.Color = update.Color |
||||||
|
label.Name = update.Name |
||||||
|
assert.NoError(t, issues_model.UpdateLabel(update)) |
||||||
|
newLabel := unittest.AssertExistsAndLoadBean(t, &issues_model.Label{ID: 1}).(*issues_model.Label) |
||||||
|
assert.EqualValues(t, label.ID, newLabel.ID) |
||||||
|
assert.EqualValues(t, label.Color, newLabel.Color) |
||||||
|
assert.EqualValues(t, label.Name, newLabel.Name) |
||||||
|
assert.EqualValues(t, label.Description, newLabel.Description) |
||||||
|
unittest.CheckConsistencyFor(t, &issues_model.Label{}, &repo_model.Repository{}) |
||||||
|
} |
||||||
|
|
||||||
|
func TestDeleteLabel(t *testing.T) { |
||||||
|
assert.NoError(t, unittest.PrepareTestDatabase()) |
||||||
|
label := unittest.AssertExistsAndLoadBean(t, &issues_model.Label{ID: 1}).(*issues_model.Label) |
||||||
|
assert.NoError(t, issues_model.DeleteLabel(label.RepoID, label.ID)) |
||||||
|
unittest.AssertNotExistsBean(t, &issues_model.Label{ID: label.ID, RepoID: label.RepoID}) |
||||||
|
|
||||||
|
assert.NoError(t, issues_model.DeleteLabel(label.RepoID, label.ID)) |
||||||
|
unittest.AssertNotExistsBean(t, &issues_model.Label{ID: label.ID}) |
||||||
|
|
||||||
|
assert.NoError(t, issues_model.DeleteLabel(unittest.NonexistentID, unittest.NonexistentID)) |
||||||
|
unittest.CheckConsistencyFor(t, &issues_model.Label{}, &repo_model.Repository{}) |
||||||
|
} |
||||||
|
|
||||||
|
func TestHasIssueLabel(t *testing.T) { |
||||||
|
assert.NoError(t, unittest.PrepareTestDatabase()) |
||||||
|
assert.True(t, issues_model.HasIssueLabel(db.DefaultContext, 1, 1)) |
||||||
|
assert.False(t, issues_model.HasIssueLabel(db.DefaultContext, 1, 2)) |
||||||
|
assert.False(t, issues_model.HasIssueLabel(db.DefaultContext, unittest.NonexistentID, unittest.NonexistentID)) |
||||||
|
} |
||||||
|
|
||||||
|
func TestNewIssueLabel(t *testing.T) { |
||||||
|
assert.NoError(t, unittest.PrepareTestDatabase()) |
||||||
|
label := unittest.AssertExistsAndLoadBean(t, &issues_model.Label{ID: 2}).(*issues_model.Label) |
||||||
|
issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 1}).(*issues_model.Issue) |
||||||
|
doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}).(*user_model.User) |
||||||
|
|
||||||
|
// add new IssueLabel
|
||||||
|
prevNumIssues := label.NumIssues |
||||||
|
assert.NoError(t, issues_model.NewIssueLabel(issue, label, doer)) |
||||||
|
unittest.AssertExistsAndLoadBean(t, &issues_model.IssueLabel{IssueID: issue.ID, LabelID: label.ID}) |
||||||
|
unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{ |
||||||
|
Type: issues_model.CommentTypeLabel, |
||||||
|
PosterID: doer.ID, |
||||||
|
IssueID: issue.ID, |
||||||
|
LabelID: label.ID, |
||||||
|
Content: "1", |
||||||
|
}) |
||||||
|
label = unittest.AssertExistsAndLoadBean(t, &issues_model.Label{ID: 2}).(*issues_model.Label) |
||||||
|
assert.EqualValues(t, prevNumIssues+1, label.NumIssues) |
||||||
|
|
||||||
|
// re-add existing IssueLabel
|
||||||
|
assert.NoError(t, issues_model.NewIssueLabel(issue, label, doer)) |
||||||
|
unittest.CheckConsistencyFor(t, &issues_model.Issue{}, &issues_model.Label{}) |
||||||
|
} |
||||||
|
|
||||||
|
func TestNewIssueLabels(t *testing.T) { |
||||||
|
assert.NoError(t, unittest.PrepareTestDatabase()) |
||||||
|
label1 := unittest.AssertExistsAndLoadBean(t, &issues_model.Label{ID: 1}).(*issues_model.Label) |
||||||
|
label2 := unittest.AssertExistsAndLoadBean(t, &issues_model.Label{ID: 2}).(*issues_model.Label) |
||||||
|
issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 5}).(*issues_model.Issue) |
||||||
|
doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}).(*user_model.User) |
||||||
|
|
||||||
|
assert.NoError(t, issues_model.NewIssueLabels(issue, []*issues_model.Label{label1, label2}, doer)) |
||||||
|
unittest.AssertExistsAndLoadBean(t, &issues_model.IssueLabel{IssueID: issue.ID, LabelID: label1.ID}) |
||||||
|
unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{ |
||||||
|
Type: issues_model.CommentTypeLabel, |
||||||
|
PosterID: doer.ID, |
||||||
|
IssueID: issue.ID, |
||||||
|
LabelID: label1.ID, |
||||||
|
Content: "1", |
||||||
|
}) |
||||||
|
unittest.AssertExistsAndLoadBean(t, &issues_model.IssueLabel{IssueID: issue.ID, LabelID: label1.ID}) |
||||||
|
label1 = unittest.AssertExistsAndLoadBean(t, &issues_model.Label{ID: 1}).(*issues_model.Label) |
||||||
|
assert.EqualValues(t, 3, label1.NumIssues) |
||||||
|
assert.EqualValues(t, 1, label1.NumClosedIssues) |
||||||
|
label2 = unittest.AssertExistsAndLoadBean(t, &issues_model.Label{ID: 2}).(*issues_model.Label) |
||||||
|
assert.EqualValues(t, 1, label2.NumIssues) |
||||||
|
assert.EqualValues(t, 1, label2.NumClosedIssues) |
||||||
|
|
||||||
|
// corner case: test empty slice
|
||||||
|
assert.NoError(t, issues_model.NewIssueLabels(issue, []*issues_model.Label{}, doer)) |
||||||
|
|
||||||
|
unittest.CheckConsistencyFor(t, &issues_model.Issue{}, &issues_model.Label{}) |
||||||
|
} |
||||||
|
|
||||||
|
func TestDeleteIssueLabel(t *testing.T) { |
||||||
|
assert.NoError(t, unittest.PrepareTestDatabase()) |
||||||
|
testSuccess := func(labelID, issueID, doerID int64) { |
||||||
|
label := unittest.AssertExistsAndLoadBean(t, &issues_model.Label{ID: labelID}).(*issues_model.Label) |
||||||
|
issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: issueID}).(*issues_model.Issue) |
||||||
|
doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: doerID}).(*user_model.User) |
||||||
|
|
||||||
|
expectedNumIssues := label.NumIssues |
||||||
|
expectedNumClosedIssues := label.NumClosedIssues |
||||||
|
if unittest.BeanExists(t, &issues_model.IssueLabel{IssueID: issueID, LabelID: labelID}) { |
||||||
|
expectedNumIssues-- |
||||||
|
if issue.IsClosed { |
||||||
|
expectedNumClosedIssues-- |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
ctx, committer, err := db.TxContext() |
||||||
|
defer committer.Close() |
||||||
|
assert.NoError(t, err) |
||||||
|
assert.NoError(t, issues_model.DeleteIssueLabel(ctx, issue, label, doer)) |
||||||
|
assert.NoError(t, committer.Commit()) |
||||||
|
|
||||||
|
unittest.AssertNotExistsBean(t, &issues_model.IssueLabel{IssueID: issueID, LabelID: labelID}) |
||||||
|
unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{ |
||||||
|
Type: issues_model.CommentTypeLabel, |
||||||
|
PosterID: doerID, |
||||||
|
IssueID: issueID, |
||||||
|
LabelID: labelID, |
||||||
|
}, `content=""`) |
||||||
|
label = unittest.AssertExistsAndLoadBean(t, &issues_model.Label{ID: labelID}).(*issues_model.Label) |
||||||
|
assert.EqualValues(t, expectedNumIssues, label.NumIssues) |
||||||
|
assert.EqualValues(t, expectedNumClosedIssues, label.NumClosedIssues) |
||||||
|
} |
||||||
|
testSuccess(1, 1, 2) |
||||||
|
testSuccess(2, 5, 2) |
||||||
|
testSuccess(1, 1, 2) // delete non-existent IssueLabel
|
||||||
|
|
||||||
|
unittest.CheckConsistencyFor(t, &issues_model.Issue{}, &issues_model.Label{}) |
||||||
|
} |
@ -0,0 +1,79 @@ |
|||||||
|
// Copyright 2020 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 issues_test |
||||||
|
|
||||||
|
import ( |
||||||
|
"testing" |
||||||
|
|
||||||
|
"code.gitea.io/gitea/models/db" |
||||||
|
issues_model "code.gitea.io/gitea/models/issues" |
||||||
|
"code.gitea.io/gitea/models/unittest" |
||||||
|
user_model "code.gitea.io/gitea/models/user" |
||||||
|
"code.gitea.io/gitea/modules/timeutil" |
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert" |
||||||
|
) |
||||||
|
|
||||||
|
func TestCancelStopwatch(t *testing.T) { |
||||||
|
assert.NoError(t, unittest.PrepareTestDatabase()) |
||||||
|
|
||||||
|
user1, err := user_model.GetUserByID(1) |
||||||
|
assert.NoError(t, err) |
||||||
|
|
||||||
|
issue1, err := issues_model.GetIssueByID(db.DefaultContext, 1) |
||||||
|
assert.NoError(t, err) |
||||||
|
issue2, err := issues_model.GetIssueByID(db.DefaultContext, 2) |
||||||
|
assert.NoError(t, err) |
||||||
|
|
||||||
|
err = issues_model.CancelStopwatch(user1, issue1) |
||||||
|
assert.NoError(t, err) |
||||||
|
unittest.AssertNotExistsBean(t, &issues_model.Stopwatch{UserID: user1.ID, IssueID: issue1.ID}) |
||||||
|
|
||||||
|
_ = unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{Type: issues_model.CommentTypeCancelTracking, PosterID: user1.ID, IssueID: issue1.ID}) |
||||||
|
|
||||||
|
assert.Nil(t, issues_model.CancelStopwatch(user1, issue2)) |
||||||
|
} |
||||||
|
|
||||||
|
func TestStopwatchExists(t *testing.T) { |
||||||
|
assert.NoError(t, unittest.PrepareTestDatabase()) |
||||||
|
|
||||||
|
assert.True(t, issues_model.StopwatchExists(1, 1)) |
||||||
|
assert.False(t, issues_model.StopwatchExists(1, 2)) |
||||||
|
} |
||||||
|
|
||||||
|
func TestHasUserStopwatch(t *testing.T) { |
||||||
|
assert.NoError(t, unittest.PrepareTestDatabase()) |
||||||
|
|
||||||
|
exists, sw, err := issues_model.HasUserStopwatch(db.DefaultContext, 1) |
||||||
|
assert.NoError(t, err) |
||||||
|
assert.True(t, exists) |
||||||
|
assert.Equal(t, int64(1), sw.ID) |
||||||
|
|
||||||
|
exists, _, err = issues_model.HasUserStopwatch(db.DefaultContext, 3) |
||||||
|
assert.NoError(t, err) |
||||||
|
assert.False(t, exists) |
||||||
|
} |
||||||
|
|
||||||
|
func TestCreateOrStopIssueStopwatch(t *testing.T) { |
||||||
|
assert.NoError(t, unittest.PrepareTestDatabase()) |
||||||
|
|
||||||
|
user2, err := user_model.GetUserByID(2) |
||||||
|
assert.NoError(t, err) |
||||||
|
user3, err := user_model.GetUserByID(3) |
||||||
|
assert.NoError(t, err) |
||||||
|
|
||||||
|
issue1, err := issues_model.GetIssueByID(db.DefaultContext, 1) |
||||||
|
assert.NoError(t, err) |
||||||
|
issue2, err := issues_model.GetIssueByID(db.DefaultContext, 2) |
||||||
|
assert.NoError(t, err) |
||||||
|
|
||||||
|
assert.NoError(t, issues_model.CreateOrStopIssueStopwatch(user3, issue1)) |
||||||
|
sw := unittest.AssertExistsAndLoadBean(t, &issues_model.Stopwatch{UserID: 3, IssueID: 1}).(*issues_model.Stopwatch) |
||||||
|
assert.LessOrEqual(t, sw.CreatedUnix, timeutil.TimeStampNow()) |
||||||
|
|
||||||
|
assert.NoError(t, issues_model.CreateOrStopIssueStopwatch(user2, issue2)) |
||||||
|
unittest.AssertNotExistsBean(t, &issues_model.Stopwatch{UserID: 2, IssueID: 2}) |
||||||
|
unittest.AssertExistsAndLoadBean(t, &issues_model.TrackedTime{UserID: 2, IssueID: 2}) |
||||||
|
} |
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue