forked from gobuffalo/buffalo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpop_transaction_test.go
127 lines (117 loc) · 2.69 KB
/
pop_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
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
package middleware
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
"time"
"github.com/gobuffalo/buffalo"
"github.com/gobuffalo/pop"
"github.com/gobuffalo/uuid"
"github.com/markbates/willie"
"github.com/pkg/errors"
"github.com/stretchr/testify/require"
)
type widget struct {
ID uuid.UUID `db:"id"`
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"`
}
func tx(fn func(tx *pop.Connection)) error {
pop.Debug = true
defer func() { pop.Debug = false }()
d, err := ioutil.TempDir("", "")
if err != nil {
return errors.WithStack(err)
}
path := filepath.Join(d, "pt_test.sqlite")
defer os.RemoveAll(path)
db, err := pop.NewConnection(&pop.ConnectionDetails{
Dialect: "sqlite",
URL: path,
})
if err != nil {
return errors.WithStack(err)
}
if err := db.Dialect.CreateDB(); err != nil {
return errors.WithStack(err)
}
if err := db.Open(); err != nil {
return err
}
if err := db.RawQuery(mig).Exec(); err != nil {
return err
}
fn(db)
return nil
}
func app(db *pop.Connection) *buffalo.App {
app := buffalo.New(buffalo.Options{})
app.Use(PopTransaction(db))
app.GET("/success", func(c buffalo.Context) error {
w := &widget{}
tx := c.Value("tx").(*pop.Connection)
if err := tx.Create(w); err != nil {
return err
}
return c.Render(201, nil)
})
app.GET("/non-success", func(c buffalo.Context) error {
w := &widget{}
tx := c.Value("tx").(*pop.Connection)
if err := tx.Create(w); err != nil {
return err
}
return c.Render(301, nil)
})
app.GET("/error", func(c buffalo.Context) error {
w := &widget{}
tx := c.Value("tx").(*pop.Connection)
if err := tx.Create(w); err != nil {
return err
}
return errors.New("boom")
})
return app
}
func Test_PopTransaction(t *testing.T) {
r := require.New(t)
err := tx(func(db *pop.Connection) {
w := willie.New(app(db))
res := w.HTML("/success").Get()
r.Equal(201, res.Code)
count, err := db.Count("widgets")
r.NoError(err)
r.Equal(1, count)
})
r.NoError(err)
}
func Test_PopTransaction_Error(t *testing.T) {
r := require.New(t)
err := tx(func(db *pop.Connection) {
w := willie.New(app(db))
res := w.HTML("/error").Get()
r.Equal(500, res.Code)
count, err := db.Count("widgets")
r.NoError(err)
r.Equal(0, count)
})
r.NoError(err)
}
func Test_PopTransaction_NonSuccess(t *testing.T) {
r := require.New(t)
err := tx(func(db *pop.Connection) {
w := willie.New(app(db))
res := w.HTML("/non-success").Get()
r.Equal(301, res.Code)
count, err := db.Count("widgets")
r.NoError(err)
r.Equal(1, count)
})
r.NoError(err)
}
const mig = `CREATE TABLE "widgets" (
"created_at" DATETIME NOT NULL,
"updated_at" DATETIME NOT NULL,
"id" TEXT PRIMARY KEY
);`