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"
"github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
)
func TestAclitemArrayTranscode(t *testing.T) {
testSuccessfulTranscode(t, "aclitem[]", []interface{}{
testutil.TestSuccessfulTranscode(t, "aclitem[]", []interface{}{
&pgtype.AclitemArray{
Elements: nil,
Dimensions: nil,

View File

@ -5,10 +5,11 @@ import (
"testing"
"github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
)
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/" tricky, ' } "" \ test user "`, Status: pgtype.Present},
pgtype.Aclitem{Status: pgtype.Null},

View File

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

View File

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

View File

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

View File

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

View File

@ -5,10 +5,11 @@ import (
"testing"
"github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
)
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{}, Status: pgtype.Present},
pgtype.Bytea{Bytes: nil, Status: pgtype.Null},

View File

@ -5,6 +5,7 @@ import (
"testing"
"github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
)
func TestCidTranscode(t *testing.T) {
@ -17,13 +18,13 @@ func TestCidTranscode(t *testing.T) {
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
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"} {
testDatabaseSQLSuccessfulTranscodeEqFunc(t, driverName, pgTypeName, values, eqFunc)
testutil.TestDatabaseSQLSuccessfulTranscodeEqFunc(t, driverName, pgTypeName, values, eqFunc)
}
}

View File

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

View File

@ -4,10 +4,11 @@ import (
"testing"
"github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
)
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: 12.9, Status: pgtype.Present},
&pgtype.Circle{Status: pgtype.Null},

View File

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

View File

@ -6,10 +6,11 @@ import (
"time"
"github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
)
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(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},

View File

@ -5,10 +5,11 @@ import (
"time"
"github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
)
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{
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) {
testSuccessfulNormalizeEqFunc(t, []normalizeTest{
testutil.TestSuccessfulNormalizeEqFunc(t, []testutil.NormalizeTest{
{
sql: "select daterange('2010-01-01', '2010-01-11', '(]')",
value: pgtype.Daterange{
SQL: "select daterange('2010-01-01', '2010-01-11', '(]')",
Value: pgtype.Daterange{
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},
LowerType: pgtype.Inclusive,

View File

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

View File

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

View File

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

View File

@ -5,10 +5,11 @@ import (
"testing"
"github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
)
func TestFloat8Transcode(t *testing.T) {
testSuccessfulTranscode(t, "float8", []interface{}{
testutil.TestSuccessfulTranscode(t, "float8", []interface{}{
pgtype.Float8{Float: -1, Status: pgtype.Present},
pgtype.Float8{Float: 0, 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/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
)
func TestHstoreArrayTranscode(t *testing.T) {
conn := mustConnectPgx(t)
defer mustClose(t, conn)
conn := testutil.MustConnectPgx(t)
defer testutil.MustClose(t, conn)
text := func(s string) pgtype.Text {
return pgtype.Text{String: s, Status: pgtype.Present}
@ -69,7 +70,7 @@ func TestHstoreArrayTranscode(t *testing.T) {
for _, fc := range formats {
ps.FieldDescriptions[0].FormatCode = fc.formatCode
vEncoder := forceEncoder(src, fc.formatCode)
vEncoder := testutil.ForceEncoder(src, fc.formatCode)
if vEncoder == nil {
t.Logf("%#v does not implement %v", src, fc.name)
continue

View File

@ -5,6 +5,7 @@ import (
"testing"
"github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
)
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
}
testSuccessfulTranscodeEqFunc(t, "hstore", values, func(ai, bi interface{}) bool {
testutil.TestSuccessfulTranscodeEqFunc(t, "hstore", values, func(ai, bi interface{}) bool {
a := ai.(pgtype.Hstore)
b := bi.(pgtype.Hstore)

View File

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

View File

@ -6,11 +6,12 @@ import (
"testing"
"github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
)
func TestInetTranscode(t *testing.T) {
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, "127.0.0.1/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"
"github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
)
func TestInt2ArrayTranscode(t *testing.T) {
testSuccessfulTranscode(t, "int2[]", []interface{}{
testutil.TestSuccessfulTranscode(t, "int2[]", []interface{}{
&pgtype.Int2Array{
Elements: nil,
Dimensions: nil,

View File

@ -6,10 +6,11 @@ import (
"testing"
"github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
)
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: -1, Status: pgtype.Present},
pgtype.Int2{Int: 0, Status: pgtype.Present},

View File

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

View File

@ -6,10 +6,11 @@ import (
"testing"
"github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
)
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: -1, Status: pgtype.Present},
pgtype.Int4{Int: 0, Status: pgtype.Present},

View File

@ -4,10 +4,11 @@ import (
"testing"
"github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
)
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{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},
@ -16,10 +17,10 @@ func TestInt4rangeTranscode(t *testing.T) {
}
func TestInt4rangeNormalize(t *testing.T) {
testSuccessfulNormalize(t, []normalizeTest{
testutil.TestSuccessfulNormalize(t, []testutil.NormalizeTest{
{
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},
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},
},
})
}

View File

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

View File

@ -6,10 +6,11 @@ import (
"testing"
"github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
)
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: -1, Status: pgtype.Present},
pgtype.Int8{Int: 0, Status: pgtype.Present},

View File

@ -4,10 +4,11 @@ import (
"testing"
"github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
)
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{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},
@ -16,10 +17,10 @@ func TestInt8rangeTranscode(t *testing.T) {
}
func TestInt8rangeNormalize(t *testing.T) {
testSuccessfulNormalize(t, []normalizeTest{
testutil.TestSuccessfulNormalize(t, []testutil.NormalizeTest{
{
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},
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},
},
})
}

View File

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

View File

@ -6,10 +6,11 @@ import (
"testing"
"github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
)
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("null"), Status: pgtype.Present},
pgtype.Json{Bytes: []byte("42"), Status: pgtype.Present},

View File

@ -6,16 +6,17 @@ import (
"testing"
"github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
)
func TestJsonbTranscode(t *testing.T) {
conn := mustConnectPgx(t)
defer mustClose(t, conn)
conn := testutil.MustConnectPgx(t)
defer testutil.MustClose(t, conn)
if _, ok := conn.ConnInfo.DataTypeForName("jsonb"); !ok {
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("null"), Status: pgtype.Present},
pgtype.Jsonb{Bytes: []byte("42"), Status: pgtype.Present},

View File

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

View File

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

View File

@ -7,10 +7,11 @@ import (
"testing"
"github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
)
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{Status: pgtype.Null},
})

View File

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

View File

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

View File

@ -7,6 +7,7 @@ import (
"testing"
"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)
@ -45,66 +46,66 @@ func mustParseBigInt(t *testing.T, src string) *big.Int {
}
func TestNumericNormalize(t *testing.T) {
testSuccessfulNormalize(t, []normalizeTest{
testutil.TestSuccessfulNormalize(t, []testutil.NormalizeTest{
{
sql: "select '0'::numeric",
value: pgtype.Numeric{Int: big.NewInt(0), Exp: 0, Status: pgtype.Present},
SQL: "select '0'::numeric",
Value: pgtype.Numeric{Int: big.NewInt(0), Exp: 0, Status: pgtype.Present},
},
{
sql: "select '1'::numeric",
value: pgtype.Numeric{Int: big.NewInt(1), Exp: 0, Status: pgtype.Present},
SQL: "select '1'::numeric",
Value: pgtype.Numeric{Int: big.NewInt(1), Exp: 0, Status: pgtype.Present},
},
{
sql: "select '10.00'::numeric",
value: pgtype.Numeric{Int: big.NewInt(1000), Exp: -2, Status: pgtype.Present},
SQL: "select '10.00'::numeric",
Value: pgtype.Numeric{Int: big.NewInt(1000), Exp: -2, Status: pgtype.Present},
},
{
sql: "select '1e-3'::numeric",
value: pgtype.Numeric{Int: big.NewInt(1), Exp: -3, Status: pgtype.Present},
SQL: "select '1e-3'::numeric",
Value: pgtype.Numeric{Int: big.NewInt(1), Exp: -3, Status: pgtype.Present},
},
{
sql: "select '-1'::numeric",
value: pgtype.Numeric{Int: big.NewInt(-1), Exp: 0, Status: pgtype.Present},
SQL: "select '-1'::numeric",
Value: pgtype.Numeric{Int: big.NewInt(-1), Exp: 0, Status: pgtype.Present},
},
{
sql: "select '10000'::numeric",
value: pgtype.Numeric{Int: big.NewInt(1), Exp: 4, Status: pgtype.Present},
SQL: "select '10000'::numeric",
Value: pgtype.Numeric{Int: big.NewInt(1), Exp: 4, Status: pgtype.Present},
},
{
sql: "select '3.14'::numeric",
value: pgtype.Numeric{Int: big.NewInt(314), Exp: -2, Status: pgtype.Present},
SQL: "select '3.14'::numeric",
Value: pgtype.Numeric{Int: big.NewInt(314), Exp: -2, Status: pgtype.Present},
},
{
sql: "select '1.1'::numeric",
value: pgtype.Numeric{Int: big.NewInt(11), Exp: -1, Status: pgtype.Present},
SQL: "select '1.1'::numeric",
Value: pgtype.Numeric{Int: big.NewInt(11), Exp: -1, Status: pgtype.Present},
},
{
sql: "select '100010001'::numeric",
value: pgtype.Numeric{Int: big.NewInt(100010001), Exp: 0, Status: pgtype.Present},
SQL: "select '100010001'::numeric",
Value: pgtype.Numeric{Int: big.NewInt(100010001), Exp: 0, Status: pgtype.Present},
},
{
sql: "select '100010001.0001'::numeric",
value: pgtype.Numeric{Int: big.NewInt(1000100010001), Exp: -4, Status: pgtype.Present},
SQL: "select '100010001.0001'::numeric",
Value: pgtype.Numeric{Int: big.NewInt(1000100010001), Exp: -4, Status: pgtype.Present},
},
{
sql: "select '4237234789234789289347892374324872138321894178943189043890124832108934.43219085471578891547854892438945012347981'::numeric",
value: pgtype.Numeric{
SQL: "select '4237234789234789289347892374324872138321894178943189043890124832108934.43219085471578891547854892438945012347981'::numeric",
Value: pgtype.Numeric{
Int: mustParseBigInt(t, "423723478923478928934789237432487213832189417894318904389012483210893443219085471578891547854892438945012347981"),
Exp: -41,
Status: pgtype.Present,
},
},
{
sql: "select '0.8925092023480223478923478978978937897879595901237890234789243679037419057877231734823098432903527585734549035904590854890345905434578345789347890402348952348905890489054234237489234987723894789234'::numeric",
value: pgtype.Numeric{
SQL: "select '0.8925092023480223478923478978978937897879595901237890234789243679037419057877231734823098432903527585734549035904590854890345905434578345789347890402348952348905890489054234237489234987723894789234'::numeric",
Value: pgtype.Numeric{
Int: mustParseBigInt(t, "8925092023480223478923478978978937897879595901237890234789243679037419057877231734823098432903527585734549035904590854890345905434578345789347890402348952348905890489054234237489234987723894789234"),
Exp: -196,
Status: pgtype.Present,
},
},
{
sql: "select '0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000123'::numeric",
value: pgtype.Numeric{
SQL: "select '0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000123'::numeric",
Value: pgtype.Numeric{
Int: mustParseBigInt(t, "123"),
Exp: -186,
Status: pgtype.Present,
@ -114,7 +115,7 @@ func TestNumericNormalize(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(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 {
a := aa.(pgtype.Numeric)
b := bb.(pgtype.Numeric)

View File

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

View File

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

View File

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

View File

@ -1,17 +1,9 @@
package pgtype_test
import (
"context"
"database/sql"
"fmt"
"io"
"net"
"os"
"reflect"
"testing"
"github.com/jackc/pgx"
"github.com/jackc/pgx/pgtype"
_ "github.com/jackc/pgx/stdlib"
_ "github.com/lib/pq"
)
@ -28,48 +20,6 @@ type _float32Slice []float32
type _float64Slice []float64
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 {
_, ipnet, err := net.ParseCIDR(s)
if err != nil {
@ -87,244 +37,3 @@ func mustParseMacaddr(t testing.TB, s string) net.HardwareAddr {
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"
"github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
)
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{Status: pgtype.Null},

View File

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

View File

@ -6,10 +6,11 @@ import (
"testing"
"github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
)
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: -1, 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/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
)
func TestRecordTranscode(t *testing.T) {
conn := mustConnectPgx(t)
defer mustClose(t, conn)
conn := testutil.MustConnectPgx(t)
defer testutil.MustClose(t, conn)
tests := []struct {
sql string

View File

@ -15,7 +15,7 @@ import (
_ "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
switch driverName {
case "github.com/lib/pq":
@ -34,7 +34,7 @@ func mustConnectDatabaseSQL(t testing.TB, driverName string) *sql.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"))
if err != nil {
t.Fatal(err)
@ -48,7 +48,7 @@ func mustConnectPgx(t testing.TB) *pgx.Conn {
return conn
}
func mustClose(t testing.TB, conn interface {
func MustClose(t testing.TB, conn interface {
Close() error
}) {
err := conn.Close()
@ -73,7 +73,7 @@ func (f forceBinaryEncoder) EncodeBinary(ci *pgtype.ConnInfo, w io.Writer) (bool
return f.e.EncodeBinary(ci, w)
}
func forceEncoder(e interface{}, formatCode int16) interface{} {
func ForceEncoder(e interface{}, formatCode int16) interface{} {
switch formatCode {
case pgx.TextFormatCode:
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) {
conn := mustConnectPgx(t)
defer mustClose(t, conn)
conn := MustConnectPgx(t)
defer MustClose(t, conn)
ps, err := conn.Prepare("test", fmt.Sprintf("select $1::%s", pgTypeName))
if err != nil {
@ -121,7 +121,7 @@ func TestPgxSuccessfulTranscodeEqFunc(t testing.TB, pgTypeName string, values []
for i, v := range values {
for _, fc := range formats {
ps.FieldDescriptions[0].FormatCode = fc.formatCode
vEncoder := forceEncoder(v, fc.formatCode)
vEncoder := ForceEncoder(v, fc.formatCode)
if vEncoder == nil {
t.Logf("Skipping: %#v does not implement %v", v, fc.name)
continue
@ -134,7 +134,7 @@ func TestPgxSuccessfulTranscodeEqFunc(t testing.TB, pgTypeName string, values []
}
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 {
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) {
conn := mustConnectPgx(t)
defer mustClose(t, conn)
conn := MustConnectPgx(t)
defer MustClose(t, conn)
for i, v := range values {
// 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) {
conn := mustConnectDatabaseSQL(t, driverName)
defer mustClose(t, conn)
conn := MustConnectDatabaseSQL(t, driverName)
defer MustClose(t, conn)
ps, err := conn.Prepare(fmt.Sprintf("select $1::%s", pgTypeName))
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) {
conn := mustConnectPgx(t)
defer mustClose(t, conn)
conn := MustConnectPgx(t)
defer MustClose(t, conn)
formats := []struct {
name string
@ -243,7 +243,7 @@ func TestPgxSuccessfulNormalizeEqFunc(t testing.TB, tests []NormalizeTest, eqFun
}
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)
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) {
conn := mustConnectDatabaseSQL(t, driverName)
defer mustClose(t, conn)
conn := MustConnectDatabaseSQL(t, driverName)
defer MustClose(t, conn)
for i, tt := range tests {
ps, err := conn.Prepare(tt.SQL)

View File

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

View File

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

View File

@ -4,10 +4,11 @@ import (
"testing"
"github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
)
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: 4294967295, OffsetNumber: 65535, Status: pgtype.Present},
pgtype.Tid{Status: pgtype.Null},

View File

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

View File

@ -6,10 +6,11 @@ import (
"time"
"github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
)
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(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},

View File

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

View File

@ -6,10 +6,11 @@ import (
"time"
"github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
)
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(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},

View File

@ -5,10 +5,11 @@ import (
"time"
"github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
)
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{
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"
"github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
)
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{
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"
"github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
)
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{Status: pgtype.Null},
})

View File

@ -4,10 +4,11 @@ import (
"testing"
"github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
)
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{0, 1, 128, 254, 255}, Len: 40, 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) {
testSuccessfulNormalize(t, []normalizeTest{
testutil.TestSuccessfulNormalize(t, []testutil.NormalizeTest{
{
sql: "select B'111111111'",
value: &pgtype.Varbit{Bytes: []byte{255, 128}, Len: 9, Status: pgtype.Present},
SQL: "select B'111111111'",
Value: &pgtype.Varbit{Bytes: []byte{255, 128}, Len: 9, Status: pgtype.Present},
},
})
}

View File

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

View File

@ -5,6 +5,7 @@ import (
"testing"
"github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/pgtype/testutil"
)
func TestXidTranscode(t *testing.T) {
@ -17,13 +18,13 @@ func TestXidTranscode(t *testing.T) {
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
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"} {
testDatabaseSQLSuccessfulTranscodeEqFunc(t, driverName, pgTypeName, values, eqFunc)
testutil.TestDatabaseSQLSuccessfulTranscodeEqFunc(t, driverName, pgTypeName, values, eqFunc)
}
}