Skip to content

Commit d748d25

Browse files
authored
Merge branch 'main' into disk-space-calc
2 parents ce0e4e6 + ade41f3 commit d748d25

File tree

3 files changed

+173
-1
lines changed

3 files changed

+173
-1
lines changed

models/fixtures/milestone.yml

+5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
content: content1
66
is_closed: false
77
num_issues: 1
8+
num_closed_issues: 0
89

910
-
1011
id: 2
@@ -13,6 +14,7 @@
1314
content: content2
1415
is_closed: false
1516
num_issues: 0
17+
num_closed_issues: 0
1618

1719
-
1820
id: 3
@@ -21,6 +23,7 @@
2123
content: content3
2224
is_closed: true
2325
num_issues: 1
26+
num_closed_issues: 0
2427

2528
-
2629
id: 4
@@ -29,6 +32,7 @@
2932
content: content random
3033
is_closed: false
3134
num_issues: 0
35+
num_closed_issues: 0
3236

3337
-
3438
id: 5
@@ -37,3 +41,4 @@
3741
content: for testing with PRs
3842
is_closed: false
3943
num_issues: 0
44+
num_closed_issues: 0

models/migrate.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ func InsertIssueComments(comments []*Comment) error {
166166
}
167167

168168
for issueID := range issueIDs {
169-
if _, err := db.Exec(ctx, "UPDATE issue set num_comments = (SELECT count(*) FROM comment WHERE issue_id = ?) WHERE id = ?", issueID, issueID); err != nil {
169+
if _, err := db.Exec(ctx, "UPDATE issue set num_comments = (SELECT count(*) FROM comment WHERE issue_id = ? AND `type`=?) WHERE id = ?", issueID, CommentTypeComment, issueID); err != nil {
170170
return err
171171
}
172172
}

models/migrate_test.go

+167
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
// Copyright 2022 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 models
6+
7+
import (
8+
"testing"
9+
10+
repo_model "code.gitea.io/gitea/models/repo"
11+
"code.gitea.io/gitea/models/unittest"
12+
user_model "code.gitea.io/gitea/models/user"
13+
14+
"github.com/stretchr/testify/assert"
15+
)
16+
17+
func TestMigrate_InsertMilestones(t *testing.T) {
18+
assert.NoError(t, unittest.PrepareTestDatabase())
19+
reponame := "repo1"
20+
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{Name: reponame}).(*repo_model.Repository)
21+
name := "milestonetest1"
22+
ms := &Milestone{
23+
RepoID: repo.ID,
24+
Name: name,
25+
}
26+
err := InsertMilestones(ms)
27+
assert.NoError(t, err)
28+
unittest.AssertExistsAndLoadBean(t, ms)
29+
repoModified := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: repo.ID}).(*repo_model.Repository)
30+
assert.EqualValues(t, repo.NumMilestones+1, repoModified.NumMilestones)
31+
32+
unittest.CheckConsistencyFor(t, &Milestone{})
33+
}
34+
35+
func assertCreateIssues(t *testing.T, reponame string, isPull bool) {
36+
assert.NoError(t, unittest.PrepareTestDatabase())
37+
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{Name: reponame}).(*repo_model.Repository)
38+
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID}).(*user_model.User)
39+
label := unittest.AssertExistsAndLoadBean(t, &Label{ID: 1}).(*Label)
40+
milestone := unittest.AssertExistsAndLoadBean(t, &Milestone{ID: 1}).(*Milestone)
41+
assert.EqualValues(t, milestone.ID, 1)
42+
reaction := &Reaction{
43+
Type: "heart",
44+
UserID: owner.ID,
45+
}
46+
47+
title := "issuetitle1"
48+
var is = &Issue{
49+
RepoID: repo.ID,
50+
MilestoneID: milestone.ID,
51+
Repo: repo,
52+
Title: title,
53+
Content: "issuecontent1",
54+
IsPull: isPull,
55+
PosterID: owner.ID,
56+
Poster: owner,
57+
IsClosed: true,
58+
Labels: []*Label{label},
59+
Reactions: []*Reaction{reaction},
60+
}
61+
err := InsertIssues(is)
62+
assert.NoError(t, err)
63+
64+
i := unittest.AssertExistsAndLoadBean(t, &Issue{Title: title}).(*Issue)
65+
unittest.AssertExistsAndLoadBean(t, &Reaction{Type: "heart", UserID: owner.ID, IssueID: i.ID})
66+
67+
labelModified := unittest.AssertExistsAndLoadBean(t, &Label{ID: 1}).(*Label)
68+
assert.EqualValues(t, label.NumIssues+1, labelModified.NumIssues)
69+
assert.EqualValues(t, label.NumClosedIssues+1, labelModified.NumClosedIssues)
70+
71+
milestoneModified := unittest.AssertExistsAndLoadBean(t, &Milestone{ID: milestone.ID}).(*Milestone)
72+
assert.EqualValues(t, milestone.NumIssues+1, milestoneModified.NumIssues)
73+
assert.EqualValues(t, milestone.NumClosedIssues+1, milestoneModified.NumClosedIssues)
74+
}
75+
76+
func TestMigrate_CreateIssuesIsPullFalse(t *testing.T) {
77+
assert.NoError(t, unittest.PrepareTestDatabase())
78+
reponame := "repo1"
79+
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{Name: reponame}).(*repo_model.Repository)
80+
81+
assertCreateIssues(t, reponame, false)
82+
83+
repoModified := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: repo.ID}).(*repo_model.Repository)
84+
assert.EqualValues(t, repo.NumIssues+1, repoModified.NumIssues)
85+
assert.EqualValues(t, repo.NumClosedIssues+1, repoModified.NumClosedIssues)
86+
}
87+
88+
func TestMigrate_CreateIssuesIsPullTrue(t *testing.T) {
89+
assert.NoError(t, unittest.PrepareTestDatabase())
90+
reponame := "repo1"
91+
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{Name: reponame}).(*repo_model.Repository)
92+
93+
assertCreateIssues(t, reponame, true)
94+
95+
repoModified := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: repo.ID}).(*repo_model.Repository)
96+
assert.EqualValues(t, repo.NumPulls+1, repoModified.NumPulls)
97+
assert.EqualValues(t, repo.NumClosedPulls+1, repoModified.NumClosedPulls)
98+
}
99+
100+
func TestMigrate_InsertIssueComments(t *testing.T) {
101+
assert.NoError(t, unittest.PrepareTestDatabase())
102+
issue := unittest.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue)
103+
_ = issue.LoadRepo()
104+
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: issue.Repo.OwnerID}).(*user_model.User)
105+
reaction := &Reaction{
106+
Type: "heart",
107+
UserID: owner.ID,
108+
}
109+
110+
comment := &Comment{
111+
PosterID: owner.ID,
112+
Poster: owner,
113+
IssueID: issue.ID,
114+
Issue: issue,
115+
Reactions: []*Reaction{reaction},
116+
}
117+
118+
err := InsertIssueComments([]*Comment{comment})
119+
assert.NoError(t, err)
120+
121+
issueModified := unittest.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue)
122+
assert.EqualValues(t, issue.NumComments+1, issueModified.NumComments)
123+
124+
unittest.CheckConsistencyFor(t, &Issue{})
125+
}
126+
127+
func TestMigrate_InsertPullRequests(t *testing.T) {
128+
assert.NoError(t, unittest.PrepareTestDatabase())
129+
reponame := "repo1"
130+
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{Name: reponame}).(*repo_model.Repository)
131+
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID}).(*user_model.User)
132+
133+
var i = &Issue{
134+
RepoID: repo.ID,
135+
Repo: repo,
136+
Title: "title1",
137+
Content: "issuecontent1",
138+
IsPull: true,
139+
PosterID: owner.ID,
140+
Poster: owner,
141+
}
142+
143+
var p = &PullRequest{
144+
Issue: i,
145+
}
146+
147+
err := InsertPullRequests(p)
148+
assert.NoError(t, err)
149+
150+
_ = unittest.AssertExistsAndLoadBean(t, &PullRequest{IssueID: i.ID}).(*PullRequest)
151+
152+
unittest.CheckConsistencyFor(t, &Issue{}, &PullRequest{})
153+
}
154+
155+
func TestMigrate_InsertReleases(t *testing.T) {
156+
assert.NoError(t, unittest.PrepareTestDatabase())
157+
158+
a := &repo_model.Attachment{
159+
UUID: "a0eebc91-9c0c-4ef7-bb6e-6bb9bd380a12",
160+
}
161+
r := &Release{
162+
Attachments: []*repo_model.Attachment{a},
163+
}
164+
165+
err := InsertReleases(r)
166+
assert.NoError(t, err)
167+
}

0 commit comments

Comments
 (0)