mirror of https://github.com/VinGarcia/ksql.git
Refactor tests so we dont depend directly on database/sql nor pq
parent
edaf3af2d2
commit
7ba6537ed9
|
@ -13,7 +13,7 @@ require (
|
|||
github.com/gotestyourself/gotestyourself v2.2.0+incompatible // indirect
|
||||
github.com/jackc/pgconn v1.10.0
|
||||
github.com/jackc/pgx/v4 v4.13.0
|
||||
github.com/lib/pq v1.10.4
|
||||
github.com/lib/pq v1.10.4 // indirect
|
||||
github.com/opencontainers/image-spec v1.0.2 // indirect
|
||||
github.com/opencontainers/runc v1.1.0 // indirect
|
||||
github.com/ory/dockertest v3.3.5+incompatible
|
||||
|
|
|
@ -6,10 +6,6 @@ import (
|
|||
"github.com/jackc/pgx/v4/pgxpool"
|
||||
"github.com/vingarcia/ksql"
|
||||
"github.com/vingarcia/ksql/sqldialect"
|
||||
|
||||
// This is imported here so the user don't
|
||||
// have to worry about it when he uses it.
|
||||
_ "github.com/lib/pq"
|
||||
)
|
||||
|
||||
// NewFromPgxPool builds a ksql.DB from a *pgxpool.Pool instance
|
||||
|
|
|
@ -2,7 +2,6 @@ package kpgx
|
|||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
|
@ -17,11 +16,13 @@ import (
|
|||
)
|
||||
|
||||
func TestAdapter(t *testing.T) {
|
||||
postgresURL, closePostgres := startPostgresDB("ksql")
|
||||
ctx := context.Background()
|
||||
|
||||
postgresURL, closePostgres := startPostgresDB(ctx, "ksql")
|
||||
defer closePostgres()
|
||||
|
||||
ksql.RunTestsForAdapter(t, "kpgx", sqldialect.PostgresDialect{}, postgresURL, func(t *testing.T) (ksql.DBAdapter, io.Closer) {
|
||||
pool, err := pgxpool.Connect(context.TODO(), postgresURL)
|
||||
pool, err := pgxpool.Connect(ctx, postgresURL)
|
||||
if err != nil {
|
||||
t.Fatal(err.Error())
|
||||
}
|
||||
|
@ -38,15 +39,15 @@ func (c closerAdapter) Close() error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func startPostgresDB(dbName string) (databaseURL string, closer func()) {
|
||||
func startPostgresDB(ctx context.Context, dbName string) (databaseURL string, closer func()) {
|
||||
// uses a sensible default on windows (tcp/http) and linux/osx (socket)
|
||||
pool, err := dockertest.NewPool("")
|
||||
dockerPool, err := dockertest.NewPool("")
|
||||
if err != nil {
|
||||
log.Fatalf("Could not connect to docker: %s", err)
|
||||
}
|
||||
|
||||
// pulls an image, creates a container based on it and runs it
|
||||
resource, err := pool.RunWithOptions(
|
||||
resource, err := dockerPool.RunWithOptions(
|
||||
&dockertest.RunOptions{
|
||||
Repository: "postgres",
|
||||
Tag: "14.0",
|
||||
|
@ -74,15 +75,16 @@ func startPostgresDB(dbName string) (databaseURL string, closer func()) {
|
|||
|
||||
resource.Expire(40) // Tell docker to hard kill the container in 40 seconds
|
||||
|
||||
var sqlDB *sql.DB
|
||||
var sqlDB *pgxpool.Pool
|
||||
// exponential backoff-retry, because the application in the container might not be ready to accept connections yet
|
||||
pool.MaxWait = 10 * time.Second
|
||||
pool.Retry(func() error {
|
||||
sqlDB, err = sql.Open("postgres", databaseUrl)
|
||||
dockerPool.MaxWait = 10 * time.Second
|
||||
dockerPool.Retry(func() error {
|
||||
sqlDB, err = pgxpool.Connect(ctx, databaseUrl)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return sqlDB.Ping()
|
||||
|
||||
return sqlDB.Ping(ctx)
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatalf("Could not connect to docker: %s", err)
|
||||
|
@ -90,7 +92,7 @@ func startPostgresDB(dbName string) (databaseURL string, closer func()) {
|
|||
sqlDB.Close()
|
||||
|
||||
return databaseUrl, func() {
|
||||
if err := pool.Purge(resource); err != nil {
|
||||
if err := dockerPool.Purge(resource); err != nil {
|
||||
log.Fatalf("Could not purge resource: %s", err)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,7 +12,7 @@ require (
|
|||
github.com/google/go-cmp v0.5.6 // indirect
|
||||
github.com/gotestyourself/gotestyourself v2.2.0+incompatible // indirect
|
||||
github.com/jackc/pgx/v5 v5.3.1
|
||||
github.com/lib/pq v1.10.4
|
||||
github.com/lib/pq v1.10.4 // indirect
|
||||
github.com/opencontainers/image-spec v1.0.2 // indirect
|
||||
github.com/opencontainers/runc v1.1.0 // indirect
|
||||
github.com/ory/dockertest v3.3.5+incompatible
|
||||
|
|
|
@ -6,10 +6,6 @@ import (
|
|||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"github.com/vingarcia/ksql"
|
||||
"github.com/vingarcia/ksql/sqldialect"
|
||||
|
||||
// This is imported here so the user don't
|
||||
// have to worry about it when he uses it.
|
||||
_ "github.com/lib/pq"
|
||||
)
|
||||
|
||||
// NewFromPgxPool builds a ksql.DB from a *pgxpool.Pool instance
|
||||
|
|
|
@ -2,7 +2,6 @@ package kpgx
|
|||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
|
@ -17,11 +16,13 @@ import (
|
|||
)
|
||||
|
||||
func TestAdapter(t *testing.T) {
|
||||
postgresURL, closePostgres := startPostgresDB("ksql")
|
||||
ctx := context.Background()
|
||||
|
||||
postgresURL, closePostgres := startPostgresDB(ctx, "ksql")
|
||||
defer closePostgres()
|
||||
|
||||
ksql.RunTestsForAdapter(t, "kpgx", sqldialect.PostgresDialect{}, postgresURL, func(t *testing.T) (ksql.DBAdapter, io.Closer) {
|
||||
pool, err := pgxpool.New(context.TODO(), postgresURL)
|
||||
pool, err := pgxpool.New(ctx, postgresURL)
|
||||
if err != nil {
|
||||
t.Fatal(err.Error())
|
||||
}
|
||||
|
@ -38,7 +39,7 @@ func (c closerAdapter) Close() error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func startPostgresDB(dbName string) (databaseURL string, closer func()) {
|
||||
func startPostgresDB(ctx context.Context, dbName string) (databaseURL string, closer func()) {
|
||||
// uses a sensible default on windows (tcp/http) and linux/osx (socket)
|
||||
pool, err := dockertest.NewPool("")
|
||||
if err != nil {
|
||||
|
@ -74,15 +75,16 @@ func startPostgresDB(dbName string) (databaseURL string, closer func()) {
|
|||
|
||||
resource.Expire(40) // Tell docker to hard kill the container in 40 seconds
|
||||
|
||||
var sqlDB *sql.DB
|
||||
var sqlDB *pgxpool.Pool
|
||||
// exponential backoff-retry, because the application in the container might not be ready to accept connections yet
|
||||
pool.MaxWait = 10 * time.Second
|
||||
pool.Retry(func() error {
|
||||
sqlDB, err = sql.Open("postgres", databaseUrl)
|
||||
sqlDB, err = pgxpool.New(ctx, databaseUrl)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return sqlDB.Ping()
|
||||
|
||||
return sqlDB.Ping(ctx)
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatalf("Could not connect to docker: %s", err)
|
||||
|
|
521
test_adapters.go
521
test_adapters.go
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue