-
-
Notifications
You must be signed in to change notification settings - Fork 110
/
json_test.go
527 lines (457 loc) · 18 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
package datatypes_test
import (
"database/sql/driver"
"encoding/json"
"strings"
"testing"
"gorm.io/datatypes"
"gorm.io/driver/mysql"
"gorm.io/gorm"
. "gorm.io/gorm/utils/tests"
)
var _ driver.Valuer = &datatypes.JSON{}
func TestJSON(t *testing.T) {
if SupportedDriver("sqlite", "mysql", "postgres") {
type UserWithJSON struct {
gorm.Model
Name string
Attributes datatypes.JSON
}
DB.Migrator().DropTable(&UserWithJSON{})
if err := DB.Migrator().AutoMigrate(&UserWithJSON{}); err != nil {
t.Errorf("failed to migrate, got error: %v", err)
}
// Go's json marshaler removes whitespace & orders keys alphabetically
// use to compare against marshaled []byte of datatypes.JSON
user1Attrs := `{"age":18,"name":"json-1","orgs":{"orga":"orga"},"tags":["tag1","tag2"],"admin":true}`
users := []UserWithJSON{{
Name: "json-1",
Attributes: datatypes.JSON([]byte(user1Attrs)),
}, {
Name: "json-2",
Attributes: datatypes.JSON([]byte(`{"name": "json-2", "age": 28, "tags": ["tag1", "tag3"], "role": "admin", "orgs": {"orgb": "orgb"}}`)),
}, {
Name: "json-3",
Attributes: datatypes.JSON([]byte(`["tag1","tag2","tag3"]`)),
}}
if err := DB.Create(&users).Error; err != nil {
t.Errorf("Failed to create users %v", err)
}
var result UserWithJSON
if err := DB.First(&result, datatypes.JSONQuery("attributes").HasKey("role")).Error; err != nil {
t.Fatalf("failed to find user with json key, got error %v", err)
}
AssertEqual(t, result.Name, users[1].Name)
var result2 UserWithJSON
if err := DB.First(&result2, datatypes.JSONQuery("attributes").HasKey("orgs", "orga")).Error; err != nil {
t.Fatalf("failed to find user with json key, got error %v", err)
}
AssertEqual(t, result2.Name, users[0].Name)
// attributes should not marshal to base64 encoded []byte
result2Attrs, err := json.Marshal(&result2.Attributes)
if err != nil {
t.Fatalf("failed to marshal result2.Attributes, got error %v", err)
}
AssertEqual(t, string(result2Attrs), user1Attrs)
// []byte should unmarshal into type datatypes.JSON
var j datatypes.JSON
if err := json.Unmarshal([]byte(user1Attrs), &j); err != nil {
t.Fatalf("failed to unmarshal user1Attrs, got error %v", err)
}
AssertEqual(t, string(j), user1Attrs)
var result3 UserWithJSON
if err := DB.First(&result3, datatypes.JSONQuery("attributes").Equals("json-1", "name")).Error; err != nil {
t.Fatalf("failed to find user with json value, got error %v", err)
}
AssertEqual(t, result3.Name, users[0].Name)
var result4 UserWithJSON
if err := DB.First(&result4, datatypes.JSONQuery("attributes").Equals("orgb", "orgs", "orgb")).Error; err != nil {
t.Fatalf("failed to find user with json value, got error %v", err)
}
AssertEqual(t, result4.Name, users[1].Name)
var results5 []UserWithJSON
if err := DB.Where(datatypes.JSONQuery("attributes").HasKey("age")).Where(datatypes.JSONQuery("attributes").Equals(true, "admin")).Find(&results5).Error; err != nil || len(results5) != 1 {
t.Fatalf("failed to find user with json value, got error %v", err)
}
AssertEqual(t, results5[0].Name, users[0].Name)
// FirstOrCreate
jsonMap := map[string]interface{}{"Attributes": datatypes.JSON(`{"age":19,"name":"json-1","orgs":{"orga":"orga"},"tags":["tag1","tag2"]}`)}
if err := DB.Where(&UserWithJSON{Name: "json-1"}).Assign(jsonMap).FirstOrCreate(&UserWithJSON{}).Error; err != nil {
t.Errorf("failed to run FirstOrCreate")
}
var result6 UserWithJSON
if err := DB.First(&result6, datatypes.JSONQuery("attributes").Equals(19, "age")).Error; err != nil {
t.Fatalf("failed to find user with json value, got error %v", err)
}
// Update
jsonMap = map[string]interface{}{"Attributes": datatypes.JSON(`{"age":29,"name":"json-1","orgs":{"orga":"orga"},"tags":["tag1","tag2"]}`)}
if err := DB.Model(&result3).Updates(jsonMap).Error; err != nil {
t.Errorf("failed to run FirstOrCreate")
}
var result7 UserWithJSON
if err := DB.First(&result7, datatypes.JSONQuery("attributes").Equals(29, "age")).Error; err != nil {
t.Fatalf("failed to find user with json value, got error %v", err)
}
var result8 UserWithJSON
if err := DB.Where(result7, "Attributes").First(&result8).Error; err != nil {
t.Fatalf("failed to find user with json value, got error %v", err)
}
var results9 []UserWithJSON
if err := DB.Where("? = ?", datatypes.JSONQuery("attributes").Extract("name"), "json-2").Find(&results9).Error; err != nil || len(results9) != 1 {
t.Fatalf("failed to find user with json value, got error %v", err)
}
AssertEqual(t, results9[0].Name, users[1].Name)
var result10 UserWithJSON
if err := DB.First(&result10, datatypes.JSONQuery("attributes").Likes("%dmi%", "role")).Error; err != nil {
t.Fatalf("failed to find user with json value, got error %v", err)
}
AssertEqual(t, result10.Name, users[1].Name)
// not support for sqlite
// JSONOverlaps
//var result9 UserWithJSON
//if err := DB.First(&result9, datatypes.JSONOverlaps("attributes", `["tag1","tag2"]`)).Error; err != nil {
// t.Fatalf("failed to find user with json value, got error %v", err)
//}
}
}
func TestJSONSliceScan(t *testing.T) {
if SupportedDriver("sqlite", "mysql", "postgres") {
type Param struct {
ID int
DisplayName string
Config datatypes.JSON
}
DB.Migrator().DropTable(&Param{})
if err := DB.Migrator().AutoMigrate(&Param{}); err != nil {
t.Errorf("failed to migrate, got error: %v", err)
}
cmp1 := Param{
DisplayName: "TestJSONSliceScan-1",
Config: datatypes.JSON("{\"param1\": 1234, \"param2\": \"test\"}"),
}
cmp2 := Param{
DisplayName: "TestJSONSliceScan-2",
Config: datatypes.JSON("{\"param1\": 456, \"param2\": \"test2\"}"),
}
if err := DB.Create(&cmp1).Error; err != nil {
t.Errorf("Failed to create param %v", err)
}
if err := DB.Create(&cmp2).Error; err != nil {
t.Errorf("Failed to create param %v", err)
}
var retSingle1 Param
if err := DB.Where("id = ?", cmp2.ID).First(&retSingle1).Error; err != nil {
t.Errorf("Failed to find param %v", err)
}
var retSingle2 Param
if err := DB.Where("id = ?", cmp2.ID).First(&retSingle2).Error; err != nil {
t.Errorf("Failed to find param %v", err)
}
AssertEqual(t, retSingle1, cmp2)
AssertEqual(t, retSingle2, cmp2)
var retMultiple []Param
if err := DB.Find(&retMultiple).Error; err != nil {
t.Errorf("Failed to find param %v", err)
}
AssertEqual(t, retSingle1, cmp2)
AssertEqual(t, retSingle2, cmp2)
}
}
func TestPostgresJSONSet(t *testing.T) {
if !SupportedDriver("postgres") {
t.Skip()
}
type UserWithJSON struct {
gorm.Model
Name string
Attributes datatypes.JSON
}
DB.Migrator().DropTable(&UserWithJSON{})
if err := DB.Migrator().AutoMigrate(&UserWithJSON{}); err != nil {
t.Errorf("failed to migrate, got error: %v", err)
}
users := []UserWithJSON{{
Name: "json-1",
Attributes: datatypes.JSON(`{"name": "json-1", "age": 18, "orgs": {"orga": "orga"}, "tags": ["tag1", "tag2"], "admin": true}`),
}, {
Name: "json-2",
Attributes: datatypes.JSON(`{"name": "json-2", "age": 28, "tags": ["tag1", "tag3"], "role": "admin", "orgs": {"orgb": "orgb"}}`),
}, {
Name: "json-3",
Attributes: datatypes.JSON(`{"name": "json-3"}`),
}, {
Name: "json-4",
Attributes: datatypes.JSON(`{"name": "json-4"}`),
}}
if err := DB.Create(&users).Error; err != nil {
t.Errorf("Failed to create users %v", err)
}
tests := []struct {
name string
userName string
path2value map[string]interface{}
expect map[string]interface{}
}{
{
name: "update int and string",
userName: "json-1",
path2value: map[string]interface{}{
"{age}": 20,
"{role}": "tester",
},
expect: map[string]interface{}{
"age": 20,
"role": "tester",
},
}, {
name: "update array child",
userName: "json-2",
path2value: map[string]interface{}{
"{tags, 0}": "tag2",
},
expect: map[string]interface{}{
"tags": []string{"tag2", "tag3"},
},
}, {
name: "update array",
userName: "json-2",
path2value: map[string]interface{}{
"{phones}": []string{"10086", "10085"},
},
expect: map[string]interface{}{
"phones": []string{"10086", "10085"},
},
}, {
name: "update by expr",
userName: "json-4",
path2value: map[string]interface{}{
"{extra}": gorm.Expr("?::jsonb", `["a", "b"]`),
},
expect: map[string]interface{}{
"extra": []string{"a", "b"},
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
jsonSet := datatypes.JSONSet("attributes")
for path, value := range test.path2value {
jsonSet = jsonSet.Set(path, value)
}
if err := DB.Model(&UserWithJSON{}).Where("name = ?", test.userName).UpdateColumn("attributes", jsonSet).Error; err != nil {
t.Fatalf("failed to update user with json key, got error %v", err)
}
var result UserWithJSON
if err := DB.First(&result, "name = ?", test.userName).Error; err != nil {
t.Fatalf("failed to find user with json key, got error %v", err)
}
actual := make(map[string]interface{})
if err := json.Unmarshal(result.Attributes, &actual); err != nil {
t.Fatalf("failed to unmarshal attributes, got err %v", err)
}
for key, value := range test.expect {
AssertEqual(t, value, test.expect[key])
}
})
}
if err := DB.Model(&UserWithJSON{}).Where("name = ?", "json-3").UpdateColumn("attributes", datatypes.JSONSet("attributes").Set("{friend}", users[0])).Error; err != nil {
t.Fatalf("failed to update user with json key, got error %v", err)
}
var result UserWithJSON
if err := DB.First(&result, "name = ?", "json-3").Error; err != nil {
t.Fatalf("failed to find user with json key, got error %v", err)
}
actual := make(map[string]json.RawMessage)
if err := json.Unmarshal(result.Attributes, &actual); err != nil {
t.Fatalf("failed to unmarshal attributes, got err %v", err)
}
var friend UserWithJSON
if err := json.Unmarshal(actual["friend"], &friend); err != nil {
t.Fatalf("failed to unmarshal attributes, got err %v", err)
}
AssertEqual(t, friend.ID, users[0].ID)
AssertEqual(t, friend.Name, users[0].Name)
}
func TestJSONSet(t *testing.T) {
if SupportedDriver("sqlite", "mysql") {
type UserWithJSON struct {
gorm.Model
Name string
Attributes datatypes.JSON
}
DB.Migrator().DropTable(&UserWithJSON{})
if err := DB.Migrator().AutoMigrate(&UserWithJSON{}); err != nil {
t.Errorf("failed to migrate, got error: %v", err)
}
var isMariaDB bool
if DB.Dialector.Name() == "mysql" {
if v, ok := DB.Dialector.(*mysql.Dialector); ok {
isMariaDB = strings.Contains(v.ServerVersion, "MariaDB")
}
}
users := []UserWithJSON{{
Name: "json-1",
Attributes: datatypes.JSON([]byte(`{"name": "json-1", "age": 18, "orgs": {"orga": "orga"}, "tags": ["tag1", "tag2"], "admin": true}`)),
}, {
Name: "json-2",
Attributes: datatypes.JSON([]byte(`{"name": "json-2", "age": 28, "tags": ["tag1", "tag3"], "role": "admin", "orgs": {"orgb": "orgb"}}`)),
}, {
Name: "json-3",
Attributes: datatypes.JSON([]byte(`{"name": "json-3"}`)),
}, {
Name: "json-4",
Attributes: datatypes.JSON([]byte(`{"name": "json-4"}`)),
}}
if err := DB.Create(&users).Error; err != nil {
t.Errorf("Failed to create users %v", err)
}
tmp := make(map[string]interface{})
// update int, string
if err := DB.Model(&UserWithJSON{}).Where("name = ?", "json-1").UpdateColumn("attributes", datatypes.JSONSet("attributes").Set("age", 20).Set("role", "tester")).Error; err != nil {
t.Fatalf("failed to update user with json key, got error %v", err)
}
var result UserWithJSON
if err := DB.First(&result, "name = ?", "json-1").Error; err != nil {
t.Fatalf("failed to find user with json key, got error %v", err)
}
_ = json.Unmarshal(result.Attributes, &tmp)
AssertEqual(t, tmp["age"], 20)
AssertEqual(t, tmp["role"], "tester")
if err := DB.Model(&UserWithJSON{}).Where("name = ?", "json-2").UpdateColumn("attributes", datatypes.JSONSet("attributes").Set("tags[0]", "tag2")).Error; err != nil {
t.Fatalf("failed to update user with json key, got error %v", err)
}
var result2 UserWithJSON
if err := DB.First(&result2, "name = ?", "json-2").Error; err != nil {
t.Fatalf("failed to find user with json key, got error %v", err)
}
_ = json.Unmarshal(result2.Attributes, &tmp)
AssertEqual(t, tmp["tags"], []string{"tag2", "tag3"})
// MariaDB does not support CAST(? AS JSON),
if isMariaDB {
if err := DB.Model(&UserWithJSON{}).Where("name = ?", "json-2").UpdateColumn("attributes", datatypes.JSONSet("attributes").Set("phones", []string{"10086", "10085"})).Error; err != nil {
t.Fatalf("failed to update user with json key, got error %v", err)
}
var result3 UserWithJSON
if err := DB.First(&result3, "name = ?", "json-2").Error; err != nil {
t.Fatalf("failed to find user with json key, got error %v", err)
}
_ = json.Unmarshal(result3.Attributes, &tmp)
AssertEqual(t, tmp["phones"], `["10086","10085"]`)
if err := DB.Model(&UserWithJSON{}).Where("name = ?", "json-3").UpdateColumn("attributes", datatypes.JSONSet("attributes").Set("friend", result)).Error; err != nil {
t.Fatalf("failed to update user with json key, got error %v", err)
}
var result4 UserWithJSON
if err := DB.First(&result4, "name = ?", "json-3").Error; err != nil {
t.Fatalf("failed to find user with json key, got error %v", err)
}
m := make(map[string]interface{})
_ = json.Unmarshal(result4.Attributes, &m)
var tmpResult UserWithJSON
_ = json.Unmarshal([]byte(m["friend"].(string)), &tmpResult)
AssertEqual(t, tmpResult.Name, result.Name)
} else {
if err := DB.Model(&UserWithJSON{}).Where("name = ?", "json-2").UpdateColumn("attributes", datatypes.JSONSet("attributes").Set("phones", []string{"10086", "10085"})).Error; err != nil {
t.Fatalf("failed to update user with json key, got error %v", err)
}
var result3 UserWithJSON
if err := DB.First(&result3, "name = ?", "json-2").Error; err != nil {
t.Fatalf("failed to find user with json key, got error %v", err)
}
_ = json.Unmarshal(result3.Attributes, &tmp)
AssertEqual(t, tmp["phones"], []string{"10086", "10085"})
if err := DB.Model(&UserWithJSON{}).Where("name = ?", "json-3").UpdateColumn("attributes", datatypes.JSONSet("attributes").Set("friend", result)).Error; err != nil {
t.Fatalf("failed to update user with json key, got error %v", err)
}
var result4 UserWithJSON
if err := DB.First(&result4, "name = ?", "json-3").Error; err != nil {
t.Fatalf("failed to find user with json key, got error %v", err)
}
m := make(map[string]UserWithJSON)
_ = json.Unmarshal(result4.Attributes, &m)
AssertEqual(t, m["friend"], result)
if DB.Dialector.Name() == "mysql" {
if err := DB.Model(&UserWithJSON{}).Where("name = ?", "json-4").UpdateColumn("attributes", datatypes.JSONSet("attributes").Set("extra", gorm.Expr("CAST(? AS JSON)", `["a", "b"]`))).Error; err != nil {
t.Fatalf("failed to update user with json key, got error %v", err)
}
} else if DB.Dialector.Name() == "sqlite" {
if err := DB.Model(&UserWithJSON{}).Where("name = ?", "json-4").UpdateColumn("attributes", datatypes.JSONSet("attributes").Set("extra", gorm.Expr("JSON(?)", `["a", "b"]`))).Error; err != nil {
t.Fatalf("failed to update user with json key, got error %v", err)
}
}
var result5 UserWithJSON
if err := DB.First(&result5, "name = ?", "json-4").Error; err != nil {
t.Fatalf("failed to find user with json key, got error %v", err)
}
_ = json.Unmarshal(result5.Attributes, &tmp)
AssertEqual(t, tmp["extra"], []string{"a", "b"})
}
}
}
func TestJSONArrayQuery(t *testing.T) {
if SupportedDriver("sqlite", "mysql") {
type Param struct {
ID int
DisplayName string
Config datatypes.JSON
}
DB.Migrator().DropTable(&Param{})
if err := DB.Migrator().AutoMigrate(&Param{}); err != nil {
t.Errorf("failed to migrate, got error: %v", err)
}
cmp1 := Param{
DisplayName: "JSONArray-1",
Config: datatypes.JSON("[\"a\", \"b\"]"),
}
cmp2 := Param{
DisplayName: "JSONArray-2",
Config: datatypes.JSON("[\"c\", \"a\"]"),
}
cmp3 := Param{
DisplayName: "JSONArray-3",
Config: datatypes.JSON("{\"test\": [\"a\", \"b\"]}"),
}
cmp4 := Param{
DisplayName: "JSONArray-4",
Config: datatypes.JSON("{\"test\": \"c\"}"),
}
if err := DB.Create(&cmp1).Error; err != nil {
t.Errorf("Failed to create param %v", err)
}
if err := DB.Create(&cmp2).Error; err != nil {
t.Errorf("Failed to create param %v", err)
}
if err := DB.Create(&cmp3).Error; err != nil {
t.Errorf("Failed to create param %v", err)
}
if err := DB.Create(&cmp4).Error; err != nil {
t.Errorf("Failed to create param %v", err)
}
var retSingle1 Param
if err := DB.Where("id = ?", cmp2.ID).First(&retSingle1).Error; err != nil {
t.Errorf("Failed to find param %v", err)
}
var retSingle2 Param
if err := DB.Where("id = ?", cmp2.ID).First(&retSingle2).Error; err != nil {
t.Errorf("Failed to find param %v", err)
}
AssertEqual(t, retSingle1, cmp2)
AssertEqual(t, retSingle2, cmp2)
var retMultiple []Param
if err := DB.Where(datatypes.JSONArrayQuery("config").Contains("c")).Find(&retMultiple).Error; err != nil {
t.Fatalf("failed to find params with json value, got error %v", err)
}
AssertEqual(t, len(retMultiple), 1)
if err := DB.Where(datatypes.JSONArrayQuery("config").Contains("a", "test")).Find(&retMultiple).Error; err != nil {
t.Fatalf("failed to find params with json value and keys, got error %v", err)
}
AssertEqual(t, len(retMultiple), 1)
if err := DB.Where(datatypes.JSONArrayQuery("config").In([]string{"c", "a"})).Find(&retMultiple).Error; err != nil {
t.Fatalf("failed to find params with json value, got error %v", err)
}
AssertEqual(t, len(retMultiple), 1)
if err := DB.Where(datatypes.JSONArrayQuery("config").In([]string{"c", "d"}, "test")).Find(&retMultiple).Error; err != nil {
t.Fatalf("failed to find params with json value and keys, got error %v", err)
}
AssertEqual(t, len(retMultiple), 1)
}
}