test: consolidate Go tests (#39962)

Consolidate all Go tests. Coverage is identical to before. Go tests are
around 5% faster while being 7% smaller. No non-test code is touched.
This commit is contained in:
silverwind authored and GitHub committed 2026-10-24 13:48:19 +00:00
1 parent ce7364e215
commit 8abda62312
388 files changed
+18410 -29561

No files matched your search

+51 -66
View File
@@ -160,44 +160,61 @@ func TestGiteaUploadRemapLocalUser(t *testing.T) {
// call remapLocalUser
uploader.sameApp = true
externalID := int64(1234567)
externalName := "username"
source := base.Release{
PublisherID: externalID,
PublisherName: externalName,
for _, tc := range []struct {
source base.Release
expectedUserID int64
}{
//
// The externalID does not match any existing user, everything
// belongs to the doer
//
{base.Release{PublisherID: 1234567, PublisherName: "username"}, doer.ID},
//
// The externalID matches a known user but the name does not match,
// everything belongs to the doer
//
{base.Release{PublisherID: user.ID, PublisherName: "username"}, doer.ID},
//
// The externalID and externalName match an existing user, everything
// belongs to the existing user
//
{base.Release{PublisherID: user.ID, PublisherName: user.Name}, user.ID},
} {
target := repo_model.Release{}
uploader.userMap = make(map[int64]int64)
err := uploader.remapUser(ctx, &tc.source, &target)
assert.NoError(t, err)
assert.Equal(t, tc.expectedUserID, target.GetUserID())
}
//
// The externalID does not match any existing user, everything
// belongs to the doer
//
target := repo_model.Release{}
uploader.userMap = make(map[int64]int64)
err := uploader.remapUser(ctx, &source, &target)
assert.NoError(t, err)
assert.Equal(t, doer.ID, target.GetUserID())
t.Run("TimelineLabelAndMilestoneComments", func(t *testing.T) {
issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 1})
label := unittest.AssertExistsAndLoadBean(t, &issues_model.Label{ID: 1})
milestone := unittest.AssertExistsAndLoadBean(t, &issues_model.Milestone{ID: 1})
//
// The externalID matches a known user but the name does not match,
// everything belongs to the doer
//
source.PublisherID = user.ID
target = repo_model.Release{}
uploader.userMap = make(map[int64]int64)
err = uploader.remapUser(ctx, &source, &target)
assert.NoError(t, err)
assert.Equal(t, doer.ID, target.GetUserID())
uploader := NewGiteaLocalUploader(ctx, doer, doer.Name, "migrated")
uploader.issues[issue.Index] = issue
uploader.labels[label.Name] = label
uploader.milestones[milestone.Name] = milestone.ID
//
// The externalID and externalName match an existing user, everything
// belongs to the existing user
//
source.PublisherName = user.Name
target = repo_model.Release{}
uploader.userMap = make(map[int64]int64)
err = uploader.remapUser(ctx, &source, &target)
assert.NoError(t, err)
assert.Equal(t, user.ID, target.GetUserID())
require.NoError(t, uploader.CreateComments(ctx,
&base.Comment{IssueIndex: issue.Index, CommentType: "label", Content: "1", Meta: map[string]any{"LabelName": label.Name}},
&base.Comment{IssueIndex: issue.Index, CommentType: "label", Meta: map[string]any{"LabelName": "deleted-upstream"}},
&base.Comment{IssueIndex: issue.Index, CommentType: "milestone", Meta: map[string]any{"MilestoneTitle": milestone.Name}},
&base.Comment{IssueIndex: issue.Index, CommentType: "milestone", Meta: map[string]any{"MilestoneTitle": milestone.Name, "Removed": true}},
&base.Comment{IssueIndex: issue.Index, CommentType: "delete_branch", Content: "feature"},
))
unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{IssueID: issue.ID, Type: issues_model.CommentTypeLabel, LabelID: label.ID, Content: "1"})
unittest.AssertCountByCond(t, "comment", builder.Eq{
"issue_id": issue.ID,
"type": issues_model.CommentTypeLabel,
"label_id": 0,
}, 0)
unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{IssueID: issue.ID, Type: issues_model.CommentTypeMilestone, MilestoneID: milestone.ID})
unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{IssueID: issue.ID, Type: issues_model.CommentTypeMilestone, OldMilestoneID: milestone.ID})
assert.Empty(t, unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{IssueID: issue.ID, Type: issues_model.CommentTypeDeleteBranch, OldRef: "feature"}).Content)
})
}
func TestGiteaUploadRemapExternalUser(t *testing.T) {
@@ -332,35 +349,3 @@ func TestGiteaUploadForkPullRequestHeadInRepository(t *testing.T) {
require.NoError(t, err)
assert.Empty(t, strings.TrimSpace(remotes), "no remote is added for a local head")
}
func TestGiteaUploadTimelineLabelAndMilestoneComments(t *testing.T) {
unittest.PrepareTestEnv(t)
doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 1})
label := unittest.AssertExistsAndLoadBean(t, &issues_model.Label{ID: 1})
milestone := unittest.AssertExistsAndLoadBean(t, &issues_model.Milestone{ID: 1})
ctx := t.Context()
uploader := NewGiteaLocalUploader(ctx, doer, doer.Name, "migrated")
uploader.issues[issue.Index] = issue
uploader.labels[label.Name] = label
uploader.milestones[milestone.Name] = milestone.ID
require.NoError(t, uploader.CreateComments(ctx,
&base.Comment{IssueIndex: issue.Index, CommentType: "label", Content: "1", Meta: map[string]any{"LabelName": label.Name}},
&base.Comment{IssueIndex: issue.Index, CommentType: "label", Meta: map[string]any{"LabelName": "deleted-upstream"}},
&base.Comment{IssueIndex: issue.Index, CommentType: "milestone", Meta: map[string]any{"MilestoneTitle": milestone.Name}},
&base.Comment{IssueIndex: issue.Index, CommentType: "milestone", Meta: map[string]any{"MilestoneTitle": milestone.Name, "Removed": true}},
&base.Comment{IssueIndex: issue.Index, CommentType: "delete_branch", Content: "feature"},
))
unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{IssueID: issue.ID, Type: issues_model.CommentTypeLabel, LabelID: label.ID, Content: "1"})
unittest.AssertCountByCond(t, "comment", builder.Eq{
"issue_id": issue.ID,
"type": issues_model.CommentTypeLabel,
"label_id": 0,
}, 0)
unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{IssueID: issue.ID, Type: issues_model.CommentTypeMilestone, MilestoneID: milestone.ID})
unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{IssueID: issue.ID, Type: issues_model.CommentTypeMilestone, OldMilestoneID: milestone.ID})
assert.Empty(t, unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{IssueID: issue.ID, Type: issues_model.CommentTypeDeleteBranch, OldRef: "feature"}).Content)
}
+27 -60
View File
@@ -599,73 +599,40 @@ func TestNoteToComment(t *testing.T) {
downloader := &GitlabDownloader{}
now := time.Now()
makeTestNote := func(id int64, body string, system bool) gitlab.Note {
return gitlab.Note{
ID: id,
for _, testCase := range []struct {
id int64
body string
system bool
commentType string
meta map[string]any
}{
{1, "This is a regular comment", false, "", map[string]any{}},
{2, "enabled an automatic merge for abcd1234", true, "pull_scheduled_merge", map[string]any{}},
{3, "changed target branch from `master` to `main`", true, "change_target_branch", map[string]any{"OldRef": "master", "NewRef": "main"}},
{4, "canceled the automatic merge", true, "pull_cancel_scheduled_merge", map[string]any{}},
} {
note := gitlab.Note{
ID: testCase.id,
Author: gitlab.NoteAuthor{
ID: 72,
Email: "test@example.com",
Username: "test",
},
Body: body,
Body: testCase.body,
CreatedAt: &now,
System: system,
System: testCase.system,
}
}
notes := []gitlab.Note{
makeTestNote(1, "This is a regular comment", false),
makeTestNote(2, "enabled an automatic merge for abcd1234", true),
makeTestNote(3, "changed target branch from `master` to `main`", true),
makeTestNote(4, "canceled the automatic merge", true),
}
comments := []base.Comment{{
IssueIndex: 17,
Index: 1,
PosterID: 72,
PosterName: "test",
PosterEmail: "test@example.com",
CommentType: "",
Content: "This is a regular comment",
Created: now,
Meta: map[string]any{},
}, {
IssueIndex: 17,
Index: 2,
PosterID: 72,
PosterName: "test",
PosterEmail: "test@example.com",
CommentType: "pull_scheduled_merge",
Content: "enabled an automatic merge for abcd1234",
Created: now,
Meta: map[string]any{},
}, {
IssueIndex: 17,
Index: 3,
PosterID: 72,
PosterName: "test",
PosterEmail: "test@example.com",
CommentType: "change_target_branch",
Content: "changed target branch from `master` to `main`",
Created: now,
Meta: map[string]any{
"OldRef": "master",
"NewRef": "main",
},
}, {
IssueIndex: 17,
Index: 4,
PosterID: 72,
PosterName: "test",
PosterEmail: "test@example.com",
CommentType: "pull_cancel_scheduled_merge",
Content: "canceled the automatic merge",
Created: now,
Meta: map[string]any{},
}}
for i, note := range notes {
actualComment := *downloader.convertNoteToComment(17, &note)
assert.Equal(t, actualComment, comments[i])
assert.Equal(t, base.Comment{
IssueIndex: 17,
Index: testCase.id,
PosterID: 72,
PosterName: "test",
PosterEmail: "test@example.com",
CommentType: testCase.commentType,
Content: testCase.body,
Created: now,
Meta: testCase.meta,
}, *downloader.convertNoteToComment(17, &note))
}
}