You've already forked testing-go-code-with-postgres
mirror of
https://github.com/xorcare/testing-go-code-with-postgres.git
synced 2025-06-30 23:23:40 +02:00
Add example of tests with transactional cleanup
This commit is contained in:
@ -31,8 +31,36 @@ type TestingT interface {
|
||||
Failed() bool
|
||||
}
|
||||
|
||||
const defaultPostgresURL = "postgresql://postgres:postgres@localhost:32260/postgres?sslmode=disable"
|
||||
|
||||
func NewWithIsolatedDatabase(t TestingT) *Postgres {
|
||||
return newPostgres(t).cloneFromReference()
|
||||
return newPostgres(t, defaultPostgresURL).cloneFromReference()
|
||||
}
|
||||
|
||||
func NewWithTransactionalCleanup(t TestingT) interface {
|
||||
ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
|
||||
QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
|
||||
} {
|
||||
// databaseName a separate database is used for transactional cleanup.
|
||||
const databaseName = "transaction"
|
||||
|
||||
postgres := newPostgres(t, defaultPostgresURL)
|
||||
postgres = postgres.replaceDBName(databaseName)
|
||||
|
||||
ctx, done := context.WithCancel(context.Background())
|
||||
t.Cleanup(done)
|
||||
|
||||
tx, err := postgres.DB().BeginTx(ctx, &sql.TxOptions{
|
||||
Isolation: sql.LevelRepeatableRead,
|
||||
ReadOnly: false,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
t.Cleanup(func() {
|
||||
require.NoError(t, tx.Rollback())
|
||||
})
|
||||
|
||||
return tx
|
||||
}
|
||||
|
||||
type Postgres struct {
|
||||
@ -45,10 +73,10 @@ type Postgres struct {
|
||||
sqlDBOnce sync.Once
|
||||
}
|
||||
|
||||
func newPostgres(t TestingT) *Postgres {
|
||||
func newPostgres(t TestingT, defaultPostgresURL string) *Postgres {
|
||||
urlStr := os.Getenv("TESTING_DB_URL")
|
||||
if urlStr == "" {
|
||||
urlStr = "postgresql://postgres:postgres@localhost:32260/postgres?sslmode=disable"
|
||||
urlStr = defaultPostgresURL
|
||||
|
||||
const format = "env TESTING_DB_URL is empty, used default value: %s"
|
||||
|
||||
@ -105,11 +133,22 @@ func (p *Postgres) cloneFromReference() *Postgres {
|
||||
require.NoError(p.t, err)
|
||||
})
|
||||
|
||||
return p.replaceDBName(newDBName)
|
||||
}
|
||||
|
||||
func (p *Postgres) replaceDBName(newDBName string) *Postgres {
|
||||
o := p.clone()
|
||||
o.url = replaceDBName(p.t, p.URL(), newDBName)
|
||||
|
||||
return o
|
||||
}
|
||||
|
||||
func (p *Postgres) clone() *Postgres {
|
||||
return &Postgres{
|
||||
t: p.t,
|
||||
|
||||
url: replaceDBName(p.t, p.URL(), newDBName),
|
||||
ref: newDBName,
|
||||
url: p.url,
|
||||
ref: p.ref,
|
||||
}
|
||||
}
|
||||
|
||||
@ -176,7 +215,7 @@ func open(t TestingT, dataSourceURL string) *sql.DB {
|
||||
|
||||
// Automatically close connection after the test is completed.
|
||||
t.Cleanup(func() {
|
||||
db.Close()
|
||||
require.NoError(t, db.Close())
|
||||
})
|
||||
|
||||
return db
|
||||
|
@ -69,9 +69,9 @@ func TestNewPostgres(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
// Act
|
||||
const sql = `CREATE TABLE "no_conflict" (id integer PRIMARY KEY)`
|
||||
_, err1 := postgres1.DB().ExecContext(ctx, sql)
|
||||
_, err2 := postgres2.DB().ExecContext(ctx, sql)
|
||||
const sqlStr = `CREATE TABLE "no_conflict" (id integer PRIMARY KEY)`
|
||||
_, err1 := postgres1.DB().ExecContext(ctx, sqlStr)
|
||||
_, err2 := postgres2.DB().ExecContext(ctx, sqlStr)
|
||||
|
||||
// Assert
|
||||
require.NoError(t, err1)
|
||||
@ -93,3 +93,54 @@ func TestNewPostgres(t *testing.T) {
|
||||
require.NotEqual(t, url1, url2)
|
||||
})
|
||||
}
|
||||
|
||||
func TestNewWithTransactionalCleanup(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("skipping test in short mode")
|
||||
}
|
||||
|
||||
t.Parallel()
|
||||
|
||||
t.Run("Successfully obtained a version", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
// Arrange
|
||||
tx := testingpg.NewWithTransactionalCleanup(t)
|
||||
ctx := context.Background()
|
||||
|
||||
// Act
|
||||
var version string
|
||||
err := tx.QueryRowContext(ctx, "SELECT version();").Scan(&version)
|
||||
|
||||
// Assert
|
||||
require.NoError(t, err)
|
||||
require.NotEmpty(t, version)
|
||||
|
||||
t.Log(version)
|
||||
})
|
||||
|
||||
t.Run("Changes are not visible in different instances", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
// Arrange
|
||||
ctx := context.Background()
|
||||
const sqlStr = `CREATE TABLE "no_conflict" (id integer PRIMARY KEY)`
|
||||
|
||||
t.Run("Arrange", func(t *testing.T) {
|
||||
tx := testingpg.NewWithTransactionalCleanup(t)
|
||||
_, err := tx.ExecContext(ctx, sqlStr)
|
||||
require.NoError(t, err)
|
||||
})
|
||||
|
||||
var err error
|
||||
|
||||
// Act
|
||||
t.Run("Act", func(t *testing.T) {
|
||||
tx := testingpg.NewWithTransactionalCleanup(t)
|
||||
_, err = tx.ExecContext(ctx, sqlStr)
|
||||
})
|
||||
|
||||
// Assert
|
||||
require.NoError(t, err, "side effects must be isolated for each instance")
|
||||
})
|
||||
}
|
||||
|
Reference in New Issue
Block a user