-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
199 additions
and
166 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
test: | ||
@go test -tags="integration" -race -coverprofile=profile.cov -v $(shell go list ./... | grep -vE 'cmd|mocks|testdata|testutil') | ||
@go test -tags="integration" -race -coverprofile=profile.cov -v $(go list ./... | grep -vE 'cmd|mocks|testdata|testutil') | ||
@go tool cover -func=profile.cov | grep total |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
//go:build integration | ||
// +build integration | ||
|
||
package pgxtxv5_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/aneshas/tx" | ||
"github.com/aneshas/tx/pgxtxv5" | ||
"github.com/aneshas/tx/testutil" | ||
"github.com/jackc/pgx/v5/pgxpool" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
var db *pgxpool.Pool | ||
|
||
func TestMain(m *testing.M) { | ||
t := new(testing.T) | ||
|
||
db, _ = testutil.SetupDB(t) | ||
|
||
m.Run() | ||
} | ||
|
||
func TestShould_Commit_Pgx_Transaction(t *testing.T) { | ||
name := "success_pgx" | ||
|
||
doPgx(t, tx.New(pgxtxv5.NewDBFromPool(db)), name, false) | ||
testutil.AssertSuccess(t, db, name) | ||
} | ||
|
||
func TestShould_Rollback_Pgx_Transaction(t *testing.T) { | ||
name := "failure_pgx" | ||
|
||
doPgx(t, tx.New(pgxtxv5.NewDBFromPool(db)), name, true) | ||
testutil.AssertFailure(t, db, name) | ||
} | ||
|
||
func doPgx(t *testing.T, transactor *tx.TX, name string, fail bool) { | ||
t.Helper() | ||
|
||
err := transactor.Do(context.TODO(), func(ctx context.Context) error { | ||
ttx, _ := pgxtxv5.From(ctx) | ||
|
||
_, err := ttx.Exec(ctx, `insert into cats (name) values($1)`, name) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if fail { | ||
return fmt.Errorf("db error") | ||
} | ||
|
||
return err | ||
}) | ||
|
||
if !fail { | ||
assert.NoError(t, err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
//go:build integration | ||
// +build integration | ||
|
||
package sqltx_test | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"fmt" | ||
"github.com/aneshas/tx" | ||
"github.com/aneshas/tx/sqltx" | ||
"github.com/aneshas/tx/testutil" | ||
"github.com/jackc/pgx/v5/pgxpool" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
var ( | ||
pool *pgxpool.Pool | ||
db *sql.DB | ||
) | ||
|
||
func TestMain(m *testing.M) { | ||
t := new(testing.T) | ||
|
||
pool, db = testutil.SetupDB(t) | ||
|
||
m.Run() | ||
} | ||
|
||
func TestShould_Commit_Sql_Transaction(t *testing.T) { | ||
name := "success_sql" | ||
|
||
doSql(t, tx.New(sqltx.NewDB(db)), name, false) | ||
testutil.AssertSuccess(t, pool, name) | ||
} | ||
|
||
func TestShould_Rollback_Sql_Transaction(t *testing.T) { | ||
name := "failure_sql" | ||
|
||
doSql(t, tx.New(sqltx.NewDB(db)), name, true) | ||
testutil.AssertFailure(t, pool, name) | ||
} | ||
|
||
func doSql(t *testing.T, transactor *tx.TX, name string, fail bool) { | ||
t.Helper() | ||
|
||
err := transactor.Do(context.TODO(), func(ctx context.Context) error { | ||
ttx, _ := sqltx.From(ctx) | ||
|
||
_, err := ttx.Exec(`insert into cats (name) values($1)`, name) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if fail { | ||
return fmt.Errorf("db error") | ||
} | ||
|
||
return err | ||
}) | ||
|
||
if !fail { | ||
assert.NoError(t, err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package testutil | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"fmt" | ||
"github.com/jackc/pgx/v5" | ||
"github.com/jackc/pgx/v5/pgxpool" | ||
"github.com/orlangure/gnomock" | ||
"github.com/orlangure/gnomock/preset/postgres" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func SetupDB(t *testing.T) (*pgxpool.Pool, *sql.DB) { | ||
t.Helper() | ||
|
||
p := postgres.Preset( | ||
postgres.WithUser("gnomock", "gnomick"), | ||
postgres.WithDatabase("mydb"), | ||
postgres.WithQueriesFile("../testutil/db/schema.sql"), | ||
) | ||
|
||
container, err := gnomock.Start(p) | ||
assert.NoError(t, err) | ||
|
||
t.Cleanup(func() { _ = gnomock.Stop(container) }) | ||
|
||
connStr := fmt.Sprintf( | ||
"host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", | ||
container.Host, container.DefaultPort(), | ||
"gnomock", "gnomick", "mydb", | ||
) | ||
|
||
pgConfig, err := pgxpool.ParseConfig(connStr) | ||
assert.NoError(t, err) | ||
|
||
pool, err := pgxpool.NewWithConfig(context.Background(), pgConfig) | ||
assert.NoError(t, err) | ||
|
||
db, err := sql.Open("postgres", connStr) | ||
assert.NoError(t, err) | ||
|
||
return pool, db | ||
} | ||
|
||
func AssertSuccess(t *testing.T, pool *pgxpool.Pool, name string) { | ||
t.Helper() | ||
|
||
row := pool.QueryRow(context.TODO(), `select name from cats where name=$1`, name) | ||
|
||
var n string | ||
|
||
err := row.Scan(&n) | ||
assert.NoError(t, err) | ||
|
||
assert.Equal(t, name, n) | ||
} | ||
|
||
func AssertFailure(t *testing.T, pool *pgxpool.Pool, name string) { | ||
t.Helper() | ||
|
||
row := pool.QueryRow(context.TODO(), `select name from cats where name=$1`, name) | ||
|
||
var n string | ||
|
||
err := row.Scan(&n) | ||
|
||
assert.ErrorIs(t, err, pgx.ErrNoRows) | ||
} |