-
Notifications
You must be signed in to change notification settings - Fork 17
/
transaction_test.go
80 lines (58 loc) · 1.6 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
78
79
80
package crud_test
import (
"context"
"database/sql"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestSuccessfulCommit(t *testing.T) {
assert.Nil(t, CreateUserProfiles())
tx, err := DB.Begin(context.Background())
assert.Nil(t, err)
n := UserProfile{}
err = tx.Read(&n, "SELECT * from user_profiles WHERE id = ?", 2)
assert.Nil(t, err)
n.Bio = "let's go somewhere"
assert.Nil(t, tx.Update(&n))
azer := UserProfile{}
err = DB.Read(&azer, "SELECT * from user_profiles WHERE id = ?", 2)
assert.Nil(t, err)
assert.Equal(t, azer.Bio, "Engineer")
assert.Nil(t, tx.Commit())
time.Sleep(time.Second * 1)
azerc := UserProfile{}
err = DB.Read(&azerc, "SELECT * from user_profiles WHERE id = ?", 2)
assert.Nil(t, err)
assert.Equal(t, azerc.Bio, "let's go somewhere")
DB.DropTables(UserProfile{})
}
func TestRollback(t *testing.T) {
assert.Nil(t, CreateUserProfiles())
tx, err := DB.Begin(context.Background())
assert.Nil(t, err)
err = tx.Create(&UserProfile{
Email: "[email protected]",
Name: "Row1",
Bio: "testing transactions",
})
assert.Nil(t, err)
err = tx.Create(&UserProfile{
Email: "[email protected]",
Name: "Row2",
Bio: "testing transactions",
})
assert.Nil(t, err)
err = tx.Create(&UserProfile{
Email: "[email protected]",
Name: "Row3",
Bio: "testing transactions, should fail",
})
assert.Error(t, err)
assert.Nil(t, tx.Rollback())
shouldNotExist := UserProfile{}
err = DB.Read(&shouldNotExist, "SELECT * from user_profiles WHERE email = ?", "[email protected]")
assert.Error(t, err)
assert.True(t, err == sql.ErrNoRows)
DB.DropTables(UserProfile{})
}