Skip to content

Commit 0db7a32

Browse files
authored
unbind the CheckConsistency for some structs so that they can be moved to sub packages easier (#17612)
* unbind the CheckConsistency for some structs so that they can be moved to sub packages easier * Fix functions name * Fix typo
1 parent 90eb9fb commit 0db7a32

File tree

1 file changed

+33
-20
lines changed

1 file changed

+33
-20
lines changed

models/consistency.go

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,11 @@ import (
1010
"testing"
1111

1212
"code.gitea.io/gitea/models/db"
13+
1314
"github.com/stretchr/testify/assert"
1415
"xorm.io/builder"
1516
)
1617

17-
// consistencyCheckable a type that can be tested for database consistency
18-
type consistencyCheckable interface {
19-
checkForConsistency(t *testing.T)
20-
}
21-
2218
// CheckConsistencyForAll test that the entire database is consistent
2319
func CheckConsistencyForAll(t *testing.T) {
2420
CheckConsistencyFor(t,
@@ -46,17 +42,34 @@ func CheckConsistencyFor(t *testing.T, beansToCheck ...interface{}) {
4642

4743
for i := 0; i < sliceValue.Len(); i++ {
4844
entity := sliceValue.Index(i).Interface()
49-
checkable, ok := entity.(consistencyCheckable)
50-
if !ok {
51-
t.Errorf("Expected %+v (of type %T) to be checkable for consistency",
52-
entity, entity)
53-
} else {
54-
checkable.checkForConsistency(t)
55-
}
45+
checkForConsistency(entity, t)
5646
}
5747
}
5848
}
5949

50+
func checkForConsistency(bean interface{}, t *testing.T) {
51+
switch b := bean.(type) {
52+
case *User:
53+
checkForUserConsistency(b, t)
54+
case *Repository:
55+
checkForRepoConsistency(b, t)
56+
case *Issue:
57+
checkForIssueConsistency(b, t)
58+
case *PullRequest:
59+
checkForPullRequestConsistency(b, t)
60+
case *Milestone:
61+
checkForMilestoneConsistency(b, t)
62+
case *Label:
63+
checkForLabelConsistency(b, t)
64+
case *Team:
65+
checkForTeamConsistency(b, t)
66+
case *Action:
67+
checkForActionConsistency(b, t)
68+
default:
69+
t.Errorf("unknown bean type: %#v", bean)
70+
}
71+
}
72+
6073
// getCount get the count of database entries matching bean
6174
func getCount(t *testing.T, e db.Engine, bean interface{}) int64 {
6275
count, err := e.Count(bean)
@@ -70,7 +83,7 @@ func assertCount(t *testing.T, bean interface{}, expected int) {
7083
"Failed consistency test, the counted bean (of type %T) was %+v", bean, bean)
7184
}
7285

73-
func (user *User) checkForConsistency(t *testing.T) {
86+
func checkForUserConsistency(user *User, t *testing.T) {
7487
assertCount(t, &Repository{OwnerID: user.ID}, user.NumRepos)
7588
assertCount(t, &Star{UID: user.ID}, user.NumStars)
7689
assertCount(t, &OrgUser{OrgID: user.ID}, user.NumMembers)
@@ -83,7 +96,7 @@ func (user *User) checkForConsistency(t *testing.T) {
8396
}
8497
}
8598

86-
func (repo *Repository) checkForConsistency(t *testing.T) {
99+
func checkForRepoConsistency(repo *Repository, t *testing.T) {
87100
assert.Equal(t, repo.LowerName, strings.ToLower(repo.Name), "repo: %+v", repo)
88101
assertCount(t, &Star{RepoID: repo.ID}, repo.NumStars)
89102
assertCount(t, &Milestone{RepoID: repo.ID}, repo.NumMilestones)
@@ -117,7 +130,7 @@ func (repo *Repository) checkForConsistency(t *testing.T) {
117130
"Unexpected number of closed milestones for repo %+v", repo)
118131
}
119132

120-
func (issue *Issue) checkForConsistency(t *testing.T) {
133+
func checkForIssueConsistency(issue *Issue, t *testing.T) {
121134
actual := getCount(t, db.GetEngine(db.DefaultContext).Where("type=?", CommentTypeComment), &Comment{IssueID: issue.ID})
122135
assert.EqualValues(t, issue.NumComments, actual,
123136
"Unexpected number of comments for issue %+v", issue)
@@ -127,13 +140,13 @@ func (issue *Issue) checkForConsistency(t *testing.T) {
127140
}
128141
}
129142

130-
func (pr *PullRequest) checkForConsistency(t *testing.T) {
143+
func checkForPullRequestConsistency(pr *PullRequest, t *testing.T) {
131144
issue := db.AssertExistsAndLoadBean(t, &Issue{ID: pr.IssueID}).(*Issue)
132145
assert.True(t, issue.IsPull)
133146
assert.EqualValues(t, issue.Index, pr.Index)
134147
}
135148

136-
func (milestone *Milestone) checkForConsistency(t *testing.T) {
149+
func checkForMilestoneConsistency(milestone *Milestone, t *testing.T) {
137150
assertCount(t, &Issue{MilestoneID: milestone.ID}, milestone.NumIssues)
138151

139152
actual := getCount(t, db.GetEngine(db.DefaultContext).Where("is_closed=?", true), &Issue{MilestoneID: milestone.ID})
@@ -147,7 +160,7 @@ func (milestone *Milestone) checkForConsistency(t *testing.T) {
147160
assert.Equal(t, completeness, milestone.Completeness)
148161
}
149162

150-
func (label *Label) checkForConsistency(t *testing.T) {
163+
func checkForLabelConsistency(label *Label, t *testing.T) {
151164
issueLabels := make([]*IssueLabel, 0, 10)
152165
assert.NoError(t, db.GetEngine(db.DefaultContext).Find(&issueLabels, &IssueLabel{LabelID: label.ID}))
153166
assert.EqualValues(t, label.NumIssues, len(issueLabels),
@@ -166,12 +179,12 @@ func (label *Label) checkForConsistency(t *testing.T) {
166179
"Unexpected number of closed issues for label %+v", label)
167180
}
168181

169-
func (team *Team) checkForConsistency(t *testing.T) {
182+
func checkForTeamConsistency(team *Team, t *testing.T) {
170183
assertCount(t, &TeamUser{TeamID: team.ID}, team.NumMembers)
171184
assertCount(t, &TeamRepo{TeamID: team.ID}, team.NumRepos)
172185
}
173186

174-
func (action *Action) checkForConsistency(t *testing.T) {
187+
func checkForActionConsistency(action *Action, t *testing.T) {
175188
repo := db.AssertExistsAndLoadBean(t, &Repository{ID: action.RepoID}).(*Repository)
176189
assert.Equal(t, repo.IsPrivate, action.IsPrivate, "action: %+v", action)
177190
}

0 commit comments

Comments
 (0)