OSDN Git Service

5f8ece573b37243768c6c42d4ad52336177e2c85
[bytom/vapor.git] / vendor / github.com / jinzhu / gorm / embedded_struct_test.go
1 package gorm_test
2
3 import "testing"
4
5 type BasePost struct {
6         Id    int64
7         Title string
8         URL   string
9 }
10
11 type Author struct {
12         ID    string
13         Name  string
14         Email string
15 }
16
17 type HNPost struct {
18         BasePost
19         Author  `gorm:"embedded_prefix:user_"` // Embedded struct
20         Upvotes int32
21 }
22
23 type EngadgetPost struct {
24         BasePost BasePost `gorm:"embedded"`
25         Author   Author   `gorm:"embedded;embedded_prefix:author_"` // Embedded struct
26         ImageUrl string
27 }
28
29 func TestPrefixColumnNameForEmbeddedStruct(t *testing.T) {
30         dialect := DB.NewScope(&EngadgetPost{}).Dialect()
31         engadgetPostScope := DB.NewScope(&EngadgetPost{})
32         if !dialect.HasColumn(engadgetPostScope.TableName(), "author_id") || !dialect.HasColumn(engadgetPostScope.TableName(), "author_name") || !dialect.HasColumn(engadgetPostScope.TableName(), "author_email") {
33                 t.Errorf("should has prefix for embedded columns")
34         }
35
36         if len(engadgetPostScope.PrimaryFields()) != 1 {
37                 t.Errorf("should have only one primary field with embedded struct, but got %v", len(engadgetPostScope.PrimaryFields()))
38         }
39
40         hnScope := DB.NewScope(&HNPost{})
41         if !dialect.HasColumn(hnScope.TableName(), "user_id") || !dialect.HasColumn(hnScope.TableName(), "user_name") || !dialect.HasColumn(hnScope.TableName(), "user_email") {
42                 t.Errorf("should has prefix for embedded columns")
43         }
44 }
45
46 func TestSaveAndQueryEmbeddedStruct(t *testing.T) {
47         DB.Save(&HNPost{BasePost: BasePost{Title: "news"}})
48         DB.Save(&HNPost{BasePost: BasePost{Title: "hn_news"}})
49         var news HNPost
50         if err := DB.First(&news, "title = ?", "hn_news").Error; err != nil {
51                 t.Errorf("no error should happen when query with embedded struct, but got %v", err)
52         } else if news.Title != "hn_news" {
53                 t.Errorf("embedded struct's value should be scanned correctly")
54         }
55
56         DB.Save(&EngadgetPost{BasePost: BasePost{Title: "engadget_news"}})
57         var egNews EngadgetPost
58         if err := DB.First(&egNews, "title = ?", "engadget_news").Error; err != nil {
59                 t.Errorf("no error should happen when query with embedded struct, but got %v", err)
60         } else if egNews.BasePost.Title != "engadget_news" {
61                 t.Errorf("embedded struct's value should be scanned correctly")
62         }
63
64         if DB.NewScope(&HNPost{}).PrimaryField() == nil {
65                 t.Errorf("primary key with embedded struct should works")
66         }
67
68         for _, field := range DB.NewScope(&HNPost{}).Fields() {
69                 if field.Name == "BasePost" {
70                         t.Errorf("scope Fields should not contain embedded struct")
71                 }
72         }
73 }
74
75 func TestEmbeddedPointerTypeStruct(t *testing.T) {
76         type HNPost struct {
77                 *BasePost
78                 Upvotes int32
79         }
80
81         DB.Create(&HNPost{BasePost: &BasePost{Title: "embedded_pointer_type"}})
82
83         var hnPost HNPost
84         if err := DB.First(&hnPost, "title = ?", "embedded_pointer_type").Error; err != nil {
85                 t.Errorf("No error should happen when find embedded pointer type, but got %v", err)
86         }
87
88         if hnPost.Title != "embedded_pointer_type" {
89                 t.Errorf("Should find correct value for embedded pointer type")
90         }
91 }