...

Source file src/github.com/jackc/pgtype/bytea_array_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 TestByteaArrayTranscode(t *testing.T) {
    12  	testutil.TestSuccessfulTranscode(t, "bytea[]", []interface{}{
    13  		&pgtype.ByteaArray{
    14  			Elements:   nil,
    15  			Dimensions: nil,
    16  			Status:     pgtype.Present,
    17  		},
    18  		&pgtype.ByteaArray{
    19  			Elements: []pgtype.Bytea{
    20  				{Bytes: []byte{1, 2, 3}, Status: pgtype.Present},
    21  				{Status: pgtype.Null},
    22  			},
    23  			Dimensions: []pgtype.ArrayDimension{{Length: 2, LowerBound: 1}},
    24  			Status:     pgtype.Present,
    25  		},
    26  		&pgtype.ByteaArray{Status: pgtype.Null},
    27  		&pgtype.ByteaArray{
    28  			Elements: []pgtype.Bytea{
    29  				{Bytes: []byte{1, 2, 3}, Status: pgtype.Present},
    30  				{Bytes: []byte{1, 2, 3}, Status: pgtype.Present},
    31  				{Bytes: []byte{}, Status: pgtype.Present},
    32  				{Bytes: []byte{1, 2, 3}, Status: pgtype.Present},
    33  				{Status: pgtype.Null},
    34  				{Bytes: []byte{1}, Status: pgtype.Present},
    35  			},
    36  			Dimensions: []pgtype.ArrayDimension{{Length: 3, LowerBound: 1}, {Length: 2, LowerBound: 1}},
    37  			Status:     pgtype.Present,
    38  		},
    39  		&pgtype.ByteaArray{
    40  			Elements: []pgtype.Bytea{
    41  				{Bytes: []byte{1, 2, 3}, Status: pgtype.Present},
    42  				{Bytes: []byte{}, Status: pgtype.Present},
    43  				{Bytes: []byte{1, 2, 3}, Status: pgtype.Present},
    44  				{Bytes: []byte{1}, Status: pgtype.Present},
    45  			},
    46  			Dimensions: []pgtype.ArrayDimension{
    47  				{Length: 2, LowerBound: 4},
    48  				{Length: 2, LowerBound: 2},
    49  			},
    50  			Status: pgtype.Present,
    51  		},
    52  	})
    53  }
    54  
    55  func TestByteaArraySet(t *testing.T) {
    56  	successfulTests := []struct {
    57  		source interface{}
    58  		result pgtype.ByteaArray
    59  	}{
    60  		{
    61  			source: [][]byte{{1, 2, 3}},
    62  			result: pgtype.ByteaArray{
    63  				Elements:   []pgtype.Bytea{{Bytes: []byte{1, 2, 3}, Status: pgtype.Present}},
    64  				Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}},
    65  				Status:     pgtype.Present},
    66  		},
    67  		{
    68  			source: (([][]byte)(nil)),
    69  			result: pgtype.ByteaArray{Status: pgtype.Null},
    70  		},
    71  		{
    72  			source: [][][]byte{{{1}}, {{2}}},
    73  			result: pgtype.ByteaArray{
    74  				Elements:   []pgtype.Bytea{{Bytes: []byte{1}, Status: pgtype.Present}, {Bytes: []byte{2}, Status: pgtype.Present}},
    75  				Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}},
    76  				Status:     pgtype.Present},
    77  		},
    78  		{
    79  			source: [][][][][]byte{{{{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}}}, {{{{10, 11, 12}, {13, 14, 15}, {16, 17, 18}}}}},
    80  			result: pgtype.ByteaArray{
    81  				Elements: []pgtype.Bytea{
    82  					{Bytes: []byte{1, 2, 3}, Status: pgtype.Present},
    83  					{Bytes: []byte{4, 5, 6}, Status: pgtype.Present},
    84  					{Bytes: []byte{7, 8, 9}, Status: pgtype.Present},
    85  					{Bytes: []byte{10, 11, 12}, Status: pgtype.Present},
    86  					{Bytes: []byte{13, 14, 15}, Status: pgtype.Present},
    87  					{Bytes: []byte{16, 17, 18}, Status: pgtype.Present}},
    88  				Dimensions: []pgtype.ArrayDimension{
    89  					{LowerBound: 1, Length: 2},
    90  					{LowerBound: 1, Length: 1},
    91  					{LowerBound: 1, Length: 1},
    92  					{LowerBound: 1, Length: 3}},
    93  				Status: pgtype.Present},
    94  		},
    95  		{
    96  			source: [2][1][]byte{{{1}}, {{2}}},
    97  			result: pgtype.ByteaArray{
    98  				Elements:   []pgtype.Bytea{{Bytes: []byte{1}, Status: pgtype.Present}, {Bytes: []byte{2}, Status: pgtype.Present}},
    99  				Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}},
   100  				Status:     pgtype.Present},
   101  		},
   102  		{
   103  			source: [2][1][1][3][]byte{{{{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}}}, {{{{10, 11, 12}, {13, 14, 15}, {16, 17, 18}}}}},
   104  			result: pgtype.ByteaArray{
   105  				Elements: []pgtype.Bytea{
   106  					{Bytes: []byte{1, 2, 3}, Status: pgtype.Present},
   107  					{Bytes: []byte{4, 5, 6}, Status: pgtype.Present},
   108  					{Bytes: []byte{7, 8, 9}, Status: pgtype.Present},
   109  					{Bytes: []byte{10, 11, 12}, Status: pgtype.Present},
   110  					{Bytes: []byte{13, 14, 15}, Status: pgtype.Present},
   111  					{Bytes: []byte{16, 17, 18}, Status: pgtype.Present}},
   112  				Dimensions: []pgtype.ArrayDimension{
   113  					{LowerBound: 1, Length: 2},
   114  					{LowerBound: 1, Length: 1},
   115  					{LowerBound: 1, Length: 1},
   116  					{LowerBound: 1, Length: 3}},
   117  				Status: pgtype.Present},
   118  		},
   119  	}
   120  
   121  	for i, tt := range successfulTests {
   122  		var r pgtype.ByteaArray
   123  		err := r.Set(tt.source)
   124  		if err != nil {
   125  			t.Errorf("%d: %v", i, err)
   126  		}
   127  
   128  		if !reflect.DeepEqual(r, tt.result) {
   129  			t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r)
   130  		}
   131  	}
   132  }
   133  
   134  func TestByteaArrayAssignTo(t *testing.T) {
   135  	var byteByteSlice [][]byte
   136  	var byteByteSliceDim2 [][][]byte
   137  	var byteByteSliceDim4 [][][][][]byte
   138  	var byteByteArraySliceDim2 [2][1][]byte
   139  	var byteByteArraySliceDim4 [2][1][1][3][]byte
   140  
   141  	simpleTests := []struct {
   142  		src      pgtype.ByteaArray
   143  		dst      interface{}
   144  		expected interface{}
   145  	}{
   146  		{
   147  			src: pgtype.ByteaArray{
   148  				Elements:   []pgtype.Bytea{{Bytes: []byte{1, 2, 3}, Status: pgtype.Present}},
   149  				Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}},
   150  				Status:     pgtype.Present,
   151  			},
   152  			dst:      &byteByteSlice,
   153  			expected: [][]byte{{1, 2, 3}},
   154  		},
   155  		{
   156  			src:      pgtype.ByteaArray{Status: pgtype.Null},
   157  			dst:      &byteByteSlice,
   158  			expected: (([][]byte)(nil)),
   159  		},
   160  		{
   161  			src:      pgtype.ByteaArray{Status: pgtype.Present},
   162  			dst:      &byteByteSlice,
   163  			expected: [][]byte{},
   164  		},
   165  		{
   166  			src: pgtype.ByteaArray{
   167  				Elements:   []pgtype.Bytea{{Bytes: []byte{1}, Status: pgtype.Present}, {Bytes: []byte{2}, Status: pgtype.Present}},
   168  				Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}},
   169  				Status:     pgtype.Present},
   170  			dst:      &byteByteSliceDim2,
   171  			expected: [][][]byte{{{1}}, {{2}}},
   172  		},
   173  		{
   174  			src: pgtype.ByteaArray{
   175  				Elements: []pgtype.Bytea{
   176  					{Bytes: []byte{1, 2, 3}, Status: pgtype.Present},
   177  					{Bytes: []byte{4, 5, 6}, Status: pgtype.Present},
   178  					{Bytes: []byte{7, 8, 9}, Status: pgtype.Present},
   179  					{Bytes: []byte{10, 11, 12}, Status: pgtype.Present},
   180  					{Bytes: []byte{13, 14, 15}, Status: pgtype.Present},
   181  					{Bytes: []byte{16, 17, 18}, Status: pgtype.Present}},
   182  				Dimensions: []pgtype.ArrayDimension{
   183  					{LowerBound: 1, Length: 2},
   184  					{LowerBound: 1, Length: 1},
   185  					{LowerBound: 1, Length: 1},
   186  					{LowerBound: 1, Length: 3}},
   187  				Status: pgtype.Present},
   188  			dst:      &byteByteSliceDim4,
   189  			expected: [][][][][]byte{{{{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}}}, {{{{10, 11, 12}, {13, 14, 15}, {16, 17, 18}}}}},
   190  		},
   191  		{
   192  			src: pgtype.ByteaArray{
   193  				Elements:   []pgtype.Bytea{{Bytes: []byte{1}, Status: pgtype.Present}, {Bytes: []byte{2}, Status: pgtype.Present}},
   194  				Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}},
   195  				Status:     pgtype.Present},
   196  			dst:      &byteByteArraySliceDim2,
   197  			expected: [2][1][]byte{{{1}}, {{2}}},
   198  		},
   199  		{
   200  			src: pgtype.ByteaArray{
   201  				Elements: []pgtype.Bytea{
   202  					{Bytes: []byte{1, 2, 3}, Status: pgtype.Present},
   203  					{Bytes: []byte{4, 5, 6}, Status: pgtype.Present},
   204  					{Bytes: []byte{7, 8, 9}, Status: pgtype.Present},
   205  					{Bytes: []byte{10, 11, 12}, Status: pgtype.Present},
   206  					{Bytes: []byte{13, 14, 15}, Status: pgtype.Present},
   207  					{Bytes: []byte{16, 17, 18}, Status: pgtype.Present}},
   208  				Dimensions: []pgtype.ArrayDimension{
   209  					{LowerBound: 1, Length: 2},
   210  					{LowerBound: 1, Length: 1},
   211  					{LowerBound: 1, Length: 1},
   212  					{LowerBound: 1, Length: 3}},
   213  				Status: pgtype.Present},
   214  			dst:      &byteByteArraySliceDim4,
   215  			expected: [2][1][1][3][]byte{{{{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}}}, {{{{10, 11, 12}, {13, 14, 15}, {16, 17, 18}}}}},
   216  		},
   217  	}
   218  
   219  	for i, tt := range simpleTests {
   220  		err := tt.src.AssignTo(tt.dst)
   221  		if err != nil {
   222  			t.Errorf("%d: %v", i, err)
   223  		}
   224  
   225  		if dst := reflect.ValueOf(tt.dst).Elem().Interface(); !reflect.DeepEqual(dst, tt.expected) {
   226  			t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst)
   227  		}
   228  	}
   229  }
   230  

View as plain text