Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue updated_unix bug #2204

Merged
merged 1 commit into from
Jul 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions models/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,7 @@ func (issue *Issue) ReadBy(userID int64) error {
}

func updateIssueCols(e Engine, issue *Issue, cols ...string) error {
cols = append(cols, "updated_unix")
if _, err := e.Id(issue.ID).Cols(cols...).Update(issue); err != nil {
return err
}
Expand Down
4 changes: 4 additions & 0 deletions models/issue_comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,11 @@ func createComment(e *xorm.Session, opts *CreateCommentOptions) (_ *Comment, err
if err != nil {
return nil, err
}
}

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

// Notify watchers for whatever action comes in, ignore if no action type.
Expand Down
41 changes: 41 additions & 0 deletions models/issue_comment_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2017 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package models

import (
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestCreateComment(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())

issue := AssertExistsAndLoadBean(t, &Issue{}).(*Issue)
repo := AssertExistsAndLoadBean(t, &Repository{ID: issue.RepoID}).(*Repository)
doer := AssertExistsAndLoadBean(t, &User{ID: repo.OwnerID}).(*User)

now := time.Now().Unix()
comment, err := CreateComment(&CreateCommentOptions{
Type: CommentTypeComment,
Doer: doer,
Repo: repo,
Issue: issue,
Content: "Hello",
})
assert.NoError(t, err)
then := time.Now().Unix()

assert.EqualValues(t, CommentTypeComment, comment.Type)
assert.EqualValues(t, "Hello", comment.Content)
assert.EqualValues(t, issue.ID, comment.IssueID)
assert.EqualValues(t, doer.ID, comment.PosterID)
AssertInt64InRange(t, now, then, comment.CreatedUnix)
AssertExistsAndLoadBean(t, comment) // assert actually added to DB

updatedIssue := AssertExistsAndLoadBean(t, &Issue{ID: issue.ID}).(*Issue)
AssertInt64InRange(t, now, then, updatedIssue.UpdatedUnix)
}
21 changes: 21 additions & 0 deletions models/issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package models
import (
"sort"
"testing"
"time"

"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -146,3 +147,23 @@ func TestIssue_ClearLabels(t *testing.T) {
AssertNotExistsBean(t, &IssueLabel{IssueID: test.issueID})
}
}

func TestUpdateIssueCols(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
issue := AssertExistsAndLoadBean(t, &Issue{}).(*Issue)

const newTitle = "New Title for unit test"
issue.Title = newTitle

prevContent := issue.Content
issue.Content = "This should have no effect"

now := time.Now().Unix()
assert.NoError(t, UpdateIssueCols(issue, "name"))
then := time.Now().Unix()

updatedIssue := AssertExistsAndLoadBean(t, &Issue{ID: issue.ID}).(*Issue)
assert.EqualValues(t, newTitle, updatedIssue.Title)
assert.EqualValues(t, prevContent, updatedIssue.Content)
AssertInt64InRange(t, now, then, updatedIssue.UpdatedUnix)
}
6 changes: 6 additions & 0 deletions models/unit_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,9 @@ func AssertSuccessfulInsert(t *testing.T, beans ...interface{}) {
func AssertCount(t *testing.T, bean interface{}, expected interface{}) {
assert.EqualValues(t, expected, GetCount(t, bean))
}

// AssertInt64InRange assert value is in range [low, high]
func AssertInt64InRange(t *testing.T, low, high, value int64) {
assert.True(t, value >= low && value <= high,
"Expected value in range [%d, %d], found %d", low, high, value)
}