-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathrepo_test.go
64 lines (57 loc) · 1.73 KB
/
repo_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
package models_test
import (
"context"
"database/sql"
"errors"
"fmt"
"testing"
"github.com/GitDataAI/jiaozifs/models"
"github.com/GitDataAI/jiaozifs/testhelper"
"github.com/brianvoe/gofakeit/v6"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
)
func TestRepoTransaction(t *testing.T) {
ctx := context.Background()
closeDB, _, db := testhelper.SetupDatabase(ctx, t)
defer closeDB()
t.Run("simple", func(t *testing.T) {
pgRepo := models.NewRepo(db)
err := pgRepo.Transaction(ctx, func(repo models.IRepo) error {
userModel := &models.User{}
require.NoError(t, gofakeit.Struct(userModel))
_, err := repo.UserRepo().Insert(ctx, userModel)
require.NoError(t, err)
return nil
})
require.NoError(t, err)
})
t.Run("transaction", func(t *testing.T) {
pgRepo := models.NewRepo(db)
repoID := uuid.New()
err := pgRepo.Transaction(ctx, func(repo models.IRepo) error {
treeNode := &models.FileTree{}
require.NoError(t, gofakeit.Struct(treeNode))
treeNode.RepositoryID = repoID
_, err := repo.FileTreeRepo(repoID).Insert(ctx, treeNode)
require.NoError(t, err)
return err
})
require.NoError(t, err)
})
t.Run("transaction rollback", func(t *testing.T) {
pgRepo := models.NewRepo(db)
var id uuid.UUID
err := pgRepo.Transaction(ctx, func(repo models.IRepo) error {
repositoryModel := &models.Repository{}
require.NoError(t, gofakeit.Struct(repositoryModel))
insertedModel, err := repo.RepositoryRepo().Insert(ctx, repositoryModel)
require.NoError(t, err)
id = insertedModel.ID
return fmt.Errorf("rollback")
})
require.Error(t, err)
_, err = pgRepo.RepositoryRepo().Get(ctx, models.NewGetRepoParams().SetID(id))
require.True(t, errors.Is(err, sql.ErrNoRows))
})
}