Skip to content

Commit

Permalink
Create a unique database for each test
Browse files Browse the repository at this point in the history
This should fix errors in parallel test runs
  • Loading branch information
tygern committed May 6, 2024
1 parent 583758d commit d713dd2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
2 changes: 2 additions & 0 deletions databases/create_databases.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ drop database if exists starter_integration;
drop database if exists starter_test;
drop database if exists starter_development;
drop user starter;
drop user super_test;

create database starter_development;
create database starter_test;
create database starter_integration;
create user starter with password 'starter';
create user super_test superuser ;
grant all privileges on database starter_development to starter;
grant all privileges on database starter_test to starter;
grant all privileges on database starter_integration to starter;
Expand Down
37 changes: 34 additions & 3 deletions pkg/testsupport/test_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,44 @@ package testsupport

import (
"database/sql"
"fmt"
"github.com/initialcapacity/ai-starter/pkg/dbsupport"
"github.com/stretchr/testify/assert"
"math/rand/v2"
"testing"
)

type TestDb struct {
DB *sql.DB
t *testing.T
DB *sql.DB
t *testing.T
testDbName string
}

func NewTestDb(t *testing.T) *TestDb {
return &TestDb{DB: dbsupport.CreateConnection("postgres://starter:starter@localhost:5432/starter_test?sslmode=disable"), t: t}
testDbName := fmt.Sprintf("starter_test_%d", rand.IntN(1_000_000))
withSuperDb(t, func(superDb *sql.DB) {

_, err := superDb.Exec(fmt.Sprintf("create database %s template starter_test", testDbName))
assert.NoError(t, err, "unable to create test database")
_, err = superDb.Exec(fmt.Sprintf("grant all privileges on database %s to starter", testDbName))
assert.NoError(t, err, "unable to grant permissions for test database")
})

return &TestDb{
DB: dbsupport.CreateConnection(fmt.Sprintf("postgres://starter:starter@localhost:5432/%s?sslmode=disable", testDbName)),
t: t,
testDbName: testDbName,
}
}

func (tdb *TestDb) Close() {
err := tdb.DB.Close()
assert.NoError(tdb.t, err)

withSuperDb(tdb.t, func(superDb *sql.DB) {
_, err = superDb.Exec(fmt.Sprintf("drop database %s", tdb.testDbName))
assert.NoError(tdb.t, err, "unable to drop test database")
})
}

func (tdb *TestDb) ClearTables() {
Expand All @@ -31,3 +52,13 @@ func (tdb *TestDb) Execute(statement string, arguments ...any) {
_, err := tdb.DB.Exec(statement, arguments...)
assert.NoError(tdb.t, err)
}

func withSuperDb(t *testing.T, action func(superDb *sql.DB)) {
superDb := dbsupport.CreateConnection("postgres://super_test@localhost:5432/postgres?sslmode=disable")
defer func(superDb *sql.DB) {
err := superDb.Close()
assert.NoError(t, err, "unable to close connection to postgres")
}(superDb)

action(superDb)
}

0 comments on commit d713dd2

Please sign in to comment.