44 "context"
55 "fmt"
66 "maps"
7- "strings"
87 "testing"
98 "time"
109
@@ -75,14 +74,11 @@ func TestMapInstanceToAttrs(t *testing.T) {
7574 description string
7675 instance edge.Instance
7776 expected map [string ]attr.Value
78- expectError bool
79- errorMsg string
8077 }{
8178 {
8279 description : "valid instance" ,
8380 instance : validInstance ,
8481 expected : validInstanceAttrs ,
85- expectError : false ,
8682 },
8783 {
8884 description : "valid instance, empty description" ,
@@ -92,54 +88,12 @@ func TestMapInstanceToAttrs(t *testing.T) {
9288 expected : fixtureAttrs (validInstanceAttrs , func (m map [string ]attr.Value ) {
9389 m ["description" ] = types .StringPointerValue (utils .Ptr ("" ))
9490 }),
95- expectError : false ,
9691 },
9792 {
98- description : "error, empty display name" ,
99- instance : fixtureInstance (func (i * edge.Instance ) {
100- i .DisplayName = ""
101- }),
102- expectError : true ,
103- errorMsg : "missing a 'displayName'" ,
104- },
105- {
106- description : "error, empty id" ,
107- instance : fixtureInstance (func (i * edge.Instance ) {
108- i .Id = ""
109- }),
110- expectError : true ,
111- errorMsg : "missing an 'id'" ,
112- },
113- {
114- description : "error, empty planId" ,
115- instance : fixtureInstance (func (i * edge.Instance ) {
116- i .PlanId = ""
117- }),
118- expectError : true ,
119- errorMsg : "missing a 'planId'" ,
120- },
121- {
122- description : "error, empty frontendUrl" ,
123- instance : fixtureInstance (func (i * edge.Instance ) {
124- i .FrontendUrl = ""
125- }),
126- expectError : true ,
127- errorMsg : "missing a 'frontendUrl'" ,
128- },
129- {
130- description : "error, empty status" ,
131- instance : fixtureInstance (func (i * edge.Instance ) {
132- i .Status = ""
133- }),
134- expectError : true ,
135- errorMsg : "missing a 'status'" ,
136- },
137- {
138- description : "error, nil description" ,
93+ description : "nil description" ,
13994 instance : fixtureInstance (func (i * edge.Instance ) {
14095 i .Description = nil
14196 }),
142- expectError : false ,
14397 expected : fixtureAttrs (validInstanceAttrs , func (m map [string ]attr.Value ) {
14498 m ["description" ] = types .StringPointerValue (nil )
14599 }),
@@ -148,21 +102,7 @@ func TestMapInstanceToAttrs(t *testing.T) {
148102
149103 for _ , tt := range tests {
150104 t .Run (tt .description , func (t * testing.T ) {
151- attrs , err := mapInstanceToAttrs (tt .instance , region )
152-
153- if tt .expectError {
154- if err == nil {
155- t .Fatalf ("Expected an error, but got nil" )
156- }
157- if tt .errorMsg != "" && ! strings .Contains (err .Error (), tt .errorMsg ) {
158- t .Errorf ("Expected error message to contain %q, but got: %v" , tt .errorMsg , err )
159- }
160- return
161- }
162-
163- if err != nil {
164- t .Fatalf ("Expected no error, but got: %v" , err )
165- }
105+ attrs := mapInstanceToAttrs (& tt .instance , region )
166106
167107 diff := cmp .Diff (tt .expected , attrs , cmpopts .EquateEmpty ())
168108 if diff != "" {
@@ -186,63 +126,27 @@ func TestBuildInstancesList(t *testing.T) {
186126 i .DisplayName = "second"
187127 })
188128
189- instanceInvalidPlan := fixtureInstance (func (i * edge.Instance ) {
190- i .PlanId = ""
191- })
192-
193- // Invalid: Empty Display Name
194- instanceEmptyName := fixtureInstance (func (i * edge.Instance ) {
195- i .DisplayName = ""
196- })
197-
198- // Invalid: Empty ID and Empty Display Name
199- instanceEmptyIdAndName := fixtureInstance (func (i * edge.Instance ) {
200- i .Id = ""
201- i .DisplayName = ""
202- })
203-
204129 // Pre-calculate expected mapped objects for the valid instances
205- attrs1 , _ := mapInstanceToAttrs (instance1 , region )
130+ attrs1 := mapInstanceToAttrs (& instance1 , region )
206131 obj1 , _ := types .ObjectValue (instanceTypes , attrs1 )
207132
208- attrs2 , _ := mapInstanceToAttrs (instance2 , region )
133+ attrs2 := mapInstanceToAttrs (& instance2 , region )
209134 obj2 , _ := types .ObjectValue (instanceTypes , attrs2 )
210135
211136 tests := []struct {
212- description string
213- instances []edge.Instance
214- expectedList []attr.Value
215- expectedDiagCount int
137+ description string
138+ instances []edge.Instance
139+ expectedList []attr.Value
216140 }{
217141 {
218- description : "empty instance list" ,
219- instances : []edge.Instance {}, // No test case for nil, since this is checked before buildInstancesList is called
220- expectedList : []attr.Value {},
221- expectedDiagCount : 0 ,
222- },
223- {
224- description : "two valid instances" ,
225- instances : []edge.Instance {instance1 , instance2 },
226- expectedList : []attr.Value {obj1 , obj2 },
227- expectedDiagCount : 0 ,
228- },
229- {
230- description : "one valid, one invalid (empty planId)" ,
231- instances : []edge.Instance {instance1 , instanceInvalidPlan },
232- expectedList : []attr.Value {obj1 },
233- expectedDiagCount : 1 ,
234- },
235- {
236- description : "one valid, one invalid (empty display name)" ,
237- instances : []edge.Instance {instance1 , instanceEmptyName },
238- expectedList : []attr.Value {obj1 },
239- expectedDiagCount : 1 ,
142+ description : "empty instance list" ,
143+ instances : []edge.Instance {}, // No test case for nil, since this is checked before buildInstancesList is called
144+ expectedList : []attr.Value {},
240145 },
241146 {
242- description : "one valid, one invalid (empty id and empty display name)" ,
243- instances : []edge.Instance {instance1 , instanceEmptyIdAndName },
244- expectedList : []attr.Value {obj1 },
245- expectedDiagCount : 1 ,
147+ description : "two valid instances" ,
148+ instances : []edge.Instance {instance1 , instance2 },
149+ expectedList : []attr.Value {obj1 , obj2 },
246150 },
247151 }
248152
@@ -252,19 +156,7 @@ func TestBuildInstancesList(t *testing.T) {
252156
253157 resultList := buildInstancesList (ctx , tt .instances , region , & diags )
254158
255- if tt .expectedDiagCount > 0 {
256- if ! diags .HasError () {
257- t .Errorf ("Expected diagnostics to have errors, but it didn't" )
258- }
259- if len (diags ) != tt .expectedDiagCount {
260- t .Errorf ("Expected %d diagnostic(s), but got %d" , tt .expectedDiagCount , len (diags ))
261- }
262- for _ , d := range diags {
263- if d .Severity () != diag .SeverityError {
264- t .Errorf ("Expected diagnostic to be an Error, but got %v" , d .Severity ())
265- }
266- }
267- } else if diags .HasError () {
159+ if diags .HasError () {
268160 t .Errorf ("Expected no errors, but got diagnostics: %v" , diags )
269161 }
270162
0 commit comments