...

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

Documentation: github.com/jackc/pgtype

     1  package pgtype_test
     2  
     3  import (
     4  	"bytes"
     5  	"reflect"
     6  	"testing"
     7  
     8  	"github.com/jackc/pgtype"
     9  	"github.com/jackc/pgtype/testutil"
    10  )
    11  
    12  func TestJSONBTranscode(t *testing.T) {
    13  	conn := testutil.MustConnectPgx(t)
    14  	defer testutil.MustCloseContext(t, conn)
    15  	if _, ok := conn.ConnInfo().DataTypeForName("jsonb"); !ok {
    16  		t.Skip("Skipping due to no jsonb type")
    17  	}
    18  
    19  	testutil.TestSuccessfulTranscode(t, "jsonb", []interface{}{
    20  		&pgtype.JSONB{Bytes: []byte("{}"), Status: pgtype.Present},
    21  		&pgtype.JSONB{Bytes: []byte("null"), Status: pgtype.Present},
    22  		&pgtype.JSONB{Bytes: []byte("42"), Status: pgtype.Present},
    23  		&pgtype.JSONB{Bytes: []byte(`"hello"`), Status: pgtype.Present},
    24  		&pgtype.JSONB{Status: pgtype.Null},
    25  	})
    26  }
    27  
    28  func TestJSONBSet(t *testing.T) {
    29  	successfulTests := []struct {
    30  		source interface{}
    31  		result pgtype.JSONB
    32  	}{
    33  		{source: "{}", result: pgtype.JSONB{Bytes: []byte("{}"), Status: pgtype.Present}},
    34  		{source: []byte("{}"), result: pgtype.JSONB{Bytes: []byte("{}"), Status: pgtype.Present}},
    35  		{source: ([]byte)(nil), result: pgtype.JSONB{Status: pgtype.Null}},
    36  		{source: (*string)(nil), result: pgtype.JSONB{Status: pgtype.Null}},
    37  		{source: []int{1, 2, 3}, result: pgtype.JSONB{Bytes: []byte("[1,2,3]"), Status: pgtype.Present}},
    38  		{source: map[string]interface{}{"foo": "bar"}, result: pgtype.JSONB{Bytes: []byte(`{"foo":"bar"}`), Status: pgtype.Present}},
    39  	}
    40  
    41  	for i, tt := range successfulTests {
    42  		var d pgtype.JSONB
    43  		err := d.Set(tt.source)
    44  		if err != nil {
    45  			t.Errorf("%d: %v", i, err)
    46  		}
    47  
    48  		if !reflect.DeepEqual(d, tt.result) {
    49  			t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, d)
    50  		}
    51  	}
    52  }
    53  
    54  func TestJSONBAssignTo(t *testing.T) {
    55  	var s string
    56  	var ps *string
    57  	var b []byte
    58  
    59  	rawStringTests := []struct {
    60  		src      pgtype.JSONB
    61  		dst      *string
    62  		expected string
    63  	}{
    64  		{src: pgtype.JSONB{Bytes: []byte("{}"), Status: pgtype.Present}, dst: &s, expected: "{}"},
    65  	}
    66  
    67  	for i, tt := range rawStringTests {
    68  		err := tt.src.AssignTo(tt.dst)
    69  		if err != nil {
    70  			t.Errorf("%d: %v", i, err)
    71  		}
    72  
    73  		if *tt.dst != tt.expected {
    74  			t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, *tt.dst)
    75  		}
    76  	}
    77  
    78  	rawBytesTests := []struct {
    79  		src      pgtype.JSONB
    80  		dst      *[]byte
    81  		expected []byte
    82  	}{
    83  		{src: pgtype.JSONB{Bytes: []byte("{}"), Status: pgtype.Present}, dst: &b, expected: []byte("{}")},
    84  		{src: pgtype.JSONB{Status: pgtype.Null}, dst: &b, expected: (([]byte)(nil))},
    85  	}
    86  
    87  	for i, tt := range rawBytesTests {
    88  		err := tt.src.AssignTo(tt.dst)
    89  		if err != nil {
    90  			t.Errorf("%d: %v", i, err)
    91  		}
    92  
    93  		if bytes.Compare(tt.expected, *tt.dst) != 0 {
    94  			t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, *tt.dst)
    95  		}
    96  	}
    97  
    98  	var mapDst map[string]interface{}
    99  	type structDst struct {
   100  		Name string `json:"name"`
   101  		Age  int    `json:"age"`
   102  	}
   103  	var strDst structDst
   104  
   105  	unmarshalTests := []struct {
   106  		src      pgtype.JSONB
   107  		dst      interface{}
   108  		expected interface{}
   109  	}{
   110  		{src: pgtype.JSONB{Bytes: []byte(`{"foo":"bar"}`), Status: pgtype.Present}, dst: &mapDst, expected: map[string]interface{}{"foo": "bar"}},
   111  		{src: pgtype.JSONB{Bytes: []byte(`{"name":"John","age":42}`), Status: pgtype.Present}, dst: &strDst, expected: structDst{Name: "John", Age: 42}},
   112  	}
   113  	for i, tt := range unmarshalTests {
   114  		err := tt.src.AssignTo(tt.dst)
   115  		if err != nil {
   116  			t.Errorf("%d: %v", i, err)
   117  		}
   118  
   119  		if dst := reflect.ValueOf(tt.dst).Elem().Interface(); !reflect.DeepEqual(dst, tt.expected) {
   120  			t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst)
   121  		}
   122  	}
   123  
   124  	pointerAllocTests := []struct {
   125  		src      pgtype.JSONB
   126  		dst      **string
   127  		expected *string
   128  	}{
   129  		{src: pgtype.JSONB{Status: pgtype.Null}, dst: &ps, expected: ((*string)(nil))},
   130  	}
   131  
   132  	for i, tt := range pointerAllocTests {
   133  		err := tt.src.AssignTo(tt.dst)
   134  		if err != nil {
   135  			t.Errorf("%d: %v", i, err)
   136  		}
   137  
   138  		if *tt.dst != tt.expected {
   139  			t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, *tt.dst)
   140  		}
   141  	}
   142  }
   143  

View as plain text