Browse Source

Integration test for activity page (#2704)

* Integration test for activity page

* Small code refactoring for acitvity page

* Move activity stats calculation logic to model
Lauris BH 1 year ago
parent
commit
c7f4f07765

+ 4 - 9
integrations/editor_test.go

@@ -89,10 +89,7 @@ func TestCreateFileOnProtectedBranch(t *testing.T) {
89 89
 
90 90
 }
91 91
 
92
-func testEditFile(t *testing.T, session *TestSession, user, repo, branch, filePath string) *TestResponse {
93
-
94
-	newContent := "Hello, World (Edited)\n"
95
-
92
+func testEditFile(t *testing.T, session *TestSession, user, repo, branch, filePath, newContent string) *TestResponse {
96 93
 	// Get to the 'edit this file' page
97 94
 	req := NewRequest(t, "GET", path.Join(user, repo, "_edit", branch, filePath))
98 95
 	resp := session.MakeRequest(t, req, http.StatusOK)
@@ -121,9 +118,7 @@ func testEditFile(t *testing.T, session *TestSession, user, repo, branch, filePa
121 118
 	return resp
122 119
 }
123 120
 
124
-func testEditFileToNewBranch(t *testing.T, session *TestSession, user, repo, branch, targetBranch, filePath string) *TestResponse {
125
-
126
-	newContent := "Hello, World (Edited)\n"
121
+func testEditFileToNewBranch(t *testing.T, session *TestSession, user, repo, branch, targetBranch, filePath, newContent string) *TestResponse {
127 122
 
128 123
 	// Get to the 'edit this file' page
129 124
 	req := NewRequest(t, "GET", path.Join(user, repo, "_edit", branch, filePath))
@@ -157,11 +152,11 @@ func testEditFileToNewBranch(t *testing.T, session *TestSession, user, repo, bra
157 152
 func TestEditFile(t *testing.T) {
158 153
 	prepareTestEnv(t)
159 154
 	session := loginUser(t, "user2")
160
-	testEditFile(t, session, "user2", "repo1", "master", "README.md")
155
+	testEditFile(t, session, "user2", "repo1", "master", "README.md", "Hello, World (Edited)\n")
161 156
 }
162 157
 
163 158
 func TestEditFileToNewBranch(t *testing.T) {
164 159
 	prepareTestEnv(t)
165 160
 	session := loginUser(t, "user2")
166
-	testEditFileToNewBranch(t, session, "user2", "repo1", "master", "feature/test", "README.md")
161
+	testEditFileToNewBranch(t, session, "user2", "repo1", "master", "feature/test", "README.md", "Hello, World (Edited)\n")
167 162
 }

+ 1 - 1
integrations/pull_create_test.go

@@ -47,6 +47,6 @@ func TestPullCreate(t *testing.T) {
47 47
 	prepareTestEnv(t)
48 48
 	session := loginUser(t, "user1")
49 49
 	testRepoFork(t, session, "user2", "repo1", "user1", "repo1")
50
-	testEditFile(t, session, "user1", "repo1", "master", "README.md")
50
+	testEditFile(t, session, "user1", "repo1", "master", "README.md", "Hello, World (Edited)\n")
51 51
 	testPullCreate(t, session, "user1", "repo1", "master")
52 52
 }

+ 2 - 2
integrations/pull_merge_test.go

@@ -49,7 +49,7 @@ func TestPullMerge(t *testing.T) {
49 49
 	prepareTestEnv(t)
50 50
 	session := loginUser(t, "user1")
51 51
 	testRepoFork(t, session, "user2", "repo1", "user1", "repo1")
52
-	testEditFile(t, session, "user1", "repo1", "master", "README.md")
52
+	testEditFile(t, session, "user1", "repo1", "master", "README.md", "Hello, World (Edited)\n")
53 53
 
54 54
 	resp := testPullCreate(t, session, "user1", "repo1", "master")
55 55
 
@@ -62,7 +62,7 @@ func TestPullCleanUpAfterMerge(t *testing.T) {
62 62
 	prepareTestEnv(t)
63 63
 	session := loginUser(t, "user1")
64 64
 	testRepoFork(t, session, "user2", "repo1", "user1", "repo1")
65
-	testEditFileToNewBranch(t, session, "user1", "repo1", "master", "feature/test", "README.md")
65
+	testEditFileToNewBranch(t, session, "user1", "repo1", "master", "feature/test", "README.md", "Hello, World (Edited)\n")
66 66
 
67 67
 	resp := testPullCreate(t, session, "user1", "repo1", "feature/test")
68 68
 

+ 61 - 0
integrations/repo_activity_test.go

@@ -0,0 +1,61 @@
1
+// Copyright 2017 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 integrations
6
+
7
+import (
8
+	"net/http"
9
+	"strings"
10
+	"testing"
11
+
12
+	"github.com/stretchr/testify/assert"
13
+)
14
+
15
+func TestRepoActivity(t *testing.T) {
16
+	prepareTestEnv(t)
17
+	session := loginUser(t, "user1")
18
+
19
+	// Create PRs (1 merged & 2 proposed)
20
+	testRepoFork(t, session, "user2", "repo1", "user1", "repo1")
21
+	testEditFile(t, session, "user1", "repo1", "master", "README.md", "Hello, World (Edited)\n")
22
+	resp := testPullCreate(t, session, "user1", "repo1", "master")
23
+	elem := strings.Split(RedirectURL(t, resp), "/")
24
+	assert.EqualValues(t, "pulls", elem[3])
25
+	testPullMerge(t, session, elem[1], elem[2], elem[4])
26
+
27
+	testEditFileToNewBranch(t, session, "user1", "repo1", "master", "feat/better_readme", "README.md", "Hello, World (Edited Again)\n")
28
+	testPullCreate(t, session, "user1", "repo1", "feat/better_readme")
29
+
30
+	testEditFileToNewBranch(t, session, "user1", "repo1", "master", "feat/much_better_readme", "README.md", "Hello, World (Edited More)\n")
31
+	testPullCreate(t, session, "user1", "repo1", "feat/much_better_readme")
32
+
33
+	// Create issues (3 new issues)
34
+	testNewIssue(t, session, "user2", "repo1", "Issue 1")
35
+	testNewIssue(t, session, "user2", "repo1", "Issue 2")
36
+	testNewIssue(t, session, "user2", "repo1", "Issue 3")
37
+
38
+	// Create releases (1 new release)
39
+	createNewRelease(t, session, "/user2/repo1", "v1.0.0", "v1.0.0", false, false)
40
+
41
+	// Open Activity page and check stats
42
+	req := NewRequest(t, "GET", "/user2/repo1/activity")
43
+	resp = session.MakeRequest(t, req, http.StatusOK)
44
+	htmlDoc := NewHTMLParser(t, resp.Body)
45
+
46
+	// Should be 1 published release
47
+	list := htmlDoc.doc.Find("#published-releases").Next().Find("p.desc")
48
+	assert.Len(t, list.Nodes, 1)
49
+
50
+	// Should be 1 merged pull request
51
+	list = htmlDoc.doc.Find("#merged-pull-requests").Next().Find("p.desc")
52
+	assert.Len(t, list.Nodes, 1)
53
+
54
+	// Should be 2 merged proposed pull requests
55
+	list = htmlDoc.doc.Find("#proposed-pull-requests").Next().Find("p.desc")
56
+	assert.Len(t, list.Nodes, 2)
57
+
58
+	// Should be 3 new issues
59
+	list = htmlDoc.doc.Find("#new-issues").Next().Find("p.desc")
60
+	assert.Len(t, list.Nodes, 3)
61
+}

+ 50 - 25
models/repo_activity.go

@@ -5,6 +5,7 @@
5 5
 package models
6 6
 
7 7
 import (
8
+	"fmt"
8 9
 	"time"
9 10
 
10 11
 	"github.com/go-xorm/xorm"
@@ -25,6 +26,30 @@ type ActivityStats struct {
25 26
 	PublishedReleaseAuthorCount int64
26 27
 }
27 28
 
29
+// GetActivityStats return stats for repository at given time range
30
+func GetActivityStats(repoID int64, timeFrom time.Time, releases, issues, prs bool) (*ActivityStats, error) {
31
+	stats := &ActivityStats{}
32
+	if releases {
33
+		if err := stats.FillReleases(repoID, timeFrom); err != nil {
34
+			return nil, fmt.Errorf("FillReleases: %v", err)
35
+		}
36
+	}
37
+	if prs {
38
+		if err := stats.FillPullRequests(repoID, timeFrom); err != nil {
39
+			return nil, fmt.Errorf("FillPullRequests: %v", err)
40
+		}
41
+	}
42
+	if issues {
43
+		if err := stats.FillIssues(repoID, timeFrom); err != nil {
44
+			return nil, fmt.Errorf("FillIssues: %v", err)
45
+		}
46
+	}
47
+	if err := stats.FillUnresolvedIssues(repoID, timeFrom, issues, prs); err != nil {
48
+		return nil, fmt.Errorf("FillUnresolvedIssues: %v", err)
49
+	}
50
+	return stats, nil
51
+}
52
+
28 53
 // ActivePRCount returns total active pull request count
29 54
 func (stats *ActivityStats) ActivePRCount() int {
30 55
 	return stats.OpenedPRCount() + stats.MergedPRCount()
@@ -85,13 +110,13 @@ func (stats *ActivityStats) PublishedReleaseCount() int {
85 110
 	return len(stats.PublishedReleases)
86 111
 }
87 112
 
88
-// FillPullRequestsForActivity returns pull request information for activity page
89
-func FillPullRequestsForActivity(stats *ActivityStats, baseRepoID int64, fromTime time.Time) error {
113
+// FillPullRequests returns pull request information for activity page
114
+func (stats *ActivityStats) FillPullRequests(repoID int64, fromTime time.Time) error {
90 115
 	var err error
91 116
 	var count int64
92 117
 
93 118
 	// Merged pull requests
94
-	sess := pullRequestsForActivityStatement(baseRepoID, fromTime, true)
119
+	sess := pullRequestsForActivityStatement(repoID, fromTime, true)
95 120
 	sess.OrderBy("pull_request.merged_unix DESC")
96 121
 	stats.MergedPRs = make(PullRequestList, 0)
97 122
 	if err = sess.Find(&stats.MergedPRs); err != nil {
@@ -102,14 +127,14 @@ func FillPullRequestsForActivity(stats *ActivityStats, baseRepoID int64, fromTim
102 127
 	}
103 128
 
104 129
 	// Merged pull request authors
105
-	sess = pullRequestsForActivityStatement(baseRepoID, fromTime, true)
130
+	sess = pullRequestsForActivityStatement(repoID, fromTime, true)
106 131
 	if _, err = sess.Select("count(distinct issue.poster_id) as `count`").Table("pull_request").Get(&count); err != nil {
107 132
 		return err
108 133
 	}
109 134
 	stats.MergedPRAuthorCount = count
110 135
 
111 136
 	// Opened pull requests
112
-	sess = pullRequestsForActivityStatement(baseRepoID, fromTime, false)
137
+	sess = pullRequestsForActivityStatement(repoID, fromTime, false)
113 138
 	sess.OrderBy("issue.created_unix ASC")
114 139
 	stats.OpenedPRs = make(PullRequestList, 0)
115 140
 	if err = sess.Find(&stats.OpenedPRs); err != nil {
@@ -120,7 +145,7 @@ func FillPullRequestsForActivity(stats *ActivityStats, baseRepoID int64, fromTim
120 145
 	}
121 146
 
122 147
 	// Opened pull request authors
123
-	sess = pullRequestsForActivityStatement(baseRepoID, fromTime, false)
148
+	sess = pullRequestsForActivityStatement(repoID, fromTime, false)
124 149
 	if _, err = sess.Select("count(distinct issue.poster_id) as `count`").Table("pull_request").Get(&count); err != nil {
125 150
 		return err
126 151
 	}
@@ -129,8 +154,8 @@ func FillPullRequestsForActivity(stats *ActivityStats, baseRepoID int64, fromTim
129 154
 	return nil
130 155
 }
131 156
 
132
-func pullRequestsForActivityStatement(baseRepoID int64, fromTime time.Time, merged bool) *xorm.Session {
133
-	sess := x.Where("pull_request.base_repo_id=?", baseRepoID).
157
+func pullRequestsForActivityStatement(repoID int64, fromTime time.Time, merged bool) *xorm.Session {
158
+	sess := x.Where("pull_request.base_repo_id=?", repoID).
134 159
 		Join("INNER", "issue", "pull_request.issue_id = issue.id")
135 160
 
136 161
 	if merged {
@@ -144,13 +169,13 @@ func pullRequestsForActivityStatement(baseRepoID int64, fromTime time.Time, merg
144 169
 	return sess
145 170
 }
146 171
 
147
-// FillIssuesForActivity returns issue information for activity page
148
-func FillIssuesForActivity(stats *ActivityStats, baseRepoID int64, fromTime time.Time) error {
172
+// FillIssues returns issue information for activity page
173
+func (stats *ActivityStats) FillIssues(repoID int64, fromTime time.Time) error {
149 174
 	var err error
150 175
 	var count int64
151 176
 
152 177
 	// Closed issues
153
-	sess := issuesForActivityStatement(baseRepoID, fromTime, true, false)
178
+	sess := issuesForActivityStatement(repoID, fromTime, true, false)
154 179
 	sess.OrderBy("issue.updated_unix DESC")
155 180
 	stats.ClosedIssues = make(IssueList, 0)
156 181
 	if err = sess.Find(&stats.ClosedIssues); err != nil {
@@ -158,14 +183,14 @@ func FillIssuesForActivity(stats *ActivityStats, baseRepoID int64, fromTime time
158 183
 	}
159 184
 
160 185
 	// Closed issue authors
161
-	sess = issuesForActivityStatement(baseRepoID, fromTime, true, false)
186
+	sess = issuesForActivityStatement(repoID, fromTime, true, false)
162 187
 	if _, err = sess.Select("count(distinct issue.poster_id) as `count`").Table("issue").Get(&count); err != nil {
163 188
 		return err
164 189
 	}
165 190
 	stats.ClosedIssueAuthorCount = count
166 191
 
167 192
 	// New issues
168
-	sess = issuesForActivityStatement(baseRepoID, fromTime, false, false)
193
+	sess = issuesForActivityStatement(repoID, fromTime, false, false)
169 194
 	sess.OrderBy("issue.created_unix ASC")
170 195
 	stats.OpenedIssues = make(IssueList, 0)
171 196
 	if err = sess.Find(&stats.OpenedIssues); err != nil {
@@ -173,7 +198,7 @@ func FillIssuesForActivity(stats *ActivityStats, baseRepoID int64, fromTime time
173 198
 	}
174 199
 
175 200
 	// Opened issue authors
176
-	sess = issuesForActivityStatement(baseRepoID, fromTime, false, false)
201
+	sess = issuesForActivityStatement(repoID, fromTime, false, false)
177 202
 	if _, err = sess.Select("count(distinct issue.poster_id) as `count`").Table("issue").Get(&count); err != nil {
178 203
 		return err
179 204
 	}
@@ -182,13 +207,13 @@ func FillIssuesForActivity(stats *ActivityStats, baseRepoID int64, fromTime time
182 207
 	return nil
183 208
 }
184 209
 
185
-// FillUnresolvedIssuesForActivity returns unresolved issue and pull request information for activity page
186
-func FillUnresolvedIssuesForActivity(stats *ActivityStats, baseRepoID int64, fromTime time.Time, issues, prs bool) error {
210
+// FillUnresolvedIssues returns unresolved issue and pull request information for activity page
211
+func (stats *ActivityStats) FillUnresolvedIssues(repoID int64, fromTime time.Time, issues, prs bool) error {
187 212
 	// Check if we need to select anything
188 213
 	if !issues && !prs {
189 214
 		return nil
190 215
 	}
191
-	sess := issuesForActivityStatement(baseRepoID, fromTime, false, true)
216
+	sess := issuesForActivityStatement(repoID, fromTime, false, true)
192 217
 	if !issues || !prs {
193 218
 		sess.And("issue.is_pull = ?", prs)
194 219
 	}
@@ -197,8 +222,8 @@ func FillUnresolvedIssuesForActivity(stats *ActivityStats, baseRepoID int64, fro
197 222
 	return sess.Find(&stats.UnresolvedIssues)
198 223
 }
199 224
 
200
-func issuesForActivityStatement(baseRepoID int64, fromTime time.Time, closed, unresolved bool) *xorm.Session {
201
-	sess := x.Where("issue.repo_id = ?", baseRepoID).
225
+func issuesForActivityStatement(repoID int64, fromTime time.Time, closed, unresolved bool) *xorm.Session {
226
+	sess := x.Where("issue.repo_id = ?", repoID).
202 227
 		And("issue.is_closed = ?", closed)
203 228
 
204 229
 	if !unresolved {
@@ -212,13 +237,13 @@ func issuesForActivityStatement(baseRepoID int64, fromTime time.Time, closed, un
212 237
 	return sess
213 238
 }
214 239
 
215
-// FillReleasesForActivity returns release information for activity page
216
-func FillReleasesForActivity(stats *ActivityStats, baseRepoID int64, fromTime time.Time) error {
240
+// FillReleases returns release information for activity page
241
+func (stats *ActivityStats) FillReleases(repoID int64, fromTime time.Time) error {
217 242
 	var err error
218 243
 	var count int64
219 244
 
220 245
 	// Published releases list
221
-	sess := releasesForActivityStatement(baseRepoID, fromTime)
246
+	sess := releasesForActivityStatement(repoID, fromTime)
222 247
 	sess.OrderBy("release.created_unix DESC")
223 248
 	stats.PublishedReleases = make([]*Release, 0)
224 249
 	if err = sess.Find(&stats.PublishedReleases); err != nil {
@@ -226,7 +251,7 @@ func FillReleasesForActivity(stats *ActivityStats, baseRepoID int64, fromTime ti
226 251
 	}
227 252
 
228 253
 	// Published releases authors
229
-	sess = releasesForActivityStatement(baseRepoID, fromTime)
254
+	sess = releasesForActivityStatement(repoID, fromTime)
230 255
 	if _, err = sess.Select("count(distinct release.publisher_id) as `count`").Table("release").Get(&count); err != nil {
231 256
 		return err
232 257
 	}
@@ -235,8 +260,8 @@ func FillReleasesForActivity(stats *ActivityStats, baseRepoID int64, fromTime ti
235 260
 	return nil
236 261
 }
237 262
 
238
-func releasesForActivityStatement(baseRepoID int64, fromTime time.Time) *xorm.Session {
239
-	return x.Where("release.repo_id = ?", baseRepoID).
263
+func releasesForActivityStatement(repoID int64, fromTime time.Time) *xorm.Session {
264
+	return x.Where("release.repo_id = ?", repoID).
240 265
 		And("release.is_draft = ?", false).
241 266
 		And("release.created_unix >= ?", fromTime.Unix())
242 267
 }

+ 4 - 24
routers/repo/activity.go

@@ -43,34 +43,14 @@ func Activity(ctx *context.Context) {
43 43
 	ctx.Data["DateUntil"] = timeUntil.Format("January 2, 2006")
44 44
 	ctx.Data["PeriodText"] = ctx.Tr("repo.activity.period." + ctx.Data["Period"].(string))
45 45
 
46
-	stats := &models.ActivityStats{}
47
-
48
-	if ctx.Repo.Repository.UnitEnabled(models.UnitTypeReleases) {
49
-		if err := models.FillReleasesForActivity(stats, ctx.Repo.Repository.ID, timeFrom); err != nil {
50
-			ctx.Handle(500, "FillReleasesForActivity", err)
51
-			return
52
-		}
53
-	}
54
-	if ctx.Repo.Repository.UnitEnabled(models.UnitTypePullRequests) {
55
-		if err := models.FillPullRequestsForActivity(stats, ctx.Repo.Repository.ID, timeFrom); err != nil {
56
-			ctx.Handle(500, "FillPullRequestsForActivity", err)
57
-			return
58
-		}
59
-	}
60
-	if ctx.Repo.Repository.UnitEnabled(models.UnitTypeIssues) {
61
-		if err := models.FillIssuesForActivity(stats, ctx.Repo.Repository.ID, timeFrom); err != nil {
62
-			ctx.Handle(500, "FillIssuesForActivity", err)
63
-			return
64
-		}
65
-	}
66
-	if err := models.FillUnresolvedIssuesForActivity(stats, ctx.Repo.Repository.ID, timeFrom,
46
+	var err error
47
+	if ctx.Data["Activity"], err = models.GetActivityStats(ctx.Repo.Repository.ID, timeFrom,
48
+		ctx.Repo.Repository.UnitEnabled(models.UnitTypeReleases),
67 49
 		ctx.Repo.Repository.UnitEnabled(models.UnitTypeIssues),
68 50
 		ctx.Repo.Repository.UnitEnabled(models.UnitTypePullRequests)); err != nil {
69
-		ctx.Handle(500, "FillUnresolvedIssuesForActivity", err)
51
+		ctx.Handle(500, "GetActivityStats", err)
70 52
 		return
71 53
 	}
72 54
 
73
-	ctx.Data["Activity"] = stats
74
-
75 55
 	ctx.HTML(200, tplActivity)
76 56
 }