1
0
mirror of https://github.com/xorcare/testing-go-code-with-postgres.git synced 2025-01-11 18:14:17 +02:00

Add example of tests with transactional cleanup

This commit is contained in:
Vasiliy Vasilyuk 2024-02-17 18:12:44 +03:00
parent 13e3596692
commit 05d30f6c09
6 changed files with 221 additions and 11 deletions

View File

@ -77,6 +77,7 @@ total: (statements) 100.0%
in [testingpg](https://github.com/xorcare/testing-go-code-with-postgres/tree/main/testingpg)
package.
- [Example of integration testing with isolated database for each testcase](https://github.com/xorcare/testing-go-code-with-postgres/blob/main/user_repository_with_isolated_database_test.go).
- [Example of integration testing with transaction cleanup for each testcase](https://github.com/xorcare/testing-go-code-with-postgres/blob/main/user_repository_with_transactional_cleanup_test.go).
- And example
of [GitHub Actions](https://github.com/xorcare/testing-go-code-with-postgres/blob/main/.github/workflows/go.yml)
and [Gitlab CI](https://github.com/xorcare/testing-go-code-with-postgres/blob/main/.gitlab-ci.yml).

View File

@ -8,7 +8,7 @@ services:
POSTGRES_DB: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_USER: postgres
POSTGRES_MULTIPLE_DATABASES: reference
POSTGRES_MULTIPLE_DATABASES: reference, transaction
healthcheck:
test: pg_isready --username "postgres" --dbname "reference"
interval: 1s
@ -21,7 +21,7 @@ services:
tmpfs:
- /var/lib/postgresql/data:rw # Necessary to speed up integration tests.
migrate:
migrate-reference:
image: migrate/migrate:v4.16.2
command: >
-source 'file:///migrations'
@ -31,3 +31,25 @@ services:
condition: service_healthy
volumes:
- ./migrations:/migrations:ro
migrate-transaction:
image: migrate/migrate:v4.16.2
command: >
-source 'file:///migrations'
-database 'postgresql://postgres:postgres@postgres:5432/transaction?sslmode=disable' up
depends_on:
postgres:
condition: service_healthy
volumes:
- ./migrations:/migrations:ro
migrate:
image: postgres:15.3-alpine3.18
command: echo 'All migrations have been successfully applied!'
depends_on:
postgres:
condition: service_healthy
migrate-reference:
condition: service_completed_successfully
migrate-transaction:
condition: service_completed_successfully

View File

@ -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

View File

@ -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")
})
}

View File

@ -8,3 +8,4 @@ package testing_go_code_with_postgres_test
// database cleanup, this file is empty. The specific tests can be found
// in the following files:
// - user_repository_with_isolated_database_test.go
// - user_repository_with_transactional_cleanup_test.go

View File

@ -0,0 +1,96 @@
// Copyright (c) 2023-2024 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 testing_go_code_with_postgres_test
import (
"context"
"testing"
"time"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
rootpkg "github.com/xorcare/testing-go-code-with-postgres"
"github.com/xorcare/testing-go-code-with-postgres/testingpg"
)
func Test_Transactional_UserRepository_CreateUser(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode")
}
t.Parallel()
newFullyFiledUser := func() rootpkg.User {
return rootpkg.User{
ID: uuid.New(),
Username: "gopher",
CreatedAt: time.Now().Truncate(time.Microsecond),
}
}
t.Run("Successfully created a User", func(t *testing.T) {
t.Parallel()
// Arrange
db := testingpg.NewWithTransactionalCleanup(t)
repo := rootpkg.NewUserRepository(db)
user := newFullyFiledUser()
// Act
err := repo.CreateUser(context.Background(), user)
// Assert
require.NoError(t, err)
gotUser, err := repo.ReadUser(context.Background(), user.ID)
require.NoError(t, err)
require.Equal(t, user, gotUser)
})
t.Run("Cannot create a user with the same ID", func(t *testing.T) {
t.Parallel()
// Arrange
db := testingpg.NewWithTransactionalCleanup(t)
repo := rootpkg.NewUserRepository(db)
user := newFullyFiledUser()
err := repo.CreateUser(context.Background(), user)
require.NoError(t, err)
// Act
err = repo.CreateUser(context.Background(), user)
// Assert
require.Error(t, err)
require.Contains(t, err.Error(), "duplicate key value violates unique constraint")
})
}
func Test_Transactional_UserRepository_ReadUser(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode")
}
t.Parallel()
t.Run("Get an error if the user does not exist", func(t *testing.T) {
t.Parallel()
// Arrange
db := testingpg.NewWithTransactionalCleanup(t)
repo := rootpkg.NewUserRepository(db)
// Act
_, err := repo.ReadUser(context.Background(), uuid.New())
// Assert
require.Error(t, err)
})
}