forked from gocraft/dbr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transaction_test.go
68 lines (52 loc) · 2.1 KB
/
transaction_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
package dbr
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestTransactionCommit(t *testing.T) {
for _, sess := range testSession {
reset(t, sess)
tx, err := sess.Begin()
require.NoError(t, err)
defer tx.RollbackUnlessCommitted()
id := 1
result, err := tx.InsertInto("dbr_people").Columns("id", "name", "email").Values(id, "Barack", "obama@whitehouse.gov").Comment("INSERT TEST").Exec()
require.NoError(t, err)
require.Len(t, sess.EventReceiver.(*testTraceReceiver).started, 1)
require.Contains(t, sess.EventReceiver.(*testTraceReceiver).started[0].eventName, "dbr.exec")
require.Contains(t, sess.EventReceiver.(*testTraceReceiver).started[0].query, "/* INSERT TEST */\n")
require.Contains(t, sess.EventReceiver.(*testTraceReceiver).started[0].query, "INSERT")
require.Contains(t, sess.EventReceiver.(*testTraceReceiver).started[0].query, "dbr_people")
require.Contains(t, sess.EventReceiver.(*testTraceReceiver).started[0].query, "name")
require.Equal(t, 1, sess.EventReceiver.(*testTraceReceiver).finished)
require.Equal(t, 0, sess.EventReceiver.(*testTraceReceiver).errored)
rowsAffected, err := result.RowsAffected()
require.NoError(t, err)
require.Equal(t, int64(1), rowsAffected)
err = tx.Commit()
require.NoError(t, err)
var person dbrPerson
err = tx.Select("*").From("dbr_people").Where(Eq("id", id)).LoadOne(&person)
require.Error(t, err)
require.Equal(t, 1, sess.EventReceiver.(*testTraceReceiver).errored)
}
}
func TestTransactionRollback(t *testing.T) {
for _, sess := range testSession {
reset(t, sess)
tx, err := sess.Begin()
require.NoError(t, err)
defer tx.RollbackUnlessCommitted()
id := 1
result, err := tx.InsertInto("dbr_people").Columns("id", "name", "email").Values(id, "Barack", "obama@whitehouse.gov").Exec()
require.NoError(t, err)
rowsAffected, err := result.RowsAffected()
require.NoError(t, err)
require.Equal(t, int64(1), rowsAffected)
err = tx.Rollback()
require.NoError(t, err)
var person dbrPerson
err = tx.Select("*").From("dbr_people").Where(Eq("id", id)).LoadOne(&person)
require.Error(t, err)
}
}