...

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

Documentation: github.com/jackc/pgtype

     1  package pgtype_test
     2  
     3  import (
     4  	"encoding/json"
     5  	"reflect"
     6  	"testing"
     7  
     8  	"github.com/jackc/pgtype"
     9  	"github.com/jackc/pgtype/testutil"
    10  )
    11  
    12  func TestJSONBArrayTranscode(t *testing.T) {
    13  	testutil.TestSuccessfulTranscode(t, "jsonb[]", []interface{}{
    14  		&pgtype.JSONBArray{
    15  			Elements:   nil,
    16  			Dimensions: nil,
    17  			Status:     pgtype.Present,
    18  		},
    19  		&pgtype.JSONBArray{
    20  			Elements: []pgtype.JSONB{
    21  				{Bytes: []byte(`"foo"`), Status: pgtype.Present},
    22  				{Status: pgtype.Null},
    23  			},
    24  			Dimensions: []pgtype.ArrayDimension{{Length: 2, LowerBound: 1}},
    25  			Status:     pgtype.Present,
    26  		},
    27  		&pgtype.JSONBArray{Status: pgtype.Null},
    28  		&pgtype.JSONBArray{
    29  			Elements: []pgtype.JSONB{
    30  				{Bytes: []byte(`"foo"`), Status: pgtype.Present},
    31  				{Bytes: []byte("null"), Status: pgtype.Present},
    32  				{Bytes: []byte("42"), Status: pgtype.Present},
    33  			},
    34  			Dimensions: []pgtype.ArrayDimension{{Length: 3, LowerBound: 1}},
    35  			Status:     pgtype.Present,
    36  		},
    37  	})
    38  }
    39  
    40  func TestJSONBArraySet(t *testing.T) {
    41  	successfulTests := []struct {
    42  		source interface{}
    43  		result pgtype.JSONBArray
    44  	}{
    45  		{source: []string{"{}"}, result: pgtype.JSONBArray{
    46  			Elements:   []pgtype.JSONB{pgtype.JSONB{Bytes: []byte("{}"), Status: pgtype.Present}},
    47  			Dimensions: []pgtype.ArrayDimension{pgtype.ArrayDimension{Length: 1, LowerBound: 1}},
    48  			Status:     pgtype.Present,
    49  		}},
    50  		{source: [][]byte{[]byte("{}")}, result: pgtype.JSONBArray{
    51  			Elements:   []pgtype.JSONB{pgtype.JSONB{Bytes: []byte("{}"), Status: pgtype.Present}},
    52  			Dimensions: []pgtype.ArrayDimension{pgtype.ArrayDimension{Length: 1, LowerBound: 1}},
    53  			Status:     pgtype.Present,
    54  		}},
    55  		{source: [][]byte{[]byte(`{"foo":1}`), []byte(`{"bar":2}`)}, result: pgtype.JSONBArray{
    56  			Elements:   []pgtype.JSONB{pgtype.JSONB{Bytes: []byte(`{"foo":1}`), Status: pgtype.Present}, pgtype.JSONB{Bytes: []byte(`{"bar":2}`), Status: pgtype.Present}},
    57  			Dimensions: []pgtype.ArrayDimension{pgtype.ArrayDimension{Length: 2, LowerBound: 1}},
    58  			Status:     pgtype.Present,
    59  		}},
    60  		{source: []json.RawMessage{json.RawMessage(`{"foo":1}`), json.RawMessage(`{"bar":2}`)}, result: pgtype.JSONBArray{
    61  			Elements:   []pgtype.JSONB{pgtype.JSONB{Bytes: []byte(`{"foo":1}`), Status: pgtype.Present}, pgtype.JSONB{Bytes: []byte(`{"bar":2}`), Status: pgtype.Present}},
    62  			Dimensions: []pgtype.ArrayDimension{pgtype.ArrayDimension{Length: 2, LowerBound: 1}},
    63  			Status:     pgtype.Present,
    64  		}},
    65  		{source: []json.RawMessage{json.RawMessage(`{"foo":12}`), json.RawMessage(`{"bar":2}`)}, result: pgtype.JSONBArray{
    66  			Elements:   []pgtype.JSONB{pgtype.JSONB{Bytes: []byte(`{"foo":12}`), Status: pgtype.Present}, pgtype.JSONB{Bytes: []byte(`{"bar":2}`), Status: pgtype.Present}},
    67  			Dimensions: []pgtype.ArrayDimension{pgtype.ArrayDimension{Length: 2, LowerBound: 1}},
    68  			Status:     pgtype.Present,
    69  		}},
    70  		{source: []json.RawMessage{json.RawMessage(`{"foo":1}`), json.RawMessage(`{"bar":{"x":2}}`)}, result: pgtype.JSONBArray{
    71  			Elements:   []pgtype.JSONB{pgtype.JSONB{Bytes: []byte(`{"foo":1}`), Status: pgtype.Present}, pgtype.JSONB{Bytes: []byte(`{"bar":{"x":2}}`), Status: pgtype.Present}},
    72  			Dimensions: []pgtype.ArrayDimension{pgtype.ArrayDimension{Length: 2, LowerBound: 1}},
    73  			Status:     pgtype.Present,
    74  		}},
    75  	}
    76  
    77  	for i, tt := range successfulTests {
    78  		var d pgtype.JSONBArray
    79  		err := d.Set(tt.source)
    80  		if err != nil {
    81  			t.Errorf("%d: %v", i, err)
    82  		}
    83  
    84  		if !reflect.DeepEqual(d, tt.result) {
    85  			t.Errorf("%d: expected %+v to convert to %+v, but it was %+v", i, tt.source, tt.result, d)
    86  		}
    87  	}
    88  }
    89  

View as plain text