Decouple unit test, remove intermediate unittestbridge
package (#17662)
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
This commit is contained in:
@ -5,177 +5,11 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
"code.gitea.io/gitea/models/db"
|
||||
"code.gitea.io/gitea/modules/unittestbridge"
|
||||
|
||||
"xorm.io/builder"
|
||||
)
|
||||
|
||||
// CheckConsistencyFor test that all matching database entries are consistent
|
||||
func CheckConsistencyFor(t unittestbridge.Tester, beansToCheck ...interface{}) {
|
||||
ta := unittestbridge.NewAsserter(t)
|
||||
for _, bean := range beansToCheck {
|
||||
sliceType := reflect.SliceOf(reflect.TypeOf(bean))
|
||||
sliceValue := reflect.MakeSlice(sliceType, 0, 10)
|
||||
|
||||
ptrToSliceValue := reflect.New(sliceType)
|
||||
ptrToSliceValue.Elem().Set(sliceValue)
|
||||
|
||||
ta.NoError(db.GetEngine(db.DefaultContext).Table(bean).Find(ptrToSliceValue.Interface()))
|
||||
sliceValue = ptrToSliceValue.Elem()
|
||||
|
||||
for i := 0; i < sliceValue.Len(); i++ {
|
||||
entity := sliceValue.Index(i).Interface()
|
||||
checkForConsistency(ta, entity)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func checkForConsistency(ta unittestbridge.Asserter, bean interface{}) {
|
||||
switch b := bean.(type) {
|
||||
case *User:
|
||||
checkForUserConsistency(b, ta)
|
||||
case *Repository:
|
||||
checkForRepoConsistency(b, ta)
|
||||
case *Issue:
|
||||
checkForIssueConsistency(b, ta)
|
||||
case *PullRequest:
|
||||
checkForPullRequestConsistency(b, ta)
|
||||
case *Milestone:
|
||||
checkForMilestoneConsistency(b, ta)
|
||||
case *Label:
|
||||
checkForLabelConsistency(b, ta)
|
||||
case *Team:
|
||||
checkForTeamConsistency(b, ta)
|
||||
case *Action:
|
||||
checkForActionConsistency(b, ta)
|
||||
default:
|
||||
ta.Errorf("unknown bean type: %#v", bean)
|
||||
}
|
||||
}
|
||||
|
||||
// getCount get the count of database entries matching bean
|
||||
func getCount(ta unittestbridge.Asserter, e db.Engine, bean interface{}) int64 {
|
||||
count, err := e.Count(bean)
|
||||
ta.NoError(err)
|
||||
return count
|
||||
}
|
||||
|
||||
// assertCount test the count of database entries matching bean
|
||||
func assertCount(ta unittestbridge.Asserter, bean interface{}, expected int) {
|
||||
ta.EqualValues(expected, getCount(ta, db.GetEngine(db.DefaultContext), bean),
|
||||
"Failed consistency test, the counted bean (of type %T) was %+v", bean, bean)
|
||||
}
|
||||
|
||||
func checkForUserConsistency(user *User, ta unittestbridge.Asserter) {
|
||||
assertCount(ta, &Repository{OwnerID: user.ID}, user.NumRepos)
|
||||
assertCount(ta, &Star{UID: user.ID}, user.NumStars)
|
||||
assertCount(ta, &OrgUser{OrgID: user.ID}, user.NumMembers)
|
||||
assertCount(ta, &Team{OrgID: user.ID}, user.NumTeams)
|
||||
assertCount(ta, &Follow{UserID: user.ID}, user.NumFollowing)
|
||||
assertCount(ta, &Follow{FollowID: user.ID}, user.NumFollowers)
|
||||
if user.Type != UserTypeOrganization {
|
||||
ta.EqualValues(0, user.NumMembers)
|
||||
ta.EqualValues(0, user.NumTeams)
|
||||
}
|
||||
}
|
||||
|
||||
func checkForRepoConsistency(repo *Repository, ta unittestbridge.Asserter) {
|
||||
ta.Equal(repo.LowerName, strings.ToLower(repo.Name), "repo: %+v", repo)
|
||||
assertCount(ta, &Star{RepoID: repo.ID}, repo.NumStars)
|
||||
assertCount(ta, &Milestone{RepoID: repo.ID}, repo.NumMilestones)
|
||||
assertCount(ta, &Repository{ForkID: repo.ID}, repo.NumForks)
|
||||
if repo.IsFork {
|
||||
db.AssertExistsAndLoadBean(ta, &Repository{ID: repo.ForkID})
|
||||
}
|
||||
|
||||
actual := getCount(ta, db.GetEngine(db.DefaultContext).Where("Mode<>?", RepoWatchModeDont), &Watch{RepoID: repo.ID})
|
||||
ta.EqualValues(repo.NumWatches, actual,
|
||||
"Unexpected number of watches for repo %+v", repo)
|
||||
|
||||
actual = getCount(ta, db.GetEngine(db.DefaultContext).Where("is_pull=?", false), &Issue{RepoID: repo.ID})
|
||||
ta.EqualValues(repo.NumIssues, actual,
|
||||
"Unexpected number of issues for repo %+v", repo)
|
||||
|
||||
actual = getCount(ta, db.GetEngine(db.DefaultContext).Where("is_pull=? AND is_closed=?", false, true), &Issue{RepoID: repo.ID})
|
||||
ta.EqualValues(repo.NumClosedIssues, actual,
|
||||
"Unexpected number of closed issues for repo %+v", repo)
|
||||
|
||||
actual = getCount(ta, db.GetEngine(db.DefaultContext).Where("is_pull=?", true), &Issue{RepoID: repo.ID})
|
||||
ta.EqualValues(repo.NumPulls, actual,
|
||||
"Unexpected number of pulls for repo %+v", repo)
|
||||
|
||||
actual = getCount(ta, db.GetEngine(db.DefaultContext).Where("is_pull=? AND is_closed=?", true, true), &Issue{RepoID: repo.ID})
|
||||
ta.EqualValues(repo.NumClosedPulls, actual,
|
||||
"Unexpected number of closed pulls for repo %+v", repo)
|
||||
|
||||
actual = getCount(ta, db.GetEngine(db.DefaultContext).Where("is_closed=?", true), &Milestone{RepoID: repo.ID})
|
||||
ta.EqualValues(repo.NumClosedMilestones, actual,
|
||||
"Unexpected number of closed milestones for repo %+v", repo)
|
||||
}
|
||||
|
||||
func checkForIssueConsistency(issue *Issue, ta unittestbridge.Asserter) {
|
||||
actual := getCount(ta, db.GetEngine(db.DefaultContext).Where("type=?", CommentTypeComment), &Comment{IssueID: issue.ID})
|
||||
ta.EqualValues(issue.NumComments, actual,
|
||||
"Unexpected number of comments for issue %+v", issue)
|
||||
if issue.IsPull {
|
||||
pr := db.AssertExistsAndLoadBean(ta, &PullRequest{IssueID: issue.ID}).(*PullRequest)
|
||||
ta.EqualValues(pr.Index, issue.Index)
|
||||
}
|
||||
}
|
||||
|
||||
func checkForPullRequestConsistency(pr *PullRequest, ta unittestbridge.Asserter) {
|
||||
issue := db.AssertExistsAndLoadBean(ta, &Issue{ID: pr.IssueID}).(*Issue)
|
||||
ta.True(issue.IsPull)
|
||||
ta.EqualValues(issue.Index, pr.Index)
|
||||
}
|
||||
|
||||
func checkForMilestoneConsistency(milestone *Milestone, ta unittestbridge.Asserter) {
|
||||
assertCount(ta, &Issue{MilestoneID: milestone.ID}, milestone.NumIssues)
|
||||
|
||||
actual := getCount(ta, db.GetEngine(db.DefaultContext).Where("is_closed=?", true), &Issue{MilestoneID: milestone.ID})
|
||||
ta.EqualValues(milestone.NumClosedIssues, actual,
|
||||
"Unexpected number of closed issues for milestone %+v", milestone)
|
||||
|
||||
completeness := 0
|
||||
if milestone.NumIssues > 0 {
|
||||
completeness = milestone.NumClosedIssues * 100 / milestone.NumIssues
|
||||
}
|
||||
ta.Equal(completeness, milestone.Completeness)
|
||||
}
|
||||
|
||||
func checkForLabelConsistency(label *Label, ta unittestbridge.Asserter) {
|
||||
issueLabels := make([]*IssueLabel, 0, 10)
|
||||
ta.NoError(db.GetEngine(db.DefaultContext).Find(&issueLabels, &IssueLabel{LabelID: label.ID}))
|
||||
ta.EqualValues(label.NumIssues, len(issueLabels),
|
||||
"Unexpected number of issue for label %+v", label)
|
||||
|
||||
issueIDs := make([]int64, len(issueLabels))
|
||||
for i, issueLabel := range issueLabels {
|
||||
issueIDs[i] = issueLabel.IssueID
|
||||
}
|
||||
|
||||
expected := int64(0)
|
||||
if len(issueIDs) > 0 {
|
||||
expected = getCount(ta, db.GetEngine(db.DefaultContext).In("id", issueIDs).Where("is_closed=?", true), &Issue{})
|
||||
}
|
||||
ta.EqualValues(expected, label.NumClosedIssues,
|
||||
"Unexpected number of closed issues for label %+v", label)
|
||||
}
|
||||
|
||||
func checkForTeamConsistency(team *Team, ta unittestbridge.Asserter) {
|
||||
assertCount(ta, &TeamUser{TeamID: team.ID}, team.NumMembers)
|
||||
assertCount(ta, &TeamRepo{TeamID: team.ID}, team.NumRepos)
|
||||
}
|
||||
|
||||
func checkForActionConsistency(action *Action, ta unittestbridge.Asserter) {
|
||||
repo := db.AssertExistsAndLoadBean(ta, &Repository{ID: action.RepoID}).(*Repository)
|
||||
ta.Equal(repo.IsPrivate, action.IsPrivate, "action: %+v", action)
|
||||
}
|
||||
|
||||
// CountOrphanedLabels return count of labels witch are broken and not accessible via ui anymore
|
||||
func CountOrphanedLabels() (int64, error) {
|
||||
noref, err := db.GetEngine(db.DefaultContext).Table("label").Where("repo_id=? AND org_id=?", 0, 0).Count("label.id")
|
||||
|
Reference in New Issue
Block a user