field_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package gorm_test
  2. import (
  3. "database/sql/driver"
  4. "encoding/hex"
  5. "fmt"
  6. "testing"
  7. "github.com/jinzhu/gorm"
  8. )
  9. type CalculateField struct {
  10. gorm.Model
  11. Name string
  12. Children []CalculateFieldChild
  13. Category CalculateFieldCategory
  14. EmbeddedField
  15. }
  16. type EmbeddedField struct {
  17. EmbeddedName string `sql:"NOT NULL;DEFAULT:'hello'"`
  18. }
  19. type CalculateFieldChild struct {
  20. gorm.Model
  21. CalculateFieldID uint
  22. Name string
  23. }
  24. type CalculateFieldCategory struct {
  25. gorm.Model
  26. CalculateFieldID uint
  27. Name string
  28. }
  29. func TestCalculateField(t *testing.T) {
  30. var field CalculateField
  31. var scope = DB.NewScope(&field)
  32. if field, ok := scope.FieldByName("Children"); !ok || field.Relationship == nil {
  33. t.Errorf("Should calculate fields correctly for the first time")
  34. }
  35. if field, ok := scope.FieldByName("Category"); !ok || field.Relationship == nil {
  36. t.Errorf("Should calculate fields correctly for the first time")
  37. }
  38. if field, ok := scope.FieldByName("embedded_name"); !ok {
  39. t.Errorf("should find embedded field")
  40. } else if _, ok := field.TagSettingsGet("NOT NULL"); !ok {
  41. t.Errorf("should find embedded field's tag settings")
  42. }
  43. }
  44. type UUID [16]byte
  45. type NullUUID struct {
  46. UUID
  47. Valid bool
  48. }
  49. func FromString(input string) (u UUID) {
  50. src := []byte(input)
  51. return FromBytes(src)
  52. }
  53. func FromBytes(src []byte) (u UUID) {
  54. dst := u[:]
  55. hex.Decode(dst[0:4], src[0:8])
  56. hex.Decode(dst[4:6], src[9:13])
  57. hex.Decode(dst[6:8], src[14:18])
  58. hex.Decode(dst[8:10], src[19:23])
  59. hex.Decode(dst[10:], src[24:])
  60. return
  61. }
  62. func (u UUID) String() string {
  63. buf := make([]byte, 36)
  64. src := u[:]
  65. hex.Encode(buf[0:8], src[0:4])
  66. buf[8] = '-'
  67. hex.Encode(buf[9:13], src[4:6])
  68. buf[13] = '-'
  69. hex.Encode(buf[14:18], src[6:8])
  70. buf[18] = '-'
  71. hex.Encode(buf[19:23], src[8:10])
  72. buf[23] = '-'
  73. hex.Encode(buf[24:], src[10:])
  74. return string(buf)
  75. }
  76. func (u UUID) Value() (driver.Value, error) {
  77. return u.String(), nil
  78. }
  79. func (u *UUID) Scan(src interface{}) error {
  80. switch src := src.(type) {
  81. case UUID: // support gorm convert from UUID to NullUUID
  82. *u = src
  83. return nil
  84. case []byte:
  85. *u = FromBytes(src)
  86. return nil
  87. case string:
  88. *u = FromString(src)
  89. return nil
  90. }
  91. return fmt.Errorf("uuid: cannot convert %T to UUID", src)
  92. }
  93. func (u *NullUUID) Scan(src interface{}) error {
  94. u.Valid = true
  95. return u.UUID.Scan(src)
  96. }
  97. func TestFieldSet(t *testing.T) {
  98. type TestFieldSetNullUUID struct {
  99. NullUUID NullUUID
  100. }
  101. scope := DB.NewScope(&TestFieldSetNullUUID{})
  102. field := scope.Fields()[0]
  103. err := field.Set(FromString("3034d44a-da03-11e8-b366-4a00070b9f00"))
  104. if err != nil {
  105. t.Fatal(err)
  106. }
  107. if id, ok := field.Field.Addr().Interface().(*NullUUID); !ok {
  108. t.Fatal()
  109. } else if !id.Valid || id.UUID.String() != "3034d44a-da03-11e8-b366-4a00070b9f00" {
  110. t.Fatal(id)
  111. }
  112. }