|
| 1 | +// Copyright 2021 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 | + |
| 5 | +package migrations |
| 6 | + |
| 7 | +import ( |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | +) |
| 12 | + |
| 13 | +func Test_addRepoIDForAttachment(t *testing.T) { |
| 14 | + type Attachment struct { |
| 15 | + ID int64 `xorm:"pk autoincr"` |
| 16 | + UUID string `xorm:"uuid UNIQUE"` |
| 17 | + RepoID int64 `xorm:"INDEX"` // this should not be zero |
| 18 | + IssueID int64 `xorm:"INDEX"` // maybe zero when creating |
| 19 | + ReleaseID int64 `xorm:"INDEX"` // maybe zero when creating |
| 20 | + UploaderID int64 `xorm:"INDEX DEFAULT 0"` |
| 21 | + } |
| 22 | + |
| 23 | + type Issue struct { |
| 24 | + ID int64 |
| 25 | + RepoID int64 |
| 26 | + } |
| 27 | + |
| 28 | + type Release struct { |
| 29 | + ID int64 |
| 30 | + RepoID int64 |
| 31 | + } |
| 32 | + |
| 33 | + // Prepare and load the testing database |
| 34 | + x, deferrable := prepareTestEnv(t, 0, new(Attachment), new(Issue), new(Release)) |
| 35 | + defer deferrable() |
| 36 | + if x == nil || t.Failed() { |
| 37 | + return |
| 38 | + } |
| 39 | + |
| 40 | + // Run the migration |
| 41 | + if err := addRepoIDForAttachment(x); err != nil { |
| 42 | + assert.NoError(t, err) |
| 43 | + return |
| 44 | + } |
| 45 | + |
| 46 | + var issueAttachments []*Attachment |
| 47 | + err := x.Where("issue_id > 0").Find(&issueAttachments) |
| 48 | + assert.NoError(t, err) |
| 49 | + for _, attach := range issueAttachments { |
| 50 | + assert.Greater(t, attach.RepoID, 0) |
| 51 | + assert.Greater(t, attach.IssueID, 0) |
| 52 | + var issue Issue |
| 53 | + has, err := x.ID(attach.IssueID).Get(&issue) |
| 54 | + assert.NoError(t, err) |
| 55 | + assert.True(t, has) |
| 56 | + assert.EqualValues(t, attach.RepoID, issue.RepoID) |
| 57 | + } |
| 58 | + |
| 59 | + var releaseAttachments []*Attachment |
| 60 | + err = x.Where("release_id > 0").Find(&releaseAttachments) |
| 61 | + assert.NoError(t, err) |
| 62 | + for _, attach := range releaseAttachments { |
| 63 | + assert.Greater(t, attach.RepoID, 0) |
| 64 | + assert.Greater(t, attach.IssueID, 0) |
| 65 | + var release Release |
| 66 | + has, err := x.ID(attach.ReleaseID).Get(&release) |
| 67 | + assert.NoError(t, err) |
| 68 | + assert.True(t, has) |
| 69 | + assert.EqualValues(t, attach.RepoID, release.RepoID) |
| 70 | + } |
| 71 | +} |
0 commit comments