2019-08-06 10:29:04 +02:00
|
|
|
package postgres
|
|
|
|
|
|
2019-08-13 10:16:26 +02:00
|
|
|
import (
|
2021-02-22 13:58:28 -05:00
|
|
|
"math"
|
2019-08-13 10:16:26 +02:00
|
|
|
"testing"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
2019-08-06 10:29:04 +02:00
|
|
|
|
2019-08-13 10:16:26 +02:00
|
|
|
func TestBool(t *testing.T) {
|
2021-12-26 17:15:43 +01:00
|
|
|
assertSerialize(t, Bool(false), `$1::boolean`, false)
|
2019-08-13 10:16:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestInt(t *testing.T) {
|
2019-12-01 18:26:01 +01:00
|
|
|
assertSerialize(t, Int(11), `$1`, int64(11))
|
2019-08-13 10:16:26 +02:00
|
|
|
}
|
|
|
|
|
|
2021-02-22 13:58:28 -05:00
|
|
|
func TestInt8(t *testing.T) {
|
|
|
|
|
val := int8(math.MinInt8)
|
2021-12-26 17:15:43 +01:00
|
|
|
assertSerialize(t, Int8(val), `$1::smallint`, val)
|
2021-02-22 13:58:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestInt16(t *testing.T) {
|
|
|
|
|
val := int16(math.MinInt16)
|
2021-12-26 17:15:43 +01:00
|
|
|
assertSerialize(t, Int16(val), `$1::smallint`, val)
|
2021-02-22 13:58:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestInt32(t *testing.T) {
|
|
|
|
|
val := int32(math.MinInt32)
|
2021-12-26 17:15:43 +01:00
|
|
|
assertSerialize(t, Int32(val), `$1::integer`, val)
|
2021-02-22 13:58:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestInt64(t *testing.T) {
|
|
|
|
|
val := int64(math.MinInt64)
|
2021-12-26 17:15:43 +01:00
|
|
|
assertSerialize(t, Int64(val), `$1::bigint`, val)
|
2021-02-22 13:58:28 -05:00
|
|
|
}
|
|
|
|
|
|
2024-11-03 11:58:39 +01:00
|
|
|
func TestUint8(t *testing.T) {
|
|
|
|
|
val := uint8(math.MaxUint8)
|
|
|
|
|
assertSerialize(t, Uint8(val), `$1::smallint`, val)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestUint16(t *testing.T) {
|
|
|
|
|
val := uint16(math.MaxUint16)
|
|
|
|
|
assertSerialize(t, Uint16(val), `$1::integer`, val)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestUint32(t *testing.T) {
|
|
|
|
|
val := uint32(math.MaxUint32)
|
|
|
|
|
assertSerialize(t, Uint32(val), `$1::bigint`, val)
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-04 19:57:42 +01:00
|
|
|
func TestUint64(t *testing.T) {
|
|
|
|
|
val := uint32(math.MaxUint32)
|
|
|
|
|
assertSerialize(t, Uint32(val), `$1::bigint`, val)
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-13 10:16:26 +02:00
|
|
|
func TestFloat(t *testing.T) {
|
2019-12-01 18:26:01 +01:00
|
|
|
assertSerialize(t, Float(12.34), `$1`, float64(12.34))
|
2024-11-01 12:34:46 +01:00
|
|
|
|
|
|
|
|
assertSerialize(t, Real(12.34), `$1::real`, float32(12.34))
|
|
|
|
|
assertSerialize(t, Double(12.34), `$1::double precision`, float64(12.34))
|
2019-08-13 10:16:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestString(t *testing.T) {
|
2022-05-05 13:01:42 +02:00
|
|
|
assertSerialize(t, String("Some text"), `$1::text`, "Some text")
|
2024-11-01 12:34:46 +01:00
|
|
|
|
|
|
|
|
assertSerialize(t, Text("Some text"), `$1::text`, "Some text")
|
|
|
|
|
assertSerialize(t, Char(20)("John Doe"), `$1::char(20)`, "John Doe")
|
|
|
|
|
assertSerialize(t, Char()("John Doe"), `$1::char`, "John Doe")
|
|
|
|
|
assertSerialize(t, VarChar(20)("John Doe"), `$1::varchar(20)`, "John Doe")
|
|
|
|
|
assertSerialize(t, VarChar()("John Doe"), `$1::varchar`, "John Doe")
|
2019-08-13 10:16:26 +02:00
|
|
|
}
|
|
|
|
|
|
2021-05-12 12:29:22 +02:00
|
|
|
func TestBytea(t *testing.T) {
|
|
|
|
|
assertSerialize(t, Bytea("Some text"), `$1::bytea`, "Some text")
|
|
|
|
|
assertSerialize(t, Bytea([]byte("Some byte array")), `$1::bytea`, []byte("Some byte array"))
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-04 14:22:33 +02:00
|
|
|
func TestJson(t *testing.T) {
|
|
|
|
|
assertSerialize(t, Json("{\"key\": \"value\"}"), `$1::json`, "{\"key\": \"value\"}")
|
|
|
|
|
assertSerialize(t, Json([]byte("{\"key\": \"value\"}")), `$1::json`, []byte("{\"key\": \"value\"}"))
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-13 10:16:26 +02:00
|
|
|
func TestDate(t *testing.T) {
|
2019-12-01 18:26:01 +01:00
|
|
|
assertSerialize(t, Date(2014, time.January, 2), `$1::date`, "2014-01-02")
|
|
|
|
|
assertSerialize(t, DateT(time.Now()), `$1::date`)
|
2019-08-13 10:16:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestTime(t *testing.T) {
|
2019-12-01 18:26:01 +01:00
|
|
|
assertSerialize(t, Time(10, 15, 30), `$1::time without time zone`, "10:15:30")
|
|
|
|
|
assertSerialize(t, TimeT(time.Now()), `$1::time without time zone`)
|
2019-08-13 10:16:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestTimez(t *testing.T) {
|
2019-12-01 18:26:01 +01:00
|
|
|
assertSerialize(t, Timez(10, 15, 30, 0, "UTC"),
|
2019-08-13 10:16:26 +02:00
|
|
|
`$1::time with time zone`, "10:15:30 UTC")
|
2019-12-01 18:26:01 +01:00
|
|
|
assertSerialize(t, TimezT(time.Now()), `$1::time with time zone`)
|
2019-08-13 10:16:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestTimestamp(t *testing.T) {
|
2019-12-01 18:26:01 +01:00
|
|
|
assertSerialize(t, Timestamp(2010, time.March, 30, 10, 15, 30),
|
2019-08-13 10:16:26 +02:00
|
|
|
`$1::timestamp without time zone`, "2010-03-30 10:15:30")
|
2019-12-01 18:26:01 +01:00
|
|
|
assertSerialize(t, TimestampT(time.Now()), `$1::timestamp without time zone`)
|
2019-08-13 10:16:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestTimestampz(t *testing.T) {
|
2019-12-01 18:26:01 +01:00
|
|
|
assertSerialize(t, Timestampz(2010, time.March, 30, 10, 15, 30, 0, "UTC"),
|
2019-08-13 10:16:26 +02:00
|
|
|
`$1::timestamp with time zone`, "2010-03-30 10:15:30 UTC")
|
2019-12-01 18:26:01 +01:00
|
|
|
assertSerialize(t, TimestampzT(time.Now()), `$1::timestamp with time zone`)
|
2019-08-06 10:29:04 +02:00
|
|
|
}
|