Skip to content

Commit 4252f64

Browse files
committed
Cleaner test structure, reduce complexity
1 parent ae4b153 commit 4252f64

File tree

1 file changed

+44
-43
lines changed

1 file changed

+44
-43
lines changed

internal/provider/framework/identity_interceptor_test.go

Lines changed: 44 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -279,46 +279,74 @@ func TestIdentityIsFullyNull(t *testing.T) {
279279
inttypes.StringIdentityAttribute("bucket", true),
280280
}
281281

282+
// Create identity schema once for all test cases
283+
identitySchema := &identityschema.Schema{
284+
Attributes: map[string]identityschema.Attribute{
285+
"account_id": identityschema.StringAttribute{},
286+
"region": identityschema.StringAttribute{},
287+
"bucket": identityschema.StringAttribute{},
288+
},
289+
}
290+
291+
ctx := context.Background()
292+
293+
// Helper function to create identity with values
294+
createIdentityWithValues := func(values map[string]string) *tfsdk.ResourceIdentity {
295+
if values == nil {
296+
return nil
297+
}
298+
identity := emtpyIdentityFromSchema(ctx, identitySchema)
299+
for attrName, value := range values {
300+
if value != "" {
301+
diags := identity.SetAttribute(ctx, path.Root(attrName), value)
302+
if diags.HasError() {
303+
t.Fatalf("unexpected error setting %s in identity: %s", attrName, fwdiag.DiagnosticsError(diags))
304+
}
305+
}
306+
}
307+
return identity
308+
}
309+
282310
testCases := map[string]struct {
283-
identityValues map[string]string
284-
expectNull bool
285-
description string
311+
identity *tfsdk.ResourceIdentity
312+
expectNull bool
313+
description string
286314
}{
287315
"all_null": {
288-
identityValues: map[string]string{},
289-
expectNull: true,
290-
description: "All attributes null should return true",
316+
identity: createIdentityWithValues(map[string]string{}),
317+
expectNull: true,
318+
description: "All attributes null should return true",
291319
},
292320
"some_null": {
293-
identityValues: map[string]string{
321+
identity: createIdentityWithValues(map[string]string{
294322
"account_id": "123456789012",
295323
// region and bucket remain null
296-
},
324+
}),
297325
expectNull: false,
298326
description: "Some attributes set should return false",
299327
},
300328
"all_set": {
301-
identityValues: map[string]string{
329+
identity: createIdentityWithValues(map[string]string{
302330
"account_id": "123456789012",
303331
"region": "us-west-2", // lintignore:AWSAT003
304332
"bucket": "test-bucket",
305-
},
333+
}),
306334
expectNull: false,
307335
description: "All attributes set should return false",
308336
},
309337
"empty_string_values": {
310-
identityValues: map[string]string{
338+
identity: createIdentityWithValues(map[string]string{
311339
"account_id": "",
312340
"region": "",
313341
"bucket": "",
314-
},
342+
}),
315343
expectNull: true,
316344
description: "Empty string values should be treated as null",
317345
},
318346
"nil_identity": {
319-
identityValues: nil, // This will result in nil identity
320-
expectNull: true,
321-
description: "Nil identity should return true",
347+
identity: createIdentityWithValues(nil),
348+
expectNull: true,
349+
description: "Nil identity should return true",
322350
},
323351
}
324352

@@ -327,34 +355,7 @@ func TestIdentityIsFullyNull(t *testing.T) {
327355
t.Parallel()
328356
ctx := context.Background()
329357

330-
// Create identity schema
331-
identitySchema := &identityschema.Schema{
332-
Attributes: map[string]identityschema.Attribute{
333-
"account_id": identityschema.StringAttribute{},
334-
"region": identityschema.StringAttribute{},
335-
"bucket": identityschema.StringAttribute{},
336-
},
337-
}
338-
339-
var identity *tfsdk.ResourceIdentity
340-
341-
// Handle nil identity case
342-
if tc.identityValues == nil {
343-
identity = nil
344-
} else {
345-
// Create identity with values
346-
identity = emtpyIdentityFromSchema(ctx, identitySchema)
347-
for attrName, value := range tc.identityValues {
348-
if value != "" {
349-
diags := identity.SetAttribute(ctx, path.Root(attrName), value)
350-
if diags.HasError() {
351-
t.Fatalf("unexpected error setting %s in identity: %s", attrName, fwdiag.DiagnosticsError(diags))
352-
}
353-
}
354-
}
355-
}
356-
357-
result := identityIsFullyNull(ctx, identity, attributes)
358+
result := identityIsFullyNull(ctx, tc.identity, attributes)
358359
if result != tc.expectNull {
359360
t.Errorf("%s: expected identityIsFullyNull to return %v, got %v",
360361
tc.description, tc.expectNull, result)

0 commit comments

Comments
 (0)