From 401f1a611e9629f9928ed7b7af5585bf8cb28ae2 Mon Sep 17 00:00:00 2001 From: Bill Moran Date: Mon, 6 May 2024 21:47:27 -0400 Subject: [PATCH] Fixes to ensure that DB tests run reliably when run in parallel --- lib/format/pgsql8/oneeighty_test.go | 4 ++-- lib/format/pgsql8/testutils.go | 19 ++++++++++--------- xmlpostgresintegration_test.go | 5 ++--- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/lib/format/pgsql8/oneeighty_test.go b/lib/format/pgsql8/oneeighty_test.go index dcc0773..cc92510 100644 --- a/lib/format/pgsql8/oneeighty_test.go +++ b/lib/format/pgsql8/oneeighty_test.go @@ -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, diff --git a/lib/format/pgsql8/testutils.go b/lib/format/pgsql8/testutils.go index 83930c3..85bd493 100644 --- a/lib/format/pgsql8/testutils.go +++ b/lib/format/pgsql8/testutils.go @@ -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 } @@ -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 } @@ -43,7 +44,7 @@ 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 @@ -51,7 +52,7 @@ func Initdb(t *testing.T) *pgx.Conn { 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) @@ -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) } @@ -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", diff --git a/xmlpostgresintegration_test.go b/xmlpostgresintegration_test.go index 332a400..5469b95 100644 --- a/xmlpostgresintegration_test.go +++ b/xmlpostgresintegration_test.go @@ -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)