...

Source file src/github.com/jackc/pgtype/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/stretchr/testify/require"
     9  )
    10  
    11  func TestParseUntypedTextArray(t *testing.T) {
    12  	tests := []struct {
    13  		source string
    14  		result pgtype.UntypedTextArray
    15  	}{
    16  		{
    17  			source: "{}",
    18  			result: pgtype.UntypedTextArray{
    19  				Elements:   nil,
    20  				Quoted:     nil,
    21  				Dimensions: nil,
    22  			},
    23  		},
    24  		{
    25  			source: "{1}",
    26  			result: pgtype.UntypedTextArray{
    27  				Elements:   []string{"1"},
    28  				Quoted:     []bool{false},
    29  				Dimensions: []pgtype.ArrayDimension{{Length: 1, LowerBound: 1}},
    30  			},
    31  		},
    32  		{
    33  			source: "{a,b}",
    34  			result: pgtype.UntypedTextArray{
    35  				Elements:   []string{"a", "b"},
    36  				Quoted:     []bool{false, false},
    37  				Dimensions: []pgtype.ArrayDimension{{Length: 2, LowerBound: 1}},
    38  			},
    39  		},
    40  		{
    41  			source: `{"NULL"}`,
    42  			result: pgtype.UntypedTextArray{
    43  				Elements:   []string{"NULL"},
    44  				Quoted:     []bool{true},
    45  				Dimensions: []pgtype.ArrayDimension{{Length: 1, LowerBound: 1}},
    46  			},
    47  		},
    48  		{
    49  			source: `{""}`,
    50  			result: pgtype.UntypedTextArray{
    51  				Elements:   []string{""},
    52  				Quoted:     []bool{true},
    53  				Dimensions: []pgtype.ArrayDimension{{Length: 1, LowerBound: 1}},
    54  			},
    55  		},
    56  		{
    57  			source: `{"He said, \"Hello.\""}`,
    58  			result: pgtype.UntypedTextArray{
    59  				Elements:   []string{`He said, "Hello."`},
    60  				Quoted:     []bool{true},
    61  				Dimensions: []pgtype.ArrayDimension{{Length: 1, LowerBound: 1}},
    62  			},
    63  		},
    64  		{
    65  			source: "{{a,b},{c,d},{e,f}}",
    66  			result: pgtype.UntypedTextArray{
    67  				Elements:   []string{"a", "b", "c", "d", "e", "f"},
    68  				Quoted:     []bool{false, false, false, false, false, false},
    69  				Dimensions: []pgtype.ArrayDimension{{Length: 3, LowerBound: 1}, {Length: 2, LowerBound: 1}},
    70  			},
    71  		},
    72  		{
    73  			source: "{{{a,b},{c,d},{e,f}},{{a,b},{c,d},{e,f}}}",
    74  			result: pgtype.UntypedTextArray{
    75  				Elements: []string{"a", "b", "c", "d", "e", "f", "a", "b", "c", "d", "e", "f"},
    76  				Quoted:   []bool{false, false, false, false, false, false, false, false, false, false, false, false},
    77  				Dimensions: []pgtype.ArrayDimension{
    78  					{Length: 2, LowerBound: 1},
    79  					{Length: 3, LowerBound: 1},
    80  					{Length: 2, LowerBound: 1},
    81  				},
    82  			},
    83  		},
    84  		{
    85  			source: "[4:4]={1}",
    86  			result: pgtype.UntypedTextArray{
    87  				Elements:   []string{"1"},
    88  				Quoted:     []bool{false},
    89  				Dimensions: []pgtype.ArrayDimension{{Length: 1, LowerBound: 4}},
    90  			},
    91  		},
    92  		{
    93  			source: "[4:5][2:3]={{a,b},{c,d}}",
    94  			result: pgtype.UntypedTextArray{
    95  				Elements: []string{"a", "b", "c", "d"},
    96  				Quoted:   []bool{false, false, false, false},
    97  				Dimensions: []pgtype.ArrayDimension{
    98  					{Length: 2, LowerBound: 4},
    99  					{Length: 2, LowerBound: 2},
   100  				},
   101  			},
   102  		},
   103  		{
   104  			source: "[-4:-2]={1,2,3}",
   105  			result: pgtype.UntypedTextArray{
   106  				Elements:   []string{"1", "2", "3"},
   107  				Quoted:     []bool{false, false, false},
   108  				Dimensions: []pgtype.ArrayDimension{{Length: 3, LowerBound: -4}},
   109  			},
   110  		},
   111  	}
   112  
   113  	for i, tt := range tests {
   114  		r, err := pgtype.ParseUntypedTextArray(tt.source)
   115  		if err != nil {
   116  			t.Errorf("%d: %v", i, err)
   117  			continue
   118  		}
   119  
   120  		if !reflect.DeepEqual(*r, tt.result) {
   121  			t.Errorf("%d: expected %+v to be parsed to %+v, but it was %+v", i, tt.source, tt.result, *r)
   122  		}
   123  	}
   124  }
   125  
   126  // https://github.com/jackc/pgx/issues/881
   127  func TestArrayAssignToEmptyToNonSlice(t *testing.T) {
   128  	var a pgtype.Int4Array
   129  	err := a.Set([]int32{})
   130  	require.NoError(t, err)
   131  
   132  	var iface interface{}
   133  	err = a.AssignTo(&iface)
   134  	require.EqualError(t, err, "cannot assign *pgtype.Int4Array to *interface {}")
   135  }
   136  

View as plain text