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