-
Notifications
You must be signed in to change notification settings - Fork 279
Share a single database across tests #2346
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,6 @@ | ||
| package testutils | ||
|
|
||
| import ( | ||
| "context" | ||
| "os/exec" | ||
| "path/filepath" | ||
| "strings" | ||
|
|
@@ -41,6 +40,11 @@ type Database struct { | |
| TestQueries *queries.Queries | ||
| } | ||
|
|
||
| var ( | ||
| oneDB *Database | ||
| dblock sync.Mutex | ||
| ) | ||
|
|
||
| // SetupDatabase creates a fresh PostgreSQL container with migrations applied | ||
| func SetupDatabase(t *testing.T) *Database { | ||
| t.Helper() | ||
|
|
@@ -49,6 +53,21 @@ func SetupDatabase(t *testing.T) *Database { | |
| t.Skip("Skipping integration test in short mode") | ||
| } | ||
|
|
||
| // cheap lookup | ||
| if oneDB != nil { | ||
| return oneDB | ||
| } | ||
|
|
||
| dblock.Lock() | ||
| defer dblock.Unlock() | ||
djeebus marked this conversation as resolved.
Show resolved
Hide resolved
djeebus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // locked lookup | ||
| if oneDB != nil { | ||
| return oneDB | ||
| } | ||
|
|
||
| // lookup failed, create new | ||
|
|
||
| // Start PostgreSQL container | ||
| container, err := postgres.Run( | ||
| t.Context(), | ||
|
|
@@ -63,12 +82,6 @@ func SetupDatabase(t *testing.T) *Database { | |
| ), | ||
| ) | ||
| require.NoError(t, err, "Failed to start postgres container") | ||
| t.Cleanup(func() { | ||
| ctx := t.Context() | ||
| ctx = context.WithoutCancel(ctx) | ||
| err := container.Terminate(ctx) | ||
| assert.NoError(t, err) | ||
| }) | ||
|
|
||
| connStr, err := container.ConnectionString(t.Context(), "sslmode=disable") | ||
| require.NoError(t, err, "Failed to get connection string") | ||
|
Comment on lines
+93
to
94
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The new singleton path starts a Useful? React with 👍 / 👎. |
||
|
|
@@ -77,34 +90,25 @@ func SetupDatabase(t *testing.T) *Database { | |
| runDatabaseMigrations(t, connStr) | ||
|
|
||
| // create test queries client | ||
| dbClient, connPool, err := pool.New(t.Context(), connStr, "tests") | ||
| dbClient, _, err := pool.New(t.Context(), connStr, "tests") | ||
djeebus marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
djeebus marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| require.NoError(t, err) | ||
| t.Cleanup(func() { | ||
| connPool.Close() | ||
| }) | ||
cursor[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| testQueries := queries.New(dbClient) | ||
|
|
||
| // Create app db client | ||
| sqlcClient, err := db.NewClient(t.Context(), connStr) | ||
| require.NoError(t, err, "Failed to create sqlc client") | ||
| t.Cleanup(func() { | ||
| err := sqlcClient.Close() | ||
| assert.NoError(t, err) | ||
| }) | ||
|
|
||
| // Create the auth db client | ||
| authDb, err := authdb.NewClient(t.Context(), connStr, connStr) | ||
| require.NoError(t, err, "Failed to create auth db client") | ||
| t.Cleanup(func() { | ||
| err := authDb.Close() | ||
| assert.NoError(t, err) | ||
| }) | ||
|
|
||
| return &Database{ | ||
| oneDB = &Database{ | ||
| SqlcClient: sqlcClient, | ||
| AuthDb: authDb, | ||
| TestQueries: testQueries, | ||
| } | ||
|
|
||
| return oneDB | ||
| } | ||
|
|
||
| // gooseMu serializes goose operations across parallel tests. | ||
|
|
@@ -125,18 +129,18 @@ func runDatabaseMigrations(t *testing.T, connStr string) { | |
| gooseMu.Lock() | ||
| defer gooseMu.Unlock() | ||
|
|
||
| db, err := goose.OpenDBWithDriver("pgx", connStr) | ||
| gooseDB, err := goose.OpenDBWithDriver("pgx", connStr) | ||
| require.NoError(t, err) | ||
| t.Cleanup(func() { | ||
| err := db.Close() | ||
| err := gooseDB.Close() | ||
| assert.NoError(t, err) | ||
| }) | ||
|
|
||
| // run the db migration | ||
| err = goose.RunWithOptionsContext( | ||
| t.Context(), | ||
| "up", | ||
| db, | ||
| gooseDB, | ||
| filepath.Join(repoRoot, "packages", "db", "migrations"), | ||
| nil, | ||
| ) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.