Skip to content

Commit

Permalink
Fixes to ensure that DB tests run reliably when run in parallel
Browse files Browse the repository at this point in the history
  • Loading branch information
williammoran committed May 7, 2024
1 parent 0493b65 commit 401f1a6
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
4 changes: 2 additions & 2 deletions lib/format/pgsql8/oneeighty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ import (
// * Data types are not normalized nor standardized nor anything like that

func TestOneEighty(t *testing.T) {
c := Initdb(t)
c := Initdb(t, "pg")
if c == nil {
t.SkipNow()
}
defer Teardowndb(t, c)
defer Teardowndb(t, c, "pg")
role := os.Getenv("DB_USER")
lib.GlobalDBSteward = lib.NewDBSteward(format.LookupMap{
ir.SqlFormatPgsql8: GlobalLookup,
Expand Down
19 changes: 10 additions & 9 deletions lib/format/pgsql8/testutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/jackc/pgx/v4"
)

func Initdb(t *testing.T) *pgx.Conn {
func Initdb(t *testing.T, dbSuffix string) *pgx.Conn {
if os.Getenv("DB_NAME") == "" {
return nil
}
Expand All @@ -21,19 +21,20 @@ func Initdb(t *testing.T) *pgx.Conn {
return nil
}
defer conn.Close(context.TODO())
_, err = conn.Exec(context.TODO(), fmt.Sprintf("DROP DATABASE IF EXISTS %s", os.Getenv("DB_NAME")))
_, err = conn.Exec(context.TODO(), fmt.Sprintf("DROP DATABASE IF EXISTS %s", os.Getenv("DB_NAME")+dbSuffix))
if err != nil {
t.Fatal(err)
return nil
}
_, err = conn.Exec(context.TODO(), fmt.Sprintf("CREATE DATABASE %s", os.Getenv("DB_NAME")))
_, err = conn.Exec(context.TODO(), fmt.Sprintf("CREATE DATABASE %s", os.Getenv("DB_NAME")+dbSuffix))
if err != nil {
t.Fatal(err)
return nil
}
_, err = conn.Exec(context.TODO(), fmt.Sprintf("CREATE ROLE %s", ir.AdditionalRole))
if err != nil {
if (err.(*pgconn.PgError)).Code != "42710" { // Role exists
code := (err.(*pgconn.PgError)).Code
if code != "42710" && code != "23505" { // Role exists
t.Fatal(err)
return nil
}
Expand All @@ -43,15 +44,15 @@ func Initdb(t *testing.T) *pgx.Conn {
t.Fatal(err)
return nil
}
conn, err = pgx.Connect(context.TODO(), userDSNFromEnv())
conn, err = pgx.Connect(context.TODO(), userDSNFromEnv(dbSuffix))
if err != nil {
t.Fatal(err)
return nil
}
return conn
}

func Teardowndb(t *testing.T, c *pgx.Conn) {
func Teardowndb(t *testing.T, c *pgx.Conn, dbSuffix string) {
err := c.Close(context.TODO())
if err != nil {
t.Fatal(err)
Expand All @@ -63,7 +64,7 @@ func Teardowndb(t *testing.T, c *pgx.Conn) {
return
}
defer conn.Close(context.TODO())
_, err = conn.Exec(context.TODO(), fmt.Sprintf("DROP DATABASE IF EXISTS %s", os.Getenv("DB_NAME")))
_, err = conn.Exec(context.TODO(), fmt.Sprintf("DROP DATABASE IF EXISTS %s", os.Getenv("DB_NAME")+dbSuffix))
if err != nil {
t.Log(err)
}
Expand All @@ -89,11 +90,11 @@ func adminDSNFromEnv() string {
)
}

func userDSNFromEnv() string {
func userDSNFromEnv(suffix string) string {
host := os.Getenv("DB_HOST")
user := os.Getenv("DB_USER")
password := os.Getenv("DB_PASSWORD")
dbName := os.Getenv("DB_NAME")
dbName := os.Getenv("DB_NAME") + suffix
port := os.Getenv("DB_PORT")
cs := fmt.Sprintf(
"host=%s user=%s password=%s dbname=%s port=%s",
Expand Down
5 changes: 2 additions & 3 deletions xmlpostgresintegration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,11 @@ var v2 string
// and ensure those commands work. It is limited: see the comments
// at the end of the test for explanation.
func TestXMLPostgresIngegration(t *testing.T) {
c := pgsql8.Initdb(t)
c := pgsql8.Initdb(t, "tl")
if c == nil {
t.SkipNow()
}
defer pgsql8.Teardowndb(t, c)
//role := os.Getenv("DB_USER")
defer pgsql8.Teardowndb(t, c, "tl")
def1, err := xml.ReadDef(strings.NewReader(v1))
if err != nil {
t.Fatal(err)
Expand Down

0 comments on commit 401f1a6

Please sign in to comment.