Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

consistency.go 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. "xorm.io/builder"
  11. )
  12. // consistencyCheckable a type that can be tested for database consistency
  13. type consistencyCheckable interface {
  14. checkForConsistency(t *testing.T)
  15. }
  16. // CheckConsistencyForAll test that the entire database is consistent
  17. func CheckConsistencyForAll(t *testing.T) {
  18. CheckConsistencyFor(t,
  19. &User{},
  20. &Repository{},
  21. &Issue{},
  22. &PullRequest{},
  23. &Milestone{},
  24. &Label{},
  25. &Team{},
  26. &Action{})
  27. }
  28. // CheckConsistencyFor test that all matching database entries are consistent
  29. func CheckConsistencyFor(t *testing.T, beansToCheck ...interface{}) {
  30. for _, bean := range beansToCheck {
  31. sliceType := reflect.SliceOf(reflect.TypeOf(bean))
  32. sliceValue := reflect.MakeSlice(sliceType, 0, 10)
  33. ptrToSliceValue := reflect.New(sliceType)
  34. ptrToSliceValue.Elem().Set(sliceValue)
  35. assert.NoError(t, x.Table(bean).Find(ptrToSliceValue.Interface()))
  36. sliceValue = ptrToSliceValue.Elem()
  37. for i := 0; i < sliceValue.Len(); i++ {
  38. entity := sliceValue.Index(i).Interface()
  39. checkable, ok := entity.(consistencyCheckable)
  40. if !ok {
  41. t.Errorf("Expected %+v (of type %T) to be checkable for consistency",
  42. entity, entity)
  43. } else {
  44. checkable.checkForConsistency(t)
  45. }
  46. }
  47. }
  48. }
  49. // getCount get the count of database entries matching bean
  50. func getCount(t *testing.T, e Engine, bean interface{}) int64 {
  51. count, err := e.Count(bean)
  52. assert.NoError(t, err)
  53. return count
  54. }
  55. // assertCount test the count of database entries matching bean
  56. func assertCount(t *testing.T, bean interface{}, expected int) {
  57. assert.EqualValues(t, expected, getCount(t, x, bean),
  58. "Failed consistency test, the counted bean (of type %T) was %+v", bean, bean)
  59. }
  60. func (user *User) checkForConsistency(t *testing.T) {
  61. assertCount(t, &Repository{OwnerID: user.ID}, user.NumRepos)
  62. assertCount(t, &Star{UID: user.ID}, user.NumStars)
  63. assertCount(t, &OrgUser{OrgID: user.ID}, user.NumMembers)
  64. assertCount(t, &Team{OrgID: user.ID}, user.NumTeams)
  65. assertCount(t, &Follow{UserID: user.ID}, user.NumFollowing)
  66. assertCount(t, &Follow{FollowID: user.ID}, user.NumFollowers)
  67. if user.Type != UserTypeOrganization {
  68. assert.EqualValues(t, 0, user.NumMembers)
  69. assert.EqualValues(t, 0, user.NumTeams)
  70. }
  71. }
  72. func (repo *Repository) checkForConsistency(t *testing.T) {
  73. assert.Equal(t, repo.LowerName, strings.ToLower(repo.Name), "repo: %+v", repo)
  74. assertCount(t, &Star{RepoID: repo.ID}, repo.NumStars)
  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("Mode<>?", RepoWatchModeDont), &Watch{RepoID: repo.ID})
  81. assert.EqualValues(t, repo.NumWatches, actual,
  82. "Unexpected number of watches for repo %+v", repo)
  83. actual = getCount(t, x.Where("is_pull=?", false), &Issue{RepoID: repo.ID})
  84. assert.EqualValues(t, repo.NumIssues, actual,
  85. "Unexpected number of issues for repo %+v", repo)
  86. actual = getCount(t, x.Where("is_pull=? AND is_closed=?", false, true), &Issue{RepoID: repo.ID})
  87. assert.EqualValues(t, repo.NumClosedIssues, actual,
  88. "Unexpected number of closed issues for repo %+v", repo)
  89. actual = getCount(t, x.Where("is_pull=?", true), &Issue{RepoID: repo.ID})
  90. assert.EqualValues(t, repo.NumPulls, actual,
  91. "Unexpected number of pulls for repo %+v", repo)
  92. actual = getCount(t, x.Where("is_pull=? AND is_closed=?", true, true), &Issue{RepoID: repo.ID})
  93. assert.EqualValues(t, repo.NumClosedPulls, actual,
  94. "Unexpected number of closed pulls for repo %+v", repo)
  95. actual = getCount(t, x.Where("is_closed=?", true), &Milestone{RepoID: repo.ID})
  96. assert.EqualValues(t, repo.NumClosedMilestones, actual,
  97. "Unexpected number of closed milestones for repo %+v", repo)
  98. }
  99. func (issue *Issue) checkForConsistency(t *testing.T) {
  100. actual := getCount(t, x.Where("type=?", CommentTypeComment), &Comment{IssueID: issue.ID})
  101. assert.EqualValues(t, issue.NumComments, actual,
  102. "Unexpected number of comments for issue %+v", issue)
  103. if issue.IsPull {
  104. pr := AssertExistsAndLoadBean(t, &PullRequest{IssueID: issue.ID}).(*PullRequest)
  105. assert.EqualValues(t, pr.Index, issue.Index)
  106. }
  107. }
  108. func (pr *PullRequest) checkForConsistency(t *testing.T) {
  109. issue := AssertExistsAndLoadBean(t, &Issue{ID: pr.IssueID}).(*Issue)
  110. assert.True(t, issue.IsPull)
  111. assert.EqualValues(t, issue.Index, pr.Index)
  112. }
  113. func (milestone *Milestone) checkForConsistency(t *testing.T) {
  114. assertCount(t, &Issue{MilestoneID: milestone.ID}, milestone.NumIssues)
  115. actual := getCount(t, x.Where("is_closed=?", true), &Issue{MilestoneID: milestone.ID})
  116. assert.EqualValues(t, milestone.NumClosedIssues, actual,
  117. "Unexpected number of closed issues for milestone %+v", milestone)
  118. }
  119. func (label *Label) checkForConsistency(t *testing.T) {
  120. issueLabels := make([]*IssueLabel, 0, 10)
  121. assert.NoError(t, x.Find(&issueLabels, &IssueLabel{LabelID: label.ID}))
  122. assert.EqualValues(t, label.NumIssues, len(issueLabels),
  123. "Unexpected number of issue for label %+v", label)
  124. issueIDs := make([]int64, len(issueLabels))
  125. for i, issueLabel := range issueLabels {
  126. issueIDs[i] = issueLabel.IssueID
  127. }
  128. expected := int64(0)
  129. if len(issueIDs) > 0 {
  130. expected = getCount(t, x.In("id", issueIDs).Where("is_closed=?", true), &Issue{})
  131. }
  132. assert.EqualValues(t, expected, label.NumClosedIssues,
  133. "Unexpected number of closed issues for label %+v", label)
  134. }
  135. func (team *Team) checkForConsistency(t *testing.T) {
  136. assertCount(t, &TeamUser{TeamID: team.ID}, team.NumMembers)
  137. assertCount(t, &TeamRepo{TeamID: team.ID}, team.NumRepos)
  138. }
  139. func (action *Action) checkForConsistency(t *testing.T) {
  140. repo := AssertExistsAndLoadBean(t, &Repository{ID: action.RepoID}).(*Repository)
  141. assert.Equal(t, repo.IsPrivate, action.IsPrivate, "action: %+v", action)
  142. }
  143. // CountOrphanedObjects count subjects with have no existing refobject anymore
  144. func CountOrphanedObjects(subject, refobject, joinCond string) (int64, error) {
  145. var ids []int64
  146. return int64(len(ids)), x.Table("`"+subject+"`").
  147. Join("LEFT", refobject, joinCond).
  148. Where(builder.IsNull{"`" + refobject + "`.id"}).
  149. Select("id").Find(&ids)
  150. }
  151. // DeleteOrphanedObjects delete subjects with have no existing refobject anymore
  152. func DeleteOrphanedObjects(subject, refobject, joinCond string) error {
  153. _, err := x.In("id", builder.Select("`"+subject+"`.id").
  154. From("`"+subject+"`").
  155. Join("LEFT", "`"+refobject+"`", joinCond).
  156. Where(builder.IsNull{"`" + refobject + "`.id"})).
  157. Delete("`" + subject + "`")
  158. return err
  159. }