mirror of
https://github.com/bitechdev/ResolveSpec.git
synced 2026-10-01 19:20:31 +00:00
test(tx): regression tests for per-request transactions across all specs
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/DATA-DOG/go-sqlmock"
|
||||
|
||||
@@ -54,3 +55,35 @@ func TestSubqueryPreloadErrorIsReturned(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// With a single pooled connection, a preload that escaped the transaction would
|
||||
// block on the pool and fail on the context timeout.
|
||||
func TestSubqueryPreloadRunsOnTheTransaction(t *testing.T) {
|
||||
db, mock, err := sqlmock.New()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer db.Close()
|
||||
db.SetMaxOpenConns(1)
|
||||
|
||||
mock.ExpectBegin()
|
||||
mock.ExpectQuery(`FROM parents`).WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(1))
|
||||
mock.ExpectQuery(`FROM children`).WillReturnRows(sqlmock.NewRows([]string{"id", "user_id"}).AddRow(10, 1))
|
||||
mock.ExpectCommit()
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
||||
defer cancel()
|
||||
var parents []preloadParent
|
||||
err = NewPgSQLAdapter(db).RunInTransaction(ctx, func(tx common.Database) error {
|
||||
return tx.NewSelect().Model(&preloadParent{}).PreloadRelation("Children").Scan(ctx, &parents)
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(parents) != 1 || len(parents[0].Children) != 1 {
|
||||
t.Fatalf("preloaded children missing: %+v", parents)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user