|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package fromproto5_test |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/google/go-cmp/cmp" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/internal/fromproto5" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/resource/identityschema" |
| 13 | + "github.com/hashicorp/terraform-plugin-framework/types/basetypes" |
| 14 | + "github.com/hashicorp/terraform-plugin-go/tfprotov5" |
| 15 | + "github.com/hashicorp/terraform-plugin-go/tftypes" |
| 16 | +) |
| 17 | + |
| 18 | +func TestIdentitySchema(t *testing.T) { |
| 19 | + t.Parallel() |
| 20 | + |
| 21 | + testCases := map[string]struct { |
| 22 | + input *tfprotov5.ResourceIdentitySchema |
| 23 | + expected *identityschema.Schema |
| 24 | + expectedErr string |
| 25 | + }{ |
| 26 | + "nil": { |
| 27 | + input: nil, |
| 28 | + expected: nil, |
| 29 | + }, |
| 30 | + "no-attrs": { |
| 31 | + input: &tfprotov5.ResourceIdentitySchema{}, |
| 32 | + expected: &identityschema.Schema{ |
| 33 | + Attributes: make(map[string]identityschema.Attribute, 0), |
| 34 | + }, |
| 35 | + }, |
| 36 | + "primitives-attrs": { |
| 37 | + input: &tfprotov5.ResourceIdentitySchema{ |
| 38 | + IdentityAttributes: []*tfprotov5.ResourceIdentitySchemaAttribute{ |
| 39 | + { |
| 40 | + Name: "bool", |
| 41 | + Type: tftypes.Bool, |
| 42 | + RequiredForImport: true, |
| 43 | + }, |
| 44 | + { |
| 45 | + Name: "number", |
| 46 | + Type: tftypes.Number, |
| 47 | + OptionalForImport: true, |
| 48 | + }, |
| 49 | + { |
| 50 | + Name: "string", |
| 51 | + Type: tftypes.String, |
| 52 | + OptionalForImport: true, |
| 53 | + }, |
| 54 | + }, |
| 55 | + }, |
| 56 | + expected: &identityschema.Schema{ |
| 57 | + Attributes: map[string]identityschema.Attribute{ |
| 58 | + "bool": identityschema.BoolAttribute{ |
| 59 | + RequiredForImport: true, |
| 60 | + }, |
| 61 | + "number": identityschema.NumberAttribute{ |
| 62 | + OptionalForImport: true, |
| 63 | + }, |
| 64 | + "string": identityschema.StringAttribute{ |
| 65 | + OptionalForImport: true, |
| 66 | + }, |
| 67 | + }, |
| 68 | + }, |
| 69 | + }, |
| 70 | + "list-attr": { |
| 71 | + input: &tfprotov5.ResourceIdentitySchema{ |
| 72 | + IdentityAttributes: []*tfprotov5.ResourceIdentitySchemaAttribute{ |
| 73 | + { |
| 74 | + Name: "list_of_bools", |
| 75 | + Type: tftypes.List{ElementType: tftypes.Bool}, |
| 76 | + RequiredForImport: true, |
| 77 | + }, |
| 78 | + }, |
| 79 | + }, |
| 80 | + expected: &identityschema.Schema{ |
| 81 | + Attributes: map[string]identityschema.Attribute{ |
| 82 | + "list_of_bools": identityschema.ListAttribute{ |
| 83 | + ElementType: basetypes.BoolType{}, |
| 84 | + RequiredForImport: true, |
| 85 | + }, |
| 86 | + }, |
| 87 | + }, |
| 88 | + }, |
| 89 | + "map-error": { |
| 90 | + input: &tfprotov5.ResourceIdentitySchema{ |
| 91 | + IdentityAttributes: []*tfprotov5.ResourceIdentitySchemaAttribute{ |
| 92 | + { |
| 93 | + Name: "map_of_strings", |
| 94 | + Type: tftypes.Map{ElementType: tftypes.String}, |
| 95 | + OptionalForImport: true, |
| 96 | + }, |
| 97 | + }, |
| 98 | + }, |
| 99 | + expectedErr: `no supported identity attribute for "map_of_strings", type: tftypes.Map`, |
| 100 | + }, |
| 101 | + "set-error": { |
| 102 | + input: &tfprotov5.ResourceIdentitySchema{ |
| 103 | + IdentityAttributes: []*tfprotov5.ResourceIdentitySchemaAttribute{ |
| 104 | + { |
| 105 | + Name: "set_of_strings", |
| 106 | + Type: tftypes.Set{ElementType: tftypes.String}, |
| 107 | + OptionalForImport: true, |
| 108 | + }, |
| 109 | + }, |
| 110 | + }, |
| 111 | + expectedErr: `no supported identity attribute for "set_of_strings", type: tftypes.Set`, |
| 112 | + }, |
| 113 | + } |
| 114 | + |
| 115 | + for name, tc := range testCases { |
| 116 | + t.Run(name, func(t *testing.T) { |
| 117 | + t.Parallel() |
| 118 | + |
| 119 | + got, err := fromproto5.IdentitySchema(context.Background(), tc.input) |
| 120 | + if err != nil { |
| 121 | + if tc.expectedErr == "" { |
| 122 | + t.Errorf("Unexpected error: %s", err) |
| 123 | + return |
| 124 | + } |
| 125 | + if err.Error() != tc.expectedErr { |
| 126 | + t.Errorf("Expected error to be %q, got %q", tc.expectedErr, err.Error()) |
| 127 | + return |
| 128 | + } |
| 129 | + // got expected error |
| 130 | + return |
| 131 | + } |
| 132 | + if tc.expectedErr != "" { |
| 133 | + t.Errorf("Expected error to be %q, got nil", tc.expectedErr) |
| 134 | + return |
| 135 | + } |
| 136 | + if diff := cmp.Diff(got, tc.expected); diff != "" { |
| 137 | + t.Errorf("Unexpected diff (+wanted, -got): %s", diff) |
| 138 | + return |
| 139 | + } |
| 140 | + }) |
| 141 | + } |
| 142 | +} |
0 commit comments