| 1 |
package tests_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"database/sql/driver" |
| 5 |
"encoding/json" |
| 6 |
"errors" |
| 7 |
"reflect" |
| 8 |
"testing" |
| 9 |
"time" |
| 10 |
|
| 11 |
"gorm.io/gorm" |
| 12 |
. "gorm.io/gorm/utils/tests" |
| 13 |
) |
| 14 |
|
| 15 |
func TestEmbeddedStruct(t *testing.T) { |
| 16 |
type ReadOnly struct { |
| 17 |
ReadOnly *bool |
| 18 |
} |
| 19 |
|
| 20 |
type BasePost struct { |
| 21 |
Id int64 |
| 22 |
Title string |
| 23 |
URL string |
| 24 |
ReadOnly |
| 25 |
} |
| 26 |
|
| 27 |
type Author struct { |
| 28 |
ID string |
| 29 |
Name string |
| 30 |
Email string |
| 31 |
} |
| 32 |
|
| 33 |
type HNPost struct { |
| 34 |
BasePost |
| 35 |
Author `gorm:"EmbeddedPrefix:user_"` // Embedded struct |
| 36 |
Upvotes int32 |
| 37 |
} |
| 38 |
|
| 39 |
type EngadgetPost struct { |
| 40 |
BasePost BasePost `gorm:"Embedded"` |
| 41 |
Author *Author `gorm:"Embedded;EmbeddedPrefix:author_"` // Embedded struct |
| 42 |
ImageUrl string |
| 43 |
} |
| 44 |
|
| 45 |
DB.Migrator().DropTable(&HNPost{}, &EngadgetPost{}) |
| 46 |
if err := DB.Migrator().AutoMigrate(&HNPost{}, &EngadgetPost{}); err != nil { |
| 47 |
t.Fatalf("failed to auto migrate, got error: %v", err) |
| 48 |
} |
| 49 |
|
| 50 |
for _, name := range []string{"author_id", "author_name", "author_email"} { |
| 51 |
if !DB.Migrator().HasColumn(&EngadgetPost{}, name) { |
| 52 |
t.Errorf("should has prefixed column %v", name) |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
stmt := gorm.Statement{DB: DB} |
| 57 |
if err := stmt.Parse(&EngadgetPost{}); err != nil { |
| 58 |
t.Fatalf("failed to parse embedded struct") |
| 59 |
} else if len(stmt.Schema.PrimaryFields) != 1 { |
| 60 |
t.Errorf("should have only one primary field with embedded struct, but got %v", len(stmt.Schema.PrimaryFields)) |
| 61 |
} |
| 62 |
|
| 63 |
for _, name := range []string{"user_id", "user_name", "user_email"} { |
| 64 |
if !DB.Migrator().HasColumn(&HNPost{}, name) { |
| 65 |
t.Errorf("should has prefixed column %v", name) |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
// save embedded struct |
| 70 |
DB.Save(&HNPost{BasePost: BasePost{Title: "news"}}) |
| 71 |
DB.Save(&HNPost{BasePost: BasePost{Title: "hn_news"}}) |
| 72 |
var news HNPost |
| 73 |
if err := DB.First(&news, "title = ?", "hn_news").Error; err != nil { |
| 74 |
t.Errorf("no error should happen when query with embedded struct, but got %v", err) |
| 75 |
} else if news.Title != "hn_news" { |
| 76 |
t.Errorf("embedded struct's value should be scanned correctly") |
| 77 |
} |
| 78 |
|
| 79 |
DB.Save(&EngadgetPost{BasePost: BasePost{Title: "engadget_news"}, Author: &Author{Name: "Edward"}}) |
| 80 |
DB.Save(&EngadgetPost{BasePost: BasePost{Title: "engadget_article"}, Author: &Author{Name: "George"}}) |
| 81 |
var egNews EngadgetPost |
| 82 |
if err := DB.First(&egNews, "title = ?", "engadget_news").Error; err != nil { |
| 83 |
t.Errorf("no error should happen when query with embedded struct, but got %v", err) |
| 84 |
} else if egNews.BasePost.Title != "engadget_news" { |
| 85 |
t.Errorf("embedded struct's value should be scanned correctly") |
| 86 |
} |
| 87 |
|
| 88 |
var egPosts []EngadgetPost |
| 89 |
if err := DB.Order("author_name asc").Find(&egPosts).Error; err != nil { |
| 90 |
t.Fatalf("no error should happen when query with embedded struct, but got %v", err) |
| 91 |
} |
| 92 |
expectAuthors := []string{"Edward", "George"} |
| 93 |
for i, post := range egPosts { |
| 94 |
t.Log(i, post.Author) |
| 95 |
if want := expectAuthors[i]; post.Author.Name != want { |
| 96 |
t.Errorf("expected author %s got %s", want, post.Author.Name) |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
func TestEmbeddedPointerTypeStruct(t *testing.T) { |
| 102 |
type BasePost struct { |
| 103 |
Id int64 |
| 104 |
Title string |
| 105 |
URL string |
| 106 |
} |
| 107 |
|
| 108 |
type Author struct { |
| 109 |
ID string |
| 110 |
Name string |
| 111 |
Email string |
| 112 |
Age int |
| 113 |
Content Content |
| 114 |
ContentPtr *Content |
| 115 |
Birthday time.Time |
| 116 |
BirthdayPtr *time.Time |
| 117 |
} |
| 118 |
|
| 119 |
type HNPost struct { |
| 120 |
*BasePost |
| 121 |
Upvotes int32 |
| 122 |
*Author `gorm:"EmbeddedPrefix:user_"` // Embedded struct |
| 123 |
} |
| 124 |
|
| 125 |
DB.Migrator().DropTable(&HNPost{}) |
| 126 |
if err := DB.Migrator().AutoMigrate(&HNPost{}); err != nil { |
| 127 |
t.Fatalf("failed to auto migrate, got error: %v", err) |
| 128 |
} |
| 129 |
|
| 130 |
DB.Create(&HNPost{BasePost: &BasePost{Title: "embedded_pointer_type"}}) |
| 131 |
|
| 132 |
var hnPost HNPost |
| 133 |
if err := DB.First(&hnPost, "title = ?", "embedded_pointer_type").Error; err != nil { |
| 134 |
t.Errorf("No error should happen when find embedded pointer type, but got %v", err) |
| 135 |
} |
| 136 |
|
| 137 |
if hnPost.Title != "embedded_pointer_type" { |
| 138 |
t.Errorf("Should find correct value for embedded pointer type") |
| 139 |
} |
| 140 |
|
| 141 |
if hnPost.Author != nil { |
| 142 |
t.Errorf("Expected to get back a nil Author but got: %v", hnPost.Author) |
| 143 |
} |
| 144 |
|
| 145 |
now := time.Now().Round(time.Second) |
| 146 |
NewPost := HNPost{ |
| 147 |
BasePost: &BasePost{Title: "embedded_pointer_type2"}, |
| 148 |
Author: &Author{ |
| 149 |
Name: "test", |
| 150 |
Content: Content{"test"}, |
| 151 |
ContentPtr: nil, |
| 152 |
Birthday: now, |
| 153 |
BirthdayPtr: nil, |
| 154 |
}, |
| 155 |
} |
| 156 |
DB.Create(&NewPost) |
| 157 |
|
| 158 |
hnPost = HNPost{} |
| 159 |
if err := DB.First(&hnPost, "title = ?", NewPost.Title).Error; err != nil { |
| 160 |
t.Errorf("No error should happen when find embedded pointer type, but got %v", err) |
| 161 |
} |
| 162 |
|
| 163 |
if hnPost.Title != NewPost.Title { |
| 164 |
t.Errorf("Should find correct value for embedded pointer type") |
| 165 |
} |
| 166 |
|
| 167 |
if hnPost.Author.Name != NewPost.Author.Name { |
| 168 |
t.Errorf("Expected to get Author name %v but got: %v", NewPost.Author.Name, hnPost.Author.Name) |
| 169 |
} |
| 170 |
|
| 171 |
if !reflect.DeepEqual(NewPost.Author.Content, hnPost.Author.Content) { |
| 172 |
t.Errorf("Expected to get Author content %v but got: %v", NewPost.Author.Content, hnPost.Author.Content) |
| 173 |
} |
| 174 |
|
| 175 |
if hnPost.Author.ContentPtr != nil { |
| 176 |
t.Errorf("Expected to get nil Author contentPtr but got: %v", hnPost.Author.ContentPtr) |
| 177 |
} |
| 178 |
|
| 179 |
if NewPost.Author.Birthday.UnixMilli() != hnPost.Author.Birthday.UnixMilli() { |
| 180 |
t.Errorf("Expected to get Author birthday with %+v but got: %+v", NewPost.Author.Birthday, hnPost.Author.Birthday) |
| 181 |
} |
| 182 |
|
| 183 |
if hnPost.Author.BirthdayPtr != nil { |
| 184 |
t.Errorf("Expected to get nil Author birthdayPtr but got: %+v", hnPost.Author.BirthdayPtr) |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
type Content struct { |
| 189 |
Content interface{} `gorm:"type:String"` |
| 190 |
} |
| 191 |
|
| 192 |
func (c Content) Value() (driver.Value, error) { |
| 193 |
// mssql driver with issue on handling null bytes https://github.com/denisenkom/go-mssqldb/issues/530, |
| 194 |
b, err := json.Marshal(c) |
| 195 |
return string(b[:]), err |
| 196 |
} |
| 197 |
|
| 198 |
func (c *Content) Scan(src interface{}) error { |
| 199 |
var value Content |
| 200 |
str, ok := src.(string) |
| 201 |
if !ok { |
| 202 |
byt, ok := src.([]byte) |
| 203 |
if !ok { |
| 204 |
return errors.New("Embedded.Scan byte assertion failed") |
| 205 |
} |
| 206 |
if err := json.Unmarshal(byt, &value); err != nil { |
| 207 |
return err |
| 208 |
} |
| 209 |
} else { |
| 210 |
if err := json.Unmarshal([]byte(str), &value); err != nil { |
| 211 |
return err |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
*c = value |
| 216 |
|
| 217 |
return nil |
| 218 |
} |
| 219 |
|
| 220 |
func TestEmbeddedScanValuer(t *testing.T) { |
| 221 |
type HNPost struct { |
| 222 |
gorm.Model |
| 223 |
Content |
| 224 |
} |
| 225 |
|
| 226 |
DB.Migrator().DropTable(&HNPost{}) |
| 227 |
if err := DB.Migrator().AutoMigrate(&HNPost{}); err != nil { |
| 228 |
t.Fatalf("failed to auto migrate, got error: %v", err) |
| 229 |
} |
| 230 |
|
| 231 |
hnPost := HNPost{Content: Content{Content: "hello world"}} |
| 232 |
|
| 233 |
if err := DB.Create(&hnPost).Error; err != nil { |
| 234 |
t.Errorf("Failed to create got error %v", err) |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
func TestEmbeddedRelations(t *testing.T) { |
| 239 |
type EmbUser struct { |
| 240 |
gorm.Model |
| 241 |
Name string |
| 242 |
Age uint |
| 243 |
Languages []Language `gorm:"many2many:EmbUserSpeak;"` |
| 244 |
} |
| 245 |
|
| 246 |
type AdvancedUser struct { |
| 247 |
EmbUser `gorm:"embedded"` |
| 248 |
Advanced bool |
| 249 |
} |
| 250 |
|
| 251 |
DB.Migrator().DropTable(&AdvancedUser{}) |
| 252 |
|
| 253 |
if err := DB.AutoMigrate(&AdvancedUser{}); err != nil { |
| 254 |
if DB.Dialector.Name() != "sqlite" { |
| 255 |
t.Errorf("Failed to auto migrate advanced user, got error %v", err) |
| 256 |
} |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
func TestEmbeddedTagSetting(t *testing.T) { |
| 261 |
type Tag1 struct { |
| 262 |
Id int64 `gorm:"autoIncrement"` |
| 263 |
} |
| 264 |
type Tag2 struct { |
| 265 |
Id int64 |
| 266 |
} |
| 267 |
|
| 268 |
type EmbeddedTag struct { |
| 269 |
Tag1 Tag1 `gorm:"Embedded;"` |
| 270 |
Tag2 Tag2 `gorm:"Embedded;EmbeddedPrefix:t2_"` |
| 271 |
Name string |
| 272 |
} |
| 273 |
|
| 274 |
DB.Migrator().DropTable(&EmbeddedTag{}) |
| 275 |
err := DB.Migrator().AutoMigrate(&EmbeddedTag{}) |
| 276 |
AssertEqual(t, err, nil) |
| 277 |
|
| 278 |
t1 := EmbeddedTag{Name: "embedded_tag"} |
| 279 |
err = DB.Save(&t1).Error |
| 280 |
AssertEqual(t, err, nil) |
| 281 |
if t1.Tag1.Id == 0 { |
| 282 |
t.Errorf("embedded struct's primary field should be rewritten") |
| 283 |
} |
| 284 |
} |