|
| 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 | +} |
0 commit comments