Added failing test for pointer to custom type
This commit is contained in:
+54
-1
@@ -1,6 +1,7 @@
|
||||
package pgtype_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"net"
|
||||
"testing"
|
||||
|
||||
@@ -9,6 +10,8 @@ import (
|
||||
_ "github.com/jackc/pgx/v4/stdlib"
|
||||
_ "github.com/lib/pq"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
errors "golang.org/x/xerrors"
|
||||
)
|
||||
|
||||
// Test for renamed types
|
||||
@@ -41,7 +44,7 @@ func mustParseMacaddr(t testing.TB, s string) net.HardwareAddr {
|
||||
return addr
|
||||
}
|
||||
|
||||
func TestConnInfoScanUnknownOID(t *testing.T) {
|
||||
func TestConnInfoScanUnknownOIDToStringsAndBytes(t *testing.T) {
|
||||
unknownOID := uint32(999999)
|
||||
srcBuf := []byte("foo")
|
||||
ci := pgtype.NewConnInfo()
|
||||
@@ -74,3 +77,53 @@ func TestConnInfoScanUnknownOID(t *testing.T) {
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, []byte("foo"), []byte(rb))
|
||||
}
|
||||
|
||||
type pgCustomType struct {
|
||||
a string
|
||||
b string
|
||||
}
|
||||
|
||||
func (ct *pgCustomType) DecodeText(ci *pgtype.ConnInfo, buf []byte) error {
|
||||
// This is not a complete parser for the text format of composite types. This is just for test purposes.
|
||||
if buf == nil {
|
||||
return errors.New("cannot parse null")
|
||||
}
|
||||
|
||||
if len(buf) < 2 {
|
||||
return errors.New("invalid text format")
|
||||
}
|
||||
|
||||
parts := bytes.Split(buf[1:len(buf)-1], []byte(","))
|
||||
if len(parts) != 2 {
|
||||
return errors.New("wrong number of parts")
|
||||
}
|
||||
|
||||
ct.a = string(parts[0])
|
||||
ct.b = string(parts[1])
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestConnInfoScanUnknownOIDToCustomType(t *testing.T) {
|
||||
unknownOID := uint32(999999)
|
||||
ci := pgtype.NewConnInfo()
|
||||
|
||||
var ct pgCustomType
|
||||
err := ci.Scan(unknownOID, pgx.TextFormatCode, []byte("(foo,bar)"), &ct)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "foo", ct.a)
|
||||
assert.Equal(t, "bar", ct.b)
|
||||
|
||||
// Scan value into pointer to custom type
|
||||
var pCt *pgCustomType
|
||||
err = ci.Scan(unknownOID, pgx.TextFormatCode, []byte("(foo,bar)"), &pCt)
|
||||
assert.NoError(t, err)
|
||||
require.NotNil(t, pCt)
|
||||
assert.Equal(t, "foo", pCt.a)
|
||||
assert.Equal(t, "bar", pCt.b)
|
||||
|
||||
// Scan null into pointer to custom type
|
||||
err = ci.Scan(unknownOID, pgx.TextFormatCode, nil, &pCt)
|
||||
assert.NoError(t, err)
|
||||
assert.Nil(t, pCt)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user