Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't rollback the transaction for implicit success #23

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pop/popmw/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func Transaction(db *pop.Connection) buffalo.MiddlewareFunc {
// check the response status code. if the code is NOT 200..399
// then it is considered "NOT SUCCESSFUL" and an error will be returned
if res, ok := c.Response().(*buffalo.Response); ok {
if res.Status < 200 || res.Status >= 400 {
if res.Status != 0 && (res.Status < 200 || res.Status >= 400) {
return errNonSuccess
}
}
Expand Down
21 changes: 21 additions & 0 deletions pop/popmw/tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ func app(db *pop.Connection) *buffalo.App {
}
return c.Render(201, nil)
})
app.GET("/success-implicit", func(c buffalo.Context) error {
w := &widget{}
tx := c.Value("tx").(*pop.Connection)
if err := tx.Create(w); err != nil {
return err
}
return nil
})
app.GET("/non-success", func(c buffalo.Context) error {
w := &widget{}
tx := c.Value("tx").(*pop.Connection)
Expand Down Expand Up @@ -94,6 +102,19 @@ func Test_PopTransaction(t *testing.T) {
r.NoError(err)
}

func Test_PopTransactionImplicit(t *testing.T) {
r := require.New(t)
err := tx(func(db *pop.Connection) {
w := httptest.New(app(db))
res := w.HTML("/success-implicit").Get()
r.Equal(200, 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) {
Expand Down