From 88b373f9eed6b0cecb8b12d95999578bdfdbc533 Mon Sep 17 00:00:00 2001 From: Vitalii Solodilov Date: Tue, 29 Nov 2022 12:34:26 +0700 Subject: [PATCH] Skipped multirange tests for postgres less than 14 version --- conn_test.go | 46 ++++++++++++++++++++++++++++++++-------------- pgxtest/pgxtest.go | 20 ++++++++++++++++++++ 2 files changed, 52 insertions(+), 14 deletions(-) diff --git a/conn_test.go b/conn_test.go index 66e5573d..26dc8192 100644 --- a/conn_test.go +++ b/conn_test.go @@ -911,6 +911,38 @@ func TestLoadRangeType(t *testing.T) { require.NoError(t, err) defer tx.Rollback(ctx) + _, err = tx.Exec(ctx, "create type examplefloatrange as range (subtype=float8, subtype_diff=float8mi)") + require.NoError(t, err) + + // Register types + newRangeType, err := conn.LoadType(ctx, "examplefloatrange") + require.NoError(t, err) + conn.TypeMap().RegisterType(newRangeType) + conn.TypeMap().RegisterDefaultPgType(pgtype.Range[float64]{}, "examplefloatrange") + + var inputRangeType = pgtype.Range[float64]{ + Lower: 1.0, + Upper: 2.0, + LowerType: pgtype.Inclusive, + UpperType: pgtype.Inclusive, + Valid: true, + } + var outputRangeType pgtype.Range[float64] + err = tx.QueryRow(ctx, "SELECT $1::examplefloatrange", inputRangeType).Scan(&outputRangeType) + require.NoError(t, err) + require.Equal(t, inputRangeType, outputRangeType) + }) +} + +func TestLoadMultiRangeType(t *testing.T) { + pgxtest.RunWithQueryExecModes(context.Background(), t, defaultConnTestRunner, nil, func(ctx context.Context, t testing.TB, conn *pgx.Conn) { + pgxtest.SkipCockroachDB(t, conn, "Server does support range types") + pgxtest.SkipPostgreSQLVersionLessThan(t, conn, 14) // multirange data type was added in 14 postgresql + + tx, err := conn.Begin(ctx) + require.NoError(t, err) + defer tx.Rollback(ctx) + _, err = tx.Exec(ctx, "create type examplefloatrange as range (subtype=float8, subtype_diff=float8mi, multirange_type_name=examplefloatmultirange)") require.NoError(t, err) @@ -925,20 +957,6 @@ func TestLoadRangeType(t *testing.T) { conn.TypeMap().RegisterType(newMultiRangeType) conn.TypeMap().RegisterDefaultPgType(pgtype.Multirange[pgtype.Range[float64]]{}, "examplefloatmultirange") - // Test range type - var inputRangeType = pgtype.Range[float64]{ - Lower: 1.0, - Upper: 2.0, - LowerType: pgtype.Inclusive, - UpperType: pgtype.Inclusive, - Valid: true, - } - var outputRangeType pgtype.Range[float64] - err = tx.QueryRow(ctx, "SELECT $1::examplefloatrange", inputRangeType).Scan(&outputRangeType) - require.NoError(t, err) - require.Equal(t, inputRangeType, outputRangeType) - - // Test multi range type var inputMultiRangeType = pgtype.Multirange[pgtype.Range[float64]]{ { Lower: 1.0, diff --git a/pgxtest/pgxtest.go b/pgxtest/pgxtest.go index 796f850d..ece6d91b 100644 --- a/pgxtest/pgxtest.go +++ b/pgxtest/pgxtest.go @@ -5,6 +5,8 @@ import ( "context" "fmt" "reflect" + "regexp" + "strconv" "testing" "github.com/jackc/pgx/v5" @@ -151,3 +153,21 @@ func SkipCockroachDB(t testing.TB, conn *pgx.Conn, msg string) { t.Skip(msg) } } + +func SkipPostgreSQLVersionLessThan(t testing.TB, conn *pgx.Conn, minVersion int64) { + serverVersionStr := conn.PgConn().ParameterStatus("server_version") + serverVersionStr = regexp.MustCompile(`^[0-9]+`).FindString(serverVersionStr) + // if not PostgreSQL do nothing + if serverVersionStr == "" { + return + } + + serverVersion, err := strconv.ParseInt(serverVersionStr, 10, 64) + if err != nil { + t.Fatalf("postgres version parsed failed: %s", err) + } + + if serverVersion < minVersion { + t.Skipf("Test requires PostgreSQL v%d+", minVersion) + } +}