forked from infobloxopen/atlas-app-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fields_test.go
95 lines (88 loc) · 1.66 KB
/
fields_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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package gorm
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
)
type Model struct {
Property string
SubModel SubModel
SubModels []SubModel
CycleModel *CycleModel
NotPreloadObj SubModel `gorm:"preload:false"`
PreloadObj SubModel `gorm:"preload:true"`
}
type CycleModel struct {
Property string
Model *Model
}
type SubModel struct {
SubProperty string
SubSubModel SubSubModel
}
type SubSubModel struct {
SubSubProperty string
}
func TestGormFieldSelection(t *testing.T) {
tests := []struct {
fs string
toPreload []string
err bool
}{
{
"property",
nil,
false,
},
{
"property,sub_model",
[]string{"SubModel"},
false,
},
{
"property,sub_model.sub_property",
[]string{"SubModel"},
false,
},
{
"sub_model,sub_models",
[]string{"SubModel", "SubModels"},
false,
},
{
"sub_model,sub_models.sub_property",
[]string{"SubModel", "SubModels"},
false,
},
{
"sub_model,sub_model.sub_sub_model.sub_sub_property",
[]string{"SubModel.SubSubModel", "SubModel"},
false,
},
{
"unknown_property",
nil,
false,
},
{
"not_preload_obj,preload_obj",
[]string{"NotPreloadObj", "PreloadObj"},
false,
},
{
"",
[]string{"SubModel.SubSubModel", "SubModel", "SubModels.SubSubModel", "SubModels", "CycleModel", "PreloadObj.SubSubModel", "PreloadObj"},
false,
},
}
for _, test := range tests {
toPreload, err := FieldSelectionStringToGorm(context.Background(), test.fs, &Model{})
if test.err {
assert.Nil(t, toPreload)
assert.NotNil(t, err)
} else {
assert.Equal(t, test.toPreload, toPreload)
assert.Nil(t, err)
}
}
}