diff --git a/pgtype/numeric_test.go b/pgtype/numeric_test.go index d5c60575..281efa0f 100644 --- a/pgtype/numeric_test.go +++ b/pgtype/numeric_test.go @@ -98,16 +98,10 @@ func TestNumericCodec(t *testing.T) { {pgtype.Numeric{Int: mustParseBigInt(t, "13423409823409243892349028349023482934092340892390101"), Exp: -92, Valid: true}, new(pgtype.Numeric), isExpectedEqNumeric(pgtype.Numeric{Int: mustParseBigInt(t, "13423409823409243892349028349023482934092340892390101"), Exp: -92, Valid: true})}, {pgtype.Numeric{Int: mustParseBigInt(t, "13423409823409243892349028349023482934092340892390101"), Exp: -93, Valid: true}, new(pgtype.Numeric), isExpectedEqNumeric(pgtype.Numeric{Int: mustParseBigInt(t, "13423409823409243892349028349023482934092340892390101"), Exp: -93, Valid: true})}, {pgtype.Numeric{NaN: true, Valid: true}, new(pgtype.Numeric), isExpectedEqNumeric(pgtype.Numeric{NaN: true, Valid: true})}, - {pgtype.Numeric{InfinityModifier: pgtype.Infinity, Valid: true}, new(pgtype.Numeric), isExpectedEqNumeric(pgtype.Numeric{InfinityModifier: pgtype.Infinity, Valid: true})}, - {pgtype.Numeric{InfinityModifier: pgtype.NegativeInfinity, Valid: true}, new(pgtype.Numeric), isExpectedEqNumeric(pgtype.Numeric{InfinityModifier: pgtype.NegativeInfinity, Valid: true})}, {longestNumeric, new(pgtype.Numeric), isExpectedEqNumeric(longestNumeric)}, {mustParseNumeric(t, "1"), new(int64), isExpectedEq(int64(1))}, {math.NaN(), new(float64), func(a interface{}) bool { return math.IsNaN(a.(float64)) }}, {float32(math.NaN()), new(float32), func(a interface{}) bool { return math.IsNaN(float64(a.(float32))) }}, - {math.Inf(1), new(float64), isExpectedEq(math.Inf(1))}, - {float32(math.Inf(1)), new(float32), isExpectedEq(float32(math.Inf(1)))}, - {math.Inf(-1), new(float64), isExpectedEq(math.Inf(-1))}, - {float32(math.Inf(-1)), new(float32), isExpectedEq(float32(math.Inf(-1)))}, {int64(-1), new(pgtype.Numeric), isExpectedEqNumeric(mustParseNumeric(t, "-1"))}, {int64(0), new(pgtype.Numeric), isExpectedEqNumeric(mustParseNumeric(t, "0"))}, {int64(1), new(pgtype.Numeric), isExpectedEqNumeric(mustParseNumeric(t, "1"))}, @@ -120,6 +114,20 @@ func TestNumericCodec(t *testing.T) { }) } +func TestNumericCodecInfinity(t *testing.T) { + skipCockroachDB(t, "server formats numeric text format differently") + skipPostgreSQLVersionLessThan(t, 14) + + testutil.RunTranscodeTests(t, "numeric", []testutil.TranscodeTestCase{ + {math.Inf(1), new(float64), isExpectedEq(math.Inf(1))}, + {float32(math.Inf(1)), new(float32), isExpectedEq(float32(math.Inf(1)))}, + {math.Inf(-1), new(float64), isExpectedEq(math.Inf(-1))}, + {float32(math.Inf(-1)), new(float32), isExpectedEq(float32(math.Inf(-1)))}, + {pgtype.Numeric{InfinityModifier: pgtype.Infinity, Valid: true}, new(pgtype.Numeric), isExpectedEqNumeric(pgtype.Numeric{InfinityModifier: pgtype.Infinity, Valid: true})}, + {pgtype.Numeric{InfinityModifier: pgtype.NegativeInfinity, Valid: true}, new(pgtype.Numeric), isExpectedEqNumeric(pgtype.Numeric{InfinityModifier: pgtype.NegativeInfinity, Valid: true})}, + }) +} + func TestNumericFloat64Valuer(t *testing.T) { for i, tt := range []struct { n pgtype.Numeric diff --git a/pgtype/pgtype_test.go b/pgtype/pgtype_test.go index 2ee32907..dd0150fb 100644 --- a/pgtype/pgtype_test.go +++ b/pgtype/pgtype_test.go @@ -4,6 +4,8 @@ import ( "database/sql" "errors" "net" + "regexp" + "strconv" "testing" "github.com/jackc/pgx/v5" @@ -76,6 +78,25 @@ func skipCockroachDB(t testing.TB, msg string) { } } +func skipPostgreSQLVersionLessThan(t testing.TB, minVersion int64) { + conn := testutil.MustConnectPgx(t) + defer testutil.MustCloseContext(t, conn) + + 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) + require.NoError(t, err) + + if serverVersion < minVersion { + t.Skipf("Test requires PostgreSQL v%d+", minVersion) + } +} + func TestTypeMapScanNilIsNoOp(t *testing.T) { m := pgtype.NewMap()