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 TestJSONTranscode(t *testing.T) {
13 testutil.TestSuccessfulTranscode(t, "json", []interface{}{
14 &pgtype.JSON{Bytes: []byte("{}"), Status: pgtype.Present},
15 &pgtype.JSON{Bytes: []byte("null"), Status: pgtype.Present},
16 &pgtype.JSON{Bytes: []byte("42"), Status: pgtype.Present},
17 &pgtype.JSON{Bytes: []byte(`"hello"`), Status: pgtype.Present},
18 &pgtype.JSON{Status: pgtype.Null},
19 })
20 }
21
22 func TestJSONSet(t *testing.T) {
23 successfulTests := []struct {
24 source interface{}
25 result pgtype.JSON
26 }{
27 {source: "{}", result: pgtype.JSON{Bytes: []byte("{}"), Status: pgtype.Present}},
28 {source: []byte("{}"), result: pgtype.JSON{Bytes: []byte("{}"), Status: pgtype.Present}},
29 {source: ([]byte)(nil), result: pgtype.JSON{Status: pgtype.Null}},
30 {source: (*string)(nil), result: pgtype.JSON{Status: pgtype.Null}},
31 {source: []int{1, 2, 3}, result: pgtype.JSON{Bytes: []byte("[1,2,3]"), Status: pgtype.Present}},
32 {source: map[string]interface{}{"foo": "bar"}, result: pgtype.JSON{Bytes: []byte(`{"foo":"bar"}`), Status: pgtype.Present}},
33 }
34
35 for i, tt := range successfulTests {
36 var d pgtype.JSON
37 err := d.Set(tt.source)
38 if err != nil {
39 t.Errorf("%d: %v", i, err)
40 }
41
42 if !reflect.DeepEqual(d, tt.result) {
43 t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, d)
44 }
45 }
46 }
47
48 func TestJSONAssignTo(t *testing.T) {
49 var s string
50 var ps *string
51 var b []byte
52
53 rawStringTests := []struct {
54 src pgtype.JSON
55 dst *string
56 expected string
57 }{
58 {src: pgtype.JSON{Bytes: []byte("{}"), Status: pgtype.Present}, dst: &s, expected: "{}"},
59 }
60
61 for i, tt := range rawStringTests {
62 err := tt.src.AssignTo(tt.dst)
63 if err != nil {
64 t.Errorf("%d: %v", i, err)
65 }
66
67 if *tt.dst != tt.expected {
68 t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, *tt.dst)
69 }
70 }
71
72 rawBytesTests := []struct {
73 src pgtype.JSON
74 dst *[]byte
75 expected []byte
76 }{
77 {src: pgtype.JSON{Bytes: []byte("{}"), Status: pgtype.Present}, dst: &b, expected: []byte("{}")},
78 {src: pgtype.JSON{Status: pgtype.Null}, dst: &b, expected: (([]byte)(nil))},
79 }
80
81 for i, tt := range rawBytesTests {
82 err := tt.src.AssignTo(tt.dst)
83 if err != nil {
84 t.Errorf("%d: %v", i, err)
85 }
86
87 if bytes.Compare(tt.expected, *tt.dst) != 0 {
88 t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, *tt.dst)
89 }
90 }
91
92 var mapDst map[string]interface{}
93 type structDst struct {
94 Name string `json:"name"`
95 Age int `json:"age"`
96 }
97 var strDst structDst
98
99 unmarshalTests := []struct {
100 src pgtype.JSON
101 dst interface{}
102 expected interface{}
103 }{
104 {src: pgtype.JSON{Bytes: []byte(`{"foo":"bar"}`), Status: pgtype.Present}, dst: &mapDst, expected: map[string]interface{}{"foo": "bar"}},
105 {src: pgtype.JSON{Bytes: []byte(`{"name":"John","age":42}`), Status: pgtype.Present}, dst: &strDst, expected: structDst{Name: "John", Age: 42}},
106 }
107 for i, tt := range unmarshalTests {
108 err := tt.src.AssignTo(tt.dst)
109 if err != nil {
110 t.Errorf("%d: %v", i, err)
111 }
112
113 if dst := reflect.ValueOf(tt.dst).Elem().Interface(); !reflect.DeepEqual(dst, tt.expected) {
114 t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst)
115 }
116 }
117
118 pointerAllocTests := []struct {
119 src pgtype.JSON
120 dst **string
121 expected *string
122 }{
123 {src: pgtype.JSON{Status: pgtype.Null}, dst: &ps, expected: ((*string)(nil))},
124 }
125
126 for i, tt := range pointerAllocTests {
127 err := tt.src.AssignTo(tt.dst)
128 if err != nil {
129 t.Errorf("%d: %v", i, err)
130 }
131
132 if *tt.dst != tt.expected {
133 t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, *tt.dst)
134 }
135 }
136 }
137
138 func TestJSONMarshalJSON(t *testing.T) {
139 successfulTests := []struct {
140 source pgtype.JSON
141 result string
142 }{
143 {source: pgtype.JSON{Status: pgtype.Null}, result: "null"},
144 {source: pgtype.JSON{Bytes: []byte("{\"a\": 1}"), Status: pgtype.Present}, result: "{\"a\": 1}"},
145 }
146 for i, tt := range successfulTests {
147 r, err := tt.source.MarshalJSON()
148 if err != nil {
149 t.Errorf("%d: %v", i, err)
150 }
151
152 if string(r) != tt.result {
153 t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, string(r))
154 }
155 }
156 }
157
158 func TestJSONUnmarshalJSON(t *testing.T) {
159 successfulTests := []struct {
160 source string
161 result pgtype.JSON
162 }{
163 {source: "null", result: pgtype.JSON{Status: pgtype.Null}},
164 {source: "{\"a\": 1}", result: pgtype.JSON{Bytes: []byte("{\"a\": 1}"), Status: pgtype.Present}},
165 }
166 for i, tt := range successfulTests {
167 var r pgtype.JSON
168 err := r.UnmarshalJSON([]byte(tt.source))
169 if err != nil {
170 t.Errorf("%d: %v", i, err)
171 }
172
173 if string(r.Bytes) != string(tt.result.Bytes) || r.Status != tt.result.Status {
174 t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r)
175 }
176 }
177 }
178
View as plain text