embedded_struct_test.go 2.8 KB

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