Finish extraction of pgtype test helpers

batch-wip
Jack Christensen 2017-04-14 12:38:33 -05:00
parent e4451b47b2
commit f418255c24
60 changed files with 202 additions and 435 deletions

View File

@ -5,10 +5,11 @@ import (
"testing" "testing"
"github.com/jackc/pgx/pgtype" "github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
) )
func TestAclitemArrayTranscode(t *testing.T) { func TestAclitemArrayTranscode(t *testing.T) {
testSuccessfulTranscode(t, "aclitem[]", []interface{}{ testutil.TestSuccessfulTranscode(t, "aclitem[]", []interface{}{
&pgtype.AclitemArray{ &pgtype.AclitemArray{
Elements: nil, Elements: nil,
Dimensions: nil, Dimensions: nil,

View File

@ -5,10 +5,11 @@ import (
"testing" "testing"
"github.com/jackc/pgx/pgtype" "github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
) )
func TestAclitemTranscode(t *testing.T) { func TestAclitemTranscode(t *testing.T) {
testSuccessfulTranscode(t, "aclitem", []interface{}{ testutil.TestSuccessfulTranscode(t, "aclitem", []interface{}{
pgtype.Aclitem{String: "postgres=arwdDxt/postgres", Status: pgtype.Present}, pgtype.Aclitem{String: "postgres=arwdDxt/postgres", Status: pgtype.Present},
pgtype.Aclitem{String: `postgres=arwdDxt/" tricky, ' } "" \ test user "`, Status: pgtype.Present}, pgtype.Aclitem{String: `postgres=arwdDxt/" tricky, ' } "" \ test user "`, Status: pgtype.Present},
pgtype.Aclitem{Status: pgtype.Null}, pgtype.Aclitem{Status: pgtype.Null},

View File

@ -5,10 +5,11 @@ import (
"testing" "testing"
"github.com/jackc/pgx/pgtype" "github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
) )
func TestBoolArrayTranscode(t *testing.T) { func TestBoolArrayTranscode(t *testing.T) {
testSuccessfulTranscode(t, "bool[]", []interface{}{ testutil.TestSuccessfulTranscode(t, "bool[]", []interface{}{
&pgtype.BoolArray{ &pgtype.BoolArray{
Elements: nil, Elements: nil,
Dimensions: nil, Dimensions: nil,

View File

@ -5,10 +5,11 @@ import (
"testing" "testing"
"github.com/jackc/pgx/pgtype" "github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
) )
func TestBoolTranscode(t *testing.T) { func TestBoolTranscode(t *testing.T) {
testSuccessfulTranscode(t, "bool", []interface{}{ testutil.TestSuccessfulTranscode(t, "bool", []interface{}{
pgtype.Bool{Bool: false, Status: pgtype.Present}, pgtype.Bool{Bool: false, Status: pgtype.Present},
pgtype.Bool{Bool: true, Status: pgtype.Present}, pgtype.Bool{Bool: true, Status: pgtype.Present},
pgtype.Bool{Bool: false, Status: pgtype.Null}, pgtype.Bool{Bool: false, Status: pgtype.Null},

View File

@ -4,10 +4,11 @@ import (
"testing" "testing"
"github.com/jackc/pgx/pgtype" "github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
) )
func TestBoxTranscode(t *testing.T) { func TestBoxTranscode(t *testing.T) {
testSuccessfulTranscode(t, "box", []interface{}{ testutil.TestSuccessfulTranscode(t, "box", []interface{}{
&pgtype.Box{ &pgtype.Box{
P: [2]pgtype.Vec2{{7.1, 5.234}, {3.14, 1.678}}, P: [2]pgtype.Vec2{{7.1, 5.234}, {3.14, 1.678}},
Status: pgtype.Present, Status: pgtype.Present,
@ -21,10 +22,10 @@ func TestBoxTranscode(t *testing.T) {
} }
func TestBoxNormalize(t *testing.T) { func TestBoxNormalize(t *testing.T) {
testSuccessfulNormalize(t, []normalizeTest{ testutil.TestSuccessfulNormalize(t, []testutil.NormalizeTest{
{ {
sql: "select '3.14, 1.678, 7.1, 5.234'::box", SQL: "select '3.14, 1.678, 7.1, 5.234'::box",
value: &pgtype.Box{ Value: &pgtype.Box{
P: [2]pgtype.Vec2{{7.1, 5.234}, {3.14, 1.678}}, P: [2]pgtype.Vec2{{7.1, 5.234}, {3.14, 1.678}},
Status: pgtype.Present, Status: pgtype.Present,
}, },

View File

@ -5,10 +5,11 @@ import (
"testing" "testing"
"github.com/jackc/pgx/pgtype" "github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
) )
func TestByteaArrayTranscode(t *testing.T) { func TestByteaArrayTranscode(t *testing.T) {
testSuccessfulTranscode(t, "bytea[]", []interface{}{ testutil.TestSuccessfulTranscode(t, "bytea[]", []interface{}{
&pgtype.ByteaArray{ &pgtype.ByteaArray{
Elements: nil, Elements: nil,
Dimensions: nil, Dimensions: nil,

View File

@ -5,10 +5,11 @@ import (
"testing" "testing"
"github.com/jackc/pgx/pgtype" "github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
) )
func TestByteaTranscode(t *testing.T) { func TestByteaTranscode(t *testing.T) {
testSuccessfulTranscode(t, "bytea", []interface{}{ testutil.TestSuccessfulTranscode(t, "bytea", []interface{}{
pgtype.Bytea{Bytes: []byte{1, 2, 3}, Status: pgtype.Present}, pgtype.Bytea{Bytes: []byte{1, 2, 3}, Status: pgtype.Present},
pgtype.Bytea{Bytes: []byte{}, Status: pgtype.Present}, pgtype.Bytea{Bytes: []byte{}, Status: pgtype.Present},
pgtype.Bytea{Bytes: nil, Status: pgtype.Null}, pgtype.Bytea{Bytes: nil, Status: pgtype.Null},

View File

@ -5,6 +5,7 @@ import (
"testing" "testing"
"github.com/jackc/pgx/pgtype" "github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
) )
func TestCidTranscode(t *testing.T) { func TestCidTranscode(t *testing.T) {
@ -17,13 +18,13 @@ func TestCidTranscode(t *testing.T) {
return reflect.DeepEqual(a, b) return reflect.DeepEqual(a, b)
} }
testPgxSuccessfulTranscodeEqFunc(t, pgTypeName, values, eqFunc) testutil.TestPgxSuccessfulTranscodeEqFunc(t, pgTypeName, values, eqFunc)
// No direct conversion from int to cid, convert through text // No direct conversion from int to cid, convert through text
testPgxSimpleProtocolSuccessfulTranscodeEqFunc(t, "text::"+pgTypeName, values, eqFunc) testutil.TestPgxSimpleProtocolSuccessfulTranscodeEqFunc(t, "text::"+pgTypeName, values, eqFunc)
for _, driverName := range []string{"github.com/lib/pq", "github.com/jackc/pgx/stdlib"} { for _, driverName := range []string{"github.com/lib/pq", "github.com/jackc/pgx/stdlib"} {
testDatabaseSQLSuccessfulTranscodeEqFunc(t, driverName, pgTypeName, values, eqFunc) testutil.TestDatabaseSQLSuccessfulTranscodeEqFunc(t, driverName, pgTypeName, values, eqFunc)
} }
} }

View File

@ -6,10 +6,11 @@ import (
"testing" "testing"
"github.com/jackc/pgx/pgtype" "github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
) )
func TestCidrArrayTranscode(t *testing.T) { func TestCidrArrayTranscode(t *testing.T) {
testSuccessfulTranscode(t, "cidr[]", []interface{}{ testutil.TestSuccessfulTranscode(t, "cidr[]", []interface{}{
&pgtype.CidrArray{ &pgtype.CidrArray{
Elements: nil, Elements: nil,
Dimensions: nil, Dimensions: nil,

View File

@ -4,10 +4,11 @@ import (
"testing" "testing"
"github.com/jackc/pgx/pgtype" "github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
) )
func TestCircleTranscode(t *testing.T) { func TestCircleTranscode(t *testing.T) {
testSuccessfulTranscode(t, "circle", []interface{}{ testutil.TestSuccessfulTranscode(t, "circle", []interface{}{
&pgtype.Circle{P: pgtype.Vec2{1.234, 5.6789}, R: 3.5, Status: pgtype.Present}, &pgtype.Circle{P: pgtype.Vec2{1.234, 5.6789}, R: 3.5, Status: pgtype.Present},
&pgtype.Circle{P: pgtype.Vec2{-1.234, -5.6789}, R: 12.9, Status: pgtype.Present}, &pgtype.Circle{P: pgtype.Vec2{-1.234, -5.6789}, R: 12.9, Status: pgtype.Present},
&pgtype.Circle{Status: pgtype.Null}, &pgtype.Circle{Status: pgtype.Null},

View File

@ -6,10 +6,11 @@ import (
"time" "time"
"github.com/jackc/pgx/pgtype" "github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
) )
func TestDateArrayTranscode(t *testing.T) { func TestDateArrayTranscode(t *testing.T) {
testSuccessfulTranscode(t, "date[]", []interface{}{ testutil.TestSuccessfulTranscode(t, "date[]", []interface{}{
&pgtype.DateArray{ &pgtype.DateArray{
Elements: nil, Elements: nil,
Dimensions: nil, Dimensions: nil,

View File

@ -6,10 +6,11 @@ import (
"time" "time"
"github.com/jackc/pgx/pgtype" "github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
) )
func TestDateTranscode(t *testing.T) { func TestDateTranscode(t *testing.T) {
testSuccessfulTranscodeEqFunc(t, "date", []interface{}{ testutil.TestSuccessfulTranscodeEqFunc(t, "date", []interface{}{
pgtype.Date{Time: time.Date(1900, 1, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, pgtype.Date{Time: time.Date(1900, 1, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present},
pgtype.Date{Time: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, pgtype.Date{Time: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present},
pgtype.Date{Time: time.Date(1999, 12, 31, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, pgtype.Date{Time: time.Date(1999, 12, 31, 0, 0, 0, 0, time.UTC), Status: pgtype.Present},

View File

@ -5,10 +5,11 @@ import (
"time" "time"
"github.com/jackc/pgx/pgtype" "github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
) )
func TestDaterangeTranscode(t *testing.T) { func TestDaterangeTranscode(t *testing.T) {
testSuccessfulTranscodeEqFunc(t, "daterange", []interface{}{ testutil.TestSuccessfulTranscodeEqFunc(t, "daterange", []interface{}{
pgtype.Daterange{LowerType: pgtype.Empty, UpperType: pgtype.Empty, Status: pgtype.Present}, pgtype.Daterange{LowerType: pgtype.Empty, UpperType: pgtype.Empty, Status: pgtype.Present},
pgtype.Daterange{ pgtype.Daterange{
Lower: pgtype.Date{Time: time.Date(1990, 12, 31, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, Lower: pgtype.Date{Time: time.Date(1990, 12, 31, 0, 0, 0, 0, time.UTC), Status: pgtype.Present},
@ -40,10 +41,10 @@ func TestDaterangeTranscode(t *testing.T) {
} }
func TestDaterangeNormalize(t *testing.T) { func TestDaterangeNormalize(t *testing.T) {
testSuccessfulNormalizeEqFunc(t, []normalizeTest{ testutil.TestSuccessfulNormalizeEqFunc(t, []testutil.NormalizeTest{
{ {
sql: "select daterange('2010-01-01', '2010-01-11', '(]')", SQL: "select daterange('2010-01-01', '2010-01-11', '(]')",
value: pgtype.Daterange{ Value: pgtype.Daterange{
Lower: pgtype.Date{Time: time.Date(2010, 1, 2, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, Lower: pgtype.Date{Time: time.Date(2010, 1, 2, 0, 0, 0, 0, time.UTC), Status: pgtype.Present},
Upper: pgtype.Date{Time: time.Date(2010, 1, 12, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, Upper: pgtype.Date{Time: time.Date(2010, 1, 12, 0, 0, 0, 0, time.UTC), Status: pgtype.Present},
LowerType: pgtype.Inclusive, LowerType: pgtype.Inclusive,

View File

@ -5,10 +5,11 @@ import (
"testing" "testing"
"github.com/jackc/pgx/pgtype" "github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
) )
func TestFloat4ArrayTranscode(t *testing.T) { func TestFloat4ArrayTranscode(t *testing.T) {
testSuccessfulTranscode(t, "float4[]", []interface{}{ testutil.TestSuccessfulTranscode(t, "float4[]", []interface{}{
&pgtype.Float4Array{ &pgtype.Float4Array{
Elements: nil, Elements: nil,
Dimensions: nil, Dimensions: nil,

View File

@ -5,10 +5,11 @@ import (
"testing" "testing"
"github.com/jackc/pgx/pgtype" "github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
) )
func TestFloat4Transcode(t *testing.T) { func TestFloat4Transcode(t *testing.T) {
testSuccessfulTranscode(t, "float4", []interface{}{ testutil.TestSuccessfulTranscode(t, "float4", []interface{}{
pgtype.Float4{Float: -1, Status: pgtype.Present}, pgtype.Float4{Float: -1, Status: pgtype.Present},
pgtype.Float4{Float: 0, Status: pgtype.Present}, pgtype.Float4{Float: 0, Status: pgtype.Present},
pgtype.Float4{Float: 0.00001, Status: pgtype.Present}, pgtype.Float4{Float: 0.00001, Status: pgtype.Present},

View File

@ -5,10 +5,11 @@ import (
"testing" "testing"
"github.com/jackc/pgx/pgtype" "github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
) )
func TestFloat8ArrayTranscode(t *testing.T) { func TestFloat8ArrayTranscode(t *testing.T) {
testSuccessfulTranscode(t, "float8[]", []interface{}{ testutil.TestSuccessfulTranscode(t, "float8[]", []interface{}{
&pgtype.Float8Array{ &pgtype.Float8Array{
Elements: nil, Elements: nil,
Dimensions: nil, Dimensions: nil,

View File

@ -5,10 +5,11 @@ import (
"testing" "testing"
"github.com/jackc/pgx/pgtype" "github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
) )
func TestFloat8Transcode(t *testing.T) { func TestFloat8Transcode(t *testing.T) {
testSuccessfulTranscode(t, "float8", []interface{}{ testutil.TestSuccessfulTranscode(t, "float8", []interface{}{
pgtype.Float8{Float: -1, Status: pgtype.Present}, pgtype.Float8{Float: -1, Status: pgtype.Present},
pgtype.Float8{Float: 0, Status: pgtype.Present}, pgtype.Float8{Float: 0, Status: pgtype.Present},
pgtype.Float8{Float: 0.00001, Status: pgtype.Present}, pgtype.Float8{Float: 0.00001, Status: pgtype.Present},

View File

@ -6,11 +6,12 @@ import (
"github.com/jackc/pgx" "github.com/jackc/pgx"
"github.com/jackc/pgx/pgtype" "github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
) )
func TestHstoreArrayTranscode(t *testing.T) { func TestHstoreArrayTranscode(t *testing.T) {
conn := mustConnectPgx(t) conn := testutil.MustConnectPgx(t)
defer mustClose(t, conn) defer testutil.MustClose(t, conn)
text := func(s string) pgtype.Text { text := func(s string) pgtype.Text {
return pgtype.Text{String: s, Status: pgtype.Present} return pgtype.Text{String: s, Status: pgtype.Present}
@ -69,7 +70,7 @@ func TestHstoreArrayTranscode(t *testing.T) {
for _, fc := range formats { for _, fc := range formats {
ps.FieldDescriptions[0].FormatCode = fc.formatCode ps.FieldDescriptions[0].FormatCode = fc.formatCode
vEncoder := forceEncoder(src, fc.formatCode) vEncoder := testutil.ForceEncoder(src, fc.formatCode)
if vEncoder == nil { if vEncoder == nil {
t.Logf("%#v does not implement %v", src, fc.name) t.Logf("%#v does not implement %v", src, fc.name)
continue continue

View File

@ -5,6 +5,7 @@ import (
"testing" "testing"
"github.com/jackc/pgx/pgtype" "github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
) )
func TestHstoreTranscode(t *testing.T) { func TestHstoreTranscode(t *testing.T) {
@ -44,7 +45,7 @@ func TestHstoreTranscode(t *testing.T) {
values = append(values, pgtype.Hstore{Map: map[string]pgtype.Text{"foo": text(s)}, Status: pgtype.Present}) // is key values = append(values, pgtype.Hstore{Map: map[string]pgtype.Text{"foo": text(s)}, Status: pgtype.Present}) // is key
} }
testSuccessfulTranscodeEqFunc(t, "hstore", values, func(ai, bi interface{}) bool { testutil.TestSuccessfulTranscodeEqFunc(t, "hstore", values, func(ai, bi interface{}) bool {
a := ai.(pgtype.Hstore) a := ai.(pgtype.Hstore)
b := bi.(pgtype.Hstore) b := bi.(pgtype.Hstore)

View File

@ -6,10 +6,11 @@ import (
"testing" "testing"
"github.com/jackc/pgx/pgtype" "github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
) )
func TestInetArrayTranscode(t *testing.T) { func TestInetArrayTranscode(t *testing.T) {
testSuccessfulTranscode(t, "inet[]", []interface{}{ testutil.TestSuccessfulTranscode(t, "inet[]", []interface{}{
&pgtype.InetArray{ &pgtype.InetArray{
Elements: nil, Elements: nil,
Dimensions: nil, Dimensions: nil,

View File

@ -6,11 +6,12 @@ import (
"testing" "testing"
"github.com/jackc/pgx/pgtype" "github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
) )
func TestInetTranscode(t *testing.T) { func TestInetTranscode(t *testing.T) {
for _, pgTypeName := range []string{"inet", "cidr"} { for _, pgTypeName := range []string{"inet", "cidr"} {
testSuccessfulTranscode(t, pgTypeName, []interface{}{ testutil.TestSuccessfulTranscode(t, pgTypeName, []interface{}{
pgtype.Inet{IPNet: mustParseCidr(t, "0.0.0.0/32"), Status: pgtype.Present}, pgtype.Inet{IPNet: mustParseCidr(t, "0.0.0.0/32"), Status: pgtype.Present},
pgtype.Inet{IPNet: mustParseCidr(t, "127.0.0.1/32"), Status: pgtype.Present}, pgtype.Inet{IPNet: mustParseCidr(t, "127.0.0.1/32"), Status: pgtype.Present},
pgtype.Inet{IPNet: mustParseCidr(t, "12.34.56.0/32"), Status: pgtype.Present}, pgtype.Inet{IPNet: mustParseCidr(t, "12.34.56.0/32"), Status: pgtype.Present},

View File

@ -5,10 +5,11 @@ import (
"testing" "testing"
"github.com/jackc/pgx/pgtype" "github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
) )
func TestInt2ArrayTranscode(t *testing.T) { func TestInt2ArrayTranscode(t *testing.T) {
testSuccessfulTranscode(t, "int2[]", []interface{}{ testutil.TestSuccessfulTranscode(t, "int2[]", []interface{}{
&pgtype.Int2Array{ &pgtype.Int2Array{
Elements: nil, Elements: nil,
Dimensions: nil, Dimensions: nil,

View File

@ -6,10 +6,11 @@ import (
"testing" "testing"
"github.com/jackc/pgx/pgtype" "github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
) )
func TestInt2Transcode(t *testing.T) { func TestInt2Transcode(t *testing.T) {
testSuccessfulTranscode(t, "int2", []interface{}{ testutil.TestSuccessfulTranscode(t, "int2", []interface{}{
pgtype.Int2{Int: math.MinInt16, Status: pgtype.Present}, pgtype.Int2{Int: math.MinInt16, Status: pgtype.Present},
pgtype.Int2{Int: -1, Status: pgtype.Present}, pgtype.Int2{Int: -1, Status: pgtype.Present},
pgtype.Int2{Int: 0, Status: pgtype.Present}, pgtype.Int2{Int: 0, Status: pgtype.Present},

View File

@ -5,10 +5,11 @@ import (
"testing" "testing"
"github.com/jackc/pgx/pgtype" "github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
) )
func TestInt4ArrayTranscode(t *testing.T) { func TestInt4ArrayTranscode(t *testing.T) {
testSuccessfulTranscode(t, "int4[]", []interface{}{ testutil.TestSuccessfulTranscode(t, "int4[]", []interface{}{
&pgtype.Int4Array{ &pgtype.Int4Array{
Elements: nil, Elements: nil,
Dimensions: nil, Dimensions: nil,

View File

@ -6,10 +6,11 @@ import (
"testing" "testing"
"github.com/jackc/pgx/pgtype" "github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
) )
func TestInt4Transcode(t *testing.T) { func TestInt4Transcode(t *testing.T) {
testSuccessfulTranscode(t, "int4", []interface{}{ testutil.TestSuccessfulTranscode(t, "int4", []interface{}{
pgtype.Int4{Int: math.MinInt32, Status: pgtype.Present}, pgtype.Int4{Int: math.MinInt32, Status: pgtype.Present},
pgtype.Int4{Int: -1, Status: pgtype.Present}, pgtype.Int4{Int: -1, Status: pgtype.Present},
pgtype.Int4{Int: 0, Status: pgtype.Present}, pgtype.Int4{Int: 0, Status: pgtype.Present},

View File

@ -4,10 +4,11 @@ import (
"testing" "testing"
"github.com/jackc/pgx/pgtype" "github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
) )
func TestInt4rangeTranscode(t *testing.T) { func TestInt4rangeTranscode(t *testing.T) {
testSuccessfulTranscode(t, "int4range", []interface{}{ testutil.TestSuccessfulTranscode(t, "int4range", []interface{}{
pgtype.Int4range{LowerType: pgtype.Empty, UpperType: pgtype.Empty, Status: pgtype.Present}, pgtype.Int4range{LowerType: pgtype.Empty, UpperType: pgtype.Empty, Status: pgtype.Present},
pgtype.Int4range{Lower: pgtype.Int4{Int: 1, Status: pgtype.Present}, Upper: pgtype.Int4{Int: 10, Status: pgtype.Present}, LowerType: pgtype.Inclusive, UpperType: pgtype.Exclusive, Status: pgtype.Present}, pgtype.Int4range{Lower: pgtype.Int4{Int: 1, Status: pgtype.Present}, Upper: pgtype.Int4{Int: 10, Status: pgtype.Present}, LowerType: pgtype.Inclusive, UpperType: pgtype.Exclusive, Status: pgtype.Present},
pgtype.Int4range{Lower: pgtype.Int4{Int: -42, Status: pgtype.Present}, Upper: pgtype.Int4{Int: -5, Status: pgtype.Present}, LowerType: pgtype.Inclusive, UpperType: pgtype.Exclusive, Status: pgtype.Present}, pgtype.Int4range{Lower: pgtype.Int4{Int: -42, Status: pgtype.Present}, Upper: pgtype.Int4{Int: -5, Status: pgtype.Present}, LowerType: pgtype.Inclusive, UpperType: pgtype.Exclusive, Status: pgtype.Present},
@ -16,10 +17,10 @@ func TestInt4rangeTranscode(t *testing.T) {
} }
func TestInt4rangeNormalize(t *testing.T) { func TestInt4rangeNormalize(t *testing.T) {
testSuccessfulNormalize(t, []normalizeTest{ testutil.TestSuccessfulNormalize(t, []testutil.NormalizeTest{
{ {
sql: "select int4range(1, 10, '(]')", SQL: "select int4range(1, 10, '(]')",
value: pgtype.Int4range{Lower: pgtype.Int4{Int: 2, Status: pgtype.Present}, Upper: pgtype.Int4{Int: 11, Status: pgtype.Present}, LowerType: pgtype.Inclusive, UpperType: pgtype.Exclusive, Status: pgtype.Present}, Value: pgtype.Int4range{Lower: pgtype.Int4{Int: 2, Status: pgtype.Present}, Upper: pgtype.Int4{Int: 11, Status: pgtype.Present}, LowerType: pgtype.Inclusive, UpperType: pgtype.Exclusive, Status: pgtype.Present},
}, },
}) })
} }

View File

@ -5,10 +5,11 @@ import (
"testing" "testing"
"github.com/jackc/pgx/pgtype" "github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
) )
func TestInt8ArrayTranscode(t *testing.T) { func TestInt8ArrayTranscode(t *testing.T) {
testSuccessfulTranscode(t, "int8[]", []interface{}{ testutil.TestSuccessfulTranscode(t, "int8[]", []interface{}{
&pgtype.Int8Array{ &pgtype.Int8Array{
Elements: nil, Elements: nil,
Dimensions: nil, Dimensions: nil,

View File

@ -6,10 +6,11 @@ import (
"testing" "testing"
"github.com/jackc/pgx/pgtype" "github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
) )
func TestInt8Transcode(t *testing.T) { func TestInt8Transcode(t *testing.T) {
testSuccessfulTranscode(t, "int8", []interface{}{ testutil.TestSuccessfulTranscode(t, "int8", []interface{}{
pgtype.Int8{Int: math.MinInt64, Status: pgtype.Present}, pgtype.Int8{Int: math.MinInt64, Status: pgtype.Present},
pgtype.Int8{Int: -1, Status: pgtype.Present}, pgtype.Int8{Int: -1, Status: pgtype.Present},
pgtype.Int8{Int: 0, Status: pgtype.Present}, pgtype.Int8{Int: 0, Status: pgtype.Present},

View File

@ -4,10 +4,11 @@ import (
"testing" "testing"
"github.com/jackc/pgx/pgtype" "github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
) )
func TestInt8rangeTranscode(t *testing.T) { func TestInt8rangeTranscode(t *testing.T) {
testSuccessfulTranscode(t, "Int8range", []interface{}{ testutil.TestSuccessfulTranscode(t, "Int8range", []interface{}{
pgtype.Int8range{LowerType: pgtype.Empty, UpperType: pgtype.Empty, Status: pgtype.Present}, pgtype.Int8range{LowerType: pgtype.Empty, UpperType: pgtype.Empty, Status: pgtype.Present},
pgtype.Int8range{Lower: pgtype.Int8{Int: 1, Status: pgtype.Present}, Upper: pgtype.Int8{Int: 10, Status: pgtype.Present}, LowerType: pgtype.Inclusive, UpperType: pgtype.Exclusive, Status: pgtype.Present}, pgtype.Int8range{Lower: pgtype.Int8{Int: 1, Status: pgtype.Present}, Upper: pgtype.Int8{Int: 10, Status: pgtype.Present}, LowerType: pgtype.Inclusive, UpperType: pgtype.Exclusive, Status: pgtype.Present},
pgtype.Int8range{Lower: pgtype.Int8{Int: -42, Status: pgtype.Present}, Upper: pgtype.Int8{Int: -5, Status: pgtype.Present}, LowerType: pgtype.Inclusive, UpperType: pgtype.Exclusive, Status: pgtype.Present}, pgtype.Int8range{Lower: pgtype.Int8{Int: -42, Status: pgtype.Present}, Upper: pgtype.Int8{Int: -5, Status: pgtype.Present}, LowerType: pgtype.Inclusive, UpperType: pgtype.Exclusive, Status: pgtype.Present},
@ -16,10 +17,10 @@ func TestInt8rangeTranscode(t *testing.T) {
} }
func TestInt8rangeNormalize(t *testing.T) { func TestInt8rangeNormalize(t *testing.T) {
testSuccessfulNormalize(t, []normalizeTest{ testutil.TestSuccessfulNormalize(t, []testutil.NormalizeTest{
{ {
sql: "select Int8range(1, 10, '(]')", SQL: "select Int8range(1, 10, '(]')",
value: pgtype.Int8range{Lower: pgtype.Int8{Int: 2, Status: pgtype.Present}, Upper: pgtype.Int8{Int: 11, Status: pgtype.Present}, LowerType: pgtype.Inclusive, UpperType: pgtype.Exclusive, Status: pgtype.Present}, Value: pgtype.Int8range{Lower: pgtype.Int8{Int: 2, Status: pgtype.Present}, Upper: pgtype.Int8{Int: 11, Status: pgtype.Present}, LowerType: pgtype.Inclusive, UpperType: pgtype.Exclusive, Status: pgtype.Present},
}, },
}) })
} }

View File

@ -4,10 +4,11 @@ import (
"testing" "testing"
"github.com/jackc/pgx/pgtype" "github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
) )
func TestIntervalTranscode(t *testing.T) { func TestIntervalTranscode(t *testing.T) {
testSuccessfulTranscode(t, "interval", []interface{}{ testutil.TestSuccessfulTranscode(t, "interval", []interface{}{
pgtype.Interval{Microseconds: 1, Status: pgtype.Present}, pgtype.Interval{Microseconds: 1, Status: pgtype.Present},
pgtype.Interval{Microseconds: 1000000, Status: pgtype.Present}, pgtype.Interval{Microseconds: 1000000, Status: pgtype.Present},
pgtype.Interval{Microseconds: 1000001, Status: pgtype.Present}, pgtype.Interval{Microseconds: 1000001, Status: pgtype.Present},
@ -29,34 +30,34 @@ func TestIntervalTranscode(t *testing.T) {
} }
func TestIntervalNormalize(t *testing.T) { func TestIntervalNormalize(t *testing.T) {
testSuccessfulNormalize(t, []normalizeTest{ testutil.TestSuccessfulNormalize(t, []testutil.NormalizeTest{
{ {
sql: "select '1 second'::interval", SQL: "select '1 second'::interval",
value: pgtype.Interval{Microseconds: 1000000, Status: pgtype.Present}, Value: pgtype.Interval{Microseconds: 1000000, Status: pgtype.Present},
}, },
{ {
sql: "select '1.000001 second'::interval", SQL: "select '1.000001 second'::interval",
value: pgtype.Interval{Microseconds: 1000001, Status: pgtype.Present}, Value: pgtype.Interval{Microseconds: 1000001, Status: pgtype.Present},
}, },
{ {
sql: "select '34223 hours'::interval", SQL: "select '34223 hours'::interval",
value: pgtype.Interval{Microseconds: 123202800000000, Status: pgtype.Present}, Value: pgtype.Interval{Microseconds: 123202800000000, Status: pgtype.Present},
}, },
{ {
sql: "select '1 day'::interval", SQL: "select '1 day'::interval",
value: pgtype.Interval{Days: 1, Status: pgtype.Present}, Value: pgtype.Interval{Days: 1, Status: pgtype.Present},
}, },
{ {
sql: "select '1 month'::interval", SQL: "select '1 month'::interval",
value: pgtype.Interval{Months: 1, Status: pgtype.Present}, Value: pgtype.Interval{Months: 1, Status: pgtype.Present},
}, },
{ {
sql: "select '1 year'::interval", SQL: "select '1 year'::interval",
value: pgtype.Interval{Months: 12, Status: pgtype.Present}, Value: pgtype.Interval{Months: 12, Status: pgtype.Present},
}, },
{ {
sql: "select '-13 mon'::interval", SQL: "select '-13 mon'::interval",
value: pgtype.Interval{Months: -13, Status: pgtype.Present}, Value: pgtype.Interval{Months: -13, Status: pgtype.Present},
}, },
}) })
} }

View File

@ -6,10 +6,11 @@ import (
"testing" "testing"
"github.com/jackc/pgx/pgtype" "github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
) )
func TestJsonTranscode(t *testing.T) { func TestJsonTranscode(t *testing.T) {
testSuccessfulTranscode(t, "json", []interface{}{ testutil.TestSuccessfulTranscode(t, "json", []interface{}{
pgtype.Json{Bytes: []byte("{}"), Status: pgtype.Present}, pgtype.Json{Bytes: []byte("{}"), Status: pgtype.Present},
pgtype.Json{Bytes: []byte("null"), Status: pgtype.Present}, pgtype.Json{Bytes: []byte("null"), Status: pgtype.Present},
pgtype.Json{Bytes: []byte("42"), Status: pgtype.Present}, pgtype.Json{Bytes: []byte("42"), Status: pgtype.Present},

View File

@ -6,16 +6,17 @@ import (
"testing" "testing"
"github.com/jackc/pgx/pgtype" "github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
) )
func TestJsonbTranscode(t *testing.T) { func TestJsonbTranscode(t *testing.T) {
conn := mustConnectPgx(t) conn := testutil.MustConnectPgx(t)
defer mustClose(t, conn) defer testutil.MustClose(t, conn)
if _, ok := conn.ConnInfo.DataTypeForName("jsonb"); !ok { if _, ok := conn.ConnInfo.DataTypeForName("jsonb"); !ok {
t.Skip("Skipping due to no jsonb type") t.Skip("Skipping due to no jsonb type")
} }
testSuccessfulTranscode(t, "jsonb", []interface{}{ testutil.TestSuccessfulTranscode(t, "jsonb", []interface{}{
pgtype.Jsonb{Bytes: []byte("{}"), Status: pgtype.Present}, pgtype.Jsonb{Bytes: []byte("{}"), Status: pgtype.Present},
pgtype.Jsonb{Bytes: []byte("null"), Status: pgtype.Present}, pgtype.Jsonb{Bytes: []byte("null"), Status: pgtype.Present},
pgtype.Jsonb{Bytes: []byte("42"), Status: pgtype.Present}, pgtype.Jsonb{Bytes: []byte("42"), Status: pgtype.Present},

View File

@ -5,15 +5,16 @@ import (
version "github.com/hashicorp/go-version" version "github.com/hashicorp/go-version"
"github.com/jackc/pgx/pgtype" "github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
) )
func TestLineTranscode(t *testing.T) { func TestLineTranscode(t *testing.T) {
conn := mustConnectPgx(t) conn := testutil.MustConnectPgx(t)
serverVersion, err := version.NewVersion(conn.RuntimeParams["server_version"]) serverVersion, err := version.NewVersion(conn.RuntimeParams["server_version"])
if err != nil { if err != nil {
t.Fatalf("cannot get server version: %v", err) t.Fatalf("cannot get server version: %v", err)
} }
mustClose(t, conn) testutil.MustClose(t, conn)
minVersion := version.Must(version.NewVersion("9.4")) minVersion := version.Must(version.NewVersion("9.4"))
@ -21,7 +22,7 @@ func TestLineTranscode(t *testing.T) {
t.Skipf("Skipping line test for server version %v", serverVersion) t.Skipf("Skipping line test for server version %v", serverVersion)
} }
testSuccessfulTranscode(t, "line", []interface{}{ testutil.TestSuccessfulTranscode(t, "line", []interface{}{
&pgtype.Line{ &pgtype.Line{
A: 1.23, B: 4.56, C: 7.89, A: 1.23, B: 4.56, C: 7.89,
Status: pgtype.Present, Status: pgtype.Present,

View File

@ -4,10 +4,11 @@ import (
"testing" "testing"
"github.com/jackc/pgx/pgtype" "github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
) )
func TestLsegTranscode(t *testing.T) { func TestLsegTranscode(t *testing.T) {
testSuccessfulTranscode(t, "lseg", []interface{}{ testutil.TestSuccessfulTranscode(t, "lseg", []interface{}{
&pgtype.Lseg{ &pgtype.Lseg{
P: [2]pgtype.Vec2{{3.14, 1.678}, {7.1, 5.234}}, P: [2]pgtype.Vec2{{3.14, 1.678}, {7.1, 5.234}},
Status: pgtype.Present, Status: pgtype.Present,

View File

@ -7,10 +7,11 @@ import (
"testing" "testing"
"github.com/jackc/pgx/pgtype" "github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
) )
func TestMacaddrTranscode(t *testing.T) { func TestMacaddrTranscode(t *testing.T) {
testSuccessfulTranscode(t, "macaddr", []interface{}{ testutil.TestSuccessfulTranscode(t, "macaddr", []interface{}{
pgtype.Macaddr{Addr: mustParseMacaddr(t, "01:23:45:67:89:ab"), Status: pgtype.Present}, pgtype.Macaddr{Addr: mustParseMacaddr(t, "01:23:45:67:89:ab"), Status: pgtype.Present},
pgtype.Macaddr{Status: pgtype.Null}, pgtype.Macaddr{Status: pgtype.Null},
}) })

View File

@ -5,10 +5,11 @@ import (
"testing" "testing"
"github.com/jackc/pgx/pgtype" "github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
) )
func TestNameTranscode(t *testing.T) { func TestNameTranscode(t *testing.T) {
testSuccessfulTranscode(t, "name", []interface{}{ testutil.TestSuccessfulTranscode(t, "name", []interface{}{
pgtype.Name{String: "", Status: pgtype.Present}, pgtype.Name{String: "", Status: pgtype.Present},
pgtype.Name{String: "foo", Status: pgtype.Present}, pgtype.Name{String: "foo", Status: pgtype.Present},
pgtype.Name{Status: pgtype.Null}, pgtype.Name{Status: pgtype.Null},

View File

@ -6,10 +6,11 @@ import (
"testing" "testing"
"github.com/jackc/pgx/pgtype" "github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
) )
func TestNumericArrayTranscode(t *testing.T) { func TestNumericArrayTranscode(t *testing.T) {
testSuccessfulTranscode(t, "numeric[]", []interface{}{ testutil.TestSuccessfulTranscode(t, "numeric[]", []interface{}{
&pgtype.NumericArray{ &pgtype.NumericArray{
Elements: nil, Elements: nil,
Dimensions: nil, Dimensions: nil,

View File

@ -7,6 +7,7 @@ import (
"testing" "testing"
"github.com/jackc/pgx/pgtype" "github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
) )
// For test purposes only. Note that it does not normalize values. e.g. (Int: 1, Exp: 3) will not equal (Int: 1000, Exp: 0) // For test purposes only. Note that it does not normalize values. e.g. (Int: 1, Exp: 3) will not equal (Int: 1000, Exp: 0)
@ -45,66 +46,66 @@ func mustParseBigInt(t *testing.T, src string) *big.Int {
} }
func TestNumericNormalize(t *testing.T) { func TestNumericNormalize(t *testing.T) {
testSuccessfulNormalize(t, []normalizeTest{ testutil.TestSuccessfulNormalize(t, []testutil.NormalizeTest{
{ {
sql: "select '0'::numeric", SQL: "select '0'::numeric",
value: pgtype.Numeric{Int: big.NewInt(0), Exp: 0, Status: pgtype.Present}, Value: pgtype.Numeric{Int: big.NewInt(0), Exp: 0, Status: pgtype.Present},
}, },
{ {
sql: "select '1'::numeric", SQL: "select '1'::numeric",
value: pgtype.Numeric{Int: big.NewInt(1), Exp: 0, Status: pgtype.Present}, Value: pgtype.Numeric{Int: big.NewInt(1), Exp: 0, Status: pgtype.Present},
}, },
{ {
sql: "select '10.00'::numeric", SQL: "select '10.00'::numeric",
value: pgtype.Numeric{Int: big.NewInt(1000), Exp: -2, Status: pgtype.Present}, Value: pgtype.Numeric{Int: big.NewInt(1000), Exp: -2, Status: pgtype.Present},
}, },
{ {
sql: "select '1e-3'::numeric", SQL: "select '1e-3'::numeric",
value: pgtype.Numeric{Int: big.NewInt(1), Exp: -3, Status: pgtype.Present}, Value: pgtype.Numeric{Int: big.NewInt(1), Exp: -3, Status: pgtype.Present},
}, },
{ {
sql: "select '-1'::numeric", SQL: "select '-1'::numeric",
value: pgtype.Numeric{Int: big.NewInt(-1), Exp: 0, Status: pgtype.Present}, Value: pgtype.Numeric{Int: big.NewInt(-1), Exp: 0, Status: pgtype.Present},
}, },
{ {
sql: "select '10000'::numeric", SQL: "select '10000'::numeric",
value: pgtype.Numeric{Int: big.NewInt(1), Exp: 4, Status: pgtype.Present}, Value: pgtype.Numeric{Int: big.NewInt(1), Exp: 4, Status: pgtype.Present},
}, },
{ {
sql: "select '3.14'::numeric", SQL: "select '3.14'::numeric",
value: pgtype.Numeric{Int: big.NewInt(314), Exp: -2, Status: pgtype.Present}, Value: pgtype.Numeric{Int: big.NewInt(314), Exp: -2, Status: pgtype.Present},
}, },
{ {
sql: "select '1.1'::numeric", SQL: "select '1.1'::numeric",
value: pgtype.Numeric{Int: big.NewInt(11), Exp: -1, Status: pgtype.Present}, Value: pgtype.Numeric{Int: big.NewInt(11), Exp: -1, Status: pgtype.Present},
}, },
{ {
sql: "select '100010001'::numeric", SQL: "select '100010001'::numeric",
value: pgtype.Numeric{Int: big.NewInt(100010001), Exp: 0, Status: pgtype.Present}, Value: pgtype.Numeric{Int: big.NewInt(100010001), Exp: 0, Status: pgtype.Present},
}, },
{ {
sql: "select '100010001.0001'::numeric", SQL: "select '100010001.0001'::numeric",
value: pgtype.Numeric{Int: big.NewInt(1000100010001), Exp: -4, Status: pgtype.Present}, Value: pgtype.Numeric{Int: big.NewInt(1000100010001), Exp: -4, Status: pgtype.Present},
}, },
{ {
sql: "select '4237234789234789289347892374324872138321894178943189043890124832108934.43219085471578891547854892438945012347981'::numeric", SQL: "select '4237234789234789289347892374324872138321894178943189043890124832108934.43219085471578891547854892438945012347981'::numeric",
value: pgtype.Numeric{ Value: pgtype.Numeric{
Int: mustParseBigInt(t, "423723478923478928934789237432487213832189417894318904389012483210893443219085471578891547854892438945012347981"), Int: mustParseBigInt(t, "423723478923478928934789237432487213832189417894318904389012483210893443219085471578891547854892438945012347981"),
Exp: -41, Exp: -41,
Status: pgtype.Present, Status: pgtype.Present,
}, },
}, },
{ {
sql: "select '0.8925092023480223478923478978978937897879595901237890234789243679037419057877231734823098432903527585734549035904590854890345905434578345789347890402348952348905890489054234237489234987723894789234'::numeric", SQL: "select '0.8925092023480223478923478978978937897879595901237890234789243679037419057877231734823098432903527585734549035904590854890345905434578345789347890402348952348905890489054234237489234987723894789234'::numeric",
value: pgtype.Numeric{ Value: pgtype.Numeric{
Int: mustParseBigInt(t, "8925092023480223478923478978978937897879595901237890234789243679037419057877231734823098432903527585734549035904590854890345905434578345789347890402348952348905890489054234237489234987723894789234"), Int: mustParseBigInt(t, "8925092023480223478923478978978937897879595901237890234789243679037419057877231734823098432903527585734549035904590854890345905434578345789347890402348952348905890489054234237489234987723894789234"),
Exp: -196, Exp: -196,
Status: pgtype.Present, Status: pgtype.Present,
}, },
}, },
{ {
sql: "select '0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000123'::numeric", SQL: "select '0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000123'::numeric",
value: pgtype.Numeric{ Value: pgtype.Numeric{
Int: mustParseBigInt(t, "123"), Int: mustParseBigInt(t, "123"),
Exp: -186, Exp: -186,
Status: pgtype.Present, Status: pgtype.Present,
@ -114,7 +115,7 @@ func TestNumericNormalize(t *testing.T) {
} }
func TestNumericTranscode(t *testing.T) { func TestNumericTranscode(t *testing.T) {
testSuccessfulTranscodeEqFunc(t, "numeric", []interface{}{ testutil.TestSuccessfulTranscodeEqFunc(t, "numeric", []interface{}{
&pgtype.Numeric{Int: big.NewInt(0), Exp: 0, Status: pgtype.Present}, &pgtype.Numeric{Int: big.NewInt(0), Exp: 0, Status: pgtype.Present},
&pgtype.Numeric{Int: big.NewInt(1), Exp: 0, Status: pgtype.Present}, &pgtype.Numeric{Int: big.NewInt(1), Exp: 0, Status: pgtype.Present},
&pgtype.Numeric{Int: big.NewInt(-1), Exp: 0, Status: pgtype.Present}, &pgtype.Numeric{Int: big.NewInt(-1), Exp: 0, Status: pgtype.Present},
@ -172,7 +173,7 @@ func TestNumericTranscodeFuzz(t *testing.T) {
} }
} }
testSuccessfulTranscodeEqFunc(t, "numeric", values, testutil.TestSuccessfulTranscodeEqFunc(t, "numeric", values,
func(aa, bb interface{}) bool { func(aa, bb interface{}) bool {
a := aa.(pgtype.Numeric) a := aa.(pgtype.Numeric)
b := bb.(pgtype.Numeric) b := bb.(pgtype.Numeric)

View File

@ -5,10 +5,11 @@ import (
"testing" "testing"
"github.com/jackc/pgx/pgtype" "github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
) )
func TestNumrangeTranscode(t *testing.T) { func TestNumrangeTranscode(t *testing.T) {
testSuccessfulTranscode(t, "numrange", []interface{}{ testutil.TestSuccessfulTranscode(t, "numrange", []interface{}{
pgtype.Numrange{ pgtype.Numrange{
LowerType: pgtype.Empty, LowerType: pgtype.Empty,
UpperType: pgtype.Empty, UpperType: pgtype.Empty,

View File

@ -5,10 +5,11 @@ import (
"testing" "testing"
"github.com/jackc/pgx/pgtype" "github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
) )
func TestOidValueTranscode(t *testing.T) { func TestOidValueTranscode(t *testing.T) {
testSuccessfulTranscode(t, "oid", []interface{}{ testutil.TestSuccessfulTranscode(t, "oid", []interface{}{
pgtype.OidValue{Uint: 42, Status: pgtype.Present}, pgtype.OidValue{Uint: 42, Status: pgtype.Present},
pgtype.OidValue{Status: pgtype.Null}, pgtype.OidValue{Status: pgtype.Null},
}) })

View File

@ -4,10 +4,11 @@ import (
"testing" "testing"
"github.com/jackc/pgx/pgtype" "github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
) )
func TestPathTranscode(t *testing.T) { func TestPathTranscode(t *testing.T) {
testSuccessfulTranscode(t, "path", []interface{}{ testutil.TestSuccessfulTranscode(t, "path", []interface{}{
&pgtype.Path{ &pgtype.Path{
P: []pgtype.Vec2{{3.14, 1.678}, {7.1, 5.234}}, P: []pgtype.Vec2{{3.14, 1.678}, {7.1, 5.234}},
Closed: false, Closed: false,

View File

@ -1,17 +1,9 @@
package pgtype_test package pgtype_test
import ( import (
"context"
"database/sql"
"fmt"
"io"
"net" "net"
"os"
"reflect"
"testing" "testing"
"github.com/jackc/pgx"
"github.com/jackc/pgx/pgtype"
_ "github.com/jackc/pgx/stdlib" _ "github.com/jackc/pgx/stdlib"
_ "github.com/lib/pq" _ "github.com/lib/pq"
) )
@ -28,48 +20,6 @@ type _float32Slice []float32
type _float64Slice []float64 type _float64Slice []float64
type _byteSlice []byte type _byteSlice []byte
func mustConnectDatabaseSQL(t testing.TB, driverName string) *sql.DB {
var sqlDriverName string
switch driverName {
case "github.com/lib/pq":
sqlDriverName = "postgres"
case "github.com/jackc/pgx/stdlib":
sqlDriverName = "pgx"
default:
t.Fatalf("Unknown driver %v", driverName)
}
db, err := sql.Open(sqlDriverName, os.Getenv("DATABASE_URL"))
if err != nil {
t.Fatal(err)
}
return db
}
func mustConnectPgx(t testing.TB) *pgx.Conn {
config, err := pgx.ParseURI(os.Getenv("DATABASE_URL"))
if err != nil {
t.Fatal(err)
}
conn, err := pgx.Connect(config)
if err != nil {
t.Fatal(err)
}
return conn
}
func mustClose(t testing.TB, conn interface {
Close() error
}) {
err := conn.Close()
if err != nil {
t.Fatal(err)
}
}
func mustParseCidr(t testing.TB, s string) *net.IPNet { func mustParseCidr(t testing.TB, s string) *net.IPNet {
_, ipnet, err := net.ParseCIDR(s) _, ipnet, err := net.ParseCIDR(s)
if err != nil { if err != nil {
@ -87,244 +37,3 @@ func mustParseMacaddr(t testing.TB, s string) net.HardwareAddr {
return addr return addr
} }
type forceTextEncoder struct {
e pgtype.TextEncoder
}
func (f forceTextEncoder) EncodeText(ci *pgtype.ConnInfo, w io.Writer) (bool, error) {
return f.e.EncodeText(ci, w)
}
type forceBinaryEncoder struct {
e pgtype.BinaryEncoder
}
func (f forceBinaryEncoder) EncodeBinary(ci *pgtype.ConnInfo, w io.Writer) (bool, error) {
return f.e.EncodeBinary(ci, w)
}
func forceEncoder(e interface{}, formatCode int16) interface{} {
switch formatCode {
case pgx.TextFormatCode:
if e, ok := e.(pgtype.TextEncoder); ok {
return forceTextEncoder{e: e}
}
case pgx.BinaryFormatCode:
if e, ok := e.(pgtype.BinaryEncoder); ok {
return forceBinaryEncoder{e: e.(pgtype.BinaryEncoder)}
}
}
return nil
}
func testSuccessfulTranscode(t testing.TB, pgTypeName string, values []interface{}) {
testSuccessfulTranscodeEqFunc(t, pgTypeName, values, func(a, b interface{}) bool {
return reflect.DeepEqual(a, b)
})
}
func testSuccessfulTranscodeEqFunc(t testing.TB, pgTypeName string, values []interface{}, eqFunc func(a, b interface{}) bool) {
testPgxSuccessfulTranscodeEqFunc(t, pgTypeName, values, eqFunc)
testPgxSimpleProtocolSuccessfulTranscodeEqFunc(t, pgTypeName, values, eqFunc)
for _, driverName := range []string{"github.com/lib/pq", "github.com/jackc/pgx/stdlib"} {
testDatabaseSQLSuccessfulTranscodeEqFunc(t, driverName, pgTypeName, values, eqFunc)
}
}
func testPgxSuccessfulTranscodeEqFunc(t testing.TB, pgTypeName string, values []interface{}, eqFunc func(a, b interface{}) bool) {
conn := mustConnectPgx(t)
defer mustClose(t, conn)
ps, err := conn.Prepare("test", fmt.Sprintf("select $1::%s", pgTypeName))
if err != nil {
t.Fatal(err)
}
formats := []struct {
name string
formatCode int16
}{
{name: "TextFormat", formatCode: pgx.TextFormatCode},
{name: "BinaryFormat", formatCode: pgx.BinaryFormatCode},
}
for i, v := range values {
for _, fc := range formats {
ps.FieldDescriptions[0].FormatCode = fc.formatCode
vEncoder := forceEncoder(v, fc.formatCode)
if vEncoder == nil {
t.Logf("Skipping: %#v does not implement %v", v, fc.name)
continue
}
// Derefence value if it is a pointer
derefV := v
refVal := reflect.ValueOf(v)
if refVal.Kind() == reflect.Ptr {
derefV = refVal.Elem().Interface()
}
result := reflect.New(reflect.TypeOf(derefV))
err := conn.QueryRow("test", forceEncoder(v, fc.formatCode)).Scan(result.Interface())
if err != nil {
t.Errorf("%v %d: %v", fc.name, i, err)
}
if !eqFunc(result.Elem().Interface(), derefV) {
t.Errorf("%v %d: expected %v, got %v", fc.name, i, derefV, result.Elem().Interface())
}
}
}
}
func testPgxSimpleProtocolSuccessfulTranscodeEqFunc(t testing.TB, pgTypeName string, values []interface{}, eqFunc func(a, b interface{}) bool) {
conn := mustConnectPgx(t)
defer mustClose(t, conn)
for i, v := range values {
// Derefence value if it is a pointer
derefV := v
refVal := reflect.ValueOf(v)
if refVal.Kind() == reflect.Ptr {
derefV = refVal.Elem().Interface()
}
result := reflect.New(reflect.TypeOf(derefV))
err := conn.QueryRowEx(
context.Background(),
fmt.Sprintf("select ($1)::%s", pgTypeName),
&pgx.QueryExOptions{SimpleProtocol: true},
v,
).Scan(result.Interface())
if err != nil {
t.Errorf("Simple protocol %d: %v", i, err)
}
if !eqFunc(result.Elem().Interface(), derefV) {
t.Errorf("Simple protocol %d: expected %v, got %v", i, derefV, result.Elem().Interface())
}
}
}
func testDatabaseSQLSuccessfulTranscodeEqFunc(t testing.TB, driverName, pgTypeName string, values []interface{}, eqFunc func(a, b interface{}) bool) {
conn := mustConnectDatabaseSQL(t, driverName)
defer mustClose(t, conn)
ps, err := conn.Prepare(fmt.Sprintf("select $1::%s", pgTypeName))
if err != nil {
t.Fatal(err)
}
for i, v := range values {
// Derefence value if it is a pointer
derefV := v
refVal := reflect.ValueOf(v)
if refVal.Kind() == reflect.Ptr {
derefV = refVal.Elem().Interface()
}
result := reflect.New(reflect.TypeOf(derefV))
err := ps.QueryRow(v).Scan(result.Interface())
if err != nil {
t.Errorf("%v %d: %v", driverName, i, err)
}
if !eqFunc(result.Elem().Interface(), derefV) {
t.Errorf("%v %d: expected %v, got %v", driverName, i, derefV, result.Elem().Interface())
}
}
}
type normalizeTest struct {
sql string
value interface{}
}
func testSuccessfulNormalize(t testing.TB, tests []normalizeTest) {
testSuccessfulNormalizeEqFunc(t, tests, func(a, b interface{}) bool {
return reflect.DeepEqual(a, b)
})
}
func testSuccessfulNormalizeEqFunc(t testing.TB, tests []normalizeTest, eqFunc func(a, b interface{}) bool) {
testPgxSuccessfulNormalizeEqFunc(t, tests, eqFunc)
for _, driverName := range []string{"github.com/lib/pq", "github.com/jackc/pgx/stdlib"} {
testDatabaseSQLSuccessfulNormalizeEqFunc(t, driverName, tests, eqFunc)
}
}
func testPgxSuccessfulNormalizeEqFunc(t testing.TB, tests []normalizeTest, eqFunc func(a, b interface{}) bool) {
conn := mustConnectPgx(t)
defer mustClose(t, conn)
formats := []struct {
name string
formatCode int16
}{
{name: "TextFormat", formatCode: pgx.TextFormatCode},
{name: "BinaryFormat", formatCode: pgx.BinaryFormatCode},
}
for i, tt := range tests {
for _, fc := range formats {
psName := fmt.Sprintf("test%d", i)
ps, err := conn.Prepare(psName, tt.sql)
if err != nil {
t.Fatal(err)
}
ps.FieldDescriptions[0].FormatCode = fc.formatCode
if forceEncoder(tt.value, fc.formatCode) == nil {
t.Logf("Skipping: %#v does not implement %v", tt.value, fc.name)
continue
}
// Derefence value if it is a pointer
derefV := tt.value
refVal := reflect.ValueOf(tt.value)
if refVal.Kind() == reflect.Ptr {
derefV = refVal.Elem().Interface()
}
result := reflect.New(reflect.TypeOf(derefV))
err = conn.QueryRow(psName).Scan(result.Interface())
if err != nil {
t.Errorf("%v %d: %v", fc.name, i, err)
}
if !eqFunc(result.Elem().Interface(), derefV) {
t.Errorf("%v %d: expected %v, got %v", fc.name, i, derefV, result.Elem().Interface())
}
}
}
}
func testDatabaseSQLSuccessfulNormalizeEqFunc(t testing.TB, driverName string, tests []normalizeTest, eqFunc func(a, b interface{}) bool) {
conn := mustConnectDatabaseSQL(t, driverName)
defer mustClose(t, conn)
for i, tt := range tests {
ps, err := conn.Prepare(tt.sql)
if err != nil {
t.Errorf("%d. %v", i, err)
continue
}
// Derefence value if it is a pointer
derefV := tt.value
refVal := reflect.ValueOf(tt.value)
if refVal.Kind() == reflect.Ptr {
derefV = refVal.Elem().Interface()
}
result := reflect.New(reflect.TypeOf(derefV))
err = ps.QueryRow().Scan(result.Interface())
if err != nil {
t.Errorf("%v %d: %v", driverName, i, err)
}
if !eqFunc(result.Elem().Interface(), derefV) {
t.Errorf("%v %d: expected %v, got %v", driverName, i, derefV, result.Elem().Interface())
}
}
}

View File

@ -4,10 +4,11 @@ import (
"testing" "testing"
"github.com/jackc/pgx/pgtype" "github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
) )
func TestPointTranscode(t *testing.T) { func TestPointTranscode(t *testing.T) {
testSuccessfulTranscode(t, "point", []interface{}{ testutil.TestSuccessfulTranscode(t, "point", []interface{}{
&pgtype.Point{P: pgtype.Vec2{1.234, 5.6789}, Status: pgtype.Present}, &pgtype.Point{P: pgtype.Vec2{1.234, 5.6789}, Status: pgtype.Present},
&pgtype.Point{P: pgtype.Vec2{-1.234, -5.6789}, Status: pgtype.Present}, &pgtype.Point{P: pgtype.Vec2{-1.234, -5.6789}, Status: pgtype.Present},
&pgtype.Point{Status: pgtype.Null}, &pgtype.Point{Status: pgtype.Null},

View File

@ -4,10 +4,11 @@ import (
"testing" "testing"
"github.com/jackc/pgx/pgtype" "github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
) )
func TestPolygonTranscode(t *testing.T) { func TestPolygonTranscode(t *testing.T) {
testSuccessfulTranscode(t, "polygon", []interface{}{ testutil.TestSuccessfulTranscode(t, "polygon", []interface{}{
&pgtype.Polygon{ &pgtype.Polygon{
P: []pgtype.Vec2{{3.14, 1.678}, {7.1, 5.234}, {5.0, 3.234}}, P: []pgtype.Vec2{{3.14, 1.678}, {7.1, 5.234}, {5.0, 3.234}},
Status: pgtype.Present, Status: pgtype.Present,

View File

@ -6,10 +6,11 @@ import (
"testing" "testing"
"github.com/jackc/pgx/pgtype" "github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
) )
func TestQCharTranscode(t *testing.T) { func TestQCharTranscode(t *testing.T) {
testPgxSuccessfulTranscodeEqFunc(t, `"char"`, []interface{}{ testutil.TestPgxSuccessfulTranscodeEqFunc(t, `"char"`, []interface{}{
pgtype.QChar{Int: math.MinInt8, Status: pgtype.Present}, pgtype.QChar{Int: math.MinInt8, Status: pgtype.Present},
pgtype.QChar{Int: -1, Status: pgtype.Present}, pgtype.QChar{Int: -1, Status: pgtype.Present},
pgtype.QChar{Int: 0, Status: pgtype.Present}, pgtype.QChar{Int: 0, Status: pgtype.Present},

View File

@ -7,11 +7,12 @@ import (
"github.com/jackc/pgx" "github.com/jackc/pgx"
"github.com/jackc/pgx/pgtype" "github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
) )
func TestRecordTranscode(t *testing.T) { func TestRecordTranscode(t *testing.T) {
conn := mustConnectPgx(t) conn := testutil.MustConnectPgx(t)
defer mustClose(t, conn) defer testutil.MustClose(t, conn)
tests := []struct { tests := []struct {
sql string sql string

View File

@ -15,7 +15,7 @@ import (
_ "github.com/lib/pq" _ "github.com/lib/pq"
) )
func mustConnectDatabaseSQL(t testing.TB, driverName string) *sql.DB { func MustConnectDatabaseSQL(t testing.TB, driverName string) *sql.DB {
var sqlDriverName string var sqlDriverName string
switch driverName { switch driverName {
case "github.com/lib/pq": case "github.com/lib/pq":
@ -34,7 +34,7 @@ func mustConnectDatabaseSQL(t testing.TB, driverName string) *sql.DB {
return db return db
} }
func mustConnectPgx(t testing.TB) *pgx.Conn { func MustConnectPgx(t testing.TB) *pgx.Conn {
config, err := pgx.ParseURI(os.Getenv("DATABASE_URL")) config, err := pgx.ParseURI(os.Getenv("DATABASE_URL"))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@ -48,7 +48,7 @@ func mustConnectPgx(t testing.TB) *pgx.Conn {
return conn return conn
} }
func mustClose(t testing.TB, conn interface { func MustClose(t testing.TB, conn interface {
Close() error Close() error
}) { }) {
err := conn.Close() err := conn.Close()
@ -73,7 +73,7 @@ func (f forceBinaryEncoder) EncodeBinary(ci *pgtype.ConnInfo, w io.Writer) (bool
return f.e.EncodeBinary(ci, w) return f.e.EncodeBinary(ci, w)
} }
func forceEncoder(e interface{}, formatCode int16) interface{} { func ForceEncoder(e interface{}, formatCode int16) interface{} {
switch formatCode { switch formatCode {
case pgx.TextFormatCode: case pgx.TextFormatCode:
if e, ok := e.(pgtype.TextEncoder); ok { if e, ok := e.(pgtype.TextEncoder); ok {
@ -102,8 +102,8 @@ func TestSuccessfulTranscodeEqFunc(t testing.TB, pgTypeName string, values []int
} }
func TestPgxSuccessfulTranscodeEqFunc(t testing.TB, pgTypeName string, values []interface{}, eqFunc func(a, b interface{}) bool) { func TestPgxSuccessfulTranscodeEqFunc(t testing.TB, pgTypeName string, values []interface{}, eqFunc func(a, b interface{}) bool) {
conn := mustConnectPgx(t) conn := MustConnectPgx(t)
defer mustClose(t, conn) defer MustClose(t, conn)
ps, err := conn.Prepare("test", fmt.Sprintf("select $1::%s", pgTypeName)) ps, err := conn.Prepare("test", fmt.Sprintf("select $1::%s", pgTypeName))
if err != nil { if err != nil {
@ -121,7 +121,7 @@ func TestPgxSuccessfulTranscodeEqFunc(t testing.TB, pgTypeName string, values []
for i, v := range values { for i, v := range values {
for _, fc := range formats { for _, fc := range formats {
ps.FieldDescriptions[0].FormatCode = fc.formatCode ps.FieldDescriptions[0].FormatCode = fc.formatCode
vEncoder := forceEncoder(v, fc.formatCode) vEncoder := ForceEncoder(v, fc.formatCode)
if vEncoder == nil { if vEncoder == nil {
t.Logf("Skipping: %#v does not implement %v", v, fc.name) t.Logf("Skipping: %#v does not implement %v", v, fc.name)
continue continue
@ -134,7 +134,7 @@ func TestPgxSuccessfulTranscodeEqFunc(t testing.TB, pgTypeName string, values []
} }
result := reflect.New(reflect.TypeOf(derefV)) result := reflect.New(reflect.TypeOf(derefV))
err := conn.QueryRow("test", forceEncoder(v, fc.formatCode)).Scan(result.Interface()) err := conn.QueryRow("test", ForceEncoder(v, fc.formatCode)).Scan(result.Interface())
if err != nil { if err != nil {
t.Errorf("%v %d: %v", fc.name, i, err) t.Errorf("%v %d: %v", fc.name, i, err)
} }
@ -147,8 +147,8 @@ func TestPgxSuccessfulTranscodeEqFunc(t testing.TB, pgTypeName string, values []
} }
func TestPgxSimpleProtocolSuccessfulTranscodeEqFunc(t testing.TB, pgTypeName string, values []interface{}, eqFunc func(a, b interface{}) bool) { func TestPgxSimpleProtocolSuccessfulTranscodeEqFunc(t testing.TB, pgTypeName string, values []interface{}, eqFunc func(a, b interface{}) bool) {
conn := mustConnectPgx(t) conn := MustConnectPgx(t)
defer mustClose(t, conn) defer MustClose(t, conn)
for i, v := range values { for i, v := range values {
// Derefence value if it is a pointer // Derefence value if it is a pointer
@ -176,8 +176,8 @@ func TestPgxSimpleProtocolSuccessfulTranscodeEqFunc(t testing.TB, pgTypeName str
} }
func TestDatabaseSQLSuccessfulTranscodeEqFunc(t testing.TB, driverName, pgTypeName string, values []interface{}, eqFunc func(a, b interface{}) bool) { func TestDatabaseSQLSuccessfulTranscodeEqFunc(t testing.TB, driverName, pgTypeName string, values []interface{}, eqFunc func(a, b interface{}) bool) {
conn := mustConnectDatabaseSQL(t, driverName) conn := MustConnectDatabaseSQL(t, driverName)
defer mustClose(t, conn) defer MustClose(t, conn)
ps, err := conn.Prepare(fmt.Sprintf("select $1::%s", pgTypeName)) ps, err := conn.Prepare(fmt.Sprintf("select $1::%s", pgTypeName))
if err != nil { if err != nil {
@ -223,8 +223,8 @@ func TestSuccessfulNormalizeEqFunc(t testing.TB, tests []NormalizeTest, eqFunc f
} }
func TestPgxSuccessfulNormalizeEqFunc(t testing.TB, tests []NormalizeTest, eqFunc func(a, b interface{}) bool) { func TestPgxSuccessfulNormalizeEqFunc(t testing.TB, tests []NormalizeTest, eqFunc func(a, b interface{}) bool) {
conn := mustConnectPgx(t) conn := MustConnectPgx(t)
defer mustClose(t, conn) defer MustClose(t, conn)
formats := []struct { formats := []struct {
name string name string
@ -243,7 +243,7 @@ func TestPgxSuccessfulNormalizeEqFunc(t testing.TB, tests []NormalizeTest, eqFun
} }
ps.FieldDescriptions[0].FormatCode = fc.formatCode ps.FieldDescriptions[0].FormatCode = fc.formatCode
if forceEncoder(tt.Value, fc.formatCode) == nil { if ForceEncoder(tt.Value, fc.formatCode) == nil {
t.Logf("Skipping: %#v does not implement %v", tt.Value, fc.name) t.Logf("Skipping: %#v does not implement %v", tt.Value, fc.name)
continue continue
} }
@ -268,8 +268,8 @@ func TestPgxSuccessfulNormalizeEqFunc(t testing.TB, tests []NormalizeTest, eqFun
} }
func TestDatabaseSQLSuccessfulNormalizeEqFunc(t testing.TB, driverName string, tests []NormalizeTest, eqFunc func(a, b interface{}) bool) { func TestDatabaseSQLSuccessfulNormalizeEqFunc(t testing.TB, driverName string, tests []NormalizeTest, eqFunc func(a, b interface{}) bool) {
conn := mustConnectDatabaseSQL(t, driverName) conn := MustConnectDatabaseSQL(t, driverName)
defer mustClose(t, conn) defer MustClose(t, conn)
for i, tt := range tests { for i, tt := range tests {
ps, err := conn.Prepare(tt.SQL) ps, err := conn.Prepare(tt.SQL)

View File

@ -5,10 +5,11 @@ import (
"testing" "testing"
"github.com/jackc/pgx/pgtype" "github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
) )
func TestTextArrayTranscode(t *testing.T) { func TestTextArrayTranscode(t *testing.T) {
testSuccessfulTranscode(t, "text[]", []interface{}{ testutil.TestSuccessfulTranscode(t, "text[]", []interface{}{
&pgtype.TextArray{ &pgtype.TextArray{
Elements: nil, Elements: nil,
Dimensions: nil, Dimensions: nil,

View File

@ -6,11 +6,12 @@ import (
"testing" "testing"
"github.com/jackc/pgx/pgtype" "github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
) )
func TestTextTranscode(t *testing.T) { func TestTextTranscode(t *testing.T) {
for _, pgTypeName := range []string{"text", "varchar"} { for _, pgTypeName := range []string{"text", "varchar"} {
testSuccessfulTranscode(t, pgTypeName, []interface{}{ testutil.TestSuccessfulTranscode(t, pgTypeName, []interface{}{
pgtype.Text{String: "", Status: pgtype.Present}, pgtype.Text{String: "", Status: pgtype.Present},
pgtype.Text{String: "foo", Status: pgtype.Present}, pgtype.Text{String: "foo", Status: pgtype.Present},
pgtype.Text{Status: pgtype.Null}, pgtype.Text{Status: pgtype.Null},

View File

@ -4,10 +4,11 @@ import (
"testing" "testing"
"github.com/jackc/pgx/pgtype" "github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
) )
func TestTidTranscode(t *testing.T) { func TestTidTranscode(t *testing.T) {
testSuccessfulTranscode(t, "tid", []interface{}{ testutil.TestSuccessfulTranscode(t, "tid", []interface{}{
pgtype.Tid{BlockNumber: 42, OffsetNumber: 43, Status: pgtype.Present}, pgtype.Tid{BlockNumber: 42, OffsetNumber: 43, Status: pgtype.Present},
pgtype.Tid{BlockNumber: 4294967295, OffsetNumber: 65535, Status: pgtype.Present}, pgtype.Tid{BlockNumber: 4294967295, OffsetNumber: 65535, Status: pgtype.Present},
pgtype.Tid{Status: pgtype.Null}, pgtype.Tid{Status: pgtype.Null},

View File

@ -6,10 +6,11 @@ import (
"time" "time"
"github.com/jackc/pgx/pgtype" "github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
) )
func TestTimestampArrayTranscode(t *testing.T) { func TestTimestampArrayTranscode(t *testing.T) {
testSuccessfulTranscodeEqFunc(t, "timestamp[]", []interface{}{ testutil.TestSuccessfulTranscodeEqFunc(t, "timestamp[]", []interface{}{
&pgtype.TimestampArray{ &pgtype.TimestampArray{
Elements: nil, Elements: nil,
Dimensions: nil, Dimensions: nil,

View File

@ -6,10 +6,11 @@ import (
"time" "time"
"github.com/jackc/pgx/pgtype" "github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
) )
func TestTimestampTranscode(t *testing.T) { func TestTimestampTranscode(t *testing.T) {
testSuccessfulTranscodeEqFunc(t, "timestamp", []interface{}{ testutil.TestSuccessfulTranscodeEqFunc(t, "timestamp", []interface{}{
pgtype.Timestamp{Time: time.Date(1800, 1, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, pgtype.Timestamp{Time: time.Date(1800, 1, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present},
pgtype.Timestamp{Time: time.Date(1900, 1, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, pgtype.Timestamp{Time: time.Date(1900, 1, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present},
pgtype.Timestamp{Time: time.Date(1905, 1, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, pgtype.Timestamp{Time: time.Date(1905, 1, 1, 0, 0, 0, 0, time.UTC), Status: pgtype.Present},

View File

@ -6,10 +6,11 @@ import (
"time" "time"
"github.com/jackc/pgx/pgtype" "github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
) )
func TestTimestamptzArrayTranscode(t *testing.T) { func TestTimestamptzArrayTranscode(t *testing.T) {
testSuccessfulTranscodeEqFunc(t, "timestamptz[]", []interface{}{ testutil.TestSuccessfulTranscodeEqFunc(t, "timestamptz[]", []interface{}{
&pgtype.TimestamptzArray{ &pgtype.TimestamptzArray{
Elements: nil, Elements: nil,
Dimensions: nil, Dimensions: nil,

View File

@ -6,10 +6,11 @@ import (
"time" "time"
"github.com/jackc/pgx/pgtype" "github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
) )
func TestTimestamptzTranscode(t *testing.T) { func TestTimestamptzTranscode(t *testing.T) {
testSuccessfulTranscodeEqFunc(t, "timestamptz", []interface{}{ testutil.TestSuccessfulTranscodeEqFunc(t, "timestamptz", []interface{}{
pgtype.Timestamptz{Time: time.Date(1800, 1, 1, 0, 0, 0, 0, time.Local), Status: pgtype.Present}, pgtype.Timestamptz{Time: time.Date(1800, 1, 1, 0, 0, 0, 0, time.Local), Status: pgtype.Present},
pgtype.Timestamptz{Time: time.Date(1900, 1, 1, 0, 0, 0, 0, time.Local), Status: pgtype.Present}, pgtype.Timestamptz{Time: time.Date(1900, 1, 1, 0, 0, 0, 0, time.Local), Status: pgtype.Present},
pgtype.Timestamptz{Time: time.Date(1905, 1, 1, 0, 0, 0, 0, time.Local), Status: pgtype.Present}, pgtype.Timestamptz{Time: time.Date(1905, 1, 1, 0, 0, 0, 0, time.Local), Status: pgtype.Present},

View File

@ -5,10 +5,11 @@ import (
"time" "time"
"github.com/jackc/pgx/pgtype" "github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
) )
func TestTsrangeTranscode(t *testing.T) { func TestTsrangeTranscode(t *testing.T) {
testSuccessfulTranscodeEqFunc(t, "tsrange", []interface{}{ testutil.TestSuccessfulTranscodeEqFunc(t, "tsrange", []interface{}{
pgtype.Tsrange{LowerType: pgtype.Empty, UpperType: pgtype.Empty, Status: pgtype.Present}, pgtype.Tsrange{LowerType: pgtype.Empty, UpperType: pgtype.Empty, Status: pgtype.Present},
pgtype.Tsrange{ pgtype.Tsrange{
Lower: pgtype.Timestamp{Time: time.Date(1990, 12, 31, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, Lower: pgtype.Timestamp{Time: time.Date(1990, 12, 31, 0, 0, 0, 0, time.UTC), Status: pgtype.Present},

View File

@ -5,10 +5,11 @@ import (
"time" "time"
"github.com/jackc/pgx/pgtype" "github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
) )
func TestTstzrangeTranscode(t *testing.T) { func TestTstzrangeTranscode(t *testing.T) {
testSuccessfulTranscodeEqFunc(t, "tstzrange", []interface{}{ testutil.TestSuccessfulTranscodeEqFunc(t, "tstzrange", []interface{}{
pgtype.Tstzrange{LowerType: pgtype.Empty, UpperType: pgtype.Empty, Status: pgtype.Present}, pgtype.Tstzrange{LowerType: pgtype.Empty, UpperType: pgtype.Empty, Status: pgtype.Present},
pgtype.Tstzrange{ pgtype.Tstzrange{
Lower: pgtype.Timestamptz{Time: time.Date(1990, 12, 31, 0, 0, 0, 0, time.UTC), Status: pgtype.Present}, Lower: pgtype.Timestamptz{Time: time.Date(1990, 12, 31, 0, 0, 0, 0, time.UTC), Status: pgtype.Present},

View File

@ -5,10 +5,11 @@ import (
"testing" "testing"
"github.com/jackc/pgx/pgtype" "github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
) )
func TestUuidTranscode(t *testing.T) { func TestUuidTranscode(t *testing.T) {
testSuccessfulTranscode(t, "uuid", []interface{}{ testutil.TestSuccessfulTranscode(t, "uuid", []interface{}{
pgtype.Uuid{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present}, pgtype.Uuid{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present},
pgtype.Uuid{Status: pgtype.Null}, pgtype.Uuid{Status: pgtype.Null},
}) })

View File

@ -4,10 +4,11 @@ import (
"testing" "testing"
"github.com/jackc/pgx/pgtype" "github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
) )
func TestVarbitTranscode(t *testing.T) { func TestVarbitTranscode(t *testing.T) {
testSuccessfulTranscode(t, "varbit", []interface{}{ testutil.TestSuccessfulTranscode(t, "varbit", []interface{}{
&pgtype.Varbit{Bytes: []byte{}, Len: 0, Status: pgtype.Present}, &pgtype.Varbit{Bytes: []byte{}, Len: 0, Status: pgtype.Present},
&pgtype.Varbit{Bytes: []byte{0, 1, 128, 254, 255}, Len: 40, Status: pgtype.Present}, &pgtype.Varbit{Bytes: []byte{0, 1, 128, 254, 255}, Len: 40, Status: pgtype.Present},
&pgtype.Varbit{Bytes: []byte{0, 1, 128, 254, 128}, Len: 33, Status: pgtype.Present}, &pgtype.Varbit{Bytes: []byte{0, 1, 128, 254, 128}, Len: 33, Status: pgtype.Present},
@ -16,10 +17,10 @@ func TestVarbitTranscode(t *testing.T) {
} }
func TestVarbitNormalize(t *testing.T) { func TestVarbitNormalize(t *testing.T) {
testSuccessfulNormalize(t, []normalizeTest{ testutil.TestSuccessfulNormalize(t, []testutil.NormalizeTest{
{ {
sql: "select B'111111111'", SQL: "select B'111111111'",
value: &pgtype.Varbit{Bytes: []byte{255, 128}, Len: 9, Status: pgtype.Present}, Value: &pgtype.Varbit{Bytes: []byte{255, 128}, Len: 9, Status: pgtype.Present},
}, },
}) })
} }

View File

@ -5,10 +5,11 @@ import (
"testing" "testing"
"github.com/jackc/pgx/pgtype" "github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
) )
func TestVarcharArrayTranscode(t *testing.T) { func TestVarcharArrayTranscode(t *testing.T) {
testSuccessfulTranscode(t, "varchar[]", []interface{}{ testutil.TestSuccessfulTranscode(t, "varchar[]", []interface{}{
&pgtype.VarcharArray{ &pgtype.VarcharArray{
Elements: nil, Elements: nil,
Dimensions: nil, Dimensions: nil,

View File

@ -5,6 +5,7 @@ import (
"testing" "testing"
"github.com/jackc/pgx/pgtype" "github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
) )
func TestXidTranscode(t *testing.T) { func TestXidTranscode(t *testing.T) {
@ -17,13 +18,13 @@ func TestXidTranscode(t *testing.T) {
return reflect.DeepEqual(a, b) return reflect.DeepEqual(a, b)
} }
testPgxSuccessfulTranscodeEqFunc(t, pgTypeName, values, eqFunc) testutil.TestPgxSuccessfulTranscodeEqFunc(t, pgTypeName, values, eqFunc)
// No direct conversion from int to xid, convert through text // No direct conversion from int to xid, convert through text
testPgxSimpleProtocolSuccessfulTranscodeEqFunc(t, "text::"+pgTypeName, values, eqFunc) testutil.TestPgxSimpleProtocolSuccessfulTranscodeEqFunc(t, "text::"+pgTypeName, values, eqFunc)
for _, driverName := range []string{"github.com/lib/pq", "github.com/jackc/pgx/stdlib"} { for _, driverName := range []string{"github.com/lib/pq", "github.com/jackc/pgx/stdlib"} {
testDatabaseSQLSuccessfulTranscodeEqFunc(t, driverName, pgTypeName, values, eqFunc) testutil.TestDatabaseSQLSuccessfulTranscodeEqFunc(t, driverName, pgTypeName, values, eqFunc)
} }
} }