-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
embedded_struct_test.go
284 lines (237 loc) · 7.32 KB
/
embedded_struct_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
package tests_test
import (
"database/sql/driver"
"encoding/json"
"errors"
"reflect"
"testing"
"time"
"gorm.io/gorm"
. "gorm.io/gorm/utils/tests"
)
func TestEmbeddedStruct(t *testing.T) {
type ReadOnly struct {
ReadOnly *bool
}
type BasePost struct {
Id int64
Title string
URL string
ReadOnly
}
type Author struct {
ID string
Name string
Email string
}
type HNPost struct {
BasePost
Author `gorm:"EmbeddedPrefix:user_"` // Embedded struct
Upvotes int32
}
type EngadgetPost struct {
BasePost BasePost `gorm:"Embedded"`
Author *Author `gorm:"Embedded;EmbeddedPrefix:author_"` // Embedded struct
ImageUrl string
}
DB.Migrator().DropTable(&HNPost{}, &EngadgetPost{})
if err := DB.Migrator().AutoMigrate(&HNPost{}, &EngadgetPost{}); err != nil {
t.Fatalf("failed to auto migrate, got error: %v", err)
}
for _, name := range []string{"author_id", "author_name", "author_email"} {
if !DB.Migrator().HasColumn(&EngadgetPost{}, name) {
t.Errorf("should has prefixed column %v", name)
}
}
stmt := gorm.Statement{DB: DB}
if err := stmt.Parse(&EngadgetPost{}); err != nil {
t.Fatalf("failed to parse embedded struct")
} else if len(stmt.Schema.PrimaryFields) != 1 {
t.Errorf("should have only one primary field with embedded struct, but got %v", len(stmt.Schema.PrimaryFields))
}
for _, name := range []string{"user_id", "user_name", "user_email"} {
if !DB.Migrator().HasColumn(&HNPost{}, name) {
t.Errorf("should has prefixed column %v", name)
}
}
// save embedded struct
DB.Save(&HNPost{BasePost: BasePost{Title: "news"}})
DB.Save(&HNPost{BasePost: BasePost{Title: "hn_news"}})
var news HNPost
if err := DB.First(&news, "title = ?", "hn_news").Error; err != nil {
t.Errorf("no error should happen when query with embedded struct, but got %v", err)
} else if news.Title != "hn_news" {
t.Errorf("embedded struct's value should be scanned correctly")
}
DB.Save(&EngadgetPost{BasePost: BasePost{Title: "engadget_news"}, Author: &Author{Name: "Edward"}})
DB.Save(&EngadgetPost{BasePost: BasePost{Title: "engadget_article"}, Author: &Author{Name: "George"}})
var egNews EngadgetPost
if err := DB.First(&egNews, "title = ?", "engadget_news").Error; err != nil {
t.Errorf("no error should happen when query with embedded struct, but got %v", err)
} else if egNews.BasePost.Title != "engadget_news" {
t.Errorf("embedded struct's value should be scanned correctly")
}
var egPosts []EngadgetPost
if err := DB.Order("author_name asc").Find(&egPosts).Error; err != nil {
t.Fatalf("no error should happen when query with embedded struct, but got %v", err)
}
expectAuthors := []string{"Edward", "George"}
for i, post := range egPosts {
t.Log(i, post.Author)
if want := expectAuthors[i]; post.Author.Name != want {
t.Errorf("expected author %s got %s", want, post.Author.Name)
}
}
}
func TestEmbeddedPointerTypeStruct(t *testing.T) {
type BasePost struct {
Id int64
Title string
URL string
}
type Author struct {
ID string
Name string
Email string
Age int
Content Content
ContentPtr *Content
Birthday time.Time
BirthdayPtr *time.Time
}
type HNPost struct {
*BasePost
Upvotes int32
*Author `gorm:"EmbeddedPrefix:user_"` // Embedded struct
}
DB.Migrator().DropTable(&HNPost{})
if err := DB.Migrator().AutoMigrate(&HNPost{}); err != nil {
t.Fatalf("failed to auto migrate, got error: %v", err)
}
DB.Create(&HNPost{BasePost: &BasePost{Title: "embedded_pointer_type"}})
var hnPost HNPost
if err := DB.First(&hnPost, "title = ?", "embedded_pointer_type").Error; err != nil {
t.Errorf("No error should happen when find embedded pointer type, but got %v", err)
}
if hnPost.Title != "embedded_pointer_type" {
t.Errorf("Should find correct value for embedded pointer type")
}
if hnPost.Author != nil {
t.Errorf("Expected to get back a nil Author but got: %v", hnPost.Author)
}
now := time.Now().Round(time.Second)
NewPost := HNPost{
BasePost: &BasePost{Title: "embedded_pointer_type2"},
Author: &Author{
Name: "test",
Content: Content{"test"},
ContentPtr: nil,
Birthday: now,
BirthdayPtr: nil,
},
}
DB.Create(&NewPost)
hnPost = HNPost{}
if err := DB.First(&hnPost, "title = ?", NewPost.Title).Error; err != nil {
t.Errorf("No error should happen when find embedded pointer type, but got %v", err)
}
if hnPost.Title != NewPost.Title {
t.Errorf("Should find correct value for embedded pointer type")
}
if hnPost.Author.Name != NewPost.Author.Name {
t.Errorf("Expected to get Author name %v but got: %v", NewPost.Author.Name, hnPost.Author.Name)
}
if !reflect.DeepEqual(NewPost.Author.Content, hnPost.Author.Content) {
t.Errorf("Expected to get Author content %v but got: %v", NewPost.Author.Content, hnPost.Author.Content)
}
if hnPost.Author.ContentPtr != nil {
t.Errorf("Expected to get nil Author contentPtr but got: %v", hnPost.Author.ContentPtr)
}
if NewPost.Author.Birthday.UnixMilli() != hnPost.Author.Birthday.UnixMilli() {
t.Errorf("Expected to get Author birthday with %+v but got: %+v", NewPost.Author.Birthday, hnPost.Author.Birthday)
}
if hnPost.Author.BirthdayPtr != nil {
t.Errorf("Expected to get nil Author birthdayPtr but got: %+v", hnPost.Author.BirthdayPtr)
}
}
type Content struct {
Content interface{} `gorm:"type:String"`
}
func (c Content) Value() (driver.Value, error) {
// mssql driver with issue on handling null bytes https://github.com/denisenkom/go-mssqldb/issues/530,
b, err := json.Marshal(c)
return string(b[:]), err
}
func (c *Content) Scan(src interface{}) error {
var value Content
str, ok := src.(string)
if !ok {
byt, ok := src.([]byte)
if !ok {
return errors.New("Embedded.Scan byte assertion failed")
}
if err := json.Unmarshal(byt, &value); err != nil {
return err
}
} else {
if err := json.Unmarshal([]byte(str), &value); err != nil {
return err
}
}
*c = value
return nil
}
func TestEmbeddedScanValuer(t *testing.T) {
type HNPost struct {
gorm.Model
Content
}
DB.Migrator().DropTable(&HNPost{})
if err := DB.Migrator().AutoMigrate(&HNPost{}); err != nil {
t.Fatalf("failed to auto migrate, got error: %v", err)
}
hnPost := HNPost{Content: Content{Content: "hello world"}}
if err := DB.Create(&hnPost).Error; err != nil {
t.Errorf("Failed to create got error %v", err)
}
}
func TestEmbeddedRelations(t *testing.T) {
type EmbUser struct {
gorm.Model
Name string
Age uint
Languages []Language `gorm:"many2many:EmbUserSpeak;"`
}
type AdvancedUser struct {
EmbUser `gorm:"embedded"`
Advanced bool
}
DB.Migrator().DropTable(&AdvancedUser{})
if err := DB.AutoMigrate(&AdvancedUser{}); err != nil {
if DB.Dialector.Name() != "sqlite" {
t.Errorf("Failed to auto migrate advanced user, got error %v", err)
}
}
}
func TestEmbeddedTagSetting(t *testing.T) {
type Tag1 struct {
Id int64 `gorm:"autoIncrement"`
}
type Tag2 struct {
Id int64
}
type EmbeddedTag struct {
Tag1 Tag1 `gorm:"Embedded;"`
Tag2 Tag2 `gorm:"Embedded;EmbeddedPrefix:t2_"`
Name string
}
DB.Migrator().DropTable(&EmbeddedTag{})
err := DB.Migrator().AutoMigrate(&EmbeddedTag{})
AssertEqual(t, err, nil)
t1 := EmbeddedTag{Name: "embedded_tag"}
err = DB.Save(&t1).Error
AssertEqual(t, err, nil)
if t1.Tag1.Id == 0 {
t.Errorf("embedded struct's primary field should be rewritten")
}
}