Skip to content

Commit fa2a513

Browse files
authored
feat: add download count field and unit testing for attachment. (#1512)
* feat: add download count field and unit testing. * fix: unit testing * refactor: improve testing. * fix: update comment * add default value. Signed-off-by: Bo-Yi Wu <[email protected]>
1 parent a2d365c commit fa2a513

File tree

4 files changed

+158
-9
lines changed

4 files changed

+158
-9
lines changed

cmd/web.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,11 @@ func runWeb(ctx *cli.Context) error {
351351
}
352352
defer fr.Close()
353353

354+
if err := attach.IncreaseDownloadCount(); err != nil {
355+
ctx.Handle(500, "Update", err)
356+
return
357+
}
358+
354359
if err = repo.ServeData(ctx, attach.Name, fr); err != nil {
355360
ctx.Handle(500, "ServeData", err)
356361
return

models/attachment.go

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ import (
2020

2121
// Attachment represent a attachment of issue/comment/release.
2222
type Attachment struct {
23-
ID int64 `xorm:"pk autoincr"`
24-
UUID string `xorm:"uuid UNIQUE"`
25-
IssueID int64 `xorm:"INDEX"`
26-
CommentID int64
27-
ReleaseID int64 `xorm:"INDEX"`
28-
Name string
29-
30-
Created time.Time `xorm:"-"`
31-
CreatedUnix int64
23+
ID int64 `xorm:"pk autoincr"`
24+
UUID string `xorm:"uuid UNIQUE"`
25+
IssueID int64 `xorm:"INDEX"`
26+
ReleaseID int64 `xorm:"INDEX"`
27+
CommentID int64
28+
Name string
29+
DownloadCount int64 `xorm:"DEFAULT 0"`
30+
Created time.Time `xorm:"-"`
31+
CreatedUnix int64
3232
}
3333

3434
// BeforeInsert is invoked from XORM before inserting an object of this type.
@@ -45,6 +45,19 @@ func (a *Attachment) AfterSet(colName string, _ xorm.Cell) {
4545
}
4646
}
4747

48+
// IncreaseDownloadCount is update download count + 1
49+
func (a *Attachment) IncreaseDownloadCount() error {
50+
sess := x.NewSession()
51+
defer sessionRelease(sess)
52+
53+
// Update download count.
54+
if _, err := sess.Exec("UPDATE `attachment` SET download_count=download_count+1 WHERE id=?", a.ID); err != nil {
55+
return fmt.Errorf("increase attachment count: %v", err)
56+
}
57+
58+
return nil
59+
}
60+
4861
// AttachmentLocalPath returns where attachment is stored in local file
4962
// system based on given UUID.
5063
func AttachmentLocalPath(uuid string) string {

models/attachment_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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 models
6+
7+
import (
8+
"testing"
9+
10+
"github.com/stretchr/testify/assert"
11+
)
12+
13+
func TestIncreaseDownloadCount(t *testing.T) {
14+
assert.NoError(t, PrepareTestDatabase())
15+
16+
attachment, err := GetAttachmentByUUID("1234567890")
17+
assert.NoError(t, err)
18+
assert.Equal(t, int64(0), attachment.DownloadCount)
19+
20+
// increase download count
21+
err = attachment.IncreaseDownloadCount()
22+
assert.NoError(t, err)
23+
24+
attachment, err = GetAttachmentByUUID("1234567890")
25+
assert.NoError(t, err)
26+
assert.Equal(t, int64(1), attachment.DownloadCount)
27+
}
28+
29+
func TestGetByCommentOrIssueID(t *testing.T) {
30+
assert.NoError(t, PrepareTestDatabase())
31+
32+
// count of attachments from issue ID
33+
attachments, err := GetAttachmentsByIssueID(1)
34+
assert.NoError(t, err)
35+
assert.Equal(t, 2, len(attachments))
36+
37+
attachments, err = GetAttachmentsByCommentID(1)
38+
assert.NoError(t, err)
39+
assert.Equal(t, 2, len(attachments))
40+
}
41+
42+
func TestDeleteAttachments(t *testing.T) {
43+
assert.NoError(t, PrepareTestDatabase())
44+
45+
count, err := DeleteAttachmentsByIssue(4, false)
46+
assert.NoError(t, err)
47+
assert.Equal(t, 1, count)
48+
49+
count, err = DeleteAttachmentsByComment(2, false)
50+
assert.NoError(t, err)
51+
assert.Equal(t, 2, count)
52+
53+
err = DeleteAttachment(&Attachment{ID: 8}, false)
54+
assert.NoError(t, err)
55+
56+
attachment, err := GetAttachmentByUUID("test-12345")
57+
assert.Error(t, err)
58+
assert.True(t, IsErrAttachmentNotExist(err))
59+
assert.Nil(t, attachment)
60+
}

models/fixtures/attachment.yml

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
-
2+
id: 1
3+
uuid: 1234567890
4+
issue_id: 1
5+
comment_id: 0
6+
name: attach1
7+
download_count: 0
8+
created_unix: 946684800
9+
10+
-
11+
id: 2
12+
uuid: 1122334455
13+
issue_id: 1
14+
comment_id: 0
15+
name: attach2
16+
download_count: 1
17+
created_unix: 946684800
18+
19+
-
20+
id: 3
21+
uuid: comment-id-1
22+
issue_id: 2
23+
comment_id: 1
24+
name: attach1
25+
download_count: 0
26+
created_unix: 946684800
27+
28+
-
29+
id: 4
30+
uuid: comment-id-2
31+
issue_id: 3
32+
comment_id: 1
33+
name: attach2
34+
download_count: 1
35+
created_unix: 946684800
36+
37+
-
38+
id: 5
39+
uuid: comment-id-3
40+
issue_id: 4
41+
comment_id: 0
42+
name: attach1
43+
download_count: 0
44+
created_unix: 946684800
45+
46+
-
47+
id: 6
48+
uuid: comment-id-4
49+
issue_id: 5
50+
comment_id: 2
51+
name: attach1
52+
download_count: 0
53+
created_unix: 946684800
54+
55+
-
56+
id: 7
57+
uuid: comment-id-5
58+
issue_id: 5
59+
comment_id: 2
60+
name: attach1
61+
download_count: 0
62+
created_unix: 946684800
63+
64+
-
65+
id: 8
66+
uuid: test-12345
67+
issue_id: 6
68+
comment_id: 0
69+
name: attach1
70+
download_count: 0
71+
created_unix: 946684800

0 commit comments

Comments
 (0)