Browse Source

Fix issue updated_unix bug (#2204)

Ethan Koenig 1 year ago
parent
commit
a27863b6d1
5 changed files with 73 additions and 0 deletions
  1. 1 0
      models/issue.go
  2. 4 0
      models/issue_comment.go
  3. 41 0
      models/issue_comment_test.go
  4. 21 0
      models/issue_test.go
  5. 6 0
      models/unit_tests.go

+ 1 - 0
models/issue.go

@@ -580,6 +580,7 @@ func (issue *Issue) ReadBy(userID int64) error {
580 580
 }
581 581
 
582 582
 func updateIssueCols(e Engine, issue *Issue, cols ...string) error {
583
+	cols = append(cols, "updated_unix")
583 584
 	if _, err := e.Id(issue.ID).Cols(cols...).Update(issue); err != nil {
584 585
 		return err
585 586
 	}

+ 4 - 0
models/issue_comment.go

@@ -399,7 +399,11 @@ func createComment(e *xorm.Session, opts *CreateCommentOptions) (_ *Comment, err
399 399
 		if err != nil {
400 400
 			return nil, err
401 401
 		}
402
+	}
402 403
 
404
+	// update the issue's updated_unix column
405
+	if err = updateIssueCols(e, opts.Issue); err != nil {
406
+		return nil, err
403 407
 	}
404 408
 
405 409
 	// Notify watchers for whatever action comes in, ignore if no action type.

+ 41 - 0
models/issue_comment_test.go

@@ -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
+}

+ 21 - 0
models/issue_test.go

@@ -7,6 +7,7 @@ package models
7 7
 import (
8 8
 	"sort"
9 9
 	"testing"
10
+	"time"
10 11
 
11 12
 	"github.com/stretchr/testify/assert"
12 13
 )
@@ -146,3 +147,23 @@ func TestIssue_ClearLabels(t *testing.T) {
146 147
 		AssertNotExistsBean(t, &IssueLabel{IssueID: test.issueID})
147 148
 	}
148 149
 }
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
+}

+ 6 - 0
models/unit_tests.go

@@ -92,3 +92,9 @@ func AssertSuccessfulInsert(t *testing.T, beans ...interface{}) {
92 92
 func AssertCount(t *testing.T, bean interface{}, expected interface{}) {
93 93
 	assert.EqualValues(t, expected, GetCount(t, bean))
94 94
 }
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
+}