Skip to content

Commit

Permalink
Fix consistency check (go-gitea#866)
Browse files Browse the repository at this point in the history
  • Loading branch information
ethantkoenig authored and lunny committed Feb 8, 2017
1 parent d2329e1 commit 181c8d9
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 10 deletions.
25 changes: 17 additions & 8 deletions models/consistency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,37 +73,46 @@ func (user *User) CheckForConsistency(t *testing.T) {
assertCount(t, &Team{OrgID: user.ID}, user.NumTeams)
assertCount(t, &Follow{UserID: user.ID}, user.NumFollowing)
assertCount(t, &Follow{FollowID: user.ID}, user.NumFollowers)
if user.Type != UserTypeOrganization {
assert.EqualValues(t, 0, user.NumMembers)
assert.EqualValues(t, 0, user.NumTeams)
}
}

func (repo *Repository) CheckForConsistency(t *testing.T) {
assertCount(t, &Star{RepoID: repo.ID}, repo.NumStars)
assertCount(t, &Watch{RepoID: repo.ID}, repo.NumWatches)
assertCount(t, &Issue{RepoID: repo.ID}, repo.NumIssues)
assertCount(t, &Milestone{RepoID: repo.ID}, repo.NumMilestones)
assertCount(t, &Repository{ForkID: repo.ID}, repo.NumForks)
if repo.IsFork {
AssertExistsAndLoadBean(t, &Repository{ID: repo.ForkID})
}

actual := getCount(t, x.Where("is_closed=1"), &Issue{RepoID: repo.ID})
actual := getCount(t, x.Where("is_pull=?", false), &Issue{RepoID: repo.ID})
assert.EqualValues(t, repo.NumIssues, actual,
"Unexpected number of issues for repo %+v", repo)

actual = getCount(t, x.Where("is_pull=? AND is_closed=?", false, true), &Issue{RepoID: repo.ID})
assert.EqualValues(t, repo.NumClosedIssues, actual,
"Unexpected number of closed issues for repo %+v", repo)

actual = getCount(t, x.Where("is_pull=1"), &Issue{RepoID: repo.ID})
actual = getCount(t, x.Where("is_pull=?", true), &Issue{RepoID: repo.ID})
assert.EqualValues(t, repo.NumPulls, actual,
"Unexpected number of pulls for repo %+v", repo)

actual = getCount(t, x.Where("is_pull=1 AND is_closed=1"), &Issue{RepoID: repo.ID})
actual = getCount(t, x.Where("is_pull=? AND is_closed=?", true, true), &Issue{RepoID: repo.ID})
assert.EqualValues(t, repo.NumClosedPulls, actual,
"Unexpected number of closed pulls for repo %+v", repo)

actual = getCount(t, x.Where("is_closed=1"), &Milestone{RepoID: repo.ID})
actual = getCount(t, x.Where("is_closed=?", true), &Milestone{RepoID: repo.ID})
assert.EqualValues(t, repo.NumClosedMilestones, actual,
"Unexpected number of closed milestones for repo %+v", repo)
}

func (issue *Issue) CheckForConsistency(t *testing.T) {
assertCount(t, &Comment{IssueID: issue.ID}, issue.NumComments)
actual := getCount(t, x.Where("type=?", CommentTypeComment), &Comment{IssueID: issue.ID})
assert.EqualValues(t, issue.NumComments, actual,
"Unexpected number of comments for issue %+v", issue)
if issue.IsPull {
pr := AssertExistsAndLoadBean(t, &PullRequest{IssueID: issue.ID}).(*PullRequest)
assert.EqualValues(t, pr.Index, issue.Index)
Expand All @@ -119,7 +128,7 @@ func (pr *PullRequest) CheckForConsistency(t *testing.T) {
func (milestone *Milestone) CheckForConsistency(t *testing.T) {
assertCount(t, &Issue{MilestoneID: milestone.ID}, milestone.NumIssues)

actual := getCount(t, x.Where("is_closed=1"), &Issue{MilestoneID: milestone.ID})
actual := getCount(t, x.Where("is_closed=?", true), &Issue{MilestoneID: milestone.ID})
assert.EqualValues(t, milestone.NumClosedIssues, actual,
"Unexpected number of closed issues for milestone %+v", milestone)
}
Expand All @@ -137,7 +146,7 @@ func (label *Label) CheckForConsistency(t *testing.T) {

expected := int64(0)
if len(issueIDs) > 0 {
expected = getCount(t, x.In("id", issueIDs).Where("is_closed=1"), &Issue{})
expected = getCount(t, x.In("id", issueIDs).Where("is_closed=?", true), &Issue{})
}
assert.EqualValues(t, expected, label.NumClosedIssues,
"Unexpected number of closed issues for label %+v", label)
Expand Down
2 changes: 1 addition & 1 deletion models/fixtures/issue.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
content: content1
is_closed: false
is_pull: false
num_comments: 1
num_comments: 0
created_unix: 946684800
updated_unix: 978307200

Expand Down
2 changes: 1 addition & 1 deletion models/fixtures/repository.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
lower_name: repo1
name: repo1
is_private: false
num_issues: 4
num_issues: 2
num_closed_issues: 1
num_pulls: 2
num_closed_pulls: 0
Expand Down
8 changes: 8 additions & 0 deletions models/issue_label_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func TestNewLabels(t *testing.T) {
for _, label := range labels {
AssertExistsAndLoadBean(t, label)
}
CheckConsistencyFor(t, &Label{}, &Repository{})
}

func TestGetLabelByID(t *testing.T) {
Expand Down Expand Up @@ -138,6 +139,7 @@ func TestUpdateLabel(t *testing.T) {
assert.NoError(t, UpdateLabel(label))
newLabel := AssertExistsAndLoadBean(t, &Label{ID: 1}).(*Label)
assert.Equal(t, *label, *newLabel)
CheckConsistencyFor(t, &Label{}, &Repository{})
}

func TestDeleteLabel(t *testing.T) {
Expand All @@ -150,6 +152,7 @@ func TestDeleteLabel(t *testing.T) {
AssertNotExistsBean(t, &Label{ID: label.ID, RepoID: label.RepoID})

assert.NoError(t, DeleteLabel(NonexistentID, NonexistentID))
CheckConsistencyFor(t, &Label{}, &Repository{})
}

func TestHasIssueLabel(t *testing.T) {
Expand Down Expand Up @@ -180,6 +183,7 @@ func TestNewIssueLabel(t *testing.T) {

// re-add existing IssueLabel
assert.NoError(t, NewIssueLabel(issue, label, doer))
CheckConsistencyFor(t, &Issue{}, &Label{})
}

func TestNewIssueLabels(t *testing.T) {
Expand Down Expand Up @@ -208,6 +212,8 @@ func TestNewIssueLabels(t *testing.T) {

// corner case: test empty slice
assert.NoError(t, NewIssueLabels(issue, []*Label{}, doer))

CheckConsistencyFor(t, &Issue{}, &Label{})
}

func TestDeleteIssueLabel(t *testing.T) {
Expand Down Expand Up @@ -241,4 +247,6 @@ func TestDeleteIssueLabel(t *testing.T) {
testSuccess(1, 1, 2)
testSuccess(2, 5, 2)
testSuccess(1, 1, 2) // delete non-existent IssueLabel

CheckConsistencyFor(t, &Issue{}, &Label{})
}

0 comments on commit 181c8d9

Please sign in to comment.