diff options
| author | Stefan Majewsky <majewsky@gmx.net> | 2026-07-31 15:47:17 +0200 |
|---|---|---|
| committer | Stefan Majewsky <majewsky@gmx.net> | 2026-07-31 15:48:30 +0200 |
| commit | 72b5d26cf9fbcbfb837cbfd24442c675a13b8dc3 (patch) | |
| tree | 246aff45a28c169937730051556cc2137e6a8067 | |
| parent | c33a7c7b3be534323d59383149f8e6470137e88d (diff) | |
| download | go-gg-72b5d26cf9fbcbfb837cbfd24442c675a13b8dc3.tar.gz | |
pgruntime: add t.Helper() and better error messages in ConnectForTest()
| -rw-r--r-- | pgruntime/connector.go | 9 |
1 files changed, 7 insertions, 2 deletions
diff --git a/pgruntime/connector.go b/pgruntime/connector.go index fa91f41..60e0de2 100644 --- a/pgruntime/connector.go +++ b/pgruntime/connector.go @@ -78,6 +78,7 @@ func OverrideDatabaseName(dbName string) TestSetupOption { // Some tests require setting up multiple separate connections to the same database. // The second return argument can be be used with [Connector.Connect] to obtain additional connections as needed. func (c Connector[T]) ConnectForTest(t assert.TestingTB, behavior ConnectionBehavior, opts ...TestSetupOption) (T, ConnectionTarget) { + t.Helper() ctx := t.Context() params := testSetupParams{ @@ -97,6 +98,7 @@ func (c Connector[T]) ConnectForTest(t assert.TestingTB, behavior ConnectionBeha } // connect to "postgres" database for the DROP/CREATE DATABASE queries + // TODO: DROP/CREATE DATABASE turns out to be very slow (in a real-world scenario: 100ms per test instead of ~10ms to wipe just the DB contents and reset sequences) target := ConnectionTarget{ HostName: "127.0.0.1", Port: testdbPort, @@ -129,8 +131,11 @@ func (c Connector[T]) prepareTestDatabase(ctx context.Context, target Connection } _, err = execQuery(ctx, db, "DROP DATABASE IF EXISTS "+quoteIdentifier(dbName), nil) if err != nil { - return errext.WithCleanup(err, "db.Close", db.GSQLClose(ctx)) + return errext.WithCleanup(fmt.Errorf("during DROP DATABASE: %w", err), "db.Close", db.GSQLClose(ctx)) } _, err = execQuery(ctx, db, "CREATE DATABASE "+quoteIdentifier(dbName), nil) - return errext.WithCleanup(err, "db.Close", db.GSQLClose(ctx)) + if err != nil { + return errext.WithCleanup(fmt.Errorf("during CREATE DATABASE: %w", err), "db.Close", db.GSQLClose(ctx)) + } + return errext.WithCleanup(nil, "db.Close", db.GSQLClose(ctx)) } |
