-
Notifications
You must be signed in to change notification settings - Fork 3
/
transactions_test.go
65 lines (54 loc) · 1.5 KB
/
transactions_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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package dbw_test
import (
"context"
"testing"
"github.com/hashicorp/go-dbw"
"github.com/hashicorp/go-dbw/internal/dbtest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestRW_Transactions(t *testing.T) {
t.Parallel()
testCtx := context.Background()
conn, _ := dbw.TestSetup(t)
t.Run("simple", func(t *testing.T) {
require := require.New(t)
id, err := dbw.NewId("u")
require.NoError(err)
w := dbw.New(conn)
tx, err := w.Begin(testCtx)
require.NoError(err)
user, err := dbtest.NewTestUser()
require.NoError(err)
require.NoError(tx.Create(testCtx, &user))
user.Name = id
rowsUpdated, err := tx.Update(testCtx, user, []string{"Name"}, nil)
require.NoError(err)
require.Equal(1, rowsUpdated)
require.NoError(tx.Commit(testCtx))
})
t.Run("rollback-success", func(t *testing.T) {
require := require.New(t)
id, err := dbw.NewId("u")
require.NoError(err)
w := dbw.New(conn)
tx, err := w.Begin(testCtx)
require.NoError(err)
user, err := dbtest.NewTestUser()
require.NoError(err)
require.NoError(tx.Create(testCtx, &user))
user.Name = id
rowsUpdated, err := tx.Update(testCtx, user, []string{"Name"}, nil)
require.NoError(err)
require.Equal(1, rowsUpdated)
require.NoError(tx.Rollback(testCtx))
})
t.Run("no-transaction", func(t *testing.T) {
assert := assert.New(t)
w := dbw.New(conn)
assert.Error(w.Rollback(testCtx))
assert.Error(w.Commit(testCtx))
})
}