@@ -4,13 +4,16 @@ import (
44 "testing"
55
66 "github.com/GoogleCloudPlatform/magic-modules/mmv1/api"
7+ "github.com/GoogleCloudPlatform/magic-modules/mmv1/google"
78 "github.com/google/go-cmp/cmp"
89)
910
1011func TestFromProperties (t * testing.T ) {
1112 cases := []struct {
1213 name string
1314 resourceMetadata * api.Resource
15+ virtualFields []* api.Type
16+ parameters []* api.Type
1417 properties []* api.Type
1518 wantFields []Field
1619 }{
@@ -59,6 +62,113 @@ func TestFromProperties(t *testing.T) {
5962 },
6063 },
6164 },
65+ {
66+ name : "nested field" ,
67+ properties : []* api.Type {
68+ {
69+ Name : "root" ,
70+ Type : "NestedObject" ,
71+ Properties : []* api.Type {
72+ {
73+ Name : "foo" ,
74+ Type : "NestedObject" ,
75+ Properties : []* api.Type {
76+ {
77+ Name : "bars" ,
78+ Type : "Array" ,
79+ ItemType : & api.Type {
80+ Type : "NestedObject" ,
81+ Properties : []* api.Type {
82+ {
83+ Name : "fooBar" ,
84+ Type : "String" ,
85+ },
86+ },
87+ },
88+ },
89+ },
90+ },
91+ },
92+ },
93+ },
94+ wantFields : []Field {
95+ {
96+ ApiField : "root.foo.bars.fooBar" ,
97+ },
98+ },
99+ },
100+ {
101+ name : "nested virtual" ,
102+ virtualFields : []* api.Type {
103+ {
104+ Name : "root" ,
105+ Type : "NestedObject" ,
106+ Properties : []* api.Type {
107+ {
108+ Name : "foo" ,
109+ Type : "String" ,
110+ },
111+ },
112+ },
113+ },
114+ wantFields : []Field {
115+ {
116+ Field : "root.foo" ,
117+ ProviderOnly : true ,
118+ },
119+ },
120+ },
121+ {
122+ name : "nested param" ,
123+ parameters : []* api.Type {
124+ {
125+ Name : "root" ,
126+ Type : "NestedObject" ,
127+ Properties : []* api.Type {
128+ {
129+ Name : "foo" ,
130+ Type : "String" ,
131+ UrlParamOnly : true ,
132+ },
133+ },
134+ },
135+ },
136+ wantFields : []Field {
137+ {
138+ Field : "root.foo" ,
139+ ProviderOnly : true ,
140+ },
141+ },
142+ },
143+ {
144+ name : "map" ,
145+ properties : []* api.Type {
146+ {
147+ Name : "root" ,
148+ Type : "Map" ,
149+ KeyName : "whatever" ,
150+ ValueType : & api.Type {
151+ Type : "NestedObject" ,
152+ Properties : []* api.Type {
153+ {
154+ Name : "foo" ,
155+ Type : "String" ,
156+ },
157+ },
158+ },
159+ },
160+ },
161+ wantFields : []Field {
162+ {
163+ Field : "root.whatever" ,
164+ ApiField : "root.key" ,
165+ },
166+ {
167+ Field : "root.foo" ,
168+ ApiField : "root.value.foo" ,
169+ },
170+ },
171+ },
62172 }
63173
64174 for _ , tc := range cases {
@@ -69,12 +179,12 @@ func TestFromProperties(t *testing.T) {
69179 if r == nil {
70180 r = & api.Resource {}
71181 }
182+ r .VirtualFields = tc .virtualFields
183+ r .Parameters = tc .parameters
184+ r .Properties = tc .properties
185+ r .SetDefault (& api.Product {})
72186
73- for _ , p := range tc .properties {
74- p .SetDefault (r )
75- }
76-
77- got := FromProperties (tc .properties )
187+ got := FromProperties (r .AllNestedProperties (google .Concat (r .RootProperties (), r .UserVirtualFields ())))
78188 if diff := cmp .Diff (tc .wantFields , got ); diff != "" {
79189 t .Errorf ("FromProperties() mismatch (-want +got):\n %s" , diff )
80190 }
0 commit comments