Skip to content

Commit b1f07f2

Browse files
committed
Add unit test for comments
1 parent e344e37 commit b1f07f2

File tree

4 files changed

+111
-28
lines changed

4 files changed

+111
-28
lines changed

models/conversations/comment_list.go

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,6 @@ import (
1414
// CommentList defines a list of comments
1515
type CommentList []*ConversationComment
1616

17-
// LoadPosters loads posters
18-
func (comments CommentList) LoadPosters(ctx context.Context) error {
19-
if len(comments) == 0 {
20-
return nil
21-
}
22-
23-
posterIDs := container.FilterSlice(comments, func(c *ConversationComment) (int64, bool) {
24-
return c.PosterID, c.Poster == nil && c.PosterID > 0
25-
})
26-
27-
posterMaps, err := getPostersByIDs(ctx, posterIDs)
28-
if err != nil {
29-
return err
30-
}
31-
32-
for _, comment := range comments {
33-
if comment.Poster == nil {
34-
comment.Poster = getPoster(comment.PosterID, posterMaps)
35-
}
36-
}
37-
return nil
38-
}
39-
4017
// getConversationIDs returns all the conversation ids on this comment list which conversation hasn't been loaded
4118
func (comments CommentList) getConversationIDs() []int64 {
4219
return container.FilterSlice(comments, func(comment *ConversationComment) (int64, bool) {
@@ -181,10 +158,6 @@ func (comments CommentList) LoadAttachments(ctx context.Context) (err error) {
181158
// LoadAttributes loads attributes of the comments, except for attachments and
182159
// comments
183160
func (comments CommentList) LoadAttributes(ctx context.Context) (err error) {
184-
if err = comments.LoadPosters(ctx); err != nil {
185-
return err
186-
}
187-
188161
if err = comments.LoadAttachments(ctx); err != nil {
189162
return err
190163
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// Copyright 2024 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package conversations_test
5+
6+
import (
7+
"testing"
8+
"time"
9+
10+
conversations_model "code.gitea.io/gitea/models/conversations"
11+
"code.gitea.io/gitea/models/db"
12+
repo_model "code.gitea.io/gitea/models/repo"
13+
"code.gitea.io/gitea/models/unittest"
14+
user_model "code.gitea.io/gitea/models/user"
15+
16+
"github.com/stretchr/testify/assert"
17+
)
18+
19+
func TestCreateComment(t *testing.T) {
20+
assert.NoError(t, unittest.PrepareTestDatabase())
21+
22+
conversation := unittest.AssertExistsAndLoadBean(t, &conversations_model.Conversation{})
23+
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: conversation.RepoID})
24+
doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
25+
26+
now := time.Now().Unix()
27+
comment, err := conversations_model.CreateComment(db.DefaultContext, &conversations_model.CreateCommentOptions{
28+
Type: conversations_model.CommentTypeComment,
29+
Doer: doer,
30+
Repo: repo,
31+
Conversation: conversation,
32+
Content: "Hello",
33+
})
34+
assert.NoError(t, err)
35+
then := time.Now().Unix()
36+
37+
assert.EqualValues(t, conversations_model.CommentTypeComment, comment.Type)
38+
assert.EqualValues(t, "Hello", comment.Content)
39+
assert.EqualValues(t, conversation.ID, comment.ConversationID)
40+
assert.EqualValues(t, doer.ID, comment.PosterID)
41+
unittest.AssertInt64InRange(t, now, then, int64(comment.CreatedUnix))
42+
unittest.AssertExistsAndLoadBean(t, comment) // assert actually added to DB
43+
44+
updatedConversation := unittest.AssertExistsAndLoadBean(t, &conversations_model.Conversation{ID: conversation.ID})
45+
unittest.AssertInt64InRange(t, now, then, int64(updatedConversation.UpdatedUnix))
46+
}
47+
48+
func TestAsCommentType(t *testing.T) {
49+
assert.Equal(t, conversations_model.CommentType(0), conversations_model.CommentTypeComment)
50+
assert.Equal(t, conversations_model.CommentTypeUndefined, conversations_model.AsCommentType(""))
51+
assert.Equal(t, conversations_model.CommentTypeUndefined, conversations_model.AsCommentType("nonsense"))
52+
assert.Equal(t, conversations_model.CommentTypeComment, conversations_model.AsCommentType("comment"))
53+
}
54+
55+
func TestMigrate_InsertConversationComments(t *testing.T) {
56+
assert.NoError(t, unittest.PrepareTestDatabase())
57+
conversation := unittest.AssertExistsAndLoadBean(t, &conversations_model.Conversation{ID: 1})
58+
_ = conversation.LoadRepo(db.DefaultContext)
59+
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: conversation.Repo.OwnerID})
60+
reaction := &conversations_model.CommentReaction{
61+
Type: "heart",
62+
UserID: owner.ID,
63+
}
64+
65+
comment := &conversations_model.ConversationComment{
66+
PosterID: owner.ID,
67+
Poster: owner,
68+
ConversationID: conversation.ID,
69+
Conversation: conversation,
70+
Reactions: []*conversations_model.CommentReaction{reaction},
71+
}
72+
73+
err := conversations_model.InsertConversationComments(db.DefaultContext, []*conversations_model.ConversationComment{comment})
74+
assert.NoError(t, err)
75+
76+
conversationModified := unittest.AssertExistsAndLoadBean(t, &conversations_model.Conversation{ID: 1})
77+
assert.EqualValues(t, conversation.NumComments+1, conversationModified.NumComments)
78+
79+
unittest.CheckConsistencyFor(t, &conversations_model.Conversation{})
80+
}

models/conversations/conversation_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2017 The Gitea Authors. All rights reserved.
1+
// Copyright 2024 The Gitea Authors. All rights reserved.
22
// SPDX-License-Identifier: MIT
33

44
package conversations_test

models/conversations/main_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2024 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package conversations_test
5+
6+
import (
7+
"testing"
8+
9+
conversations_model "code.gitea.io/gitea/models/conversations"
10+
"code.gitea.io/gitea/models/unittest"
11+
12+
_ "code.gitea.io/gitea/models"
13+
_ "code.gitea.io/gitea/models/actions"
14+
_ "code.gitea.io/gitea/models/activities"
15+
_ "code.gitea.io/gitea/models/repo"
16+
_ "code.gitea.io/gitea/models/user"
17+
18+
"github.com/stretchr/testify/assert"
19+
)
20+
21+
func TestFixturesAreConsistent(t *testing.T) {
22+
assert.NoError(t, unittest.PrepareTestDatabase())
23+
unittest.CheckConsistencyFor(t,
24+
&conversations_model.Conversation{},
25+
)
26+
}
27+
28+
func TestMain(m *testing.M) {
29+
unittest.MainTest(m)
30+
}

0 commit comments

Comments
 (0)