forked from go-gorm/gorm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slice_test.go
70 lines (54 loc) · 1.68 KB
/
slice_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package gorm_test
import (
"database/sql/driver"
"encoding/json"
"testing"
)
func TestScannableSlices(t *testing.T) {
if err := DB.AutoMigrate(&RecordWithSlice{}).Error; err != nil {
t.Errorf("Should create table with slice values correctly: %s", err)
}
r1 := RecordWithSlice{
Strings: ExampleStringSlice{"a", "b", "c"},
Structs: ExampleStructSlice{
{"name1", "value1"},
{"name2", "value2"},
},
}
if err := DB.Save(&r1).Error; err != nil {
t.Errorf("Should save record with slice values")
}
var r2 RecordWithSlice
if err := DB.Find(&r2).Error; err != nil {
t.Errorf("Should fetch record with slice values")
}
if len(r2.Strings) != 3 || r2.Strings[0] != "a" || r2.Strings[1] != "b" || r2.Strings[2] != "c" {
t.Errorf("Should have serialised and deserialised a string array")
}
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" {
t.Errorf("Should have serialised and deserialised a struct array")
}
}
type RecordWithSlice struct {
ID uint64
Strings ExampleStringSlice `sql:"type:text"`
Structs ExampleStructSlice `sql:"type:text"`
}
type ExampleStringSlice []string
func (l ExampleStringSlice) Value() (driver.Value, error) {
return json.Marshal(l)
}
func (l *ExampleStringSlice) Scan(input interface{}) error {
return json.Unmarshal(input.([]byte), l)
}
type ExampleStruct struct {
Name string
Value string
}
type ExampleStructSlice []ExampleStruct
func (l ExampleStructSlice) Value() (driver.Value, error) {
return json.Marshal(l)
}
func (l *ExampleStructSlice) Scan(input interface{}) error {
return json.Unmarshal(input.([]byte), l)
}