Skip to content

Commit e344e37

Browse files
committed
Add unit test for conversations
1 parent 933b4b4 commit e344e37

File tree

2 files changed

+291
-0
lines changed

2 files changed

+291
-0
lines changed

models/conversations/conversation.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,3 +338,27 @@ func (conversation *Conversation) loadReactions(ctx context.Context) (err error)
338338
}
339339
return nil
340340
}
341+
342+
// InsertConversations insert issues to database
343+
func InsertConversations(ctx context.Context, conversations ...*Conversation) error {
344+
ctx, committer, err := db.TxContext(ctx)
345+
if err != nil {
346+
return err
347+
}
348+
defer committer.Close()
349+
350+
for _, conversation := range conversations {
351+
if err := insertConversation(ctx, conversation); err != nil {
352+
return err
353+
}
354+
}
355+
return committer.Commit()
356+
}
357+
358+
func insertConversation(ctx context.Context, conversation *Conversation) error {
359+
sess := db.GetEngine(ctx)
360+
if _, err := sess.NoAutoTime().Insert(conversation); err != nil {
361+
return err
362+
}
363+
return nil
364+
}
Lines changed: 267 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,267 @@
1+
// Copyright 2017 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package conversations_test
5+
6+
import (
7+
"context"
8+
"fmt"
9+
"sync"
10+
"testing"
11+
"time"
12+
13+
conversations_model "code.gitea.io/gitea/models/conversations"
14+
"code.gitea.io/gitea/models/db"
15+
repo_model "code.gitea.io/gitea/models/repo"
16+
"code.gitea.io/gitea/models/unittest"
17+
"code.gitea.io/gitea/modules/setting"
18+
19+
"github.com/stretchr/testify/assert"
20+
"xorm.io/builder"
21+
)
22+
23+
func Test_GetConversationIDsByRepoID(t *testing.T) {
24+
assert.NoError(t, unittest.PrepareTestDatabase())
25+
26+
ids, err := conversations_model.GetConversationIDsByRepoID(db.DefaultContext, 1)
27+
assert.NoError(t, err)
28+
assert.Len(t, ids, 5)
29+
}
30+
31+
func TestConversationAPIURL(t *testing.T) {
32+
assert.NoError(t, unittest.PrepareTestDatabase())
33+
conversation := unittest.AssertExistsAndLoadBean(t, &conversations_model.Conversation{ID: 1})
34+
err := conversation.LoadAttributes(db.DefaultContext)
35+
36+
assert.NoError(t, err)
37+
assert.Equal(t, "https://try.gitea.io/api/v1/repos/user2/repo1/conversations/1", conversation.APIURL(db.DefaultContext))
38+
}
39+
40+
func TestGetConversationsByIDs(t *testing.T) {
41+
assert.NoError(t, unittest.PrepareTestDatabase())
42+
testSuccess := func(expectedConversationIDs, nonExistentConversationIDs []int64) {
43+
conversations, err := conversations_model.GetConversationsByIDs(db.DefaultContext, append(expectedConversationIDs, nonExistentConversationIDs...), true)
44+
assert.NoError(t, err)
45+
actualConversationIDs := make([]int64, len(conversations))
46+
for i, conversation := range conversations {
47+
actualConversationIDs[i] = conversation.ID
48+
}
49+
assert.Equal(t, expectedConversationIDs, actualConversationIDs)
50+
}
51+
testSuccess([]int64{1, 2, 3}, []int64{})
52+
testSuccess([]int64{1, 2, 3}, []int64{unittest.NonexistentID})
53+
testSuccess([]int64{3, 2, 1}, []int64{})
54+
}
55+
56+
func TestUpdateConversationCols(t *testing.T) {
57+
assert.NoError(t, unittest.PrepareTestDatabase())
58+
conversation := unittest.AssertExistsAndLoadBean(t, &conversations_model.Conversation{})
59+
60+
now := time.Now().Unix()
61+
assert.NoError(t, conversations_model.UpdateConversationCols(db.DefaultContext, conversation, "name"))
62+
then := time.Now().Unix()
63+
64+
updatedConversation := unittest.AssertExistsAndLoadBean(t, &conversations_model.Conversation{ID: conversation.ID})
65+
unittest.AssertInt64InRange(t, now, then, int64(updatedConversation.UpdatedUnix))
66+
}
67+
68+
func TestConversations(t *testing.T) {
69+
assert.NoError(t, unittest.PrepareTestDatabase())
70+
for _, test := range []struct {
71+
Opts conversations_model.ConversationsOptions
72+
ExpectedConversationIDs []int64
73+
}{
74+
{
75+
conversations_model.ConversationsOptions{
76+
AssigneeID: 1,
77+
SortType: "oldest",
78+
},
79+
[]int64{1, 6},
80+
},
81+
{
82+
conversations_model.ConversationsOptions{
83+
RepoCond: builder.In("repo_id", 1, 3),
84+
SortType: "oldest",
85+
Paginator: &db.ListOptions{
86+
Page: 1,
87+
PageSize: 4,
88+
},
89+
},
90+
[]int64{1, 2, 3, 5},
91+
},
92+
{
93+
conversations_model.ConversationsOptions{
94+
LabelIDs: []int64{1},
95+
Paginator: &db.ListOptions{
96+
Page: 1,
97+
PageSize: 4,
98+
},
99+
},
100+
[]int64{2, 1},
101+
},
102+
{
103+
conversations_model.ConversationsOptions{
104+
LabelIDs: []int64{1, 2},
105+
Paginator: &db.ListOptions{
106+
Page: 1,
107+
PageSize: 4,
108+
},
109+
},
110+
[]int64{}, // conversations with **both** label 1 and 2, none of these conversations matches, TODO: add more tests
111+
},
112+
{
113+
conversations_model.ConversationsOptions{
114+
MilestoneIDs: []int64{1},
115+
},
116+
[]int64{2},
117+
},
118+
} {
119+
conversations, err := conversations_model.Conversations(db.DefaultContext, &test.Opts)
120+
assert.NoError(t, err)
121+
if assert.Len(t, conversations, len(test.ExpectedConversationIDs)) {
122+
for i, conversation := range conversations {
123+
assert.EqualValues(t, test.ExpectedConversationIDs[i], conversation.ID)
124+
}
125+
}
126+
}
127+
}
128+
129+
func TestConversation_InsertConversation(t *testing.T) {
130+
assert.NoError(t, unittest.PrepareTestDatabase())
131+
132+
// there are 5 conversations and max index is 5 on repository 1, so this one should 6
133+
conversation := testInsertConversation(t, "my conversation1", "special conversation's comments?", 6)
134+
_, err := db.DeleteByID[conversations_model.Conversation](db.DefaultContext, conversation.ID)
135+
assert.NoError(t, err)
136+
137+
conversation = testInsertConversation(t, `my conversation2, this is my son's love \n \r \ `, "special conversation's '' comments?", 7)
138+
_, err = db.DeleteByID[conversations_model.Conversation](db.DefaultContext, conversation.ID)
139+
assert.NoError(t, err)
140+
}
141+
142+
func TestResourceIndex(t *testing.T) {
143+
assert.NoError(t, unittest.PrepareTestDatabase())
144+
145+
var wg sync.WaitGroup
146+
for i := 0; i < 100; i++ {
147+
wg.Add(1)
148+
go func(i int) {
149+
testInsertConversation(t, fmt.Sprintf("conversation %d", i+1), "my conversation", 0)
150+
wg.Done()
151+
}(i)
152+
}
153+
wg.Wait()
154+
}
155+
156+
func TestCorrectConversationStats(t *testing.T) {
157+
assert.NoError(t, unittest.PrepareTestDatabase())
158+
159+
// Because the condition is to have chunked database look-ups,
160+
// We have to more conversations than `maxQueryParameters`, we will insert.
161+
// maxQueryParameters + 10 conversations into the testDatabase.
162+
// Each new conversations will have a constant description "Bugs are nasty"
163+
// Which will be used later on.
164+
165+
conversationAmount := conversations_model.MaxQueryParameters + 10
166+
167+
var wg sync.WaitGroup
168+
for i := 0; i < conversationAmount; i++ {
169+
wg.Add(1)
170+
go func(i int) {
171+
testInsertConversation(t, fmt.Sprintf("Conversation %d", i+1), "Bugs are nasty", 0)
172+
wg.Done()
173+
}(i)
174+
}
175+
wg.Wait()
176+
177+
// Now we will get all conversationID's that match the "Bugs are nasty" query.
178+
conversations, err := conversations_model.Conversations(context.TODO(), &conversations_model.ConversationsOptions{
179+
Paginator: &db.ListOptions{
180+
PageSize: conversationAmount,
181+
},
182+
RepoIDs: []int64{1},
183+
})
184+
total := int64(len(conversations))
185+
var ids []int64
186+
for _, conversation := range conversations {
187+
ids = append(ids, conversation.ID)
188+
}
189+
190+
// Just to be sure.
191+
assert.NoError(t, err)
192+
assert.EqualValues(t, conversationAmount, total)
193+
194+
// Now we will call the GetConversationStats with these IDs and if working,
195+
// get the correct stats back.
196+
conversationStats, err := conversations_model.GetConversationStats(db.DefaultContext, &conversations_model.ConversationsOptions{
197+
RepoIDs: []int64{1},
198+
ConversationIDs: ids,
199+
})
200+
201+
// Now check the values.
202+
assert.NoError(t, err)
203+
assert.EqualValues(t, conversationStats.OpenCount, conversationAmount)
204+
}
205+
206+
func TestCountConversations(t *testing.T) {
207+
assert.NoError(t, unittest.PrepareTestDatabase())
208+
count, err := conversations_model.CountConversations(db.DefaultContext, &conversations_model.ConversationsOptions{})
209+
assert.NoError(t, err)
210+
assert.EqualValues(t, 22, count)
211+
}
212+
213+
func TestConversationLoadAttributes(t *testing.T) {
214+
assert.NoError(t, unittest.PrepareTestDatabase())
215+
setting.Service.EnableTimetracking = true
216+
217+
conversationList := conversations_model.ConversationList{
218+
unittest.AssertExistsAndLoadBean(t, &conversations_model.Conversation{ID: 1}),
219+
unittest.AssertExistsAndLoadBean(t, &conversations_model.Conversation{ID: 4}),
220+
}
221+
222+
for _, conversation := range conversationList {
223+
assert.NoError(t, conversation.LoadAttributes(db.DefaultContext))
224+
assert.EqualValues(t, conversation.RepoID, conversation.Repo.ID)
225+
for _, comment := range conversation.Comments {
226+
assert.EqualValues(t, conversation.ID, comment.ConversationID)
227+
}
228+
}
229+
}
230+
231+
func assertCreateConversations(t *testing.T, isPull bool) {
232+
assert.NoError(t, unittest.PrepareTestDatabase())
233+
reponame := "repo1"
234+
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{Name: reponame})
235+
236+
conversationID := int64(99)
237+
is := &conversations_model.Conversation{
238+
RepoID: repo.ID,
239+
Repo: repo,
240+
ID: conversationID,
241+
}
242+
err := conversations_model.InsertConversations(db.DefaultContext, is)
243+
assert.NoError(t, err)
244+
245+
unittest.AssertExistsAndLoadBean(t, &conversations_model.Conversation{RepoID: repo.ID, ID: conversationID})
246+
}
247+
248+
func testInsertConversation(t *testing.T, title, content string, expectIndex int64) *conversations_model.Conversation {
249+
var newConversation conversations_model.Conversation
250+
t.Run(title, func(t *testing.T) {
251+
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
252+
253+
conversation := conversations_model.Conversation{
254+
RepoID: repo.ID,
255+
}
256+
err := conversations_model.NewConversation(db.DefaultContext, repo, &conversation, nil)
257+
assert.NoError(t, err)
258+
259+
has, err := db.GetEngine(db.DefaultContext).ID(conversation.ID).Get(&newConversation)
260+
assert.NoError(t, err)
261+
assert.True(t, has)
262+
if expectIndex > 0 {
263+
assert.EqualValues(t, expectIndex, newConversation.Index)
264+
}
265+
})
266+
return &newConversation
267+
}

0 commit comments

Comments
 (0)