gitea源码

v294_test.go 1.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Copyright 2024 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package v1_22
  4. import (
  5. "testing"
  6. "code.gitea.io/gitea/models/migrations/base"
  7. "github.com/stretchr/testify/assert"
  8. "xorm.io/xorm/schemas"
  9. )
  10. func Test_AddUniqueIndexForProjectIssue(t *testing.T) {
  11. type ProjectIssue struct { //revive:disable-line:exported
  12. ID int64 `xorm:"pk autoincr"`
  13. IssueID int64 `xorm:"INDEX"`
  14. ProjectID int64 `xorm:"INDEX"`
  15. }
  16. // Prepare and load the testing database
  17. x, deferable := base.PrepareTestEnv(t, 0, new(ProjectIssue))
  18. defer deferable()
  19. if x == nil || t.Failed() {
  20. return
  21. }
  22. cnt, err := x.Table("project_issue").Where("project_id=1 AND issue_id=1").Count()
  23. assert.NoError(t, err)
  24. assert.EqualValues(t, 2, cnt)
  25. assert.NoError(t, AddUniqueIndexForProjectIssue(x))
  26. cnt, err = x.Table("project_issue").Where("project_id=1 AND issue_id=1").Count()
  27. assert.NoError(t, err)
  28. assert.EqualValues(t, 1, cnt)
  29. tables, err := x.DBMetas()
  30. assert.NoError(t, err)
  31. assert.Len(t, tables, 1)
  32. found := false
  33. for _, index := range tables[0].Indexes {
  34. if index.Type == schemas.UniqueType {
  35. found = true
  36. assert.ElementsMatch(t, index.Cols, []string{"project_id", "issue_id"})
  37. break
  38. }
  39. }
  40. assert.True(t, found)
  41. }