-
Notifications
You must be signed in to change notification settings - Fork 73
/
db_test.go
173 lines (149 loc) · 6.03 KB
/
db_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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package reform_test
import (
"database/sql"
"errors"
"testing"
"github.com/AlekSi/pointer"
"github.com/brianvoe/gofakeit/v6"
"github.com/jackc/pgx"
"github.com/jackc/pgx/stdlib"
"github.com/lib/pq"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/reform.v1"
"gopkg.in/reform.v1/dialects/postgresql"
. "gopkg.in/reform.v1/internal/test/models"
)
func TestBeginCommit(t *testing.T) {
db, tx := setupTX(t)
defer teardown(t, db)
person := &Person{ID: 42, Email: pointer.ToString(gofakeit.Email())}
assert.NoError(t, insertPersonWithID(t, tx.Querier, person))
assert.NoError(t, tx.Commit())
assert.Equal(t, tx.Commit(), reform.ErrTxDone)
assert.Equal(t, tx.Rollback(), reform.ErrTxDone)
assert.NoError(t, db.Reload(person))
assert.NoError(t, db.Delete(person))
}
func TestBeginRollback(t *testing.T) {
db, tx := setupTX(t)
defer teardown(t, db)
person := &Person{ID: 42, Email: pointer.ToString(gofakeit.Email())}
assert.NoError(t, insertPersonWithID(t, tx.Querier, person))
assert.NoError(t, tx.Rollback())
assert.Equal(t, tx.Commit(), reform.ErrTxDone)
assert.Equal(t, tx.Rollback(), reform.ErrTxDone)
assert.Equal(t, db.Reload(person), reform.ErrNoRows)
}
// This behavior is checked for documentation purposes only. reform does not rely on it.
func TestErrorInTransaction(t *testing.T) {
db := setupDB(t)
defer teardown(t, db)
if db.Dialect == postgresql.Dialect {
t.Skipf("%s works differently, see TestAbortedTransaction", db.Dialect)
}
person1 := &Person{ID: 42, Email: pointer.ToString(gofakeit.Email())}
person2 := &Person{ID: 43, Email: pointer.ToString(gofakeit.Email())}
// commit works
tx, err := db.Begin()
require.NoError(t, err)
assert.NoError(t, insertPersonWithID(t, tx.Querier, person1))
assert.Error(t, insertPersonWithID(t, tx.Querier, person1)) // duplicate PK
assert.NoError(t, insertPersonWithID(t, tx.Querier, person2)) // INSERT works
assert.NoError(t, tx.Commit())
assert.Equal(t, tx.Commit(), reform.ErrTxDone)
assert.Equal(t, tx.Rollback(), reform.ErrTxDone)
assert.NoError(t, db.Reload(person1))
assert.NoError(t, db.Reload(person2))
assert.NoError(t, db.Delete(person1))
assert.NoError(t, db.Delete(person2))
// rollback works
tx, err = db.Begin()
require.NoError(t, err)
assert.NoError(t, insertPersonWithID(t, tx.Querier, person1))
assert.Error(t, insertPersonWithID(t, tx.Querier, person1)) // duplicate PK
assert.NoError(t, insertPersonWithID(t, tx.Querier, person2)) // INSERT works
assert.NoError(t, tx.Rollback())
assert.Equal(t, tx.Commit(), reform.ErrTxDone)
assert.Equal(t, tx.Rollback(), reform.ErrTxDone)
assert.EqualError(t, db.Reload(person1), reform.ErrNoRows.Error())
assert.EqualError(t, db.Reload(person2), reform.ErrNoRows.Error())
}
// This behavior is checked for documentation purposes only. reform does not rely on it.
// http://postgresql.nabble.com/Current-transaction-is-aborted-commands-ignored-until-end-of-transaction-block-td5109252.html
func TestAbortedTransaction(t *testing.T) {
db := setupDB(t)
defer teardown(t, db)
if db.Dialect != postgresql.Dialect {
t.Skipf("%s works differently, see TestErrorInTransaction", db.Dialect)
}
person1 := &Person{ID: 42, Email: pointer.ToString(gofakeit.Email())}
person2 := &Person{ID: 43, Email: pointer.ToString(gofakeit.Email())}
// commit fails
tx, err := db.Begin()
require.NoError(t, err)
assert.NoError(t, insertPersonWithID(t, tx.Querier, person1))
assert.Contains(t, insertPersonWithID(t, tx.Querier, person1).Error(), `duplicate key value violates unique constraint "people_pkey"`)
assert.Contains(t, insertPersonWithID(t, tx.Querier, person2).Error(), `current transaction is aborted, commands ignored until end of transaction block`)
err = tx.Commit()
require.Error(t, err)
switch db.DBInterface().(*sql.DB).Driver().(type) {
case *pq.Driver:
assert.Equal(t, pq.ErrInFailedTransaction, err)
case *stdlib.Driver:
assert.Equal(t, pgx.ErrTxCommitRollback, err)
default:
t.Fatalf("unexpected driver, error %v", err)
}
assert.Equal(t, tx.Rollback(), reform.ErrTxDone)
assert.EqualError(t, db.Reload(person1), reform.ErrNoRows.Error())
assert.EqualError(t, db.Reload(person2), reform.ErrNoRows.Error())
// rollback works
tx, err = db.Begin()
require.NoError(t, err)
assert.NoError(t, insertPersonWithID(t, tx.Querier, person1))
assert.Contains(t, insertPersonWithID(t, tx.Querier, person1).Error(), `duplicate key value violates unique constraint "people_pkey"`)
assert.Contains(t, insertPersonWithID(t, tx.Querier, person2).Error(), `current transaction is aborted, commands ignored until end of transaction block`)
assert.NoError(t, tx.Rollback())
assert.Equal(t, tx.Commit(), reform.ErrTxDone)
assert.Equal(t, tx.Rollback(), reform.ErrTxDone)
assert.EqualError(t, db.Reload(person1), reform.ErrNoRows.Error())
assert.EqualError(t, db.Reload(person2), reform.ErrNoRows.Error())
}
func TestInTransaction(t *testing.T) {
db := setupDB(t)
defer teardown(t, db)
person := &Person{ID: 42, Email: pointer.ToString(gofakeit.Email())}
// error in closure
err := db.InTransaction(func(tx *reform.TX) error {
assert.NoError(t, insertPersonWithID(t, tx.Querier, person))
return errors.New("epic error")
})
assert.EqualError(t, err, "epic error")
assert.Equal(t, db.Reload(person), reform.ErrNoRows)
// panic in closure
assert.Panics(t, func() {
err = db.InTransaction(func(tx *reform.TX) error {
assert.NoError(t, insertPersonWithID(t, tx.Querier, person))
panic("epic panic!")
})
})
assert.Equal(t, db.Reload(person), reform.ErrNoRows)
// duplicate PK in closure
err = db.InTransaction(func(tx *reform.TX) error {
assert.NoError(t, insertPersonWithID(t, tx.Querier, person))
err = insertPersonWithID(t, tx.Querier, person)
assert.Error(t, err)
return err
})
assert.Error(t, err)
assert.Equal(t, db.Reload(person), reform.ErrNoRows)
// no error
err = db.InTransaction(func(tx *reform.TX) error {
assert.NoError(t, insertPersonWithID(t, tx.Querier, person))
return nil
})
assert.NoError(t, err)
assert.NoError(t, db.Reload(person))
assert.NoError(t, db.Delete(person))
}