test(tx): regression tests for per-request transactions across all specs

This commit is contained in:
2026-09-30 23:19:58 +02:00
parent 2042205817
commit 7f84debdc5
9 changed files with 712 additions and 1 deletions
@@ -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)
}
}