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

consistency_test.go 5.6 kB

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