Skip to content

Commit 181c8d9

Browse files
ethantkoeniglunny
authored andcommitted
Fix consistency check (#866)
1 parent d2329e1 commit 181c8d9

File tree

4 files changed

+27
-10
lines changed

4 files changed

+27
-10
lines changed

models/consistency_test.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,37 +73,46 @@ func (user *User) CheckForConsistency(t *testing.T) {
7373
assertCount(t, &Team{OrgID: user.ID}, user.NumTeams)
7474
assertCount(t, &Follow{UserID: user.ID}, user.NumFollowing)
7575
assertCount(t, &Follow{FollowID: user.ID}, user.NumFollowers)
76+
if user.Type != UserTypeOrganization {
77+
assert.EqualValues(t, 0, user.NumMembers)
78+
assert.EqualValues(t, 0, user.NumTeams)
79+
}
7680
}
7781

7882
func (repo *Repository) CheckForConsistency(t *testing.T) {
7983
assertCount(t, &Star{RepoID: repo.ID}, repo.NumStars)
8084
assertCount(t, &Watch{RepoID: repo.ID}, repo.NumWatches)
81-
assertCount(t, &Issue{RepoID: repo.ID}, repo.NumIssues)
8285
assertCount(t, &Milestone{RepoID: repo.ID}, repo.NumMilestones)
8386
assertCount(t, &Repository{ForkID: repo.ID}, repo.NumForks)
8487
if repo.IsFork {
8588
AssertExistsAndLoadBean(t, &Repository{ID: repo.ForkID})
8689
}
8790

88-
actual := getCount(t, x.Where("is_closed=1"), &Issue{RepoID: repo.ID})
91+
actual := getCount(t, x.Where("is_pull=?", false), &Issue{RepoID: repo.ID})
92+
assert.EqualValues(t, repo.NumIssues, actual,
93+
"Unexpected number of issues for repo %+v", repo)
94+
95+
actual = getCount(t, x.Where("is_pull=? AND is_closed=?", false, true), &Issue{RepoID: repo.ID})
8996
assert.EqualValues(t, repo.NumClosedIssues, actual,
9097
"Unexpected number of closed issues for repo %+v", repo)
9198

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

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

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

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

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

138147
expected := int64(0)
139148
if len(issueIDs) > 0 {
140-
expected = getCount(t, x.In("id", issueIDs).Where("is_closed=1"), &Issue{})
149+
expected = getCount(t, x.In("id", issueIDs).Where("is_closed=?", true), &Issue{})
141150
}
142151
assert.EqualValues(t, expected, label.NumClosedIssues,
143152
"Unexpected number of closed issues for label %+v", label)

models/fixtures/issue.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
content: content1
99
is_closed: false
1010
is_pull: false
11-
num_comments: 1
11+
num_comments: 0
1212
created_unix: 946684800
1313
updated_unix: 978307200
1414

models/fixtures/repository.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
lower_name: repo1
55
name: repo1
66
is_private: false
7-
num_issues: 4
7+
num_issues: 2
88
num_closed_issues: 1
99
num_pulls: 2
1010
num_closed_pulls: 0

models/issue_label_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ func TestNewLabels(t *testing.T) {
5454
for _, label := range labels {
5555
AssertExistsAndLoadBean(t, label)
5656
}
57+
CheckConsistencyFor(t, &Label{}, &Repository{})
5758
}
5859

5960
func TestGetLabelByID(t *testing.T) {
@@ -138,6 +139,7 @@ func TestUpdateLabel(t *testing.T) {
138139
assert.NoError(t, UpdateLabel(label))
139140
newLabel := AssertExistsAndLoadBean(t, &Label{ID: 1}).(*Label)
140141
assert.Equal(t, *label, *newLabel)
142+
CheckConsistencyFor(t, &Label{}, &Repository{})
141143
}
142144

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

152154
assert.NoError(t, DeleteLabel(NonexistentID, NonexistentID))
155+
CheckConsistencyFor(t, &Label{}, &Repository{})
153156
}
154157

155158
func TestHasIssueLabel(t *testing.T) {
@@ -180,6 +183,7 @@ func TestNewIssueLabel(t *testing.T) {
180183

181184
// re-add existing IssueLabel
182185
assert.NoError(t, NewIssueLabel(issue, label, doer))
186+
CheckConsistencyFor(t, &Issue{}, &Label{})
183187
}
184188

185189
func TestNewIssueLabels(t *testing.T) {
@@ -208,6 +212,8 @@ func TestNewIssueLabels(t *testing.T) {
208212

209213
// corner case: test empty slice
210214
assert.NoError(t, NewIssueLabels(issue, []*Label{}, doer))
215+
216+
CheckConsistencyFor(t, &Issue{}, &Label{})
211217
}
212218

213219
func TestDeleteIssueLabel(t *testing.T) {
@@ -241,4 +247,6 @@ func TestDeleteIssueLabel(t *testing.T) {
241247
testSuccess(1, 1, 2)
242248
testSuccess(2, 5, 2)
243249
testSuccess(1, 1, 2) // delete non-existent IssueLabel
250+
251+
CheckConsistencyFor(t, &Issue{}, &Label{})
244252
}

0 commit comments

Comments
 (0)