...

Source file src/github.com/jackc/pgtype/bytea_test.go

Documentation: github.com/jackc/pgtype

     1  package pgtype_test
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/jackc/pgtype"
     8  	"github.com/jackc/pgtype/testutil"
     9  )
    10  
    11  func TestByteaTranscode(t *testing.T) {
    12  	testutil.TestSuccessfulTranscode(t, "bytea", []interface{}{
    13  		&pgtype.Bytea{Bytes: []byte{1, 2, 3}, Status: pgtype.Present},
    14  		&pgtype.Bytea{Bytes: []byte{}, Status: pgtype.Present},
    15  		&pgtype.Bytea{Bytes: nil, Status: pgtype.Null},
    16  	})
    17  }
    18  
    19  func TestByteaSet(t *testing.T) {
    20  	successfulTests := []struct {
    21  		source interface{}
    22  		result pgtype.Bytea
    23  	}{
    24  		{source: []byte{1, 2, 3}, result: pgtype.Bytea{Bytes: []byte{1, 2, 3}, Status: pgtype.Present}},
    25  		{source: []byte{}, result: pgtype.Bytea{Bytes: []byte{}, Status: pgtype.Present}},
    26  		{source: []byte(nil), result: pgtype.Bytea{Status: pgtype.Null}},
    27  		{source: _byteSlice{1, 2, 3}, result: pgtype.Bytea{Bytes: []byte{1, 2, 3}, Status: pgtype.Present}},
    28  		{source: _byteSlice(nil), result: pgtype.Bytea{Status: pgtype.Null}},
    29  	}
    30  
    31  	for i, tt := range successfulTests {
    32  		var r pgtype.Bytea
    33  		err := r.Set(tt.source)
    34  		if err != nil {
    35  			t.Errorf("%d: %v", i, err)
    36  		}
    37  
    38  		if !reflect.DeepEqual(r, tt.result) {
    39  			t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r)
    40  		}
    41  	}
    42  }
    43  
    44  func TestByteaAssignTo(t *testing.T) {
    45  	var buf []byte
    46  	var _buf _byteSlice
    47  	var pbuf *[]byte
    48  	var _pbuf *_byteSlice
    49  
    50  	simpleTests := []struct {
    51  		src      pgtype.Bytea
    52  		dst      interface{}
    53  		expected interface{}
    54  	}{
    55  		{src: pgtype.Bytea{Bytes: []byte{1, 2, 3}, Status: pgtype.Present}, dst: &buf, expected: []byte{1, 2, 3}},
    56  		{src: pgtype.Bytea{Bytes: []byte{1, 2, 3}, Status: pgtype.Present}, dst: &_buf, expected: _byteSlice{1, 2, 3}},
    57  		{src: pgtype.Bytea{Bytes: []byte{1, 2, 3}, Status: pgtype.Present}, dst: &pbuf, expected: &[]byte{1, 2, 3}},
    58  		{src: pgtype.Bytea{Bytes: []byte{1, 2, 3}, Status: pgtype.Present}, dst: &_pbuf, expected: &_byteSlice{1, 2, 3}},
    59  		{src: pgtype.Bytea{Status: pgtype.Null}, dst: &pbuf, expected: ((*[]byte)(nil))},
    60  		{src: pgtype.Bytea{Status: pgtype.Null}, dst: &_pbuf, expected: ((*_byteSlice)(nil))},
    61  	}
    62  
    63  	for i, tt := range simpleTests {
    64  		err := tt.src.AssignTo(tt.dst)
    65  		if err != nil {
    66  			t.Errorf("%d: %v", i, err)
    67  		}
    68  
    69  		if dst := reflect.ValueOf(tt.dst).Elem().Interface(); !reflect.DeepEqual(dst, tt.expected) {
    70  			t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst)
    71  		}
    72  	}
    73  }
    74  

View as plain text