@@ -10,15 +10,11 @@ import (
10
10
"testing"
11
11
12
12
"code.gitea.io/gitea/models/db"
13
+
13
14
"github.com/stretchr/testify/assert"
14
15
"xorm.io/builder"
15
16
)
16
17
17
- // consistencyCheckable a type that can be tested for database consistency
18
- type consistencyCheckable interface {
19
- checkForConsistency (t * testing.T )
20
- }
21
-
22
18
// CheckConsistencyForAll test that the entire database is consistent
23
19
func CheckConsistencyForAll (t * testing.T ) {
24
20
CheckConsistencyFor (t ,
@@ -46,17 +42,34 @@ func CheckConsistencyFor(t *testing.T, beansToCheck ...interface{}) {
46
42
47
43
for i := 0 ; i < sliceValue .Len (); i ++ {
48
44
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 )
56
46
}
57
47
}
58
48
}
59
49
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
+
60
73
// getCount get the count of database entries matching bean
61
74
func getCount (t * testing.T , e db.Engine , bean interface {}) int64 {
62
75
count , err := e .Count (bean )
@@ -70,7 +83,7 @@ func assertCount(t *testing.T, bean interface{}, expected int) {
70
83
"Failed consistency test, the counted bean (of type %T) was %+v" , bean , bean )
71
84
}
72
85
73
- func (user * User ) checkForConsistency ( t * testing.T ) {
86
+ func checkForUserConsistency (user * User , t * testing.T ) {
74
87
assertCount (t , & Repository {OwnerID : user .ID }, user .NumRepos )
75
88
assertCount (t , & Star {UID : user .ID }, user .NumStars )
76
89
assertCount (t , & OrgUser {OrgID : user .ID }, user .NumMembers )
@@ -83,7 +96,7 @@ func (user *User) checkForConsistency(t *testing.T) {
83
96
}
84
97
}
85
98
86
- func (repo * Repository ) checkForConsistency ( t * testing.T ) {
99
+ func checkForRepoConsistency (repo * Repository , t * testing.T ) {
87
100
assert .Equal (t , repo .LowerName , strings .ToLower (repo .Name ), "repo: %+v" , repo )
88
101
assertCount (t , & Star {RepoID : repo .ID }, repo .NumStars )
89
102
assertCount (t , & Milestone {RepoID : repo .ID }, repo .NumMilestones )
@@ -117,7 +130,7 @@ func (repo *Repository) checkForConsistency(t *testing.T) {
117
130
"Unexpected number of closed milestones for repo %+v" , repo )
118
131
}
119
132
120
- func (issue * Issue ) checkForConsistency ( t * testing.T ) {
133
+ func checkForIssueConsistency (issue * Issue , t * testing.T ) {
121
134
actual := getCount (t , db .GetEngine (db .DefaultContext ).Where ("type=?" , CommentTypeComment ), & Comment {IssueID : issue .ID })
122
135
assert .EqualValues (t , issue .NumComments , actual ,
123
136
"Unexpected number of comments for issue %+v" , issue )
@@ -127,13 +140,13 @@ func (issue *Issue) checkForConsistency(t *testing.T) {
127
140
}
128
141
}
129
142
130
- func (pr * PullRequest ) checkForConsistency ( t * testing.T ) {
143
+ func checkForPullRequestConsistency (pr * PullRequest , t * testing.T ) {
131
144
issue := db .AssertExistsAndLoadBean (t , & Issue {ID : pr .IssueID }).(* Issue )
132
145
assert .True (t , issue .IsPull )
133
146
assert .EqualValues (t , issue .Index , pr .Index )
134
147
}
135
148
136
- func (milestone * Milestone ) checkForConsistency ( t * testing.T ) {
149
+ func checkForMilestoneConsistency (milestone * Milestone , t * testing.T ) {
137
150
assertCount (t , & Issue {MilestoneID : milestone .ID }, milestone .NumIssues )
138
151
139
152
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) {
147
160
assert .Equal (t , completeness , milestone .Completeness )
148
161
}
149
162
150
- func (label * Label ) checkForConsistency ( t * testing.T ) {
163
+ func checkForLabelConsistency (label * Label , t * testing.T ) {
151
164
issueLabels := make ([]* IssueLabel , 0 , 10 )
152
165
assert .NoError (t , db .GetEngine (db .DefaultContext ).Find (& issueLabels , & IssueLabel {LabelID : label .ID }))
153
166
assert .EqualValues (t , label .NumIssues , len (issueLabels ),
@@ -166,12 +179,12 @@ func (label *Label) checkForConsistency(t *testing.T) {
166
179
"Unexpected number of closed issues for label %+v" , label )
167
180
}
168
181
169
- func (team * Team ) checkForConsistency ( t * testing.T ) {
182
+ func checkForTeamConsistency (team * Team , t * testing.T ) {
170
183
assertCount (t , & TeamUser {TeamID : team .ID }, team .NumMembers )
171
184
assertCount (t , & TeamRepo {TeamID : team .ID }, team .NumRepos )
172
185
}
173
186
174
- func (action * Action ) checkForConsistency ( t * testing.T ) {
187
+ func checkForActionConsistency (action * Action , t * testing.T ) {
175
188
repo := db .AssertExistsAndLoadBean (t , & Repository {ID : action .RepoID }).(* Repository )
176
189
assert .Equal (t , repo .IsPrivate , action .IsPrivate , "action: %+v" , action )
177
190
}
0 commit comments