summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2021-11-12 08:56:45 +0800
committerGitHub <noreply@github.com>2021-11-12 08:56:45 +0800
commit0db7a32b9233d2c0039df4e89356ef0cbcef6dae (patch)
tree67cf94fcea5e8eea643a2b8e44b18891723894b2
parent90eb9fb889e5d3a5845576dbc63e3792f3da33f2 (diff)
downloadgitea-0db7a32b9233d2c0039df4e89356ef0cbcef6dae.tar.gz
gitea-0db7a32b9233d2c0039df4e89356ef0cbcef6dae.zip
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
-rw-r--r--models/consistency.go53
1 files changed, 33 insertions, 20 deletions
diff --git a/models/consistency.go b/models/consistency.go
index b9b590cfd4..ab81456916 100644
--- a/models/consistency.go
+++ b/models/consistency.go
@@ -10,15 +10,11 @@ import (
"testing"
"code.gitea.io/gitea/models/db"
+
"github.com/stretchr/testify/assert"
"xorm.io/builder"
)
-// consistencyCheckable a type that can be tested for database consistency
-type consistencyCheckable interface {
- checkForConsistency(t *testing.T)
-}
-
// CheckConsistencyForAll test that the entire database is consistent
func CheckConsistencyForAll(t *testing.T) {
CheckConsistencyFor(t,
@@ -46,17 +42,34 @@ func CheckConsistencyFor(t *testing.T, beansToCheck ...interface{}) {
for i := 0; i < sliceValue.Len(); i++ {
entity := sliceValue.Index(i).Interface()
- checkable, ok := entity.(consistencyCheckable)
- if !ok {
- t.Errorf("Expected %+v (of type %T) to be checkable for consistency",
- entity, entity)
- } else {
- checkable.checkForConsistency(t)
- }
+ checkForConsistency(entity, t)
}
}
}
+func checkForConsistency(bean interface{}, t *testing.T) {
+ switch b := bean.(type) {
+ case *User:
+ checkForUserConsistency(b, t)
+ case *Repository:
+ checkForRepoConsistency(b, t)
+ case *Issue:
+ checkForIssueConsistency(b, t)
+ case *PullRequest:
+ checkForPullRequestConsistency(b, t)
+ case *Milestone:
+ checkForMilestoneConsistency(b, t)
+ case *Label:
+ checkForLabelConsistency(b, t)
+ case *Team:
+ checkForTeamConsistency(b, t)
+ case *Action:
+ checkForActionConsistency(b, t)
+ default:
+ t.Errorf("unknown bean type: %#v", bean)
+ }
+}
+
// getCount get the count of database entries matching bean
func getCount(t *testing.T, e db.Engine, bean interface{}) int64 {
count, err := e.Count(bean)
@@ -70,7 +83,7 @@ func assertCount(t *testing.T, bean interface{}, expected int) {
"Failed consistency test, the counted bean (of type %T) was %+v", bean, bean)
}
-func (user *User) checkForConsistency(t *testing.T) {
+func checkForUserConsistency(user *User, t *testing.T) {
assertCount(t, &Repository{OwnerID: user.ID}, user.NumRepos)
assertCount(t, &Star{UID: user.ID}, user.NumStars)
assertCount(t, &OrgUser{OrgID: user.ID}, user.NumMembers)
@@ -83,7 +96,7 @@ func (user *User) checkForConsistency(t *testing.T) {
}
}
-func (repo *Repository) checkForConsistency(t *testing.T) {
+func checkForRepoConsistency(repo *Repository, t *testing.T) {
assert.Equal(t, repo.LowerName, strings.ToLower(repo.Name), "repo: %+v", repo)
assertCount(t, &Star{RepoID: repo.ID}, repo.NumStars)
assertCount(t, &Milestone{RepoID: repo.ID}, repo.NumMilestones)
@@ -117,7 +130,7 @@ func (repo *Repository) checkForConsistency(t *testing.T) {
"Unexpected number of closed milestones for repo %+v", repo)
}
-func (issue *Issue) checkForConsistency(t *testing.T) {
+func checkForIssueConsistency(issue *Issue, t *testing.T) {
actual := getCount(t, db.GetEngine(db.DefaultContext).Where("type=?", CommentTypeComment), &Comment{IssueID: issue.ID})
assert.EqualValues(t, issue.NumComments, actual,
"Unexpected number of comments for issue %+v", issue)
@@ -127,13 +140,13 @@ func (issue *Issue) checkForConsistency(t *testing.T) {
}
}
-func (pr *PullRequest) checkForConsistency(t *testing.T) {
+func checkForPullRequestConsistency(pr *PullRequest, t *testing.T) {
issue := db.AssertExistsAndLoadBean(t, &Issue{ID: pr.IssueID}).(*Issue)
assert.True(t, issue.IsPull)
assert.EqualValues(t, issue.Index, pr.Index)
}
-func (milestone *Milestone) checkForConsistency(t *testing.T) {
+func checkForMilestoneConsistency(milestone *Milestone, t *testing.T) {
assertCount(t, &Issue{MilestoneID: milestone.ID}, milestone.NumIssues)
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) {
assert.Equal(t, completeness, milestone.Completeness)
}
-func (label *Label) checkForConsistency(t *testing.T) {
+func checkForLabelConsistency(label *Label, t *testing.T) {
issueLabels := make([]*IssueLabel, 0, 10)
assert.NoError(t, db.GetEngine(db.DefaultContext).Find(&issueLabels, &IssueLabel{LabelID: label.ID}))
assert.EqualValues(t, label.NumIssues, len(issueLabels),
@@ -166,12 +179,12 @@ func (label *Label) checkForConsistency(t *testing.T) {
"Unexpected number of closed issues for label %+v", label)
}
-func (team *Team) checkForConsistency(t *testing.T) {
+func checkForTeamConsistency(team *Team, t *testing.T) {
assertCount(t, &TeamUser{TeamID: team.ID}, team.NumMembers)
assertCount(t, &TeamRepo{TeamID: team.ID}, team.NumRepos)
}
-func (action *Action) checkForConsistency(t *testing.T) {
+func checkForActionConsistency(action *Action, t *testing.T) {
repo := db.AssertExistsAndLoadBean(t, &Repository{ID: action.RepoID}).(*Repository)
assert.Equal(t, repo.IsPrivate, action.IsPrivate, "action: %+v", action)
}