-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmerge_request_test.go
112 lines (94 loc) · 3.52 KB
/
merge_request_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package models_test
import (
"context"
"testing"
"time"
"github.com/GitDataAI/jiaozifs/models"
"github.com/GitDataAI/jiaozifs/testhelper"
"github.com/GitDataAI/jiaozifs/utils"
"github.com/brianvoe/gofakeit/v6"
"github.com/google/go-cmp/cmp"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
)
func TestMergeRequestRepoInsert(t *testing.T) {
ctx := context.Background()
closeDB, _, db := testhelper.SetupDatabase(ctx, t)
defer closeDB()
mrRepo := models.NewMergeRequestRepo(db)
t.Run("insert and get", func(t *testing.T) {
mrModel := &models.MergeRequest{}
require.NoError(t, gofakeit.Struct(mrModel))
mrModel.MergeState = models.MergeStateInit
newMrModel, err := mrRepo.Insert(ctx, mrModel)
require.NoError(t, err)
require.NotEqual(t, uuid.Nil, newMrModel.ID)
getMRParams := models.NewGetMergeRequestParams().
SetID(newMrModel.ID).
SetTargetBranch(newMrModel.TargetBranchID).
SetSourceBranch(newMrModel.SourceBranchID).
SetNumber(newMrModel.Sequence).SetState(models.MergeStateInit).SetTargetRepo(newMrModel.TargetRepoID)
mrModel, err = mrRepo.Get(ctx, getMRParams)
require.NoError(t, err)
require.True(t, cmp.Equal(mrModel, newMrModel, testhelper.DBTimeCmpOpt))
})
t.Run("delete", func(t *testing.T) {
mrModel := &models.MergeRequest{}
require.NoError(t, gofakeit.Struct(mrModel))
newMrModel, err := mrRepo.Insert(ctx, mrModel)
require.NoError(t, err)
require.NotEqual(t, uuid.Nil, newMrModel.ID)
deleteParams := models.NewDeleteMergeRequestParams().SetTargetRepo(newMrModel.TargetRepoID).SetNumber(newMrModel.Sequence)
affectRows, err := mrRepo.Delete(ctx, deleteParams)
require.NoError(t, err)
require.Equal(t, int64(1), affectRows)
})
t.Run("list", func(t *testing.T) {
startT := time.Now()
targetID := uuid.New()
for i := 0; i < 10; i++ {
mrModel := &models.MergeRequest{}
require.NoError(t, gofakeit.Struct(mrModel))
mrModel.TargetRepoID = targetID
mrModel.UpdatedAt = time.Now()
mrModel.CreatedAt = time.Now()
newMrModel, err := mrRepo.Insert(ctx, mrModel)
require.NoError(t, err)
require.NotEqual(t, uuid.Nil, newMrModel.ID)
}
t.Run("first page", func(t *testing.T) {
mrs, hasMore, err := mrRepo.List(ctx, models.NewListMergeRequestParams().SetTargetRepoID(targetID).SetAmount(5))
require.NoError(t, err)
require.True(t, hasMore)
require.Len(t, mrs, 5)
})
t.Run("last page", func(t *testing.T) {
mrs, hasMore, err := mrRepo.List(ctx, models.NewListMergeRequestParams().SetTargetRepoID(targetID).SetAmount(5).SetAfter(startT))
require.NoError(t, err)
require.False(t, hasMore)
require.Len(t, mrs, 0)
})
})
t.Run("updatebyid", func(t *testing.T) {
mrModel := &models.MergeRequest{}
require.NoError(t, gofakeit.Struct(mrModel))
newMrModel, err := mrRepo.Insert(ctx, mrModel)
require.NoError(t, err)
require.NotEqual(t, uuid.Nil, newMrModel.ID)
newMrModel.Title = "Merge: xxxxx"
newMrModel.Description = utils.String("vvvv")
updateMrParams := models.NewUpdateMergeRequestParams(newMrModel.TargetRepoID, newMrModel.Sequence).
SetTitle("Merge: xxxx").
SetDescription("test update").
SetState(models.MergeStateClosed)
err = mrRepo.UpdateByID(ctx, updateMrParams)
require.NoError(t, err)
getMRParams := models.NewGetMergeRequestParams().
SetID(newMrModel.ID)
mrModel, err = mrRepo.Get(ctx, getMRParams)
require.NoError(t, err)
require.Equal(t, "Merge: xxxx", mrModel.Title)
require.Equal(t, "test update", *mrModel.Description)
require.Equal(t, models.MergeStateClosed, mrModel.MergeState)
})
}