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.

169 lines
5.9 KiB

  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, &Milestone{RepoID: repo.ID}, repo.NumMilestones)
  75. assertCount(t, &Repository{ForkID: repo.ID}, repo.NumForks)
  76. if repo.IsFork {
  77. AssertExistsAndLoadBean(t, &Repository{ID: repo.ForkID})
  78. }
  79. actual := getCount(t, x.Where("Mode<>?", RepoWatchModeDont), &Watch{RepoID: repo.ID})
  80. assert.EqualValues(t, repo.NumWatches, actual,
  81. "Unexpected number of watches for repo %+v", repo)
  82. actual = getCount(t, x.Where("is_pull=?", false), &Issue{RepoID: repo.ID})
  83. assert.EqualValues(t, repo.NumIssues, actual,
  84. "Unexpected number of issues for repo %+v", repo)
  85. actual = getCount(t, x.Where("is_pull=? AND is_closed=?", false, true), &Issue{RepoID: repo.ID})
  86. assert.EqualValues(t, repo.NumClosedIssues, actual,
  87. "Unexpected number of closed issues for repo %+v", repo)
  88. actual = getCount(t, x.Where("is_pull=?", true), &Issue{RepoID: repo.ID})
  89. assert.EqualValues(t, repo.NumPulls, actual,
  90. "Unexpected number of pulls for repo %+v", repo)
  91. actual = getCount(t, x.Where("is_pull=? AND is_closed=?", true, true), &Issue{RepoID: repo.ID})
  92. assert.EqualValues(t, repo.NumClosedPulls, actual,
  93. "Unexpected number of closed pulls for repo %+v", repo)
  94. actual = getCount(t, x.Where("is_closed=?", true), &Milestone{RepoID: repo.ID})
  95. assert.EqualValues(t, repo.NumClosedMilestones, actual,
  96. "Unexpected number of closed milestones for repo %+v", repo)
  97. }
  98. func (issue *Issue) checkForConsistency(t *testing.T) {
  99. actual := getCount(t, x.Where("type=?", CommentTypeComment), &Comment{IssueID: issue.ID})
  100. assert.EqualValues(t, issue.NumComments, actual,
  101. "Unexpected number of comments for issue %+v", issue)
  102. if issue.IsPull {
  103. pr := AssertExistsAndLoadBean(t, &PullRequest{IssueID: issue.ID}).(*PullRequest)
  104. assert.EqualValues(t, pr.Index, issue.Index)
  105. }
  106. }
  107. func (pr *PullRequest) checkForConsistency(t *testing.T) {
  108. issue := AssertExistsAndLoadBean(t, &Issue{ID: pr.IssueID}).(*Issue)
  109. assert.True(t, issue.IsPull)
  110. assert.EqualValues(t, issue.Index, pr.Index)
  111. }
  112. func (milestone *Milestone) checkForConsistency(t *testing.T) {
  113. assertCount(t, &Issue{MilestoneID: milestone.ID}, milestone.NumIssues)
  114. actual := getCount(t, x.Where("is_closed=?", true), &Issue{MilestoneID: milestone.ID})
  115. assert.EqualValues(t, milestone.NumClosedIssues, actual,
  116. "Unexpected number of closed issues for milestone %+v", milestone)
  117. }
  118. func (label *Label) checkForConsistency(t *testing.T) {
  119. issueLabels := make([]*IssueLabel, 0, 10)
  120. assert.NoError(t, x.Find(&issueLabels, &IssueLabel{LabelID: label.ID}))
  121. assert.EqualValues(t, label.NumIssues, len(issueLabels),
  122. "Unexpected number of issue for label %+v", label)
  123. issueIDs := make([]int64, len(issueLabels))
  124. for i, issueLabel := range issueLabels {
  125. issueIDs[i] = issueLabel.IssueID
  126. }
  127. expected := int64(0)
  128. if len(issueIDs) > 0 {
  129. expected = getCount(t, x.In("id", issueIDs).Where("is_closed=?", true), &Issue{})
  130. }
  131. assert.EqualValues(t, expected, label.NumClosedIssues,
  132. "Unexpected number of closed issues for label %+v", label)
  133. }
  134. func (team *Team) checkForConsistency(t *testing.T) {
  135. assertCount(t, &TeamUser{TeamID: team.ID}, team.NumMembers)
  136. assertCount(t, &TeamRepo{TeamID: team.ID}, team.NumRepos)
  137. }
  138. func (action *Action) checkForConsistency(t *testing.T) {
  139. repo := AssertExistsAndLoadBean(t, &Repository{ID: action.RepoID}).(*Repository)
  140. assert.Equal(t, repo.IsPrivate, action.IsPrivate, "action: %+v", action)
  141. }