mirror of
https://github.com/jackc/pgx.git
synced 2025-04-28 14:07:51 +00:00
Set having the capability to assign an object of the same type was inconsistently implemented. Some places it was not implemented at all, some places it was a shallow copy, some places a deep copy. Given that it doesn't seem likely to ever be used, and if it is needed it is easy enough to do outside of the library this code has been removed.
73 lines
2.3 KiB
Go
73 lines
2.3 KiB
Go
package pgtype_test
|
|
|
|
import (
|
|
"reflect"
|
|
"testing"
|
|
|
|
"github.com/jackc/pgx/pgtype"
|
|
)
|
|
|
|
func TestByteaTranscode(t *testing.T) {
|
|
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},
|
|
})
|
|
}
|
|
|
|
func TestByteaSet(t *testing.T) {
|
|
successfulTests := []struct {
|
|
source interface{}
|
|
result pgtype.Bytea
|
|
}{
|
|
{source: []byte{1, 2, 3}, result: pgtype.Bytea{Bytes: []byte{1, 2, 3}, Status: pgtype.Present}},
|
|
{source: []byte{}, result: pgtype.Bytea{Bytes: []byte{}, Status: pgtype.Present}},
|
|
{source: []byte(nil), result: pgtype.Bytea{Status: pgtype.Null}},
|
|
{source: _byteSlice{1, 2, 3}, result: pgtype.Bytea{Bytes: []byte{1, 2, 3}, Status: pgtype.Present}},
|
|
{source: _byteSlice(nil), result: pgtype.Bytea{Status: pgtype.Null}},
|
|
}
|
|
|
|
for i, tt := range successfulTests {
|
|
var r pgtype.Bytea
|
|
err := r.Set(tt.source)
|
|
if err != nil {
|
|
t.Errorf("%d: %v", i, err)
|
|
}
|
|
|
|
if !reflect.DeepEqual(r, tt.result) {
|
|
t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestByteaAssignTo(t *testing.T) {
|
|
var buf []byte
|
|
var _buf _byteSlice
|
|
var pbuf *[]byte
|
|
var _pbuf *_byteSlice
|
|
|
|
simpleTests := []struct {
|
|
src pgtype.Bytea
|
|
dst interface{}
|
|
expected interface{}
|
|
}{
|
|
{src: pgtype.Bytea{Bytes: []byte{1, 2, 3}, Status: pgtype.Present}, dst: &buf, expected: []byte{1, 2, 3}},
|
|
{src: pgtype.Bytea{Bytes: []byte{1, 2, 3}, Status: pgtype.Present}, dst: &_buf, expected: _byteSlice{1, 2, 3}},
|
|
{src: pgtype.Bytea{Bytes: []byte{1, 2, 3}, Status: pgtype.Present}, dst: &pbuf, expected: &[]byte{1, 2, 3}},
|
|
{src: pgtype.Bytea{Bytes: []byte{1, 2, 3}, Status: pgtype.Present}, dst: &_pbuf, expected: &_byteSlice{1, 2, 3}},
|
|
{src: pgtype.Bytea{Status: pgtype.Null}, dst: &pbuf, expected: ((*[]byte)(nil))},
|
|
{src: pgtype.Bytea{Status: pgtype.Null}, dst: &_pbuf, expected: ((*_byteSlice)(nil))},
|
|
}
|
|
|
|
for i, tt := range simpleTests {
|
|
err := tt.src.AssignTo(tt.dst)
|
|
if err != nil {
|
|
t.Errorf("%d: %v", i, err)
|
|
}
|
|
|
|
if dst := reflect.ValueOf(tt.dst).Elem().Interface(); !reflect.DeepEqual(dst, tt.expected) {
|
|
t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst)
|
|
}
|
|
}
|
|
}
|