-
-
Notifications
You must be signed in to change notification settings - Fork 108
/
json_map_test.go
184 lines (151 loc) · 5.34 KB
/
json_map_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
package datatypes_test
import (
"database/sql/driver"
"encoding/json"
"fmt"
"testing"
"gorm.io/datatypes"
"gorm.io/gorm"
. "gorm.io/gorm/utils/tests"
)
var _ driver.Valuer = &datatypes.JSON{}
func TestJSONMap(t *testing.T) {
if SupportedDriver("sqlite", "mysql", "postgres") {
type UserWithJSONMap struct {
gorm.Model
Name string
Attributes datatypes.JSONMap
}
DB.Migrator().DropTable(&UserWithJSONMap{})
if err := DB.Migrator().AutoMigrate(&UserWithJSONMap{}); 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
user1AttrsStr := `{"age":18,"name":"json-1","orgs":{"orga":"orga"},"tags":["tag1","tag2"]}`
user1Attrs := map[string]interface{}{
"age": 18,
"name": "json-1",
"orgs": map[string]interface{}{
"orga": "orga",
},
"tags": []interface{}{"tag1", "tag2"},
}
user2Attrs := map[string]interface{}{
"name": "json-2",
"age": 28,
"tags": []interface{}{"tag1", "tag3"},
"role": "admin",
"orgs": map[string]interface{}{
"orgb": "orgb",
},
}
users := []UserWithJSONMap{{
Name: "json-1",
Attributes: datatypes.JSONMap(user1Attrs),
}, {
Name: "json-2",
Attributes: datatypes.JSONMap(user2Attrs),
},
{
Name: "json-3",
Attributes: datatypes.JSONMap{},
},
}
if err := DB.Create(&users).Error; err != nil {
t.Errorf("Failed to create users %v", err)
}
var result UserWithJSONMap
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 UserWithJSONMap
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)
AssertEqual(t, result2.Attributes, user1Attrs)
// 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), user1AttrsStr)
// []byte should unmarshal into type datatypes.JSONMap
var j datatypes.JSONMap
if err := json.Unmarshal([]byte(user1AttrsStr), &j); err != nil {
t.Fatalf("failed to unmarshal user1Attrs, got error %v", err)
}
AssertEqual(t, fmt.Sprint(j), fmt.Sprint(user1Attrs))
var result3 UserWithJSONMap
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 UserWithJSONMap
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)
// FirstOrCreate
jsonMap := map[string]interface{}{"Attributes": datatypes.JSON(`{"age":19,"name":"json-1","orgs":{"orga":"orga"},"tags":["tag1","tag2"]}`)}
if err := DB.Where(&UserWithJSONMap{Name: "json-1"}).Assign(jsonMap).FirstOrCreate(&UserWithJSONMap{}).Error; err != nil {
t.Errorf("failed to run FirstOrCreate")
}
var result5 UserWithJSONMap
if err := DB.First(&result5, datatypes.JSONQuery("attributes").Equals(19, "age")).Error; err != nil {
t.Fatalf("failed to find user with json value, got error %v", err)
}
var result6 UserWithJSONMap
if err := DB.Where("name = ?", "json-3").First(&result6).Error; err != nil {
t.Fatalf("failed to find user with json value, got error %v", err)
}
AssertEqual(t, result6.Attributes, datatypes.JSONMap{})
type UserWithJSONMapPtr struct {
gorm.Model
Name string
Attributes *datatypes.JSONMap
}
DB.Migrator().DropTable(&UserWithJSONMapPtr{})
if err := DB.Migrator().AutoMigrate(&UserWithJSONMapPtr{}); err != nil {
t.Errorf("failed to migrate, got error: %v", err)
}
jm1 := datatypes.JSONMap(user1Attrs)
ujmps := []*UserWithJSONMapPtr{
{
Name: "json-4",
Attributes: &jm1,
},
{
Name: "json-5",
},
}
if err := DB.Create(&ujmps).Error; err != nil {
t.Errorf("Failed to create users %v", err)
}
var result7 UserWithJSONMapPtr
if err := DB.Where("name = ?", "json-4").First(&result7).Error; err != nil {
t.Fatalf("failed to find user with json value, got error %v", err)
}
AssertEqual(t, *result7.Attributes, jm1)
var result8 UserWithJSONMapPtr
if err := DB.Where("name = ?", "json-5").First(&result8).Error; err != nil {
t.Fatalf("failed to find user with json value, got error %v", err)
}
AssertEqual(t, result8.Attributes, nil)
var result9 UserWithJSONMapPtr
if err := DB.Where(result8, "Attributes").First(&result9).Error; err != nil {
t.Fatalf("failed to find user with json value, got error %v", err)
}
}
}
func TestJSONMap_Scan(t *testing.T) {
content := `{"user_id": 1085238870184050699, "name": "Name of user"}`
obj := make(datatypes.JSONMap)
err := obj.Scan([]byte(content))
if err != nil {
t.Fatalf("decode error %v", err)
}
AssertEqual(t, obj["user_id"], 1085238870184050699)
}