-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_test.go
226 lines (189 loc) · 6.8 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package pgtype_test
import (
"context"
"database/sql"
"database/sql/driver"
"encoding/json"
"errors"
"testing"
pgx "github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxtest"
"github.com/stretchr/testify/require"
)
func isExpectedEqMap(a any) func(any) bool {
return func(v any) bool {
aa := a.(map[string]any)
bb := v.(map[string]any)
if (aa == nil) != (bb == nil) {
return false
}
if aa == nil {
return true
}
if len(aa) != len(bb) {
return false
}
for k := range aa {
if aa[k] != bb[k] {
return false
}
}
return true
}
}
func TestJSONCodec(t *testing.T) {
type jsonStruct struct {
Name string `json:"name"`
Age int `json:"age"`
}
pgxtest.RunValueRoundTripTests(context.Background(), t, defaultConnTestRunner, nil, "json", []pgxtest.ValueRoundTripTest{
{nil, new(*jsonStruct), isExpectedEq((*jsonStruct)(nil))},
{map[string]any(nil), new(*string), isExpectedEq((*string)(nil))},
{map[string]any(nil), new([]byte), isExpectedEqBytes([]byte(nil))},
{[]byte(nil), new([]byte), isExpectedEqBytes([]byte(nil))},
{nil, new([]byte), isExpectedEqBytes([]byte(nil))},
// Test sql.Scanner. (https://github.com/jackc/pgx/issues/1418)
{"42", new(sql.NullInt64), isExpectedEq(sql.NullInt64{Int64: 42, Valid: true})},
// Test driver.Valuer. (https://github.com/jackc/pgx/issues/1430)
{sql.NullInt64{Int64: 42, Valid: true}, new(sql.NullInt64), isExpectedEq(sql.NullInt64{Int64: 42, Valid: true})},
// Test driver.Valuer is used before json.Marshaler (https://github.com/jackc/pgx/issues/1805)
{Issue1805(7), new(Issue1805), isExpectedEq(Issue1805(7))},
})
pgxtest.RunValueRoundTripTests(context.Background(), t, defaultConnTestRunner, pgxtest.KnownOIDQueryExecModes, "json", []pgxtest.ValueRoundTripTest{
{[]byte("{}"), new([]byte), isExpectedEqBytes([]byte("{}"))},
{[]byte("null"), new([]byte), isExpectedEqBytes([]byte("null"))},
{[]byte("42"), new([]byte), isExpectedEqBytes([]byte("42"))},
{[]byte(`"hello"`), new([]byte), isExpectedEqBytes([]byte(`"hello"`))},
{[]byte(`"hello"`), new(string), isExpectedEq(`"hello"`)},
{map[string]any{"foo": "bar"}, new(map[string]any), isExpectedEqMap(map[string]any{"foo": "bar"})},
{jsonStruct{Name: "Adam", Age: 10}, new(jsonStruct), isExpectedEq(jsonStruct{Name: "Adam", Age: 10})},
})
}
type Issue1805 int
func (i *Issue1805) Scan(src any) error {
var source []byte
switch src.(type) {
case string:
source = []byte(src.(string))
case []byte:
source = src.([]byte)
default:
return errors.New("unknown source type")
}
var newI int
if err := json.Unmarshal(source, &newI); err != nil {
return err
}
*i = Issue1805(newI)
return nil
}
func (i Issue1805) Value() (driver.Value, error) {
b, err := json.Marshal(int(i))
return string(b), err
}
func (i Issue1805) UnmarshalJSON(bytes []byte) error {
return errors.New("UnmarshalJSON called")
}
func (i Issue1805) MarshalJSON() ([]byte, error) {
return nil, errors.New("MarshalJSON called")
}
// https://github.com/jackc/pgx/issues/1273#issuecomment-1221414648
func TestJSONCodecUnmarshalSQLNull(t *testing.T) {
defaultConnTestRunner.RunTest(context.Background(), t, func(ctx context.Context, t testing.TB, conn *pgx.Conn) {
// Slices are nilified
slice := []string{"foo", "bar", "baz"}
err := conn.QueryRow(ctx, "select null::json").Scan(&slice)
require.NoError(t, err)
require.Nil(t, slice)
// Maps are nilified
m := map[string]any{"foo": "bar"}
err = conn.QueryRow(ctx, "select null::json").Scan(&m)
require.NoError(t, err)
require.Nil(t, m)
m = map[string]interface{}{"foo": "bar"}
err = conn.QueryRow(ctx, "select null::json").Scan(&m)
require.NoError(t, err)
require.Nil(t, m)
// Pointer to pointer are nilified
n := 42
p := &n
err = conn.QueryRow(ctx, "select null::json").Scan(&p)
require.NoError(t, err)
require.Nil(t, p)
// A string cannot scan a NULL.
str := "foobar"
err = conn.QueryRow(ctx, "select null::json").Scan(&str)
require.EqualError(t, err, "can't scan into dest[0]: cannot scan NULL into *string")
// A non-string cannot scan a NULL.
err = conn.QueryRow(ctx, "select null::json").Scan(&n)
require.EqualError(t, err, "can't scan into dest[0]: cannot scan NULL into *int")
})
}
// https://github.com/jackc/pgx/issues/1470
func TestJSONCodecPointerToPointerToString(t *testing.T) {
defaultConnTestRunner.RunTest(context.Background(), t, func(ctx context.Context, t testing.TB, conn *pgx.Conn) {
var s *string
err := conn.QueryRow(ctx, "select '{}'::json").Scan(&s)
require.NoError(t, err)
require.NotNil(t, s)
require.Equal(t, "{}", *s)
err = conn.QueryRow(ctx, "select null::json").Scan(&s)
require.NoError(t, err)
require.Nil(t, s)
})
}
// https://github.com/jackc/pgx/issues/1691
func TestJSONCodecPointerToPointerToInt(t *testing.T) {
defaultConnTestRunner.RunTest(context.Background(), t, func(ctx context.Context, t testing.TB, conn *pgx.Conn) {
n := 44
p := &n
err := conn.QueryRow(ctx, "select 'null'::jsonb").Scan(&p)
require.NoError(t, err)
require.Nil(t, p)
})
}
// https://github.com/jackc/pgx/issues/1691
func TestJSONCodecPointerToPointerToStruct(t *testing.T) {
defaultConnTestRunner.RunTest(context.Background(), t, func(ctx context.Context, t testing.TB, conn *pgx.Conn) {
type ImageSize struct {
Height int `json:"height"`
Width int `json:"width"`
Str string `json:"str"`
}
is := &ImageSize{Height: 100, Width: 100, Str: "str"}
err := conn.QueryRow(ctx, `select 'null'::jsonb`).Scan(&is)
require.NoError(t, err)
require.Nil(t, is)
})
}
func TestJSONCodecClearExistingValueBeforeUnmarshal(t *testing.T) {
defaultConnTestRunner.RunTest(context.Background(), t, func(ctx context.Context, t testing.TB, conn *pgx.Conn) {
m := map[string]any{}
err := conn.QueryRow(ctx, `select '{"foo": "bar"}'::json`).Scan(&m)
require.NoError(t, err)
require.Equal(t, map[string]any{"foo": "bar"}, m)
err = conn.QueryRow(ctx, `select '{"baz": "quz"}'::json`).Scan(&m)
require.NoError(t, err)
require.Equal(t, map[string]any{"baz": "quz"}, m)
})
}
type ParentIssue1681 struct {
Child ChildIssue1681
}
func (t *ParentIssue1681) MarshalJSON() ([]byte, error) {
return []byte(`{"custom":"thing"}`), nil
}
type ChildIssue1681 struct{}
func (t ChildIssue1681) MarshalJSON() ([]byte, error) {
return []byte(`{"someVal": false}`), nil
}
// https://github.com/jackc/pgx/issues/1681
func TestJSONCodecEncodeJSONMarshalerThatCanBeWrapped(t *testing.T) {
skipCockroachDB(t, "CockroachDB treats json as jsonb. This causes it to format differently than PostgreSQL.")
defaultConnTestRunner.RunTest(context.Background(), t, func(ctx context.Context, t testing.TB, conn *pgx.Conn) {
var jsonStr string
err := conn.QueryRow(context.Background(), "select $1::json", &ParentIssue1681{}).Scan(&jsonStr)
require.NoError(t, err)
require.Equal(t, `{"custom":"thing"}`, jsonStr)
})
}