Consistency checks for unit tests (#853)
parent
94130da63a
commit
ceae143e78
@ -0,0 +1,149 @@ |
|||||||
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a MIT-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package models |
||||||
|
|
||||||
|
import ( |
||||||
|
"reflect" |
||||||
|
"testing" |
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert" |
||||||
|
) |
||||||
|
|
||||||
|
// 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, |
||||||
|
&User{}, |
||||||
|
&Repository{}, |
||||||
|
&Issue{}, |
||||||
|
&PullRequest{}, |
||||||
|
&Milestone{}, |
||||||
|
&Label{}, |
||||||
|
&Team{}) |
||||||
|
} |
||||||
|
|
||||||
|
// CheckConsistencyFor test that all matching database entries are consistent
|
||||||
|
func CheckConsistencyFor(t *testing.T, beansToCheck ...interface{}) { |
||||||
|
for _, bean := range beansToCheck { |
||||||
|
sliceType := reflect.SliceOf(reflect.TypeOf(bean)) |
||||||
|
sliceValue := reflect.MakeSlice(sliceType, 0, 10) |
||||||
|
|
||||||
|
ptrToSliceValue := reflect.New(sliceType) |
||||||
|
ptrToSliceValue.Elem().Set(sliceValue) |
||||||
|
|
||||||
|
assert.NoError(t, x.Find(ptrToSliceValue.Interface())) |
||||||
|
sliceValue = ptrToSliceValue.Elem() |
||||||
|
|
||||||
|
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) |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
// getCount get the count of database entries matching bean
|
||||||
|
func getCount(t *testing.T, e Engine, bean interface{}) int64 { |
||||||
|
count, err := e.Count(bean) |
||||||
|
assert.NoError(t, err) |
||||||
|
return count |
||||||
|
} |
||||||
|
|
||||||
|
// assertCount test the count of database entries matching bean
|
||||||
|
func assertCount(t *testing.T, bean interface{}, expected int) { |
||||||
|
assert.EqualValues(t, expected, getCount(t, x, bean), |
||||||
|
"Failed consistency test, the counted bean (of type %T) was %+v", bean, bean) |
||||||
|
} |
||||||
|
|
||||||
|
func (user *User) CheckForConsistency(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) |
||||||
|
assertCount(t, &Team{OrgID: user.ID}, user.NumTeams) |
||||||
|
assertCount(t, &Follow{UserID: user.ID}, user.NumFollowing) |
||||||
|
assertCount(t, &Follow{FollowID: user.ID}, user.NumFollowers) |
||||||
|
} |
||||||
|
|
||||||
|
func (repo *Repository) CheckForConsistency(t *testing.T) { |
||||||
|
assertCount(t, &Star{RepoID: repo.ID}, repo.NumStars) |
||||||
|
assertCount(t, &Watch{RepoID: repo.ID}, repo.NumWatches) |
||||||
|
assertCount(t, &Issue{RepoID: repo.ID}, repo.NumIssues) |
||||||
|
assertCount(t, &Milestone{RepoID: repo.ID}, repo.NumMilestones) |
||||||
|
assertCount(t, &Repository{ForkID: repo.ID}, repo.NumForks) |
||||||
|
if repo.IsFork { |
||||||
|
AssertExistsAndLoadBean(t, &Repository{ID: repo.ForkID}) |
||||||
|
} |
||||||
|
|
||||||
|
actual := getCount(t, x.Where("is_closed=1"), &Issue{RepoID: repo.ID}) |
||||||
|
assert.EqualValues(t, repo.NumClosedIssues, actual, |
||||||
|
"Unexpected number of closed issues for repo %+v", repo) |
||||||
|
|
||||||
|
actual = getCount(t, x.Where("is_pull=1"), &Issue{RepoID: repo.ID}) |
||||||
|
assert.EqualValues(t, repo.NumPulls, actual, |
||||||
|
"Unexpected number of pulls for repo %+v", repo) |
||||||
|
|
||||||
|
actual = getCount(t, x.Where("is_pull=1 AND is_closed=1"), &Issue{RepoID: repo.ID}) |
||||||
|
assert.EqualValues(t, repo.NumClosedPulls, actual, |
||||||
|
"Unexpected number of closed pulls for repo %+v", repo) |
||||||
|
|
||||||
|
actual = getCount(t, x.Where("is_closed=1"), &Milestone{RepoID: repo.ID}) |
||||||
|
assert.EqualValues(t, repo.NumClosedMilestones, actual, |
||||||
|
"Unexpected number of closed milestones for repo %+v", repo) |
||||||
|
} |
||||||
|
|
||||||
|
func (issue *Issue) CheckForConsistency(t *testing.T) { |
||||||
|
assertCount(t, &Comment{IssueID: issue.ID}, issue.NumComments) |
||||||
|
if issue.IsPull { |
||||||
|
pr := AssertExistsAndLoadBean(t, &PullRequest{IssueID: issue.ID}).(*PullRequest) |
||||||
|
assert.EqualValues(t, pr.Index, issue.Index) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
func (pr *PullRequest) CheckForConsistency(t *testing.T) { |
||||||
|
issue := 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) { |
||||||
|
assertCount(t, &Issue{MilestoneID: milestone.ID}, milestone.NumIssues) |
||||||
|
|
||||||
|
actual := getCount(t, x.Where("is_closed=1"), &Issue{MilestoneID: milestone.ID}) |
||||||
|
assert.EqualValues(t, milestone.NumClosedIssues, actual, |
||||||
|
"Unexpected number of closed issues for milestone %+v", milestone) |
||||||
|
} |
||||||
|
|
||||||
|
func (label *Label) CheckForConsistency(t *testing.T) { |
||||||
|
issueLabels := make([]*IssueLabel, 0, 10) |
||||||
|
assert.NoError(t, x.Find(&issueLabels, &IssueLabel{LabelID: label.ID})) |
||||||
|
assert.EqualValues(t, 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(t, x.In("id", issueIDs).Where("is_closed=1"), &Issue{}) |
||||||
|
} |
||||||
|
assert.EqualValues(t, expected, label.NumClosedIssues, |
||||||
|
"Unexpected number of closed issues for label %+v", label) |
||||||
|
} |
||||||
|
|
||||||
|
func (team *Team) CheckForConsistency(t *testing.T) { |
||||||
|
assertCount(t, &TeamUser{TeamID: team.ID}, team.NumMembers) |
||||||
|
assertCount(t, &TeamRepo{TeamID: team.ID}, team.NumRepos) |
||||||
|
} |
Loading…
Reference in new issue