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 set of small fixes to testingpg package
Added some set of disparate improvements, to improve readability.
This commit is contained in:
@ -25,7 +25,7 @@ type TestingT interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func New(t TestingT) *Postgres {
|
func New(t TestingT) *Postgres {
|
||||||
return newPostgres(t).cloneFromReference(t)
|
return newPostgres(t).cloneFromReference()
|
||||||
}
|
}
|
||||||
|
|
||||||
type Postgres struct {
|
type Postgres struct {
|
||||||
@ -34,8 +34,8 @@ type Postgres struct {
|
|||||||
url string
|
url string
|
||||||
ref string
|
ref string
|
||||||
|
|
||||||
conn *pgxpool.Pool
|
pgxpool *pgxpool.Pool
|
||||||
once sync.Once
|
pgxpoolOnce sync.Once
|
||||||
}
|
}
|
||||||
|
|
||||||
func newPostgres(t TestingT) *Postgres {
|
func newPostgres(t TestingT) *Postgres {
|
||||||
@ -51,16 +51,11 @@ func newPostgres(t TestingT) *Postgres {
|
|||||||
refDatabase = "reference"
|
refDatabase = "reference"
|
||||||
}
|
}
|
||||||
|
|
||||||
pool, err := pgxpool.New(context.Background(), urlStr)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
return &Postgres{
|
return &Postgres{
|
||||||
t: t,
|
t: t,
|
||||||
|
|
||||||
url: urlStr,
|
url: urlStr,
|
||||||
ref: refDatabase,
|
ref: refDatabase,
|
||||||
|
|
||||||
conn: pool,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -69,42 +64,40 @@ func (p *Postgres) URL() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *Postgres) PgxPool() *pgxpool.Pool {
|
func (p *Postgres) PgxPool() *pgxpool.Pool {
|
||||||
p.once.Do(func() {
|
p.pgxpoolOnce.Do(func() {
|
||||||
p.conn = newPGxPool(p.t, p.URL())
|
p.pgxpool = newPGxPool(p.t, p.URL())
|
||||||
})
|
})
|
||||||
|
|
||||||
return p.conn
|
return p.pgxpool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Postgres) cloneFromReference(t TestingT) *Postgres {
|
func (p *Postgres) cloneFromReference() *Postgres {
|
||||||
newDatabaseName := uuid.New().String()
|
newDatabaseName := uuid.New().String()
|
||||||
|
|
||||||
const sqlTemplate = `CREATE DATABASE %q WITH TEMPLATE %s;`
|
|
||||||
sql := fmt.Sprintf(
|
sql := fmt.Sprintf(
|
||||||
sqlTemplate,
|
`CREATE DATABASE %q WITH TEMPLATE %s;`,
|
||||||
newDatabaseName,
|
newDatabaseName,
|
||||||
p.ref,
|
p.ref,
|
||||||
)
|
)
|
||||||
|
|
||||||
_, err := p.PgxPool().Exec(context.Background(), sql)
|
_, err := p.PgxPool().Exec(context.Background(), sql)
|
||||||
require.NoError(t, err)
|
require.NoError(p.t, err)
|
||||||
|
|
||||||
// Automatically drop database copy after the test is completed.
|
// Automatically drop database copy after the test is completed.
|
||||||
t.Cleanup(func() {
|
p.t.Cleanup(func() {
|
||||||
sql := fmt.Sprintf(`DROP DATABASE %q WITH (FORCE);`, newDatabaseName)
|
sql := fmt.Sprintf(`DROP DATABASE %q WITH (FORCE);`, newDatabaseName)
|
||||||
|
|
||||||
ctx, done := context.WithTimeout(context.Background(), time.Minute)
|
ctx, done := context.WithTimeout(context.Background(), time.Minute)
|
||||||
defer done()
|
defer done()
|
||||||
|
|
||||||
_, err := p.conn.Exec(ctx, sql)
|
_, err := p.PgxPool().Exec(ctx, sql)
|
||||||
require.NoError(t, err)
|
require.NoError(p.t, err)
|
||||||
})
|
})
|
||||||
|
|
||||||
urlString := replaceDBName(t, p.URL(), newDatabaseName)
|
|
||||||
|
|
||||||
return &Postgres{
|
return &Postgres{
|
||||||
t: p.t,
|
t: p.t,
|
||||||
url: urlString,
|
|
||||||
|
url: replaceDBName(p.t, p.URL(), newDatabaseName),
|
||||||
ref: newDatabaseName,
|
ref: newDatabaseName,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user