本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

167 lines
5.8KB

  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package models
  5. import (
  6. "reflect"
  7. "strings"
  8. "testing"
  9. "github.com/stretchr/testify/assert"
  10. )
  11. // consistencyCheckable a type that can be tested for database consistency
  12. type consistencyCheckable interface {
  13. checkForConsistency(t *testing.T)
  14. }
  15. // CheckConsistencyForAll test that the entire database is consistent
  16. func CheckConsistencyForAll(t *testing.T) {
  17. CheckConsistencyFor(t,
  18. &User{},
  19. &Repository{},
  20. &Issue{},
  21. &PullRequest{},
  22. &Milestone{},
  23. &Label{},
  24. &Team{},
  25. &Action{})
  26. }
  27. // CheckConsistencyFor test that all matching database entries are consistent
  28. func CheckConsistencyFor(t *testing.T, beansToCheck ...interface{}) {
  29. for _, bean := range beansToCheck {
  30. sliceType := reflect.SliceOf(reflect.TypeOf(bean))
  31. sliceValue := reflect.MakeSlice(sliceType, 0, 10)
  32. ptrToSliceValue := reflect.New(sliceType)
  33. ptrToSliceValue.Elem().Set(sliceValue)
  34. assert.NoError(t, x.Table(bean).Find(ptrToSliceValue.Interface()))
  35. sliceValue = ptrToSliceValue.Elem()
  36. for i := 0; i < sliceValue.Len(); i++ {
  37. entity := sliceValue.Index(i).Interface()
  38. checkable, ok := entity.(consistencyCheckable)
  39. if !ok {
  40. t.Errorf("Expected %+v (of type %T) to be checkable for consistency",
  41. entity, entity)
  42. } else {
  43. checkable.checkForConsistency(t)
  44. }
  45. }
  46. }
  47. }
  48. // getCount get the count of database entries matching bean
  49. func getCount(t *testing.T, e Engine, bean interface{}) int64 {
  50. count, err := e.Count(bean)
  51. assert.NoError(t, err)
  52. return count
  53. }
  54. // assertCount test the count of database entries matching bean
  55. func assertCount(t *testing.T, bean interface{}, expected int) {
  56. assert.EqualValues(t, expected, getCount(t, x, bean),
  57. "Failed consistency test, the counted bean (of type %T) was %+v", bean, bean)
  58. }
  59. func (user *User) checkForConsistency(t *testing.T) {
  60. assertCount(t, &Repository{OwnerID: user.ID}, user.NumRepos)
  61. assertCount(t, &Star{UID: user.ID}, user.NumStars)
  62. assertCount(t, &OrgUser{OrgID: user.ID}, user.NumMembers)
  63. assertCount(t, &Team{OrgID: user.ID}, user.NumTeams)
  64. assertCount(t, &Follow{UserID: user.ID}, user.NumFollowing)
  65. assertCount(t, &Follow{FollowID: user.ID}, user.NumFollowers)
  66. if user.Type != UserTypeOrganization {
  67. assert.EqualValues(t, 0, user.NumMembers)
  68. assert.EqualValues(t, 0, user.NumTeams)
  69. }
  70. }
  71. func (repo *Repository) checkForConsistency(t *testing.T) {
  72. assert.Equal(t, repo.LowerName, strings.ToLower(repo.Name), "repo: %+v", repo)
  73. assertCount(t, &Star{RepoID: repo.ID}, repo.NumStars)
  74. assertCount(t, &Watch{RepoID: repo.ID}, repo.NumWatches)
  75. assertCount(t, &Milestone{RepoID: repo.ID}, repo.NumMilestones)
  76. assertCount(t, &Repository{ForkID: repo.ID}, repo.NumForks)
  77. if repo.IsFork {
  78. AssertExistsAndLoadBean(t, &Repository{ID: repo.ForkID})
  79. }
  80. actual := getCount(t, x.Where("is_pull=?", false), &Issue{RepoID: repo.ID})
  81. assert.EqualValues(t, repo.NumIssues, actual,
  82. "Unexpected number of issues for repo %+v", repo)
  83. actual = getCount(t, x.Where("is_pull=? AND is_closed=?", false, true), &Issue{RepoID: repo.ID})
  84. assert.EqualValues(t, repo.NumClosedIssues, actual,
  85. "Unexpected number of closed issues for repo %+v", repo)
  86. actual = getCount(t, x.Where("is_pull=?", true), &Issue{RepoID: repo.ID})
  87. assert.EqualValues(t, repo.NumPulls, actual,
  88. "Unexpected number of pulls for repo %+v", repo)
  89. actual = getCount(t, x.Where("is_pull=? AND is_closed=?", true, true), &Issue{RepoID: repo.ID})
  90. assert.EqualValues(t, repo.NumClosedPulls, actual,
  91. "Unexpected number of closed pulls for repo %+v", repo)
  92. actual = getCount(t, x.Where("is_closed=?", true), &Milestone{RepoID: repo.ID})
  93. assert.EqualValues(t, repo.NumClosedMilestones, actual,
  94. "Unexpected number of closed milestones for repo %+v", repo)
  95. }
  96. func (issue *Issue) checkForConsistency(t *testing.T) {
  97. actual := getCount(t, x.Where("type=?", CommentTypeComment), &Comment{IssueID: issue.ID})
  98. assert.EqualValues(t, issue.NumComments, actual,
  99. "Unexpected number of comments for issue %+v", issue)
  100. if issue.IsPull {
  101. pr := AssertExistsAndLoadBean(t, &PullRequest{IssueID: issue.ID}).(*PullRequest)
  102. assert.EqualValues(t, pr.Index, issue.Index)
  103. }
  104. }
  105. func (pr *PullRequest) checkForConsistency(t *testing.T) {
  106. issue := AssertExistsAndLoadBean(t, &Issue{ID: pr.IssueID}).(*Issue)
  107. assert.True(t, issue.IsPull)
  108. assert.EqualValues(t, issue.Index, pr.Index)
  109. }
  110. func (milestone *Milestone) checkForConsistency(t *testing.T) {
  111. assertCount(t, &Issue{MilestoneID: milestone.ID}, milestone.NumIssues)
  112. actual := getCount(t, x.Where("is_closed=?", true), &Issue{MilestoneID: milestone.ID})
  113. assert.EqualValues(t, milestone.NumClosedIssues, actual,
  114. "Unexpected number of closed issues for milestone %+v", milestone)
  115. }
  116. func (label *Label) checkForConsistency(t *testing.T) {
  117. issueLabels := make([]*IssueLabel, 0, 10)
  118. assert.NoError(t, x.Find(&issueLabels, &IssueLabel{LabelID: label.ID}))
  119. assert.EqualValues(t, label.NumIssues, len(issueLabels),
  120. "Unexpected number of issue for label %+v", label)
  121. issueIDs := make([]int64, len(issueLabels))
  122. for i, issueLabel := range issueLabels {
  123. issueIDs[i] = issueLabel.IssueID
  124. }
  125. expected := int64(0)
  126. if len(issueIDs) > 0 {
  127. expected = getCount(t, x.In("id", issueIDs).Where("is_closed=?", true), &Issue{})
  128. }
  129. assert.EqualValues(t, expected, label.NumClosedIssues,
  130. "Unexpected number of closed issues for label %+v", label)
  131. }
  132. func (team *Team) checkForConsistency(t *testing.T) {
  133. assertCount(t, &TeamUser{TeamID: team.ID}, team.NumMembers)
  134. assertCount(t, &TeamRepo{TeamID: team.ID}, team.NumRepos)
  135. }
  136. func (action *Action) checkForConsistency(t *testing.T) {
  137. repo := AssertExistsAndLoadBean(t, &Repository{ID: action.RepoID}).(*Repository)
  138. assert.Equal(t, repo.IsPrivate, action.IsPrivate, "action: %+v", action)
  139. }
上海开阖软件有限公司 沪ICP备12045867号-1