-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcommit_test.go
77 lines (66 loc) · 2.34 KB
/
commit_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
package models_test
import (
"context"
"testing"
"github.com/GitDataAI/jiaozifs/models"
"github.com/GitDataAI/jiaozifs/testhelper"
"github.com/brianvoe/gofakeit/v6"
"github.com/google/go-cmp/cmp"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
)
func TestCommitRepo(t *testing.T) {
ctx := context.Background()
closeDB, _, db := testhelper.SetupDatabase(ctx, t)
defer closeDB()
repoID := uuid.New()
commitRepo := models.NewCommitRepo(db, repoID)
require.Equal(t, commitRepo.RepositoryID(), repoID)
commitModel := &models.Commit{}
require.NoError(t, gofakeit.Struct(commitModel))
commitModel.RepositoryID = repoID
newCommitModel, err := commitRepo.Insert(ctx, commitModel)
require.NoError(t, err)
commitModel, err = commitRepo.Commit(ctx, commitModel.Hash)
require.NoError(t, err)
require.True(t, cmp.Equal(commitModel, newCommitModel, testhelper.DBTimeCmpOpt))
t.Run("mis match repo id", func(t *testing.T) {
mistMatchModel := &models.Commit{}
require.NoError(t, gofakeit.Struct(mistMatchModel))
_, err := commitRepo.Insert(ctx, mistMatchModel)
require.ErrorIs(t, err, models.ErrRepoIDMisMatch)
})
}
func TestDeleteCommit(t *testing.T) {
ctx := context.Background()
closeDB, _, db := testhelper.SetupDatabase(ctx, t)
defer closeDB()
t.Run("delete commit", func(t *testing.T) {
repoID := uuid.New()
commitRepo := models.NewCommitRepo(db, repoID)
require.Equal(t, commitRepo.RepositoryID(), repoID)
toDeleteModel := &models.Commit{}
require.NoError(t, gofakeit.Struct(toDeleteModel))
toDeleteModel.RepositoryID = repoID
toDeleteModel, err := commitRepo.Insert(ctx, toDeleteModel)
require.NoError(t, err)
affectRows, err := commitRepo.Delete(ctx, models.NewDeleteParams().SetHash(toDeleteModel.Hash))
require.NoError(t, err)
require.Equal(t, int64(1), affectRows)
})
t.Run("delete batch", func(t *testing.T) {
repoID := uuid.New()
commitRepo := models.NewCommitRepo(db, repoID)
require.Equal(t, commitRepo.RepositoryID(), repoID)
for i := 0; i < 5; i++ {
toDeleteModel := &models.Commit{}
require.NoError(t, gofakeit.Struct(toDeleteModel))
toDeleteModel.RepositoryID = repoID
_, err := commitRepo.Insert(ctx, toDeleteModel)
require.NoError(t, err)
}
affectRows, err := commitRepo.Delete(ctx, models.NewDeleteParams())
require.NoError(t, err)
require.Equal(t, int64(5), affectRows)
})
}