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