-
Notifications
You must be signed in to change notification settings - Fork 208
/
Copy pathtransaction_test.go
77 lines (61 loc) · 2.45 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
69
70
71
72
73
74
75
76
77
package dbr
import (
"testing"
"github.com/gocraft/dbr/v2/dialect"
"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()
elem_count := 1
if sess.Dialect == dialect.MSSQL {
tx.UpdateBySql("SET IDENTITY_INSERT dbr_people ON;").Exec()
elem_count += 1
}
id := 1
result, err := tx.InsertInto("dbr_people").Columns("id", "name", "email").Values(id, "Barack", "[email protected]").Comment("INSERT TEST").Exec()
require.NoError(t, err)
require.Len(t, sess.EventReceiver.(*testTraceReceiver).started, elem_count)
require.Contains(t, sess.EventReceiver.(*testTraceReceiver).started[elem_count-1].eventName, "dbr.exec")
require.Contains(t, sess.EventReceiver.(*testTraceReceiver).started[elem_count-1].query, "/* INSERT TEST */\n")
require.Contains(t, sess.EventReceiver.(*testTraceReceiver).started[elem_count-1].query, "INSERT")
require.Contains(t, sess.EventReceiver.(*testTraceReceiver).started[elem_count-1].query, "dbr_people")
require.Contains(t, sess.EventReceiver.(*testTraceReceiver).started[elem_count-1].query, "name")
require.Equal(t, elem_count, 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()
if sess.Dialect == dialect.MSSQL {
tx.UpdateBySql("SET IDENTITY_INSERT dbr_people ON;").Exec()
}
id := 1
result, err := tx.InsertInto("dbr_people").Columns("id", "name", "email").Values(id, "Barack", "[email protected]").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)
}
}