forked from infobloxopen/atlas-app-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fields_test.go
198 lines (184 loc) · 4.17 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package gorm
import (
"context"
"fmt"
"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"`
NonCamelMODEL NonCamelMODEL
NonCAMEL2Model NonCAMEL2Model
NonCamelSUBMODEL NonCamelSUBMODEL
SubModelMix SubModelMix
SubModelMix2 SubModelMix2
NonCamelModelMIX NonCamelModelMIX
}
type NonCamelModelMIX struct {
NonCamelMixProperty string
SubModel SubModel
}
type SubModelMix struct {
SubModelMixProperty string
NonCamelMODEL NonCamelMODEL
}
type SubModelMix2 struct {
SubModelMixProperty string
NonCamelSUBMODEL NonCamelSUBMODEL
}
type NonCamelMODEL struct {
NonCamelProperty string
NonCamelSUBMODEL NonCamelSUBMODEL
}
type NonCamelSUBMODEL struct {
NonCamelSubProperty string
}
type NonCAMEL2Model struct {
NonCamelProperty string
Model *Model
}
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
}{
{
"sub_model_mix2.non_camel_submodel",
[]string{"SubModelMix2.NonCamelSUBMODEL", "SubModelMix2"},
false,
},
{
"non_camel_model_mix.sub_model.sub_property",
[]string{"NonCamelModelMIX.SubModel", "NonCamelModelMIX"},
false,
},
{
"non_camel_model_mix,sub_model,non_camel_2_model,cycle_model",
[]string{"CycleModel", "NonCAMEL2Model", "NonCamelModelMIX", "SubModel"},
false,
},
{
"sub_model_mix.non_camel_model",
[]string{"SubModelMix.NonCamelMODEL", "SubModelMix"},
false,
},
{
"non_camel_model.non_camel_submodel.noncamelsubproperty",
[]string{"NonCamelMODEL.NonCamelSUBMODEL", "NonCamelMODEL"},
false,
},
{
"non_camel_model.non_camel_submodel.noncamelsubproperty",
[]string{"NonCamelMODEL.NonCamelSUBMODEL", "NonCamelMODEL"},
false,
},
{
"non_camel_model.non_camel_submodel",
[]string{"NonCamelMODEL.NonCamelSUBMODEL", "NonCamelMODEL"},
false,
},
{
"non_camel_model",
[]string{"NonCamelMODEL"},
false,
},
{
"non_camel_model.noncamelproperty",
[]string{"NonCamelMODEL"},
false,
},
{
"non_CAMEL_2_Model,Non_camel_2_model,non_camel2_model,non_camel_2model",
[]string{"NonCAMEL2Model", "NonCAMEL2Model", "NonCAMEL2Model", "NonCAMEL2Model"},
false,
},
{
"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",
[]string{"SubModel"},
false,
},
{
"sub_model.sub_sub_model",
[]string{"SubModel.SubSubModel", "SubModel"},
false,
},
{
"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",
"NonCamelMODEL.NonCamelSUBMODEL", "NonCamelMODEL", "NonCAMEL2Model", "NonCamelSUBMODEL",
"SubModelMix.NonCamelMODEL.NonCamelSUBMODEL", "SubModelMix.NonCamelMODEL", "SubModelMix",
"SubModelMix2.NonCamelSUBMODEL", "SubModelMix2",
"NonCamelModelMIX.SubModel.SubSubModel", "NonCamelModelMIX.SubModel", "NonCamelModelMIX"},
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 {
fmt.Printf("expected=%v\nactual=%v\n\n", test.toPreload, toPreload)
assert.Equal(t, test.toPreload, toPreload)
assert.Nil(t, err)
}
}
}