From d5807f01ed61cc92a4914beab6a031bc7c553083 Mon Sep 17 00:00:00 2001 From: Jack Christensen Date: Tue, 12 Jul 2022 06:57:56 -0500 Subject: [PATCH] Restore test from v4 --- pgtype/pgtype_test.go | 51 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/pgtype/pgtype_test.go b/pgtype/pgtype_test.go index e2465afc..b1b7e951 100644 --- a/pgtype/pgtype_test.go +++ b/pgtype/pgtype_test.go @@ -203,6 +203,57 @@ func TestTypeMapScanUnknownOIDIntoSQLScanner(t *testing.T) { assert.False(t, s.Valid) } +type pgCustomInt int64 + +func (ci *pgCustomInt) Scan(src interface{}) error { + *ci = pgCustomInt(src.(int64)) + return nil +} + +func TestScanPlanBinaryInt32ScanScanner(t *testing.T) { + ci := pgtype.NewMap() + src := []byte{0, 42} + var v pgCustomInt + + plan := ci.PlanScan(pgtype.Int2OID, pgtype.BinaryFormatCode, &v) + err := plan.Scan(src, &v) + require.NoError(t, err) + require.EqualValues(t, 42, v) + + ptr := new(pgCustomInt) + plan = ci.PlanScan(pgtype.Int2OID, pgtype.BinaryFormatCode, &ptr) + err = plan.Scan(src, &ptr) + require.NoError(t, err) + require.EqualValues(t, 42, *ptr) + + ptr = new(pgCustomInt) + err = plan.Scan(nil, &ptr) + require.NoError(t, err) + assert.Nil(t, ptr) + + ptr = nil + plan = ci.PlanScan(pgtype.Int2OID, pgtype.BinaryFormatCode, &ptr) + err = plan.Scan(src, &ptr) + require.NoError(t, err) + require.EqualValues(t, 42, *ptr) + + ptr = nil + plan = ci.PlanScan(pgtype.Int2OID, pgtype.BinaryFormatCode, &ptr) + err = plan.Scan(nil, &ptr) + require.NoError(t, err) + assert.Nil(t, ptr) +} + +// Test for https://github.com/jackc/pgtype/issues/164 +func TestScanPlanInterface(t *testing.T) { + ci := pgtype.NewMap() + src := []byte{0, 42} + var v interface{} + plan := ci.PlanScan(pgtype.Int2OID, pgtype.BinaryFormatCode, v) + err := plan.Scan(src, v) + assert.Error(t, err) +} + func BenchmarkTypeMapScanInt4IntoBinaryDecoder(b *testing.B) { m := pgtype.NewMap() src := []byte{0, 0, 0, 42}