OSDN Git Service

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