Skip to content

Commit 1a8d551

Browse files
authored
Informer interface (#27)
* Informer interface
1 parent 3e4c478 commit 1a8d551

10 files changed

+84
-18
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func TestDatabase(t *testing.T) {
9595
configuration.
9696
*/
9797

98-
pool := testdock.GetPgxPool(t,
98+
pool, _ := testdock.GetPgxPool(t,
9999
testdock.DefaultPostgresDSN,
100100
testdock.WithMigrations("migrations", testdock.GooseMigrateFactoryPGX),
101101
)
@@ -115,7 +115,7 @@ import (
115115

116116
func TestMongoDB(t *testing.T) {
117117
// Get a connection to a test database
118-
db := testdock.GetMongoDatabase(t, testdock.DefaultMongoDSN,
118+
db, _ := testdock.GetMongoDatabase(t, testdock.DefaultMongoDSN,
119119
testdock.WithMode(testdock.RunModeDocker),
120120
testdock.WithMigrations("migrations", testdock.GolangMigrateFactory),
121121
)
@@ -166,7 +166,7 @@ TestDock supports two popular migration tools:
166166
<https://github.com/pressly/goose>
167167

168168
```go
169-
db := GetPqConn(t,
169+
db, _ := GetPqConn(t,
170170
"postgres://postgres:secret@127.0.0.1:5432/postgres?sslmode=disable",
171171
testdock.WithMigrations("migrations/pg/goose", testdock.GooseMigrateFactoryPQ),
172172
testdock.WithDockerImage("17.2"),
@@ -178,7 +178,7 @@ TestDock supports two popular migration tools:
178178
<https://github.com/golang-migrate/migrate>
179179

180180
```go
181-
db := GetMongoDatabase(t,
181+
db, _ := GetMongoDatabase(t,
182182
testdock.DefaultMongoDSN,
183183
WithDockerRepository("mongo"),
184184
WithDockerImage("6.0.20"),

db.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@ import (
1111
"github.com/cenkalti/backoff/v5"
1212
)
1313

14+
// Informer interface for database information.
15+
type Informer interface {
16+
// DSN returns the real database connection string.
17+
DSN() string
18+
// Host returns the host of the database server.
19+
Host() string
20+
// Port returns the port of the database server.
21+
Port() int
22+
// DatabaseName returns the database name for testing.
23+
DatabaseName() string
24+
}
25+
1426
const (
1527
// DefaultRetryTimeout is the default retry timeout.
1628
DefaultRetryTimeout = time.Second * 3
@@ -210,3 +222,23 @@ func (d *testDB) retryConnect(info string, op func() error) error {
210222

211223
return nil
212224
}
225+
226+
// DSN returns the real database connection string.
227+
func (d *testDB) DSN() string {
228+
return d.url.replaceDatabase(d.databaseName).string(false)
229+
}
230+
231+
// Host returns the database host.
232+
func (d *testDB) Host() string {
233+
return d.url.Host
234+
}
235+
236+
// Port returns the database port.
237+
func (d *testDB) Port() int {
238+
return d.url.Port
239+
}
240+
241+
// DatabaseName returns the database name for testing.
242+
func (d *testDB) DatabaseName() string {
243+
return d.databaseName
244+
}

mongodb.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
const mongoDriverName = "mongodb"
1414

1515
// GetMongoDatabase initializes a test MongoDB database, applies migrations, and returns a database connection.
16-
func GetMongoDatabase(tb testing.TB, dsn string, opt ...Option) *mongo.Database {
16+
func GetMongoDatabase(tb testing.TB, dsn string, opt ...Option) (*mongo.Database, Informer) {
1717
tb.Helper()
1818

1919
url, err := parseURL(dsn)
@@ -42,7 +42,7 @@ func GetMongoDatabase(tb testing.TB, dsn string, opt ...Option) *mongo.Database
4242

4343
tb.Cleanup(func() { _ = client.Disconnect(context.Background()) })
4444

45-
return client.Database(tDB.databaseName)
45+
return client.Database(tDB.databaseName), tDB
4646
}
4747

4848
// connectDB connects to MongoDB with retries

mongodb_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@ func TestMongoDB(t *testing.T) {
1414
ctx := context.Background()
1515

1616
// Create test database and return a MongoDB client
17-
db := GetMongoDatabase(t,
17+
db, informer := GetMongoDatabase(t,
1818
DefaultMongoDSN,
1919
WithDockerRepository("mongo"),
2020
WithDockerImage("6.0.20"),
2121
WithMigrations("migrations/mongodb", GolangMigrateFactory),
2222
)
2323

24+
checkInformer(t, DefaultMongoDSN, informer)
25+
2426
// Test collection
2527
collection := db.Collection("test_collection")
2628

mysql.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010

1111
// GetMySQLConn inits a test mysql database, applies migrations.
1212
// Use user root for docker test database.
13-
func GetMySQLConn(tb testing.TB, dsn string, opt ...Option) *sql.DB {
13+
func GetMySQLConn(tb testing.TB, dsn string, opt ...Option) (*sql.DB, Informer) {
1414
tb.Helper()
1515

1616
url, err := parseURL(dsn)

mysql_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ import (
99
func Test_MySQLDB(t *testing.T) {
1010
t.Parallel()
1111

12-
db := GetMySQLConn(t,
12+
db, informer := GetMySQLConn(t,
1313
DefaultMySQLDSN,
1414
WithMigrations("migrations/pg/goose", GooseMigrateFactoryMySQL),
1515
WithRetryTimeout(time.Second*5),
1616
WithTotalRetryDuration(time.Second*60), //nolint:mnd // for Docker 30s not enough
1717
)
1818

19+
checkInformer(t, DefaultMySQLDSN, informer)
20+
1921
testSQLHelper(t, db)
2022
}
2123

postgresql.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414

1515
// GetPgxPool inits a test postgresql (pgx driver) database, applies migrations,
1616
// and returns pgx connection pool to the database.
17-
func GetPgxPool(tb testing.TB, dsn string, opt ...Option) *pgxpool.Pool {
17+
func GetPgxPool(tb testing.TB, dsn string, opt ...Option) (*pgxpool.Pool, Informer) {
1818
tb.Helper()
1919

2020
tDB := newTDB(tb, "pgx", dsn, getPostgresOptions(tb, dsn, opt...))
@@ -26,12 +26,12 @@ func GetPgxPool(tb testing.TB, dsn string, opt ...Option) *pgxpool.Pool {
2626

2727
tb.Cleanup(func() { db.Close() })
2828

29-
return db
29+
return db, tDB
3030
}
3131

3232
// GetPqConn inits a test postgresql (pq driver) database, applies migrations,
3333
// and returns sql connection to the database.
34-
func GetPqConn(tb testing.TB, dsn string, opt ...Option) *sql.DB {
34+
func GetPqConn(tb testing.TB, dsn string, opt ...Option) (*sql.DB, Informer) {
3535
tb.Helper()
3636

3737
tDB := newTDB(tb, "postgres", dsn, getPostgresOptions(tb, dsn, opt...))
@@ -43,7 +43,7 @@ func GetPqConn(tb testing.TB, dsn string, opt ...Option) *sql.DB {
4343

4444
tb.Cleanup(func() { _ = db.Close() })
4545

46-
return db
46+
return db, tDB
4747
}
4848

4949
// connectPgxDB connects to the database with retries using pgx.

postgresql_test.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,32 +13,36 @@ const testPostgresImage = "17.2"
1313
func Test_PgxGooseDB(t *testing.T) {
1414
t.Parallel()
1515

16-
db := GetPgxPool(t,
16+
db, informer := GetPgxPool(t,
1717
DefaultPostgresDSN,
1818
WithMigrations("migrations/pg/goose", GooseMigrateFactoryPGX),
1919
WithDockerImage(testPostgresImage),
2020
)
2121

22+
checkInformer(t, DefaultPostgresDSN, informer)
23+
2224
testPgxHelper(t, db)
2325
}
2426

2527
func Test_PgxGomigrateDB(t *testing.T) {
2628
t.Parallel()
2729

28-
db := GetPgxPool(t,
30+
db, informer := GetPgxPool(t,
2931
DefaultPostgresDSN,
3032
WithMigrations("migrations/pg/gomigrate", GolangMigrateFactory),
3133
WithDockerImage(testPostgresImage),
3234
WithMode(RunModeDocker), // force run in docker
3335
)
3436

37+
checkInformer(t, DefaultPostgresDSN, informer)
38+
3539
testPgxHelper(t, db)
3640
}
3741

3842
func Test_LibPGDB(t *testing.T) {
3943
t.Parallel()
4044

41-
db := GetPqConn(t,
45+
db, _ := GetPqConn(t,
4246
DefaultPostgresDSN,
4347
WithMigrations("migrations/pg/goose", GooseMigrateFactoryPQ),
4448
WithDockerImage(testPostgresImage),

prepare_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package testdock
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
)
8+
9+
func checkInformer(t *testing.T, defaultDSN string, informer Informer) {
10+
t.Helper()
11+
12+
defaultURL, err := parseURL(defaultDSN)
13+
require.NoError(t, err)
14+
15+
url, err := parseURL(informer.DSN())
16+
require.NoError(t, err)
17+
18+
require.NotEqual(t, defaultURL.Database, url.Database)
19+
require.NotEqual(t, defaultURL.Database, informer.DatabaseName())
20+
require.Equal(t, defaultURL.User, url.User)
21+
require.Equal(t, defaultURL.Password, url.Password)
22+
require.Equal(t, defaultURL.Host, informer.Host())
23+
require.Equal(t, url.Port, informer.Port())
24+
}

sql.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
// GetSQLConn inits a test database, applies migrations, and returns sql connection to the database.
1010
// driver: https://go.dev/wiki/SQLDrivers.
1111
// Do not forget to import corresponding driver package.
12-
func GetSQLConn(tb testing.TB, driver, dsn string, opt ...Option) *sql.DB {
12+
func GetSQLConn(tb testing.TB, driver, dsn string, opt ...Option) (*sql.DB, Informer) {
1313
tb.Helper()
1414

1515
tDB := newTDB(tb, driver, dsn, opt)
@@ -19,9 +19,11 @@ func GetSQLConn(tb testing.TB, driver, dsn string, opt ...Option) *sql.DB {
1919
tDB.logger.Fatalf("%v", err)
2020
}
2121

22+
23+
2224
tb.Cleanup(func() { _ = db.Close() })
2325

24-
return db
26+
return db, tDB
2527
}
2628

2729
// connectSQLDB connects to the database with retries using database/sql.

0 commit comments

Comments
 (0)