Skip to content

Commit a27863b

Browse files
ethantkoeniglunny
authored andcommittedJul 27, 2017
Fix issue updated_unix bug (#2204)
1 parent 5f37944 commit a27863b

File tree

5 files changed

+73
-0
lines changed

5 files changed

+73
-0
lines changed
 

‎models/issue.go

+1
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,7 @@ func (issue *Issue) ReadBy(userID int64) error {
580580
}
581581

582582
func updateIssueCols(e Engine, issue *Issue, cols ...string) error {
583+
cols = append(cols, "updated_unix")
583584
if _, err := e.Id(issue.ID).Cols(cols...).Update(issue); err != nil {
584585
return err
585586
}

‎models/issue_comment.go

+4
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,11 @@ func createComment(e *xorm.Session, opts *CreateCommentOptions) (_ *Comment, err
399399
if err != nil {
400400
return nil, err
401401
}
402+
}
402403

404+
// update the issue's updated_unix column
405+
if err = updateIssueCols(e, opts.Issue); err != nil {
406+
return nil, err
403407
}
404408

405409
// Notify watchers for whatever action comes in, ignore if no action type.

‎models/issue_comment_test.go

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
"time"
10+
11+
"github.com/stretchr/testify/assert"
12+
)
13+
14+
func TestCreateComment(t *testing.T) {
15+
assert.NoError(t, PrepareTestDatabase())
16+
17+
issue := AssertExistsAndLoadBean(t, &Issue{}).(*Issue)
18+
repo := AssertExistsAndLoadBean(t, &Repository{ID: issue.RepoID}).(*Repository)
19+
doer := AssertExistsAndLoadBean(t, &User{ID: repo.OwnerID}).(*User)
20+
21+
now := time.Now().Unix()
22+
comment, err := CreateComment(&CreateCommentOptions{
23+
Type: CommentTypeComment,
24+
Doer: doer,
25+
Repo: repo,
26+
Issue: issue,
27+
Content: "Hello",
28+
})
29+
assert.NoError(t, err)
30+
then := time.Now().Unix()
31+
32+
assert.EqualValues(t, CommentTypeComment, comment.Type)
33+
assert.EqualValues(t, "Hello", comment.Content)
34+
assert.EqualValues(t, issue.ID, comment.IssueID)
35+
assert.EqualValues(t, doer.ID, comment.PosterID)
36+
AssertInt64InRange(t, now, then, comment.CreatedUnix)
37+
AssertExistsAndLoadBean(t, comment) // assert actually added to DB
38+
39+
updatedIssue := AssertExistsAndLoadBean(t, &Issue{ID: issue.ID}).(*Issue)
40+
AssertInt64InRange(t, now, then, updatedIssue.UpdatedUnix)
41+
}

‎models/issue_test.go

+21
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package models
77
import (
88
"sort"
99
"testing"
10+
"time"
1011

1112
"github.com/stretchr/testify/assert"
1213
)
@@ -146,3 +147,23 @@ func TestIssue_ClearLabels(t *testing.T) {
146147
AssertNotExistsBean(t, &IssueLabel{IssueID: test.issueID})
147148
}
148149
}
150+
151+
func TestUpdateIssueCols(t *testing.T) {
152+
assert.NoError(t, PrepareTestDatabase())
153+
issue := AssertExistsAndLoadBean(t, &Issue{}).(*Issue)
154+
155+
const newTitle = "New Title for unit test"
156+
issue.Title = newTitle
157+
158+
prevContent := issue.Content
159+
issue.Content = "This should have no effect"
160+
161+
now := time.Now().Unix()
162+
assert.NoError(t, UpdateIssueCols(issue, "name"))
163+
then := time.Now().Unix()
164+
165+
updatedIssue := AssertExistsAndLoadBean(t, &Issue{ID: issue.ID}).(*Issue)
166+
assert.EqualValues(t, newTitle, updatedIssue.Title)
167+
assert.EqualValues(t, prevContent, updatedIssue.Content)
168+
AssertInt64InRange(t, now, then, updatedIssue.UpdatedUnix)
169+
}

‎models/unit_tests.go

+6
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,9 @@ func AssertSuccessfulInsert(t *testing.T, beans ...interface{}) {
9292
func AssertCount(t *testing.T, bean interface{}, expected interface{}) {
9393
assert.EqualValues(t, expected, GetCount(t, bean))
9494
}
95+
96+
// AssertInt64InRange assert value is in range [low, high]
97+
func AssertInt64InRange(t *testing.T, low, high, value int64) {
98+
assert.True(t, value >= low && value <= high,
99+
"Expected value in range [%d, %d], found %d", low, high, value)
100+
}

0 commit comments

Comments
 (0)
Please sign in to comment.