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
Publish an example testing go code with Postgres
This commit is contained in:
116
testingpg/testingpg.go
Normal file
116
testingpg/testingpg.go
Normal file
@ -0,0 +1,116 @@
|
||||
// Copyright (c) 2023 Vasiliy Vasilyuk. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package testingpg
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
type TestingT interface {
|
||||
require.TestingT
|
||||
|
||||
Logf(format string, args ...any)
|
||||
Cleanup(f func())
|
||||
}
|
||||
|
||||
func New(t TestingT) *Postgres {
|
||||
return newPostgres(t).cloneFromReference(t)
|
||||
}
|
||||
|
||||
type Postgres struct {
|
||||
url string
|
||||
ref string
|
||||
|
||||
conn *pgxpool.Pool
|
||||
}
|
||||
|
||||
func newPostgres(t TestingT) *Postgres {
|
||||
urlStr := os.Getenv("TESTING_DB_URL")
|
||||
if urlStr == "" {
|
||||
urlStr = "postgresql://postgres:postgres@localhost:32260/postgres?sslmode=disable"
|
||||
const format = "env TESTING_DB_URL is empty, used default value: %s"
|
||||
t.Logf(format, urlStr)
|
||||
}
|
||||
|
||||
refDatabase := os.Getenv("TESTING_DB_REF")
|
||||
if refDatabase == "" {
|
||||
refDatabase = "reference"
|
||||
}
|
||||
|
||||
pool, err := pgxpool.New(context.Background(), urlStr)
|
||||
require.NoError(t, err)
|
||||
|
||||
return &Postgres{
|
||||
url: urlStr,
|
||||
ref: refDatabase,
|
||||
|
||||
conn: pool,
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Postgres) URL() string {
|
||||
return p.url
|
||||
}
|
||||
|
||||
func (p *Postgres) PgxPool() *pgxpool.Pool {
|
||||
return p.conn
|
||||
}
|
||||
|
||||
func (p *Postgres) cloneFromReference(t TestingT) *Postgres {
|
||||
cfg, err := pgxpool.ParseConfig(p.url)
|
||||
require.NoError(t, err)
|
||||
|
||||
pool, err := pgxpool.New(context.Background(), p.url)
|
||||
require.NoError(t, err)
|
||||
|
||||
newDatabaseName := uuid.New().String()
|
||||
|
||||
const sqlTemplate = `CREATE DATABASE %q WITH TEMPLATE %s OWNER %s;`
|
||||
sql := fmt.Sprintf(
|
||||
sqlTemplate,
|
||||
newDatabaseName,
|
||||
p.ref,
|
||||
cfg.ConnConfig.User,
|
||||
)
|
||||
_, err = pool.Exec(context.Background(), sql)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Automatically drop database copy after the test is completed.
|
||||
t.Cleanup(func() {
|
||||
sql := fmt.Sprintf(`DROP DATABASE %q WITH (FORCE);`, newDatabaseName)
|
||||
|
||||
ctx, done := context.WithTimeout(context.Background(), time.Minute)
|
||||
defer done()
|
||||
|
||||
_, err := p.conn.Exec(ctx, sql)
|
||||
require.NoError(t, err)
|
||||
})
|
||||
|
||||
urlString := replaceDBName(t, cfg, newDatabaseName)
|
||||
newPool, err := pgxpool.New(context.Background(), urlString)
|
||||
require.NoError(t, err)
|
||||
|
||||
return &Postgres{
|
||||
url: urlString,
|
||||
ref: newDatabaseName,
|
||||
|
||||
conn: newPool,
|
||||
}
|
||||
}
|
||||
|
||||
func replaceDBName(t TestingT, cfg *pgxpool.Config, dbname string) string {
|
||||
r, err := url.Parse(cfg.ConnString())
|
||||
require.NoError(t, err)
|
||||
r.Path = dbname
|
||||
return r.String()
|
||||
}
|
95
testingpg/testingpg_test.go
Normal file
95
testingpg/testingpg_test.go
Normal file
@ -0,0 +1,95 @@
|
||||
// Copyright (c) 2023 Vasiliy Vasilyuk. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package testingpg_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/xorcare/testing-go-code-with-postgres/testingpg"
|
||||
)
|
||||
|
||||
func TestNewPostgres(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("skipping test in short mode")
|
||||
}
|
||||
|
||||
t.Parallel()
|
||||
|
||||
t.Run("Successfully connect by URL and get version", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
// Arrange
|
||||
postgres := testingpg.New(t)
|
||||
|
||||
ctx := context.Background()
|
||||
dbPool, err := pgxpool.New(ctx, postgres.URL())
|
||||
require.NoError(t, err)
|
||||
|
||||
// Act
|
||||
var version string
|
||||
err = dbPool.QueryRow(ctx, "SELECT version();").Scan(&version)
|
||||
|
||||
// Assert
|
||||
require.NoError(t, err)
|
||||
require.NotEmpty(t, version)
|
||||
t.Log(version)
|
||||
})
|
||||
|
||||
t.Run("Successfully obtained a version using a pre-configured conn", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
// Arrange
|
||||
postgres := testingpg.New(t)
|
||||
ctx := context.Background()
|
||||
|
||||
// Act
|
||||
var version string
|
||||
err := postgres.PgxPool().QueryRow(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
|
||||
postgres1 := testingpg.New(t)
|
||||
postgres2 := testingpg.New(t)
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
// Act
|
||||
const sql = `CREATE TABLE "no_conflict" (id integer PRIMARY KEY)`
|
||||
_, err1 := postgres1.PgxPool().Exec(ctx, sql)
|
||||
_, err2 := postgres2.PgxPool().Exec(ctx, sql)
|
||||
|
||||
// Assert
|
||||
require.NoError(t, err1)
|
||||
require.NoError(t, err2, "databases must be isolated for each instance")
|
||||
})
|
||||
|
||||
t.Run("URL is different at different instances", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
// Arrange
|
||||
postgres1 := testingpg.New(t)
|
||||
postgres2 := testingpg.New(t)
|
||||
|
||||
// Act
|
||||
url1 := postgres1.URL()
|
||||
url2 := postgres2.URL()
|
||||
|
||||
// Assert
|
||||
require.NotEqual(t, url1, url2)
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user