...

Source file src/github.com/jackc/pgx/v5/pgtype/array_test.go

Documentation: github.com/jackc/pgx/v5/pgtype

     1  package pgtype
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  func TestParseUntypedTextArray(t *testing.T) {
     9  	tests := []struct {
    10  		source string
    11  		result untypedTextArray
    12  	}{
    13  		{
    14  			source: "{}",
    15  			result: untypedTextArray{
    16  				Elements:   []string{},
    17  				Quoted:     []bool{},
    18  				Dimensions: []ArrayDimension{},
    19  			},
    20  		},
    21  		{
    22  			source: "{1}",
    23  			result: untypedTextArray{
    24  				Elements:   []string{"1"},
    25  				Quoted:     []bool{false},
    26  				Dimensions: []ArrayDimension{{Length: 1, LowerBound: 1}},
    27  			},
    28  		},
    29  		{
    30  			source: "{a,b}",
    31  			result: untypedTextArray{
    32  				Elements:   []string{"a", "b"},
    33  				Quoted:     []bool{false, false},
    34  				Dimensions: []ArrayDimension{{Length: 2, LowerBound: 1}},
    35  			},
    36  		},
    37  		{
    38  			source: `{"NULL"}`,
    39  			result: untypedTextArray{
    40  				Elements:   []string{"NULL"},
    41  				Quoted:     []bool{true},
    42  				Dimensions: []ArrayDimension{{Length: 1, LowerBound: 1}},
    43  			},
    44  		},
    45  		{
    46  			source: `{""}`,
    47  			result: untypedTextArray{
    48  				Elements:   []string{""},
    49  				Quoted:     []bool{true},
    50  				Dimensions: []ArrayDimension{{Length: 1, LowerBound: 1}},
    51  			},
    52  		},
    53  		{
    54  			source: `{"He said, \"Hello.\""}`,
    55  			result: untypedTextArray{
    56  				Elements:   []string{`He said, "Hello."`},
    57  				Quoted:     []bool{true},
    58  				Dimensions: []ArrayDimension{{Length: 1, LowerBound: 1}},
    59  			},
    60  		},
    61  		{
    62  			source: "{{a,b},{c,d},{e,f}}",
    63  			result: untypedTextArray{
    64  				Elements:   []string{"a", "b", "c", "d", "e", "f"},
    65  				Quoted:     []bool{false, false, false, false, false, false},
    66  				Dimensions: []ArrayDimension{{Length: 3, LowerBound: 1}, {Length: 2, LowerBound: 1}},
    67  			},
    68  		},
    69  		{
    70  			source: "{{{a,b},{c,d},{e,f}},{{a,b},{c,d},{e,f}}}",
    71  			result: untypedTextArray{
    72  				Elements: []string{"a", "b", "c", "d", "e", "f", "a", "b", "c", "d", "e", "f"},
    73  				Quoted:   []bool{false, false, false, false, false, false, false, false, false, false, false, false},
    74  				Dimensions: []ArrayDimension{
    75  					{Length: 2, LowerBound: 1},
    76  					{Length: 3, LowerBound: 1},
    77  					{Length: 2, LowerBound: 1},
    78  				},
    79  			},
    80  		},
    81  		{
    82  			source: "[4:4]={1}",
    83  			result: untypedTextArray{
    84  				Elements:   []string{"1"},
    85  				Quoted:     []bool{false},
    86  				Dimensions: []ArrayDimension{{Length: 1, LowerBound: 4}},
    87  			},
    88  		},
    89  		{
    90  			source: "[4:5][2:3]={{a,b},{c,d}}",
    91  			result: untypedTextArray{
    92  				Elements: []string{"a", "b", "c", "d"},
    93  				Quoted:   []bool{false, false, false, false},
    94  				Dimensions: []ArrayDimension{
    95  					{Length: 2, LowerBound: 4},
    96  					{Length: 2, LowerBound: 2},
    97  				},
    98  			},
    99  		},
   100  		{
   101  			source: "[-4:-2]={1,2,3}",
   102  			result: untypedTextArray{
   103  				Elements:   []string{"1", "2", "3"},
   104  				Quoted:     []bool{false, false, false},
   105  				Dimensions: []ArrayDimension{{Length: 3, LowerBound: -4}},
   106  			},
   107  		},
   108  	}
   109  
   110  	for i, tt := range tests {
   111  		r, err := parseUntypedTextArray(tt.source)
   112  		if err != nil {
   113  			t.Errorf("%d: %v", i, err)
   114  			continue
   115  		}
   116  
   117  		if !reflect.DeepEqual(*r, tt.result) {
   118  			t.Errorf("%d: expected %+v to be parsed to %+v, but it was %+v", i, tt.source, tt.result, *r)
   119  		}
   120  	}
   121  }
   122  

View as plain text