scaner_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package gorm_test
  2. import (
  3. "database/sql/driver"
  4. "encoding/json"
  5. "errors"
  6. "testing"
  7. "github.com/jinzhu/gorm"
  8. )
  9. func TestScannableSlices(t *testing.T) {
  10. if err := DB.AutoMigrate(&RecordWithSlice{}).Error; err != nil {
  11. t.Errorf("Should create table with slice values correctly: %s", err)
  12. }
  13. r1 := RecordWithSlice{
  14. Strings: ExampleStringSlice{"a", "b", "c"},
  15. Structs: ExampleStructSlice{
  16. {"name1", "value1"},
  17. {"name2", "value2"},
  18. },
  19. }
  20. if err := DB.Save(&r1).Error; err != nil {
  21. t.Errorf("Should save record with slice values")
  22. }
  23. var r2 RecordWithSlice
  24. if err := DB.Find(&r2).Error; err != nil {
  25. t.Errorf("Should fetch record with slice values")
  26. }
  27. if len(r2.Strings) != 3 || r2.Strings[0] != "a" || r2.Strings[1] != "b" || r2.Strings[2] != "c" {
  28. t.Errorf("Should have serialised and deserialised a string array")
  29. }
  30. if len(r2.Structs) != 2 || r2.Structs[0].Name != "name1" || r2.Structs[0].Value != "value1" || r2.Structs[1].Name != "name2" || r2.Structs[1].Value != "value2" {
  31. t.Errorf("Should have serialised and deserialised a struct array")
  32. }
  33. }
  34. type RecordWithSlice struct {
  35. ID uint64
  36. Strings ExampleStringSlice `sql:"type:text"`
  37. Structs ExampleStructSlice `sql:"type:text"`
  38. }
  39. type ExampleStringSlice []string
  40. func (l ExampleStringSlice) Value() (driver.Value, error) {
  41. bytes, err := json.Marshal(l)
  42. return string(bytes), err
  43. }
  44. func (l *ExampleStringSlice) Scan(input interface{}) error {
  45. switch value := input.(type) {
  46. case string:
  47. return json.Unmarshal([]byte(value), l)
  48. case []byte:
  49. return json.Unmarshal(value, l)
  50. default:
  51. return errors.New("not supported")
  52. }
  53. }
  54. type ExampleStruct struct {
  55. Name string
  56. Value string
  57. }
  58. type ExampleStructSlice []ExampleStruct
  59. func (l ExampleStructSlice) Value() (driver.Value, error) {
  60. bytes, err := json.Marshal(l)
  61. return string(bytes), err
  62. }
  63. func (l *ExampleStructSlice) Scan(input interface{}) error {
  64. switch value := input.(type) {
  65. case string:
  66. return json.Unmarshal([]byte(value), l)
  67. case []byte:
  68. return json.Unmarshal(value, l)
  69. default:
  70. return errors.New("not supported")
  71. }
  72. }
  73. type ScannerDataType struct {
  74. Street string `sql:"TYPE:varchar(24)"`
  75. }
  76. func (ScannerDataType) Value() (driver.Value, error) {
  77. return nil, nil
  78. }
  79. func (*ScannerDataType) Scan(input interface{}) error {
  80. return nil
  81. }
  82. type ScannerDataTypeTestStruct struct {
  83. Field1 int
  84. ScannerDataType *ScannerDataType `sql:"TYPE:json"`
  85. }
  86. type ScannerDataType2 struct {
  87. Street string `sql:"TYPE:varchar(24)"`
  88. }
  89. func (ScannerDataType2) Value() (driver.Value, error) {
  90. return nil, nil
  91. }
  92. func (*ScannerDataType2) Scan(input interface{}) error {
  93. return nil
  94. }
  95. type ScannerDataTypeTestStruct2 struct {
  96. Field1 int
  97. ScannerDataType *ScannerDataType2
  98. }
  99. func TestScannerDataType(t *testing.T) {
  100. scope := gorm.Scope{Value: &ScannerDataTypeTestStruct{}}
  101. if field, ok := scope.FieldByName("ScannerDataType"); ok {
  102. if DB.Dialect().DataTypeOf(field.StructField) != "json" {
  103. t.Errorf("data type for scanner is wrong")
  104. }
  105. }
  106. scope = gorm.Scope{Value: &ScannerDataTypeTestStruct2{}}
  107. if field, ok := scope.FieldByName("ScannerDataType"); ok {
  108. if DB.Dialect().DataTypeOf(field.StructField) != "varchar(24)" {
  109. t.Errorf("data type for scanner is wrong")
  110. }
  111. }
  112. }