Skip to content

Commit ae4b153

Browse files
committed
Add unit tests
1 parent ab11f94 commit ae4b153

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed

internal/provider/framework/identity_interceptor_test.go

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,3 +269,96 @@ func (c mockClient) ValidateInContextRegionInPartition(ctx context.Context) erro
269269
func (c mockClient) AwsConfig(context.Context) aws.Config { // nosemgrep:ci.aws-in-func-name
270270
panic("not implemented") //lintignore:R009
271271
}
272+
273+
func TestIdentityIsFullyNull(t *testing.T) {
274+
t.Parallel()
275+
276+
attributes := []inttypes.IdentityAttribute{
277+
inttypes.StringIdentityAttribute("account_id", false),
278+
inttypes.StringIdentityAttribute("region", false),
279+
inttypes.StringIdentityAttribute("bucket", true),
280+
}
281+
282+
testCases := map[string]struct {
283+
identityValues map[string]string
284+
expectNull bool
285+
description string
286+
}{
287+
"all_null": {
288+
identityValues: map[string]string{},
289+
expectNull: true,
290+
description: "All attributes null should return true",
291+
},
292+
"some_null": {
293+
identityValues: map[string]string{
294+
"account_id": "123456789012",
295+
// region and bucket remain null
296+
},
297+
expectNull: false,
298+
description: "Some attributes set should return false",
299+
},
300+
"all_set": {
301+
identityValues: map[string]string{
302+
"account_id": "123456789012",
303+
"region": "us-west-2", // lintignore:AWSAT003
304+
"bucket": "test-bucket",
305+
},
306+
expectNull: false,
307+
description: "All attributes set should return false",
308+
},
309+
"empty_string_values": {
310+
identityValues: map[string]string{
311+
"account_id": "",
312+
"region": "",
313+
"bucket": "",
314+
},
315+
expectNull: true,
316+
description: "Empty string values should be treated as null",
317+
},
318+
"nil_identity": {
319+
identityValues: nil, // This will result in nil identity
320+
expectNull: true,
321+
description: "Nil identity should return true",
322+
},
323+
}
324+
325+
for name, tc := range testCases {
326+
t.Run(name, func(t *testing.T) {
327+
t.Parallel()
328+
ctx := context.Background()
329+
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+
if result != tc.expectNull {
359+
t.Errorf("%s: expected identityIsFullyNull to return %v, got %v",
360+
tc.description, tc.expectNull, result)
361+
}
362+
})
363+
}
364+
}

0 commit comments

Comments
 (0)